By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,784 Members | 1,182 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,784 IT Pros & Developers. It's quick & easy.

Problem calling math.cos()

P: n/a
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?
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.

Apr 22 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
In article <3e*****************@newsfe15.lga>, Sambo <sa***@void.com>
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
lot easier to read.
Apr 22 '06 #2

P: n/a
Sambo <sa***@void.com> 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
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
Apr 22 '06 #3

P: n/a
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(
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.
import electronics
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' 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
ro*********@gmail.com

"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

Apr 22 '06 #4

P: n/a
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.
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]
.... ac_add_a_ph(10, 0, 6, 45) 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 *
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]
.... ac_add_a_ph(10, 0, 6, 45)

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

--
Felipe.

Apr 22 '06 #5

P: n/a
[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.
Apr 22 '06 #6

P: n/a
In article <ma***************************************@python. org>,
"Tim Peters" <ti********@gmail.com> 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)

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?
Apr 22 '06 #7

P: n/a
[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 :-)
Apr 22 '06 #8

P: n/a
Roy Smith wrote:
In article <3e*****************@newsfe15.lga>, Sambo <sa***@void.com>
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?

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.
Apr 22 '06 #9

P: n/a
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)

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
ro*********@gmail.com

"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

Apr 22 '06 #10

P: n/a
Alex Martelli <al*****@yahoo.com> 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!
Apr 23 '06 #11

P: n/a
Thomas Bellman <be*****@lysator.liu.se> wrote:
Alex Martelli <al*****@yahoo.com> 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
Apr 23 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.