[Numpy-discussion] cos -- NameError: global name 'cos' is not defined
Wayne Watson
sierra_mtnview@sbcglobal....
Mon Dec 21 13:44:44 CST 2009
Yes, one can get both sin and cos via the interactive shell, if math is
imported as you have done.
However, I thought math itself always present to a program module? In
my program, sin exists but not cos, so one is forced to use math.cos().
Why one but not the other?
Keith Goodman wrote:
> 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
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
--
Wayne Watson (Watson Adventures, Prop., Nevada City, CA)
(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet
"... humans'innate skills with numbers isn't much
better than that of rats and dolphins."
-- Stanislas Dehaene, neurosurgeon
Web Page: <www.speckledwithstars.net/>
More information about the NumPy-Discussion
mailing list