top | item 41940628

(no title)

hpcjoe | 1 year ago

Same in Julia, but no need to "import math", its already built in :D

    joe@zap:~ $ julia
                   _
       _       _ _(_)_     |  Documentation: https://docs.julialang.org    
      (_)     | (_) (_)    |
       _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
      | | | | | | |/ _` |  |
      | | |_| | | | (_| |  |  Version 1.10.5 (2024-08-27)
     _/ |\__'_|_|_|\__'_|  |
    |__/                   |

    julia> z=1+2*im
    1 + 2im

    julia> z*conj(z)
    5 + 0im

    julia> sqrt(z*conj(z))
    2.23606797749979 + 0.0im

    julia> abs(z)
    2.23606797749979

discuss

order

zahlman|1 year ago

it isn't necessary in Python, either. GP is only importing it for square roots, but exponentiation (via the ** operator) by .5 works fine with complex numbers in Python as well. It even handles complex exponents (although you'd have to look up the branch cut semantics etc. in the documentation):

    >>> (1+1j)**(1+1j)
    (0.2739572538301211+0.5837007587586147j)
Ironically, the `math` library doesn't handle that:

    >>> math.pow(1+1j, 1+1j)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: must be real number, not complex

rdtsc|1 year ago

Definitely. I was mainly trying to avoid using `**` as non-Python users might be more familiar with '^' instead.

cbolton|1 year ago

Works in Julia:

  julia> (1+im)^(1+im)
  0.2739572538301211 + 0.5837007587586147im