in reply to root function

Perl with the help of PDL::Complex can cope with Complex Numbers. See below:

pp2@nereida:~/.ssh$ perl -wde 0 main::(-e:1): 0 DB<1> use PDL DB<2> use PDL::Complex DB<3> $x = r2C(-27) # From real to complex DB<4> $r = Croots $x, 3 # There are three complex roots DB<5> print $r # It is sad that there is a bug # with "" overload Use of uninitialized value in numeric ... [ 1.5 +2.59807621135332i -3 +3.67381906146713e-16i 1.5 -2.59807621135332i ] DB<6> x $ra # The answer is a PDL object 0 PDL::Complex=SCALAR(0x8a6a0a8) -> 144899352 DB<7> p re $r # Get the "real" components [1.5 -3 1.5] DB<8> p im $r # And the imaginary ones: [ 2.5980762 3.6738191e-16 -2.5980762] DB<9> $y = $r ** 3 # Check the solution: DB<10> p re $y [-27 -27 -27] DB<11> p im $y # Almost 0, as expected [3.3064372e-15 9.9193115e-15 6.8636015e-14]
Hope it helps

Casiano

Replies are listed 'Best First'.
Re^2: root function
by etj (Priest) on Jun 06, 2022 at 02:17 UTC
    And PDL 2.040 or so introduced "native" complex numbers, without the visible real/imaginary dimension. It doesn't yet have an analogue of the Croots (multiple complex roots), evidently due to a lack of demand.

    It would not be hard to implement, since the n roots of a complex number can be calculated very easily using https://en.wikipedia.org/wiki/De_Moivre%27s_formula: root_n = r(cos(theta + 2pi/n) + i sin(theta + 2pi/n)). Pull requests welcome!