4

I'm trying to solve numerically an equation using Python2.7. This is the whole code:

from sympy import *
from sympy import Symbol
from sympy.solvers import nsolve
from scipy import *
from pylab import *
import numpy as np

# Symbols
theta = Symbol('theta')
phi = Symbol('phi')
phi0 = Symbol('phi0')
H0 = Symbol('H0')
# Constants
a = 0.05 
b = 0.05**2/(8*pi*1e-7)
c= 0.001/(4*pi*1e-7)
phi0 = 60*pi/180 
H0 = -0.03/(4*pi*1e-7)
def m(theta,phi):
    return np.array([sin(theta)*cos(phi), sin(theta)*cos(phi), cos(phi)])
def h(phi0):
    return np.array([cos(phi0), sin(phi0), 0])
def k(theta,phi,phi0):
    return np.vdot(m(theta,phi),h(phi0))
def F(theta,phi,phi0,H0): 
    return -(a*H0)*k(theta,phi,phi0)+b*(cos(theta)**2)+c*(sin(2*theta)**2)+sin(theta)**4*sin(2*phi)**2
def F_phi(theta,phi,phi0,H0):
    return simplify(diff(F(theta,phi,phi0,H0),phi))
def G(phi):
    return F_phi(pi/2,phi,phi0,H0)
solution = nsolve(G(phi), phi)
print solution

and this is the traceback that I have:

Traceback (most recent call last):
File "Test.py", line 33, in <module>
solution = nsolve(G(phi), phi)
File "Test.py", line 32, in G
return F_phi(pi/2,phi,phi0,H0)
File "Test.py", line 30, in F_phi
return simplify(diff(F(theta,phi,phi0,H0),phi))
File "Test.py", line 28, in F
return -(a*H0)*k(theta,phi,phi0)+b*(cos(theta)**2)+c*(sin(2*theta)**2)+sin(theta)**4*sin(2*phi)**2
File "Test.py", line 26, in k
return np.vdot(m(theta,phi),h(phi0))
File "Test.py", line 22, in m
return np.array([sin(theta)*cos(phi), sin(theta)*cos(phi), cos(phi)])
AttributeError: cos

I am using the sympy, numpy and pylab libraries. So, I don't get a problem with the cos function. Any help?

aymenbh
  • 151
  • 4
  • 10
  • 2
    Please include the full traceback; there is no attribute access here to *cause* the exception, so we are also probably missing some code. – Martijn Pieters Nov 11 '12 at 22:22
  • Sorry I thought the code I included was sufficient. I edited the post to include the whole code and the traceback. – aymenbh Nov 11 '12 at 23:47
  • After fixing the imports the IndexError will probably disappear, because it has the same cause. If it doesn't just let me know in a comment! – jorgeca Nov 12 '12 at 00:27

2 Answers2

9

The problem is using star imports instead of importing each package under a different namespace.

This imports function sympy.functions.elementary.trigonometric.cos under the cos name:

from sympy import *

After that, you import <ufunc 'cos'> under the name cos, overwriting the previous definition:

from scipy import *

Then, it overwrites the previous cos function by another copy of exactly the same function (from the matplotlib package):

from pylab import *

This also imports the same <ufunc 'cos'> but under the np.cos name. This is the proper way to import things:

import numpy as np

In the end, you're left with a copy of the cos function that knows how to apply itself to floats, not sympy objects. When you try to apply that function to sympy objects like phi you get the AttributeError. All in all, the solution to this particular problem is to fix the imports and know if you want the functions from sympy or the ones from numpy.

jorgeca
  • 5,482
  • 3
  • 24
  • 36
  • I just kept the imports from sympy, the IndexError disappeared but I still have a problem which I will address in another post. – aymenbh Nov 12 '12 at 01:20
2

Did you import the cos function? It's in the math module

from math import cos

Same thing for sin.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • When I try to import the functions from the math module, I get almost the same traceback with this error: "File "/usr/lib64/python2.7/site-packages/sympy/core/expr.py", line 221, in __float__ raise TypeError("can't convert expression to float") TypeError: can't convert expression to float". But when I import them from sympy, I get this: " File "/usr/lib64/python2.7/site-packages/numpy/lib/function_base.py", line 984, in diff slice1[axis] = slice(1, None) IndexError: list assignment index out of range" – aymenbh Nov 11 '12 at 23:59
  • He imported it 4 times... (3 under the same name). After fixing that, he's left wit other problems (the IndexError), but maybe that should be a different question? – jorgeca Nov 12 '12 at 00:10