473,399 Members | 3,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Problem calling math.cos()

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
11 7302
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
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
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
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
[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
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
[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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Karl Max | last post by:
Hi all, I'm new here. Name's Max from tuscany, and I don't speak english very well :-) I am not veteran at coding, I am most like an "artisan coder" since commodore 64 times. Now the problem:...
2
by: os2 | last post by:
hi i tried to improve performance on for some trigonometry calcule i read http://www.javaworld.com/javatips/jw-javatip141.html? i do tmp@linux:~/bench> echo $CLASSPATH
3
by: Cary West | last post by:
Hello all, having a small problem with a trig routine using python math module. This code is designed to convert geodetic coordinates to lambert conformal conic coordinates. It implements the...
15
by: Morgan Cheng | last post by:
Hi, I am writing a program that will take a lot of Math.Cos & Math.Sin operation. I am afraid this will be source of performance impact. Anybody knows how Math.cos & Math.Sin is implemented?...
15
by: Chris | last post by:
>>from math import * 0.0 1.2246063538223773e-016 -2.4492127076447545e-016 1.0 -1.0 1.0 The cosine function works fine, but I'm getting weird answers for sine. Is this a bug? Am I doing...
7
by: shaun roe | last post by:
Hi, I am using an XSLT to generate an SVG client-side in Firefox. The user opens an XML file in Firefox and sees a display. In doing so, I have to convert from cartesian to polar coordinates, so...
8
by: pgpgpg | last post by:
Hi, I'm new to Python so apologies in advance for the simple nature of this question. I'm iterating through a list of dicts, with the intention of pulling out the 4 values associated with the key...
5
by: aguirre.adolfo | last post by:
Hi, I am a very newbie who would very much appreciate some hints. Python 2.52. on Windows XP for now. Soon on Ubuntu 8 I am teaching myself Python following free tutorials. I can solve...
10
by: manjava | last post by:
I have a problem in my method I know how to continue to find the shortest distance and then extract cinques the nearest distance method must return cinques longitudes and latitudes with their cities...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.