import numpy as np
import sympy as sym
from numpy import sin
from sympy import symbols, diff
func = lambda x: sin(x)
x = symbols('x')
print diff(func(x),x)
This works if I replace my function with a polynomial, or if I place the trig function directly into the diff operator. But in this format I get AttributeError: sin.
如果我用多项式替换我的函数,或者我将trig函数直接放入diff运算符,这是有效的。但是在这种格式中,我得到了AttributeError:sin。
Basically I think python can't recognize func as just being a trig function which it knows how to symbolically integrate. I could just have sympy import sin and then things would work, but then I'm stuck with func referencing sin in the sympy namespace and there are future things I want to do with func which require that it be defined using sin in the numpy namespace.
基本上我认为python不能将func识别为只知道如何进行符号集成的trig函数。我可能只有同情导入罪,然后事情就会起作用,但是后来我坚持使用func在sympy命名空间中引用sin,并且我想用func进行未来的事情,这需要在numpy命名空间中使用sin来定义它。
2 个解决方案
#1
2
You should build your expression up symbolically using SymPy functions, and then use lambdify
to convert them into things that can be evaluated with NumPy.
您应该使用SymPy函数以符号方式构建表达式,然后使用lambdify将它们转换为可以使用NumPy计算的内容。
#2
0
This simply isn't how you use sympy. You can't use normal functions in conjunction with numpy--you need to build symbolic expressions using the stuff it provides.
这根本不是你如何使用sympy。你不能将普通函数与numpy结合使用 - 你需要使用它提供的东西来构建符号表达式。
To write code to get the derivative of sin(x), you would do
要编写代码来获得sin(x)的导数,你会这样做
import sympy as sym
from sympy import sin, symbols, diff
x = symbols('x')
print diff(sin(x), x)
If you have some other particular case you're having trouble with, you'll have to show it. You can't mix sympy with non-sympy stuff in this way, so there isn't some general feedback that can be provided.
如果您遇到其他特殊情况,则需要进行展示。你不能以这种方式将同情与非同情的东西混在一起,所以没有一些可以提供的一般性反馈。
#1
2
You should build your expression up symbolically using SymPy functions, and then use lambdify
to convert them into things that can be evaluated with NumPy.
您应该使用SymPy函数以符号方式构建表达式,然后使用lambdify将它们转换为可以使用NumPy计算的内容。
#2
0
This simply isn't how you use sympy. You can't use normal functions in conjunction with numpy--you need to build symbolic expressions using the stuff it provides.
这根本不是你如何使用sympy。你不能将普通函数与numpy结合使用 - 你需要使用它提供的东西来构建符号表达式。
To write code to get the derivative of sin(x), you would do
要编写代码来获得sin(x)的导数,你会这样做
import sympy as sym
from sympy import sin, symbols, diff
x = symbols('x')
print diff(sin(x), x)
If you have some other particular case you're having trouble with, you'll have to show it. You can't mix sympy with non-sympy stuff in this way, so there isn't some general feedback that can be provided.
如果您遇到其他特殊情况,则需要进行展示。你不能以这种方式将同情与非同情的东西混在一起,所以没有一些可以提供的一般性反馈。