Problem calling math.cos()

S

Sambo

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. ???
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?
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.
 
R

Roy Smith

Sambo <[email protected]> said:
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. ???
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:
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
lot easier to read.
 
A

Alex Martelli

Sambo said:
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. ???
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
that error in your code.

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
 
R

Robert Kern

Sambo said:
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. ???

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(
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 )
[~]$ python
Python 2.4.1 (#2, Mar 31 2005, 00:05:10)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1666)] on darwin
Type "help", "copyright", "credits" or "license" for more information.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
electronics.ac_add_a_ph(10, 0, 6, 45) [14.861117513241918, 0.2895134725436232]

--
Robert Kern
(e-mail address removed)

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
F

Felipe Almeida Lessa

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
Type "help", "copyright", "credits" or "license" for more information..... 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 ?
.... 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
.... ph3 = math.atan(ac3.imag / ac3.real)
.... return [abs(amp3), ph3]
....[14.86111751324192, 0.28951347254362308]




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

def polar(rho, theta, theta_in_radians=False)
"""Creates a complex number from its polar form."""
# Avoid repeating yourself by creating different functions
if not theta_in_radians:
theta = math.radians(theta)
return complex(rho * math.cos(theta), rho * math.sin(theta))

def ac_add_a_ph(amp1, ph1, amp2, ph2):
"""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,
 
T

Tim Peters

[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)

instead.
 
R

Roy Smith

"Tim Peters said:
[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)

instead.

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?
 
T

Tim Peters

[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 :)
 
S

Sambo

Roy said:
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:


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
lot easier to read.

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.
 
R

Robert Kern

Tim said:
[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)

instead.

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
(e-mail address removed)

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
T

Thomas Bellman

Alex Martelli said:
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>.
 
A

Alex Martelli

Thomas Bellman said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top