通过应用SymPy函数计算数组的值会导致AttributeError

时间:2018-12-30 18:23:24

标签: python function numpy lambda sympy

我有一个数字数组,我想用一个简单的方程式计算值。

import numpy as np
import sympy as sym

x_values = np.linspace(0, 2, 100)
function = lambda x : 10*sym.sin(9*x)
function(x_values)

我得到:

 AttributeError: 'ImmutableDenseNDimArray' object has no attribute 'could_extract_minus_sign'

2 个答案:

答案 0 :(得分:2)

您可以使用sympy.lambdify来执行此操作,如下所示:

import numpy as np
import sympy as sym

x = sym.symbols("x")
x_values = np.linspace(0, 2, 100)
function = sym.lambdify(x, 10*sym.sin(9*x), "numpy")
function(x_values)

答案 1 :(得分:0)

您应该定义一个函数

  import numpy as   np
    import sympy as sym
    x_values = np.linspace(0, 2, 100)
    def function(x):
         lambda x : 10*sym.sin(9*x)
         return x

    print(function(x_values))

代码:http://tpcg.io/cBEnHu 或者你可以这样做

import numpy as np
from sympy import sin
import pylab as pl
from sympy.abc import x
from sympy import Function
from sympy import *
x_values = np.linspace(0, 2, 100)


function = lambdify(x, 10*sin(9*x))
print(function(x_values ))

代码:http://tpcg.io/cBEnHu