math.exp(complex)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Eppstein

    math.exp(complex)

    Why doesn't this work?
    [color=blue][color=green][color=darkred]
    >>> import math
    >>> math.exp(1j*mat h.pi)[/color][/color][/color]
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: can't convert complex to float; use e.g. abs(z)

    The expected answer, of course, is -1.

    It does work if you do it like this:
    [color=blue][color=green][color=darkred]
    >>> math.e**(1j*mat h.pi)[/color][/color][/color]
    (-1+1.22464679914 73532e-16j)

    So why not math.exp(comple x)?

    --
    David Eppstein http://www.ics.uci.edu/~eppstein/
    Univ. of California, Irvine, School of Information & Computer Science
  • Raymond Hettinger

    #2
    Re: math.exp(comple x)

    [David Eppstein][color=blue]
    > Why doesn't this work?
    >[color=green][color=darkred]
    > >>> import math
    > >>> math.exp(1j*mat h.pi)[/color][/color]
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > TypeError: can't convert complex to float; use e.g. abs(z)
    >
    > The expected answer, of course, is -1.
    >
    > It does work if you do it like this:
    >[color=green][color=darkred]
    > >>> math.e**(1j*mat h.pi)[/color][/color]
    > (-1+1.22464679914 73532e-16j)
    >
    > So why not math.exp(comple x)?[/color]

    Use the cmath module for complex ops:

    [color=blue][color=green][color=darkred]
    >>> import cmath
    >>> import math
    >>> cmath.exp(1j*ma th.pi)[/color][/color][/color]

    (-1+1.22460635382 23773e-016j)


    Raymond Hettinger


    Comment

    • John J. Lee

      #3
      Re: math.exp(comple x)

      David Eppstein <eppstein@ics.u ci.edu> writes:
      [color=blue]
      > Why doesn't this work?
      >[color=green][color=darkred]
      > >>> import math
      > >>> math.exp(1j*mat h.pi)[/color][/color]
      > Traceback (most recent call last):[/color]
      [...][color=blue]
      > So why not math.exp(comple x)?[/color]

      import cmath as math

      :-)


      John

      Comment

      • David Eppstein

        #4
        Re: math.exp(comple x)

        In article <ENnbb.10075$Uv 2.662@nwrdny02. gnilink.net>,
        "Raymond Hettinger" <vze4rx4y@veriz on.net> wrote:
        [color=blue][color=green]
        > > So why not math.exp(comple x)?[/color]
        >
        > Use the cmath module for complex ops:[/color]

        Thanks, but then my next question is, why are math and cmath different
        from each other?

        And, is there any important difference (e.g. in numerical accuracy)
        between cmath.exp(x) and math.e**x ?

        --
        David Eppstein http://www.ics.uci.edu/~eppstein/
        Univ. of California, Irvine, School of Information & Computer Science

        Comment

        Working...