'float' Object Has No Attribute 'sin'
Solution 1:
You've mixed up your imports a bit by importing the individual functions and also referring to them using np.
:
import numpy as np
from numpy import sin, cos, pi
[...]sin(n*np.pi*x/L +n*np.pi/2)[...]
Either:
import numpy as np
[...]np.sin(n*np.pi*X/l + n*np.pi/2[...]
Or:
from numpy import sin, cos, pi
[...]sin(n*pi*X/l + n*pi/2[...]
Also you never import sym
so the code you've posted is not the code that's producing that error as the compiler would trip over that first. Can you post the actual code that generated this error, or ideally a minimal example.
Solution 2:
Fix Your Imports
Check @Ari Cooper-Davis's answer for these fixes first. Make sure the code you post compiles if your going to include import statements.
Here's one way:
import sympy as sym
import numpy as np
from scipy.integrate import quad
You're not going to need numpy's sin, cos, and pi methods because we'll use the sympy ones.
Whitespace
Make sure you when you have an expression, the whitespace around your operators is even. In other words, you can have 1 + 2
and 1+2
but not 1 +2
or 1+ 2
. This is just good practice for readability and helps you catch errors in the long run. This long expression strings get really confusing really fast.
Sympy vs. Numpy
These two libraries don't tend to play nice together. One is symbolic and the other is numeric.
As a way to fix this, replace all your np
calls with sym
(assuming you use import sympy as sym
). Sympy has it's own version of sin and pi which you are interchanging with Numpy's versions.
Like so:
return ((2/L)**(1/2)) * sym.sin(n * sym.pi * (x/L) + n * sym.pi/2) * 4.7 * (1 - sym.exp(-x))**2 * (2/L)**(1/2) * sym.sin(r * sym.pi * x/L + r * sym.pi/2)
Solution 3:
If you make a numpy array from mixed objects, such as numbers, strings or symbols, you get an object dtype array.
np.sin(arr)
on such an array is performed by calling [x.sin() for x in arr]
, i.e. it delegates the task to the sin
method of each element. Most objects, including floats, don't have such a method, so this fails. The same applies to other numpy
functions such as exp
and sqrt
. Somethings delegate just fine, such as addition.
The only indication of your using sympy
is the sym.Matrix
call. I don't understand what you are trying to do with that. What kind of symbolic form are you expecting? sympy
isn't going to 'reach' into the scipy/numpy
code and convert it to symbols. At best it will try to evaluate the code with by passing symbols to the numpy code, resulting in this error.
Mixing sympy and numpy is tricky, and more often than not wrong.
Why sympy lambdify function cannot identify numpy sum function and multiply function
is another example of trying to use sympy and numpy together. My answer is long and and convoluted, but I left it that way to give a sense of how tricky it is to use the two packages together.
In an isympy
session:
In[8]: MatrixOut[8]: sympy.matrices.dense.MutableDenseMatrixIn[9]: Matrix(3,3, lambda n,r: n+r)
Out[9]:
⎡012⎤
⎢ ⎥
⎢123⎥
⎢ ⎥
⎣234⎦
Matrix
makes a sympy object by passing the indices to the function. We see this more clearly with:
In [10]: deffoo(n,r):
...: print(n,r, type(n), type(r))
...: return n+r
...:
In [11]: Matrix(3,3,foo)
00 <class'sympy.core.numbers.Zero'> <class'sympy.core.numbers.Zero'>
01 <class'sympy.core.numbers.Zero'> <class'sympy.core.numbers.One'>
...
Out[11]:
⎡012⎤
⎢ ⎥
⎢123⎥
⎢ ⎥
⎣234⎦
If we try to use np.sin
in this function:
In [15]: def foo(n,r):
...: return np.sin(r)
...:
In [16]: Matrix(3,3,foo)
---------------------------------------------------------------------------
AttributeError Traceback (most recent calllast)
AttributeError: 'Zero' object has no attribute 'sin'
...
As I explained above, trying to use nonumeric values as np.sin
arguments results in the error in delegation to a sin
method.
Your code further complicates things by passing the values through the scipy.quad
, which in turn passes the integration variable x
plus n
and r
to Phi_V
.
Still in the isympy
session,
In [26]: L = 1
...:
...: r_e = 1.4
...:
...: mu = 916
Using the function that @Ian suggests, using the sympy
versions of sin
, etc:
In [27]: def foo(n,r):
...: return ((2/L)**(1/2)) * sin(n * pi * (x/L) + n * pi/2) * 4.7 * (1 -
...: exp(-x))**2 * (2/L)**(1/2) * sin(r * pi * x/L + r * pi/2)
...:
In [28]: foo(1,2)
Out[28]:
2
⎛ -x⎞
-9.4⋅⎝1 - ℯ ⎠ ⋅sin(2⋅π⋅x)⋅cos(π⋅x)
In [29]: Matrix(3,3,foo)
Out[29]:
⎡000 ⎤
⎢ ⎥
⎢ 22 ⎥
⎢ ⎛ -x⎞ 2 ⎛ -x⎞ ⎥
⎢09.4⋅⎝1 - ℯ ⎠ ⋅cos (π⋅x) -9.4⋅⎝1 - ℯ ⎠ ⋅sin(2⋅π⋅x)⋅cos(π⋅x)⎥
⎢ ⎥
⎢ 22 ⎥
⎢ ⎛ -x⎞ ⎛ -x⎞ 2 ⎥
⎣0-9.4⋅⎝1 - ℯ ⎠ ⋅sin(2⋅π⋅x)⋅cos(π⋅x) 9.4⋅⎝1 - ℯ ⎠ ⋅sin (2⋅π⋅x) ⎦
But this doesn't display anything about the quad
integration. Nor can it be used as the objective function in quad
.
Post a Comment for "'float' Object Has No Attribute 'sin'"