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


In reply to Re: root function by casiano
in thread root function by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.