Problem calling math.cos()

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

  1. Sambo

    Sambo Guest

    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.
     
    Sambo, Apr 22, 2006
    #1
    1. Advertising

  2. Sambo

    Roy Smith Guest

    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
    lot easier to read.
     
    Roy Smith, Apr 22, 2006
    #2
    1. Advertising

  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 )


    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
     
    Alex Martelli, Apr 22, 2006
    #3
  4. Sambo

    Robert Kern Guest

    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


    "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
     
    Robert Kern, Apr 22, 2006
    #4
  5. 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.
     
    Felipe Almeida Lessa, Apr 22, 2006
    #5
  6. Sambo

    Tim Peters Guest

    [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.
     
    Tim Peters, Apr 22, 2006
    #6
  7. Sambo

    Roy Smith Guest

    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)
    >
    > 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?
     
    Roy Smith, Apr 22, 2006
    #7
  8. Sambo

    Tim Peters Guest

    [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
    #8
  9. Sambo

    Sambo Guest

    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
    >>.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.
     
    Sambo, Apr 22, 2006
    #9
  10. Sambo

    Robert Kern Guest

    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


    "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
     
    Robert Kern, Apr 22, 2006
    #10
  11. 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
    #11
  12. 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
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. aj
    Replies:
    10
    Views:
    5,626
    james
    Dec 31, 2005
  2. Active8
    Replies:
    4
    Views:
    397
    Active8
    Feb 22, 2005
  3. Brandon McCombs

    Math.cos() problems

    Brandon McCombs, Dec 6, 2007, in forum: Java
    Replies:
    8
    Views:
    426
    Richard F.L.R.Snashall
    Dec 8, 2007
  4. harryos
    Replies:
    8
    Views:
    545
    Roedy Green
    Nov 4, 2009
  5. VK
    Replies:
    15
    Views:
    1,337
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page