Problem calling math.cos()

Discussion in 'Python' started by Sambo, Apr 22, 2006.

1. SamboGuest

I have the following module:
-------------------------------
import math

def ac_add_a_ph( amp1, ph1, amp2, ph2 ):

amp3 = 0.0
ph3 = 0.0
ac1 = ( 0, 0j )
ac2 = ( 0, 0j )
ac3 = ( 0, 0j )
ac1 = complex( amp1 * math.cos( math.radians( ph1 ) ), amp1 * math.sin( math.radians( ph1 ) ) )
ac2 = complex( amp2 * math.cos( math.radians( ph2 ) ), amp2 * math.sin( math.radians( ph2 ) ) )
ac3 = ac1 + ac2
amp3 = math.abs( ac3 )
ph3 = math.atan( ac3.imag / ac3.real )
return [amp3, ph3]
--------------------------------------
when I import it (electronics) in python.exe in windows2000 and
try to use it, it croaks. ???

>>> import math
>>> import electronics
>>> print electronics.ac_add_a_ph( 10, 0 , 6 , 45 )

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "f:\devel\python\electronics.py", line 10, in ac_add_a_ph
ac1 = complex( amp1 * math.cos( math.radians( ph1 ) ), amp1 * math.sin( math
NameError: global name 'cos' is not defined
>>>

global?? huh?
what does abs stand for? why is that not absolute value? hmmm.
Hmm, complex numbers, cool I don't even have any idea where C
stands on this.

Sambo, Apr 22, 2006

2. Roy SmithGuest

In article <3ev2g.9680\$%>, Sambo <>
wrote:

> I have the following module:
> -------------------------------
> import math
>
> def ac_add_a_ph( amp1, ph1, amp2, ph2 ):
>
> amp3 = 0.0
> ph3 = 0.0
> ac1 = ( 0, 0j )
> ac2 = ( 0, 0j )
> ac3 = ( 0, 0j )
> ac1 = complex( amp1 * math.cos( math.radians( ph1 ) ), amp1 * math.sin(
> math.radians( ph1 ) ) )
> ac2 = complex( amp2 * math.cos( math.radians( ph2 ) ), amp2 * math.sin(
> math.radians( ph2 ) ) )
> ac3 = ac1 + ac2
> amp3 = math.abs( ac3 )
> ph3 = math.atan( ac3.imag / ac3.real )
> return [amp3, ph3]
> --------------------------------------
> when I import it (electronics) in python.exe in windows2000 and
> try to use it, it croaks. ???
>
> >>> import math
> >>> import electronics
> >>> print electronics.ac_add_a_ph( 10, 0 , 6 , 45 )

> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> File "f:\devel\python\electronics.py", line 10, in ac_add_a_ph
> ac1 = complex( amp1 * math.cos( math.radians( ph1 ) ), amp1 * math.sin(
> math
> .radians( ph1 ) ) )
> NameError: global name 'cos' is not defined
> >>>

That's not what I get when I run it (admittedly, not on windows). I get:

>>> import math
>>> import electronics
>>> print electronics.ac_add_a_ph( 10, 0 , 6 , 45 )

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "electronics.py", line 13, in ac_add_a_ph
amp3 = math.abs( ac3 )
AttributeError: 'module' object has no attribute 'abs'
>>>

which is exactly what I expected, since abs (which is indeed absolute
value) is a built-in function, not a part of the math module. Are you sure
the stack trace you posted matches the source code you posted?

By the way, when using math functions, I find it's usually easier to import
them into my namespace by doing "from math import *", then I can just use
sin(), cos(), etc directly, instead of having to do math.sin() or
math.cos(). Especially for common math functions, this makes your code a

Roy Smith, Apr 22, 2006

3. Alex MartelliGuest

Sambo <> wrote:

> I have the following module:
> -------------------------------
> import math
>
> def ac_add_a_ph( amp1, ph1, amp2, ph2 ):
>
> amp3 = 0.0
> ph3 = 0.0
> ac1 = ( 0, 0j )
> ac2 = ( 0, 0j )
> ac3 = ( 0, 0j )

You're defining ac1, ac2, ac3 as tuples, each with two items. That's
silly: remove these three useless and confusing lines (the first two are
prety silly too). No damage, but, avoidable extra confusion.

> ac1 = complex( amp1 * math.cos( math.radians( ph1 ) ), amp1 * \

math.sin( math.radians( ph1 ) ) )
> ac2 = complex( amp2 * math.cos( math.radians( ph2 ) ), amp2 * \

math.sin( math.radians( ph2 ) ) )
> ac3 = ac1 + ac2
> amp3 = math.abs( ac3 )
> ph3 = math.atan( ac3.imag / ac3.real )
> return [amp3, ph3]
> --------------------------------------
> when I import it (electronics) in python.exe in windows2000 and
> try to use it, it croaks. ???
>
> >>> import math
> >>> import electronics
> >>> print electronics.ac_add_a_ph( 10, 0 , 6 , 45 )

> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> File "f:\devel\python\electronics.py", line 10, in ac_add_a_ph
> ac1 = complex( amp1 * math.cos( math.radians( ph1 ) ), amp1 * \

math.sin( math
> .radians( ph1 ) ) )

[[some lines split to respect NNTP's constraint on 80-char lines]]

> NameError: global name 'cos' is not defined
> >>>

>
>
> global?? huh?

Weird -- I can't reproduce this; it's the kind of symptom you get when
mistakenly using a comma instead of a dot, for example, but I don't see

What I _do_ see is an "AttributeError: 'module' object has no attribute
'abs'" on the amp3 assignment -- of course, because that's indeed the
fact (abs is a builtin, not a member to module math).

Most likely, you got a bit confused and didn't copy-and-paste exactly
what was going on.

> what does abs stand for? why is that not absolute value? hmmm.

abs does stand for absolute-value.

> Hmm, complex numbers, cool I don't even have any idea where C
> stands on this.

C has no stand on complex numbers.

Alex

Alex Martelli, Apr 22, 2006
4. Robert KernGuest

Sambo wrote:
> I have the following module:
> -------------------------------
> import math
>
> def ac_add_a_ph( amp1, ph1, amp2, ph2 ):
>
> amp3 = 0.0
> ph3 = 0.0
> ac1 = ( 0, 0j )
> ac2 = ( 0, 0j )
> ac3 = ( 0, 0j )
> ac1 = complex( amp1 * math.cos( math.radians( ph1 ) ), amp1 * math.sin( math.radians( ph1 ) ) )
> ac2 = complex( amp2 * math.cos( math.radians( ph2 ) ), amp2 * math.sin( math.radians( ph2 ) ) )
> ac3 = ac1 + ac2
> amp3 = math.abs( ac3 )
> ph3 = math.atan( ac3.imag / ac3.real )
> return [amp3, ph3]
> --------------------------------------
> when I import it (electronics) in python.exe in windows2000 and
> try to use it, it croaks. ???
>
>>>>import math
>>>>import electronics
>>>>print electronics.ac_add_a_ph( 10, 0 , 6 , 45 )

>
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> File "f:\devel\python\electronics.py", line 10, in ac_add_a_ph
> ac1 = complex( amp1 * math.cos( math.radians( ph1 ) ), amp1 * math.sin( math
> .radians( ph1 ) ) )
> NameError: global name 'cos' is not defined
>
> global?? huh?

That's not what I get.

[~]\$ cat electronics.py
import math

def ac_add_a_ph( amp1, ph1, amp2, ph2 ):

amp3 = 0.0
ph3 = 0.0
ac1 = ( 0, 0j )
ac2 = ( 0, 0j )
ac3 = ( 0, 0j )
ac1 = complex( amp1 * math.cos( math.radians( ph1 ) ), amp1 * math.sin(
ac2 = complex( amp2 * math.cos( math.radians( ph2 ) ), amp2 * math.sin(
ac3 = ac1 + ac2
amp3 = math.abs( ac3 )
ph3 = math.atan( ac3.imag / ac3.real )
[~]\$ python
Python 2.4.1 (#2, Mar 31 2005, 00:05:10)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1666)] on darwin
>>> import electronics

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "electronics.py", line 13, in ac_add_a_ph
amp3 = math.abs( ac3 )
AttributeError: 'module' object has no attribute 'abs'
>>>

> what does abs stand for? why is that not absolute value? hmmm.
> Hmm, complex numbers, cool I don't even have any idea where C
> stands on this.

Change math.abs() to abs(). It's a builtin function. Yes, it does compute the
absolute value. Fixing that:

>>> import electronics

[14.861117513241918, 0.2895134725436232]
>>>

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
an underlying truth."
-- Umberto Eco

Robert Kern, Apr 22, 2006
5. Felipe Almeida LessaGuest

Em SÃ¡b, 2006-04-22 Ã s 15:14 -0400, Sambo escreveu:
> when I import it (electronics) in python.exe in windows2000 and
> try to use it, it croaks. ???

\$ python2.4
Python 2.4.3 (#2, Mar 30 2006, 21:52:26)
[GCC 4.0.3 (Debian 4.0.3-1)] on linux2
>>> import math
>>> def ac_add_a_ph( amp1, ph1, amp2, ph2 ):

.... amp3 = 0.0
.... ph3 = 0.0
.... ac1 = ( 0, 0j )
.... ac2 = ( 0, 0j )
.... ac3 = ( 0, 0j )
.... ac1 = complex( amp1 * math.cos( math.radians( ph1 ) ), amp1 *
math.sin( math.radians( ph1 ) ) )
.... ac2 = complex( amp2 * math.cos( math.radians( ph2 ) ), amp2 *
math.sin( math.radians( ph2 ) ) )
.... ac3 = ac1 + ac2
.... amp3 = math.abs( ac3 )
.... ph3 = math.atan( ac3.imag / ac3.real )
.... return [amp3, ph3]
....

Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 10, in ac_add_a_ph
AttributeError: 'module' object has no attribute 'abs'
>>> abs

<built-in function abs>
>>> def ac_add_a_ph(amp1, ph1, amp2, ph2):

.... ac1 = complex(amp1 * math.cos(math.radians(ph1)), amp1 *
.... ac2 = complex(amp2 * math.cos(math.radians(ph2)), amp2 *
.... ac3 = ac1 + ac2
.... ph3 = math.atan(ac3.imag / ac3.real)
.... return [abs(amp3), ph3]
....

[14.86111751324192, 0.28951347254362308]

So:
-------------------------------
import math

"""Creates a complex number from its polar form."""
# Avoid repeating yourself by creating different functions
return complex(rho * math.cos(theta), rho * math.sin(theta))

"""Add two complexes together from their polar form."""
# You don't have to initialize the variables with "0.0" and such.
ac3 = polar(amp1, ph1) + polar(amp2, ph2)
ph3 = math.atan(ac3.imag / ac3.real)
return (abs(ac3), ph3) # Use a tuple in this case
--------------------------------------

*But*, I encourage you using the complex numbers themselves instead of
converting to and from over and over.

HTH,

--
Felipe.

Felipe Almeida Lessa, Apr 22, 2006
6. Tim PetersGuest

[Robert Kern]
> ...
> ph3 = math.atan( ac3.imag / ac3.real )
> ...

Don't do that: atan2 is the correct way to compute the angle, because
the signs of both inputs are needed to determine the correct quadrant.
So do:

ph3 = math.atan2(ac3.imag, ac3.real)

Tim Peters, Apr 22, 2006
7. Roy SmithGuest

In article <>,
"Tim Peters" <> wrote:

> [Robert Kern]
> > ...
> > ph3 = math.atan( ac3.imag / ac3.real )
> > ...

>
> Don't do that: atan2 is the correct way to compute the angle, because
> the signs of both inputs are needed to determine the correct quadrant.
> So do:
>
> ph3 = math.atan2(ac3.imag, ac3.real)
>

I certainly agree about using atan2() instead of atan(), but I'm surprised
there's not an easier way to get the phase of a complex, just like abs()
gives you the modulus. I can see why you wouldn't want to pollute the
global namespace with another built-in just for this purpose, but surely a
complex.phase property wouldn't hurt?

Roy Smith, Apr 22, 2006
8. Tim PetersGuest

[Roy Smith]
> I certainly agree about using atan2() instead of atan(), but I'm surprised
> there's not an easier way to get the phase of a complex, just like abs()
> gives you the modulus. I can see why you wouldn't want to pollute the
> global namespace with another built-in just for this purpose, but surely a
> complex.phase property wouldn't hurt?

Or method. "Does anyone care enough to do the work?" is the real
question. I don't

Tim Peters, Apr 22, 2006
9. SamboGuest

Roy Smith wrote:
> In article <3ev2g.9680\$%>, Sambo <>
> wrote:
>
>
>>I have the following module:
>>-------------------------------
>>import math
>>
>>def ac_add_a_ph( amp1, ph1, amp2, ph2 ):
>>
>> amp3 = 0.0
>> ph3 = 0.0
>> ac1 = ( 0, 0j )
>> ac2 = ( 0, 0j )
>> ac3 = ( 0, 0j )
>> ac1 = complex( amp1 * math.cos( math.radians( ph1 ) ), amp1 * math.sin(
>> math.radians( ph1 ) ) )
>> ac2 = complex( amp2 * math.cos( math.radians( ph2 ) ), amp2 * math.sin(
>> math.radians( ph2 ) ) )
>> ac3 = ac1 + ac2
>> amp3 = math.abs( ac3 )
>> ph3 = math.atan( ac3.imag / ac3.real )
>> return [amp3, ph3]
>>--------------------------------------
>>when I import it (electronics) in python.exe in windows2000 and
>>try to use it, it croaks. ???
>>
>>
>>>>>import math
>>>>>import electronics
>>>>>print electronics.ac_add_a_ph( 10, 0 , 6 , 45 )

>>
>>Traceback (most recent call last):
>> File "<stdin>", line 1, in ?
>> File "f:\devel\python\electronics.py", line 10, in ac_add_a_ph
>> ac1 = complex( amp1 * math.cos( math.radians( ph1 ) ), amp1 * math.sin(
>> math
>>NameError: global name 'cos' is not defined
>>

>
> That's not what I get when I run it (admittedly, not on windows). I get:
>
>
>>>>import math
>>>>import electronics
>>>>print electronics.ac_add_a_ph( 10, 0 , 6 , 45 )

>
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> File "electronics.py", line 13, in ac_add_a_ph
> amp3 = math.abs( ac3 )
> AttributeError: 'module' object has no attribute 'abs'
>
>
> which is exactly what I expected, since abs (which is indeed absolute
> value) is a built-in function, not a part of the math module. Are you sure
> the stack trace you posted matches the source code you posted?
>

Well I took the abs( 'complex' ) from the python documentation (python24.chm)
section 3.1.1
has the following comment after it '# sqrt(a.real**2 + a.imag**2)'

> By the way, when using math functions, I find it's usually easier to import
> them into my namespace by doing "from math import *", then I can just use
> sin(), cos(), etc directly, instead of having to do math.sin() or
> math.cos(). Especially for common math functions, this makes your code a

Ah, I thought I used to use module functions without the module name.

I think my problem is reimporting electronics(.py) after modifications.
Yes, now it complains about abs().
looks like enother reason to dump this w2000 installation just so I can
install python from scratch and use idle.

Sambo, Apr 22, 2006
10. Robert KernGuest

Tim Peters wrote:
> [Robert Kern]
>
>>...
>> ph3 = math.atan( ac3.imag / ac3.real )
>>...

>
> Don't do that: atan2 is the correct way to compute the angle, because
> the signs of both inputs are needed to determine the correct quadrant.
> So do:
>
> ph3 = math.atan2(ac3.imag, ac3.real)
>

Hey, I just copied his code to show that it gave a different error than the one
he said. I didn't bother to fix it.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
an underlying truth."
-- Umberto Eco

Robert Kern, Apr 22, 2006
11. Thomas BellmanGuest

Alex Martelli <> wrote:

> C has no stand on complex numbers.

If by that you mean that C does not have complex numbers, then
you are wrong. C99 defines the three types float _Complex,
double _Complex, and long double _Complex, and also the header
<complex.h>.

--
Thomas Bellman, Lysator Computer Club, Linköping University, Sweden
"God is real, but Jesus is an integer." ! bellman @ lysator.liu.se
! Make Love -- Nicht Wahr!

Thomas Bellman, Apr 23, 2006
12. Alex MartelliGuest

Thomas Bellman <> wrote:

> Alex Martelli <> wrote:
>
> > C has no stand on complex numbers.

>
> If by that you mean that C does not have complex numbers, then
> you are wrong. C99 defines the three types float _Complex,
> double _Complex, and long double _Complex, and also the header
> <complex.h>.

Yeah, I was thinking of the previous (C89) standard, the one which IS
(still) required to build Python.

Alex

Alex Martelli, Apr 23, 2006