Reputation: 1
import math
import numpy as np
import matplotlib.pyplot as plt
import simpy as sp
init_printing()
var('x,y,z')
a = math.sqrt(10/7)
c = (y-math.sqrt(18/7)-1)
b = np.sin(math.pi/2*c)-z
f = lambda x,y,z: x**2+y**2+z**2-4
g = lambda x, y, z: (9/2)*(x**2+z**2)+y**2-9
h = lambda y, z: np.sqrt(10/7) * np.sin((np.pi/2) * y - np.sqrt(18/7)-1)- z
F = [[f(x, y, z)],
[g(x, y, z)],
[h(y, z)]]
As I try to define the fuction h(x)
I get this:
AttributeError Traceback (most recent call last)
AttributeError: 'Add' object has no attribute 'sin'
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last)
<ipython-input-68-da3a7e28861c> in <module>
10 a = math.sqrt(10/7)
11 c = (y-math.sqrt(18/7)-1)
---> 12 b = np.sin((math.pi/2*c))-z
13
14
TypeError: loop of ufunc does not support argument 0 of type Add which has no callable sin method
I tried to separete the function in parts a, b and c so the error could be more easy to find. Now I know I have some problem with the var('x, y, z')
and the np.sin()/math.sin()
, but I can't find how to fix it.
Upvotes: 0
Views: 8102
Reputation: 231385
Pay close attention to the type
of the variables. When in doubt, check, don't just guess or assume
var('x,y,z')
these are sympy
symbols (though here I'm guessing what var
is doing)
a = math.sqrt(10/7)
this must be a python float, produced by the math.sqrt
function.
c = (y-math.sqrt(18/7)-1)
Assuming y
is sympy, then c
itself is a sympy
expression, probably an Add
.
b = np.sin(math.pi/2*c)-z
math.pi/2*c
is sympy expression.
np.sin
is a numpy ufunc
, and only works with numpy arrays. That means it first does
np.array(math.pi/2*c)
making a single element object dtype array.
np.sin
given an object dtype array, passes the task to the sin
method of that object. But noone defines a sin
method. It's always a function.
Using np.sin
, or many other numpy
functions on sympy
expressions just does not work. Don't mix sympy
and numpy
until you know what you are doing!
In [5]: sp.var('x,y,z')
Out[5]: (x, y, z)
In [6]: y
Out[6]: y
In [7]: a = math.sqrt(10/7)
...: c = (y-math.sqrt(18/7)-1)
In [8]: a
Out[8]: 1.1952286093343936
In [9]: c
Out[9]: y - 2.60356745147455
In [10]: type(c)
Out[10]: sympy.core.add.Add
In [11]: math.pi/2*c
Out[11]: 1.5707963267949*y - 4.08967418933897
In [12]: np.array(_)
Out[12]: array(1.5707963267949*y - 4.08967418933897, dtype=object)
In [13]: np.sin(_)
AttributeError: 'Add' object has no attribute 'sin'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<ipython-input-13-2b409a55b2a2>", line 1, in <module>
np.sin(_)
TypeError: loop of ufunc does not support argument 0 of type Add which has no callable sin method
In [14]: a = sp.sqrt(10/7)
...: c = (y-sp.sqrt(18/7)-1)
In [15]: c
Out[15]: y - 2.60356745147455
In [16]: sp.pi/2*c
Out[16]: pi*(y - 2.60356745147455)/2
In [17]: sp.sin(sp.pi/2*c)
Out[17]: sin(pi*(y/2 - 1.30178372573727))
Upvotes: 1