[Numpy-discussion] cos -- NameError: global name 'cos' is not defined

Keith Goodman kwgoodman@gmail....
Mon Dec 21 11:44:42 CST 2009


On Mon, Dec 21, 2009 at 9:40 AM, Wayne Watson
<sierra_mtnview@sbcglobal.net> wrote:
> In this code,
> ===========start
> import math
> import numpy as np
> from numpy import matrix
> def sinD(D): # given in degrees, convert to radians
>    return sin(radians(D))
> def cosD(D):
>    return cos(radians(D))   <<--------------
> def acosD(D):
>    acos(radians(D))
>    return=====end
> the << line produces, "NameError: global name 'cos' is not defined", but
> the sin() above it does not? They are both built-in functions.

>> sin(10)
NameError: name 'sin' is not defined

Oh, right, there is no built-in sin function. I need to import it:

>> import numpy as np
>> import math
>>
>> math.sin(1)
   0.8414709848078965
>> np.sin(1)
   0.8414709848078965

or

>> from numpy import sin
>> sin(1)
   0.8414709848078965


More information about the NumPy-Discussion mailing list