Python AttributeError:cos

无人久伴 提交于 2019-12-01 06:20:51

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.

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

from math import cos

Same thing for sin.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!