在数学方面,我可以解决"x + 1 <2":
Reduce[x + 1 < 2, x]
Run Code Online (Sandbox Code Playgroud)
我可以用SymPy做同样的事情吗?我该怎么办?
我对 sympy 中的单位有一个小问题。如果我执行以下表达式:
import sympy.physics.units as u
print u.kg + u.kg
Run Code Online (Sandbox Code Playgroud)
我得到的结果是:
2*kg
Run Code Online (Sandbox Code Playgroud)
这不是我想要的,我只想拥有公斤。有什么问题?
//编辑我看到有人在https://github.com/sympy/sympy/pull/1389中研究这个问题,有人知道更多吗?
我有Python 3.3.3和SymPy 0.7.4.1,都是x64版本,并在我的计算机上本地安装.我正在使用PSPad作为Python脚本编写的配置编辑器.
sympy在模块中使用库中的导入时,应该求解一组三个线性方程:
from sympy import Matrix, solve_linear_system
from sympy.abc import x, y, z
def main():
system = Matrix (((3,2,-1,1) ,(2,-2,4,-2),(-1,0.5,-1,0)))
print(solve_linear_system(system, x, y,z))
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)
编辑器PSPad控制台输出返回以下内容:
traceback (most recent call last): File "C:\Users\GOODLU~1\AppData\Local\Temp\PSpad\securesafety_DISK_5GB\Programmation\linear system solve SYMPY.py", line 1, in <module>
from sympy import Matrix,solve_linear_system File "C:\Users\GOODLU~1\AppData\Local\Temp\PSpad\securesafety_DISK_5GB\Programmation\sympy.py", line 2, in <module>
from sympy import var,Eq,solve ImportError: cannot import name var
Process completed, Exit Code 1.
Execution time: 00:00.134
Run Code Online (Sandbox Code Playgroud)
实际上,我对这些问题非常怀疑:
为什么,当键入相同的东西,没有对象def main(),并在IDLE中逐行输入时,一切都正确解决,如:{x: 1.00000000000000, …
我尝试在这样的总和中索引矩阵
from sympy import *
vx1,vx2,vx3,vx4,vx5, vy1,vy2,vy3,vy4,vy5, = symbols('vx1 vx2 vx3 vx4 vx5 vy1 vy2 vy3 vy4 vy5')
vx=Matrix([vx1,vx2,vx3,vx4,vx5])
vy=Matrix([vy1,vy2,vy3,vy4,vy5])
p, n = symbols('p n', integer=True)
vx[0]
vx[1]
vx[2]
vx[3]
summation(p, (p, 0, 4))
summation(vx[p], (p, 0, 4))
Run Code Online (Sandbox Code Playgroud)
但似乎 sympy 不能这样做:
NameError: IndexError: Invalid index a[p]
Run Code Online (Sandbox Code Playgroud)
有办法吗?
我在Python中运行以下内容并期望E [1]中的列是A的特征向量,但它们不是.只有Sympy.Matrix.eigenvects()似乎做得对.为什么这个错误?
A
Out[194]:
matrix([[-3, 3, 2],
[ 1, -1, -2],
[-1, -3, 0]])
E = np.linalg.eig(A)
E
Out[196]:
(array([ 2., -4., -2.]),
matrix([[ -2.01889132e-16, 9.48683298e-01, 8.94427191e-01],
[ 5.54700196e-01, -3.16227766e-01, -3.71551690e-16],
[ -8.32050294e-01, 2.73252305e-17, 4.47213595e-01]]))
A*E[1] / E[1]
Out[205]:
matrix([[ 6.59900617, -4. , -2. ],
[ 2. , -4. , -3.88449298],
[ 2. , 8.125992 , -2. ]])
Run Code Online (Sandbox Code Playgroud) 我想在同意中解决这个微分方程:
f'(x) = f(x+1)
Run Code Online (Sandbox Code Playgroud)
我试试这个:
from sympy import *
x = symbols("x")
f = Function("f")
f_ = Derivative(f,x)
dsolve(f_(x) - f(x+1), f(x))
Run Code Online (Sandbox Code Playgroud)
但得到一个错误:"'衍生'对象不可调用".
当我用"f_"替换"f_(x)"时,我得到一个不同的错误:"TypeError:doit()缺少1个必需的位置参数:'self'".
这个的正确语法是什么?
为什么Sympy在使用时会抛出Type错误scipy.stats.norm?我怎样才能解决这个问题?
from sympy import Eq, Symbol, solve, Piecewise
from scipy.stats import norm
import numpy as np
x = Symbol('x')
eqn = Eq((x-0.2)/0.3, norm.cdf((np.log(100/110) + x**2/2)/x))
print(solve(eqn))
Run Code Online (Sandbox Code Playgroud)
输出:
TypeError: cannot determine truth value of Relational
Run Code Online (Sandbox Code Playgroud) 据我所知,在Python sin(pi)和cos(pi/2)不会产生0,但我用的矩阵进行计算,我需要使用这些值。
我使用SymPy,并在第一的价值观sin(pi)和cos(pi/2)是有点讨厌。经过一些乘法后,它们开始妨碍您。有没有办法使这些值0在整个模块中等于?如何在表达式中间更改它?
我将使用此矩阵作为示例:
A = Matrix([
[(-sin(theta1)*sin(theta2)*cos(alpha2) + cos(theta1)*cos(theta2))*cos(theta3) + (-sin(theta1)*cos(alpha2)*cos(theta2) - sin(theta2)*cos(theta1))*sin(theta3)*cos(alpha3) + sin(alpha2)*sin(alpha3)*sin(theta1)*sin(theta3), -(-sin(theta1)*sin(theta2)*cos(alpha2) + cos(theta1)*cos(theta2))*sin(theta3) + (-sin(theta1)*cos(alpha2)*cos(theta2) - sin(theta2)*cos(theta1))*cos(alpha3)*cos(theta3) + sin(alpha2)*sin(alpha3)*sin(theta1)*cos(theta3), -(-sin(theta1)*cos(alpha2)*cos(theta2) - sin(theta2)*cos(theta1))*sin(alpha3) + sin(alpha2)*sin(theta1)*cos(alpha3), a3*(-sin(theta1)*sin(theta2)*cos(alpha2) + cos(theta1)*cos(theta2)) + d2*sin(alpha2)*sin(theta1) - d3*(-sin(theta1)*cos(alpha2)*cos(theta2) - sin(theta2)*cos(theta1))*sin(alpha3) + d3*sin(alpha2)*sin(theta1)*cos(alpha3)],
[(-sin(theta1)*sin(theta2) + cos(alpha2)*cos(theta1)*cos(theta2))*sin(theta3)*cos(alpha3) + (sin(theta1)*cos(theta2) + sin(theta2)*cos(alpha2)*cos(theta1))*cos(theta3) - sin(alpha2)*sin(alpha3)*sin(theta3)*cos(theta1), (-sin(theta1)*sin(theta2) + cos(alpha2)*cos(theta1)*cos(theta2))*cos(alpha3)*cos(theta3) - (sin(theta1)*cos(theta2) + sin(theta2)*cos(alpha2)*cos(theta1))*sin(theta3) - sin(alpha2)*sin(alpha3)*cos(theta1)*cos(theta3), -(-sin(theta1)*sin(theta2) + cos(alpha2)*cos(theta1)*cos(theta2))*sin(alpha3) - sin(alpha2)*cos(alpha3)*cos(theta1), a3*(sin(theta1)*cos(theta2) + sin(theta2)*cos(alpha2)*cos(theta1)) - d2*sin(alpha2)*cos(theta1) …Run Code Online (Sandbox Code Playgroud) 我正在编写一个脚本,我试图在其中处理出现在 sympy 表达式中的不同数字。为了提取我需要的东西,我使用了类似的东西: isinstance(expr, sympy.numbers.Float),它检查出现在我的表达式中的数字是否是一个浮点数。这适用于大多数数字(浮点数、整数和有理数)。但是,我对负数有一些问题。例如,如果我这样做:
eq = parse_expr("cos(2*a)+cos(0.5*b)")
srepr(eq)
Run Code Online (Sandbox Code Playgroud)
我得到这个输出:
Add(cos(Mul(Integer(2), Symbol('a'))), cos(Mul(Float('0.5', precision=53), Symbol('b'))))
Run Code Online (Sandbox Code Playgroud)
这表示我有一个整数 2 和一个浮点数 0.5,这正是我所需要的。但是,如果我这样做:
eq = parse_expr("cos(-2*a)+cos(0.5*b)")
srepr(eq)
Run Code Online (Sandbox Code Playgroud)
我得到这个输出:
Add(cos(Mul(Integer(2), Symbol('a'))), cos(Mul(Float('0.5', precision=53), Symbol('b'))))
Run Code Online (Sandbox Code Playgroud)
所以基本上减号被忽略了。为什么会这样,我怎样才能让它保留减号并输出某种形式的东西Integer(-2)而不是Integer(2)?
谢谢!
我想计算的Gradient,并Hessian与各变量以下功能x和y。任何人都可以帮忙吗?非常感谢。
我从github 中找到了一个用于计算 Rosenbrock 函数的相关代码。
def objfun(x,y):
return 10*(y-x**2)**2 + (1-x)**2
def gradient(x,y):
return np.array([-40*x*y + 40*x**3 -2 + 2*x, 20*(y-x**2)])
def hessian(x,y):
return np.array([[120*x*x - 40*y+2, -40*x],[-40*x, 20]])
Run Code Online (Sandbox Code Playgroud)
更新:
from sympy import symbols, hessian, Function, N
x, y = symbols('x y')
f = symbols('f', cls=Function)
f = (1/2)*np.power(x, 2) + 5*np.power(y, 2) + (2/3)*np.power((x-2), 4) + 8*np.power((y+1), 4)
H = hessian(f, [x, y]).subs([(x,1), (y,1)])
print(np.array(H))
print(N(H.condition_number()))
Run Code Online (Sandbox Code Playgroud)
输出:
[[9.00000000000000 0] …Run Code Online (Sandbox Code Playgroud)