我试图用一个函数组成一个矩阵,并通过n,r的不同值来创建不同的列和行。当我没有定义函数时,它就可以正常工作,但是一旦定义了这些函数,就无法通过这些值而不会得到错误的“零”对象没有属性“ sin”
我已经尝试过浮动,并转换为numpy
import numpy as np
from numpy import sin, pi, sqrt, cos
from sympy import sqrt, sin, cos, pi, Matrix
#from sympy.abc import n, r, x, L
A=w**2/2
Phi_n= sqrt(2/L)*sym.sin(n*sym.pi*x/L + n*sym.pi/2) #When n*np.pi*x
Phi_r= sqrt(2/L)*sym.sin(r*sym.pi*x/L + r*sym.pi/2)
V_Func=A*integrate(Phi_r*x**2*Phi_n, (x, -L/2, L/2))
from sympy.utilities.lambdify import lambdify, implemented_function
from sympy import Function
V_F = lambdify((n, r), V_Func)
V_F(0,0)
返回0
from sympy import Matrix
N = 5
sym.Matrix(N, N, lambda n,r:V_F(n,r))
这将返回错误,零对象没有属性sin
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-222-c996f5317753> in <module>
1 from sympy import Matrix
2 N = 5
----> 3 sym.Matrix(N, N, lambda n,r:V_F(n,r))
~/anaconda3/lib/python3.7/site-packages/sympy/matrices/dense.py in __new__(cls, *args, **kwargs)
431 class MutableDenseMatrix(DenseMatrix, MatrixBase):
432 def __new__(cls, *args, **kwargs):
--> 433 return cls._new(*args, **kwargs)
434
435 @classmethod
~/anaconda3/lib/python3.7/site-packages/sympy/matrices/dense.py in _new(cls, *args, **kwargs)
443 rows, cols, flat_list = args
444 else:
--> 445 rows, cols, flat_list = cls._handle_creation_inputs(*args, **kwargs)
446 flat_list = list(flat_list) # create a shallow copy
447 self = object.__new__(cls)
~/anaconda3/lib/python3.7/site-packages/sympy/matrices/matrices.py in _handle_creation_inputs(cls, *args, **kwargs)
2337 flat_list.extend(
2338 [cls._sympify(op(cls._sympify(i), cls._sympify(j)))
-> 2339 for j in range(cols)])
2340
2341 # Matrix(2, 2, [1, 2, 3, 4])
~/anaconda3/lib/python3.7/site-packages/sympy/matrices/matrices.py in <listcomp>(.0)
2337 flat_list.extend(
2338 [cls._sympify(op(cls._sympify(i), cls._sympify(j)))
-> 2339 for j in range(cols)])
2340
2341 # Matrix(2, 2, [1, 2, 3, 4])
<ipython-input-222-c996f5317753> in <lambda>(n, r)
1 from sympy import Matrix
2 N = 5
----> 3 sym.Matrix(N, N, lambda n,r:V_F(n,r))
<lambdifygenerated-51> in _lambdifygenerated(n, r)
1 def _lambdifygenerated(n, r):
----> 2 return ((1/2)*w**2*select([logical_or.reduce((logical_and.reduce((equal(n, 0),equal(r, 0))),logical_and.reduce((equal(n, 0),equal(n, r),equal(r, 0))),logical_and.reduce((equal(n, 0),equal(r, 0),equal(n, -r))),logical_and.reduce((equal(n, 0),equal(n, r),equal(r, 0),equal(n, -r))))),logical_or.reduce((equal(n, -r),logical_and.reduce((equal(n, 0),equal(n, -r))),logical_and.reduce((equal(n, r),equal(n, -r))),logical_and.reduce((equal(r, 0),equal(n, -r))),logical_and.reduce((equal(n, 0),equal(n, r),equal(n, -r))),logical_and.reduce((equal(n, r),equal(r, 0),equal(n, -r))))),logical_or.reduce((equal(n, r),logical_and.reduce((equal(n, 0),equal(n, r))),logical_and.reduce((equal(n, r),equal(r, 0))))),True], [0,-2*((1/48)*L**3 - 1/8*L**3/(pi**2*r**2))/L + 2*(-1/48*L**3*sin(pi*r)**2 - 1/48*L**3*cos(pi*r)**2 + (1/8)*L**3*sin(pi*r)*cos(pi*r)/(pi*r) - 1/8*L**3*sin(pi*r)**2/(pi**2*r**2) + (1/8)*L**3*cos(pi*r)**2/(pi**2*r**2) - 1/4*L**3*sin(pi*r)*cos(pi*r)/(pi**3*r**3))/L,-2*(-1/48*L**3 + (1/8)*L**3/(pi**2*r**2))/L + 2*((1/48)*L**3*sin(pi*r)**2 + (1/48)*L**3*cos(pi*r)**2 - 1/8*L**3*sin(pi*r)*cos(pi*r)/(pi*r) + (1/8)*L**3*sin(pi*r)**2/(pi**2*r**2) - 1/8*L**3*cos(pi*r)**2/(pi**2*r**2) + (1/4)*L**3*sin(pi*r)*cos(pi*r)/(pi**3*r**3))/L,-2*(2*pi*L**3*n**3*r/(-pi**3*n**6 + 3*pi**3*n**4*r**2 - 3*pi**3*n**2*r**4 + pi**3*r**6) - 2*pi*L**3*n*r**3/(-pi**3*n**6 + 3*pi**3*n**4*r**2 - 3*pi**3*n**2*r**4 + pi**3*r**6))/L + 2*((1/4)*pi**2*L**3*n**5*sin(pi*r)*cos(pi*n)/(-pi**3*n**6 + 3*pi**3*n**4*r**2 - 3*pi**3*n**2*r**4 + pi**3*r**6) - 1/4*pi**2*L**3*n**4*r*sin(pi*n)*cos(pi*r)/(-pi**3*n**6 + 3*pi**3*n**4*r**2 - 3*pi**3*n**2*r**4 + pi**3*r**6) - pi*L**3*n**4*sin(pi*n)*sin(pi*r)/(-pi**3*n**6 + 3*pi**3*n**4*r**2 - 3*pi**3*n**2*r**4 + pi**3*r**6) - 1/2*pi**2*L**3*n**3*r**2*sin(pi*r)*cos(pi*n)/(-pi**3*n**6 + 3*pi**3*n**4*r**2 - 3*pi**3*n**2*r**4 + pi**3*r**6) - 2*pi*L**3*n**3*r*cos(pi*n)*cos(pi*r)/(-pi**3*n**6 + 3*pi**3*n**4*r**2 - 3*pi**3*n**2*r**4 + pi**3*r**6) - 2*L**3*n**3*sin(pi*r)*cos(pi*n)/(-pi**3*n**6 + 3*pi**3*n**4*r**2 - 3*pi**3*n**2*r**4 + pi**3*r**6) + (1/2)*pi**2*L**3*n**2*r**3*sin(pi*n)*cos(pi*r)/(-pi**3*n**6 + 3*pi**3*n**4*r**2 - 3*pi**3*n**2*r**4 + pi**3*r**6) + 6*L**3*n**2*r*sin(pi*n)*cos(pi*r)/(-pi**3*n**6 + 3*pi**3*n**4*r**2 - 3*pi**3*n**2*r**4 + pi**3*r**6) + (1/4)*pi**2*L**3*n*r**4*sin(pi*r)*cos(pi*n)/(-pi**3*n**6 + 3*pi**3*n**4*r**2 - 3*pi**3*n**2*r**4 + pi**3*r**6) + 2*pi*L**3*n*r**3*cos(pi*n)*cos(pi*r)/(-pi**3*n**6 + 3*pi**3*n**4*r**2 - 3*pi**3*n**2*r**4 + pi**3*r**6) - 6*L**3*n*r**2*sin(pi*r)*cos(pi*n)/(-pi**3*n**6 + 3*pi**3*n**4*r**2 - 3*pi**3*n**2*r**4 + pi**3*r**6) - 1/4*pi**2*L**3*r**5*sin(pi*n)*cos(pi*r)/(-pi**3*n**6 + 3*pi**3*n**4*r**2 - 3*pi**3*n**2*r**4 + pi**3*r**6) + pi*L**3*r**4*sin(pi*n)*sin(pi*r)/(-pi**3*n**6 + 3*pi**3*n**4*r**2 - 3*pi**3*n**2*r**4 + pi**3*r**6) + 2*L**3*r**3*sin(pi*n)*cos(pi*r)/(-pi**3*n**6 + 3*pi**3*n**4*r**2 - 3*pi**3*n**2*r**4 + pi**3*r**6))/L], default=numpy.nan))
AttributeError: 'Zero' object has no attribute 'sin'