home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / Complex.pm < prev    next >
Encoding:
Perl POD Document  |  2002-09-09  |  15.1 KB  |  740 lines

  1. ############# Class : cmplx ##############
  2. package Math::Cephes::Complex;
  3. use strict;
  4. use vars qw(%OWNER %BLESSEDMEMBERS %ITERATORS 
  5.         @EXPORT_OK %EXPORT_TAGS $VERSION);
  6. require Math::Cephes;
  7.  
  8. require Exporter;
  9. *import = \&Exporter::import;
  10. #my @cmplx = qw(clog cexp csin ccos ctan ccot casin cmplx
  11. #           cacos catan cadd csub cmul cdiv cmov cneg cabs csqrt
  12. #           csinh ccosh ctanh cpow casinh cacosh catanh);
  13. @EXPORT_OK = qw(cmplx);
  14. #%EXPORT_TAGS = ('cmplx' => [qw(cmplx)]);
  15.  
  16. %OWNER = ();
  17. %BLESSEDMEMBERS = ();
  18. %ITERATORS = ();
  19. $VERSION = '0.36';
  20.  
  21. sub new {
  22.     my $self = shift;
  23.     my @args = @_;
  24.     $self = Math::Cephes::new_cmplx(@args);
  25.     return undef if (!defined($self));
  26.     bless $self, "Math::Cephes::Complex";
  27.     $OWNER{$self} = 1;
  28.     my %retval;
  29.     tie %retval, "Math::Cephes::Complex", $self;
  30.     return bless \%retval,"Math::Cephes::Complex";
  31. }
  32.  
  33. sub r {
  34.     my ($self, $value) = @_;
  35.     return $self->{r} unless $value;
  36.     $self->{r} = $value;
  37.     return $value;
  38. }
  39.  
  40. sub i {
  41.     my ($self, $value) = @_;
  42.     return $self->{i} unless $value;
  43.     $self->{i} = $value;
  44.     return $value;
  45. }
  46.  
  47. sub cmplx {
  48.   return Math::Cephes::Complex->new(@_);
  49. }
  50.  
  51. sub TIEHASH {
  52.     my ($classname,$obj) = @_;
  53.     return bless $obj, $classname;
  54. }
  55.  
  56. sub as_string {
  57.   my $z = shift;
  58.   my $string;
  59.   my $re = $z->{r};
  60.   my $im = $z->{i};
  61.   if (int($im) == 0) {
  62.     $string = "$re";
  63.   }
  64.   else {
  65.     $string = sprintf "%f %s %f %s", $re, 
  66.       (int( $im / abs($im) ) == -1) ? '-' : '+' , 
  67.     ($im < 0) ? abs($im) : $im, 'i';
  68.   }
  69.   return $string;
  70. }
  71.  
  72. sub DESTROY {
  73.   return undef if ref($_[0]) ne 'HASH';
  74.     my $self = tied(%{$_[0]});
  75.     delete $ITERATORS{$self};
  76.     if (exists $OWNER{$self}) {
  77.         Math::Cephes::delete_cmplx($self);
  78.         delete $OWNER{$self};
  79.     }
  80. }
  81.  
  82. sub DISOWN {
  83.     my $self = shift;
  84.     my $ptr = tied(%$self);
  85.     delete $OWNER{$ptr};
  86.     };
  87.  
  88. sub ACQUIRE {
  89.     my $self = shift;
  90.     my $ptr = tied(%$self);
  91.     $OWNER{$ptr} = 1;
  92.     };
  93.  
  94. sub FETCH {
  95.     my ($self,$field) = @_;
  96.     no strict 'refs';
  97.     my $member_func = "Math::Cephes::cmplx_${field}_get";
  98.     my $val = &$member_func($self);
  99.     if (exists $BLESSEDMEMBERS{$field}) {
  100.         return undef if (!defined($val));
  101.         my %retval;
  102.         tie %retval,$BLESSEDMEMBERS{$field},$val;
  103.         return bless \%retval, $BLESSEDMEMBERS{$field};
  104.     }
  105.     return $val;
  106. }
  107.  
  108. sub STORE {
  109.     my ($self,$field,$newval) = @_;
  110.     no strict 'refs';
  111.     my $member_func = "Math::Cephes::cmplx_${field}_set";
  112.     if (exists $BLESSEDMEMBERS{$field}) {
  113.         &$member_func($self,tied(%{$newval}));
  114.     } else {
  115.         &$member_func($self,$newval);
  116.     }
  117. }
  118.  
  119. sub FIRSTKEY {
  120.     my $self = shift;
  121.     $ITERATORS{$self} = ['r', 'i', ];
  122.     my $first = shift @{$ITERATORS{$self}};
  123.     return $first;
  124. }
  125.  
  126. sub NEXTKEY {
  127.     my $self = shift;
  128.     my $nelem = scalar @{$ITERATORS{$self}};
  129.     if ($nelem > 0) {
  130.         my $member = shift @{$ITERATORS{$self}};
  131.         return $member;
  132.     } else {
  133.         $ITERATORS{$self} = ['r', 'i', ];
  134.         return ();
  135.     }
  136. }
  137.  
  138. sub cadd {
  139.   my ($z1, $z2) = @_;
  140.   my $z = Math::Cephes::Complex->new();
  141.   Math::Cephes::cadd($z1, $z2, $z);
  142.   return $z;
  143. }
  144.  
  145. sub csub {
  146.   my ($z1, $z2) = @_;
  147.   my $z = Math::Cephes::Complex->new();
  148.   Math::Cephes::csub($z2, $z1, $z);
  149.   return $z;
  150. }
  151.  
  152. sub cmul {
  153.   my ($z1, $z2) = @_;
  154.   my $z = Math::Cephes::Complex->new();
  155.   Math::Cephes::cmul($z1, $z2, $z);
  156.   return $z;
  157. }
  158.  
  159. sub cdiv {
  160.   my ($z1, $z2) = @_;
  161.   my $z = Math::Cephes::Complex->new();
  162.   Math::Cephes::cdiv($z2, $z1, $z);
  163.   return $z;
  164. }
  165.  
  166. sub cpow {
  167.   my ($z1, $z2) = @_;
  168.   my $z = Math::Cephes::Complex->new();
  169.   Math::Cephes::cpow($z1, $z2, $z);
  170.   return $z;
  171. }
  172.  
  173. sub clog {
  174.   my ($z1) = @_;
  175.   my $z = Math::Cephes::Complex->new();
  176.   Math::Cephes::clog($z1, $z);
  177.   return $z;
  178. }
  179. sub cexp {
  180.   my ($z1) = @_;
  181.   my $z = Math::Cephes::Complex->new();
  182.   Math::Cephes::cexp($z1, $z);
  183.   return $z;
  184. }
  185. sub csin {
  186.   my ($z1) = @_;
  187.   my $z = Math::Cephes::Complex->new();
  188.   Math::Cephes::csin($z1, $z);
  189.   return $z;
  190. }
  191. sub ccos {
  192.   my ($z1) = @_;
  193.   my $z = Math::Cephes::Complex->new();
  194.   Math::Cephes::ccos($z1, $z);
  195.   return $z;
  196. }
  197. sub ctan {
  198.   my ($z1) = @_;
  199.   my $z = Math::Cephes::Complex->new();
  200.   Math::Cephes::ctan($z1, $z);
  201.   return $z;
  202. }
  203. sub ccot {
  204.   my ($z1) = @_;
  205.   my $z = Math::Cephes::Complex->new();
  206.   Math::Cephes::ccot($z1, $z);
  207.   return $z;
  208. }
  209. sub casin {
  210.   my ($z1) = @_;
  211.   my $z = Math::Cephes::Complex->new();
  212.   Math::Cephes::casin($z1, $z);
  213.   return $z;
  214. }
  215. sub cacos {
  216.   my ($z1) = @_;
  217.   my $z = Math::Cephes::Complex->new();
  218.   Math::Cephes::cacos($z1, $z);
  219.   return $z;
  220. }
  221. sub catan {
  222.   my ($z1) = @_;
  223.   my $z = Math::Cephes::Complex->new();
  224.   Math::Cephes::catan($z1, $z);
  225.   return $z;
  226. }
  227. sub cmov {
  228.   my ($z1) = @_;
  229.   my $z = Math::Cephes::Complex->new();
  230.   Math::Cephes::cmov($z1, $z);
  231.   return $z;
  232. }
  233. sub cneg {
  234.   my ($z1) = @_;
  235.   Math::Cephes::cneg($z1);
  236.   return $z1;
  237. }
  238. sub csqrt {
  239.   my ($z1) = @_;
  240.   my $z = Math::Cephes::Complex->new();
  241.   Math::Cephes::csqrt($z1, $z);
  242.   return $z;
  243. }
  244. sub cabs {
  245.   my ($z1) = @_;
  246.   my $abs = Math::Cephes::cabs($z1);
  247.   return $abs;
  248. }
  249.  
  250. sub csinh {
  251.   my ($z1) = @_;
  252.   my $z = Math::Cephes::Complex->new();
  253.   Math::Cephes::csinh($z1, $z);
  254.   return $z;
  255. }
  256. sub ccosh {
  257.   my ($z1) = @_;
  258.   my $z = Math::Cephes::Complex->new();
  259.   Math::Cephes::ccosh($z1, $z);
  260.   return $z;
  261. }
  262. sub ctanh {
  263.   my ($z1) = @_;
  264.   my $z = Math::Cephes::Complex->new();
  265.   Math::Cephes::ctanh($z1, $z);
  266.   return $z;
  267. }
  268. sub casinh {
  269.   my ($z1) = @_;
  270.   my $z = Math::Cephes::Complex->new();
  271.   Math::Cephes::casinh($z1, $z);
  272.   return $z;
  273. }
  274. sub cacosh {
  275.   my ($z1) = @_;
  276.   my $z = Math::Cephes::Complex->new();
  277.   Math::Cephes::cacosh($z1, $z);
  278.   return $z;
  279. }
  280. sub catanh {
  281.   my ($z1) = @_;
  282.   my $z = Math::Cephes::Complex->new();
  283.   Math::Cephes::catanh($z1, $z);
  284.   return $z;
  285. }
  286.  
  287. 1;
  288.  
  289. __END__
  290.  
  291. =head1 NAME
  292.  
  293.   Math::Cephes::Complex - Perl interface to the cephes complex number routines
  294.  
  295. =head1 SYNOPSIS
  296.  
  297.   use Math::Cephes::Complex qw(cmplx);
  298.   my $z1 = cmplx(2,3);          # $z1 = 2 + 3 i
  299.   my $z2 = cmplx(3,4);          # $z2 = 3 + 4 i
  300.   my $z3 = $z1->radd($z2);      # $z3 = $z1 + $z2
  301.  
  302. =head1 DESCRIPTION
  303.  
  304. This module is a layer on top of the basic routines in the
  305. cephes math library to handle complex numbers. A complex 
  306. number is created via any of the following syntaxes:
  307.  
  308.   my $f = Math::Cephes::Complex->new(3, 2);   # $f = 3 + 2 i
  309.   my $g = new Math::Cephes::Complex(5, 3);    # $g = 5 + 3 i
  310.   my $h = cmplx(7, 5);                        # $h = 7 + 5 i
  311.  
  312. the last one being available by importing I<cmplx>. If no arguments
  313. are specified, as in
  314.  
  315.  my $h = cmplx();
  316.  
  317. then the defaults $z = 0 + 0 i are assumed. The real and imaginary 
  318. part of a complex number are represented respectively by
  319.  
  320.    $f->{r}; $f->{i};
  321.  
  322. or, as methods,
  323.  
  324.    $f->r;  $f->i;
  325.  
  326. and can be set according to
  327.  
  328.   $f->{r} = 4; $f->{i} = 9;
  329.  
  330. or, again, as methods,
  331.  
  332.   $f->r(4);   $f->i(9);
  333.  
  334. The complex number can be printed out as
  335.  
  336.   print $f->as_string;
  337.  
  338. A summary of the usage is as follows.
  339.  
  340. =over 4
  341.  
  342. =item I<csin>: Complex circular sine
  343.  
  344.  SYNOPSIS:
  345.  
  346.  # void csin();
  347.  # cmplx z, w;
  348.  
  349.  $z = cmplx(2, 3);    # $z = 2 + 3 i
  350.  $w = $z->csin;
  351.  print $w->{r}, '  ', $w->{i};  # prints real and imaginary parts of $w
  352.  print $w->as_string;           # prints $w as Re($w) + i Im($w)
  353.  
  354.  DESCRIPTION:
  355.  
  356.  If
  357.      z = x + iy,
  358.  
  359.  then
  360.  
  361.      w = sin x  cosh y  +  i cos x sinh y.
  362.  
  363. =item I<ccos>: Complex circular cosine
  364.  
  365.  SYNOPSIS:
  366.  
  367.  # void ccos();
  368.  # cmplx z, w;
  369.  
  370.  $z = cmplx(2, 3);    # $z = 2 + 3 i
  371.  $w = $z->ccos;
  372.  print $w->{r}, '  ', $w->{i};  # prints real and imaginary parts of $w
  373.  print $w->as_string;           # prints $w as Re($w) + i Im($w)
  374.  
  375.  DESCRIPTION:
  376.  
  377.  If
  378.      z = x + iy,
  379.  
  380.  then
  381.  
  382.      w = cos x  cosh y  -  i sin x sinh y.
  383.  
  384. =item I<ctan>: Complex circular tangent
  385.  
  386.  SYNOPSIS:
  387.  
  388.  # void ctan();
  389.  # cmplx z, w;
  390.  
  391.  $z = cmplx(2, 3);    # $z = 2 + 3 i
  392.  $w = $z->ctan;
  393.  print $w->{r}, '  ', $w->{i};  # prints real and imaginary parts of $w
  394.  print $w->as_string;           # prints $w as Re($w) + i Im($w)
  395.  
  396.  DESCRIPTION:
  397.  
  398.  If
  399.      z = x + iy,
  400.  
  401.  then
  402.  
  403.            sin 2x  +  i sinh 2y
  404.      w  =  --------------------.
  405.             cos 2x  +  cosh 2y
  406.  
  407.  On the real axis the denominator is zero at odd multiples
  408.  of PI/2.  The denominator is evaluated by its Taylor
  409.  series near these points.
  410.  
  411. =item I<ccot>: Complex circular cotangent
  412.  
  413.  SYNOPSIS:
  414.  
  415.  # void ccot();
  416.  # cmplx z, w;
  417.  
  418.  $z = cmplx(2, 3);    # $z = 2 + 3 i
  419.  $w = $z->ccot;
  420.  print $w->{r}, '  ', $w->{i};  # prints real and imaginary parts of $w
  421.  print $w->as_string;           # prints $w as Re($w) + i Im($w)
  422.  
  423.  DESCRIPTION:
  424.  
  425.  If
  426.      z = x + iy,
  427.  
  428.  then
  429.  
  430.            sin 2x  -  i sinh 2y
  431.      w  =  --------------------.
  432.             cosh 2y  -  cos 2x
  433.  
  434.  On the real axis, the denominator has zeros at even
  435.  multiples of PI/2.  Near these points it is evaluated
  436.  by a Taylor series.
  437.  
  438. =item I<casin>: Complex circular arc sine
  439.  
  440.  SYNOPSIS:
  441.  
  442.  # void casin();
  443.  # cmplx z, w;
  444.  
  445.  $z = cmplx(2, 3);    # $z = 2 + 3 i
  446.  $w = $z->casin;
  447.  print $w->{r}, '  ', $w->{i};  # prints real and imaginary parts of $w
  448.  print $w->as_string;           # prints $w as Re($w) + i Im($w)
  449.  
  450.  DESCRIPTION:
  451.  
  452.  Inverse complex sine:
  453.  
  454.                                2
  455.  w = -i clog( iz + csqrt( 1 - z ) ).
  456.  
  457. =item I<cacos>: Complex circular arc cosine
  458.  
  459.  SYNOPSIS:
  460.  
  461.  # void cacos();
  462.  # cmplx z, w;
  463.  
  464.  $z = cmplx(2, 3);    # $z = 2 + 3 i
  465.  $w = $z->cacos;
  466.  print $w->{r}, '  ', $w->{i};  # prints real and imaginary parts of $w
  467.  print $w->as_string;           # prints $w as Re($w) + i Im($w)
  468.  
  469.  DESCRIPTION:
  470.  
  471.  w = arccos z  =  PI/2 - arcsin z.
  472.  
  473. =item I<catan>: Complex circular arc tangent
  474.  
  475.  SYNOPSIS:
  476.  
  477.  # void catan();
  478.  # cmplx z, w;
  479.  
  480.  $z = cmplx(2, 3);    # $z = 2 + 3 i
  481.  $w = $z->catan;
  482.  print $w->{r}, '  ', $w->{i};  # prints real and imaginary parts of $w
  483.  print $w->as_string;           # prints $w as Re($w) + i Im($w)
  484.  
  485.  DESCRIPTION:
  486.  
  487.  If
  488.      z = x + iy,
  489.  
  490.  then
  491.           1       (    2x     )
  492.  Re w  =  - arctan(-----------)  +  k PI
  493.           2       (     2    2)
  494.                   (1 - x  - y )
  495.  
  496.                ( 2         2)
  497.           1    (x  +  (y+1) )
  498.  Im w  =  - log(------------)
  499.           4    ( 2         2)
  500.                (x  +  (y-1) )
  501.  
  502.  Where k is an arbitrary integer.
  503.  
  504. =item I<csinh>: Complex hyperbolic sine
  505.  
  506.   SYNOPSIS:
  507.  
  508.   # void csinh();
  509.   # cmplx z, w;
  510.  
  511.  $z = cmplx(2, 3);    # $z = 2 + 3 i
  512.  $w = $z->csinh;
  513.  print $w->{r}, '  ', $w->{i};  # prints real and imaginary parts of $w
  514.  print $w->as_string;           # prints $w as Re($w) + i Im($w)
  515.  
  516.  
  517.   DESCRIPTION:
  518.  
  519.   csinh z = (cexp(z) - cexp(-z))/2
  520.           = sinh x * cos y  +  i cosh x * sin y .
  521.  
  522. =item I<casinh>: Complex inverse hyperbolic sine
  523.  
  524.   SYNOPSIS:
  525.  
  526.   # void casinh();
  527.   # cmplx z, w;
  528.  
  529.  $z = cmplx(2, 3);    # $z = 2 + 3 i
  530.  $w = $z->casinh;
  531.  print $w->{r}, '  ', $w->{i};  # prints real and imaginary parts of $w
  532.  print $w->as_string;           # prints $w as Re($w) + i Im($w)
  533.  
  534.   DESCRIPTION:
  535.  
  536.   casinh z = -i casin iz .
  537.  
  538. =item I<ccosh>: Complex hyperbolic cosine
  539.  
  540.   SYNOPSIS:
  541.  
  542.   # void ccosh();
  543.   # cmplx z, w;
  544.   
  545.  $z = cmplx(2, 3);    # $z = 2 + 3 i
  546.  $w = $z->ccosh;
  547.  print $w->{r}, '  ', $w->{i};  # prints real and imaginary parts of $w
  548.  print $w->as_string;           # prints $w as Re($w) + i Im($w)
  549.  
  550.   DESCRIPTION:
  551.  
  552.   ccosh(z) = cosh x  cos y + i sinh x sin y .
  553.  
  554. =item I<cacosh>: Complex inverse hyperbolic cosine
  555.  
  556.  
  557.   SYNOPSIS:
  558.  
  559.   # void cacosh();
  560.   # cmplx z, w;
  561.  
  562.  $z = cmplx(2, 3);    # $z = 2 + 3 i
  563.  $w = $z->cacosh;
  564.  print $w->{r}, '  ', $w->{i};  # prints real and imaginary parts of $w 
  565.  print $w->as_string;           # prints $w as Re($w) + i Im($w)
  566.  
  567.   DESCRIPTION:
  568.  
  569.   acosh z = i acos z .
  570.  
  571. =item I<ctanh>: Complex hyperbolic tangent
  572.  
  573.  SYNOPSIS:
  574.  
  575.  # void ctanh();
  576.  # cmplx z, w;
  577.  
  578.  $z = cmplx(2, 3);    # $z = 2 + 3 i
  579.  $w = $z->ctanh;
  580.  print $w->{r}, '  ', $w->{i};  # prints real and imaginary parts of $w  
  581.  print $w->as_string;           # prints $w as Re($w) + i Im($w)
  582.  
  583.  DESCRIPTION:
  584.  
  585.  tanh z = (sinh 2x  +  i sin 2y) / (cosh 2x + cos 2y) .
  586.  
  587. =item I<catanh>: Complex inverse hyperbolic tangent
  588.  
  589.   SYNOPSIS:
  590.  
  591.   # void catanh();
  592.   # cmplx z, w;
  593.  
  594.  $z = cmplx(2, 3);    # $z = 2 + 3 i
  595.  $w = $z->catanh;
  596.  print $w->{r}, '  ', $w->{i};  # prints real and imaginary parts of $w  
  597.  print $w->as_string;           # prints $w as Re($w) + i Im($w)
  598.  
  599.   DESCRIPTION:
  600.  
  601.   Inverse tanh, equal to  -i catan (iz);
  602.  
  603. =item I<cpow>: Complex power function 
  604.  
  605.   SYNOPSIS:
  606.  
  607.   # void cpow();
  608.   # cmplx a, z, w;
  609.  
  610.  $a = cmplx(5, 6);    # $z = 5 + 6 i
  611.  $z = cmplx(2, 3);    # $z = 2 + 3 i
  612.  $w = $a->cpow($z);
  613.  print $w->{r}, '  ', $w->{i};  # prints real and imaginary parts of $w  
  614.  print $w->as_string;           # prints $w as Re($w) + i Im($w)
  615.  
  616.   DESCRIPTION:
  617.  
  618.   Raises complex A to the complex Zth power.
  619.   Definition is per AMS55 # 4.2.8,
  620.   analytically equivalent to cpow(a,z) = cexp(z clog(a)). 
  621.  
  622. =item I<cmplx>: Complex number arithmetic
  623.  
  624.  SYNOPSIS:
  625.  
  626.  # typedef struct {
  627.  #     double r;     real part
  628.  #     double i;     imaginary part
  629.  #    }cmplx;
  630.  
  631.  # cmplx *a, *b, *c;
  632.  
  633.  $a = cmplx(3, 5);   # $a = 3 + 5 i
  634.  $b = cmplx(2, 3);   # $b = 2 + 3 i
  635.  
  636.  $c = $a->cadd( $b );  #   c = a + b
  637.  $c = $a->csub( $b );  #   c = a - b
  638.  $c = $a->cmul( $b );  #   c = a * b
  639.  $c = $a->cdiv( $b );  #   c = a / b
  640.  $c = $a->cneg;        #   c = -a
  641.  $c = $a->cmov;        #   c = a
  642.  
  643.  print $c->{r}, '  ', $c->{i};   # prints real and imaginary parts of $c
  644.  print $c->as_string;           # prints $c as Re($c) + i Im($c)
  645.  
  646.  
  647.  DESCRIPTION:
  648.  
  649.  Addition:
  650.     c.r  =  b.r + a.r
  651.     c.i  =  b.i + a.i
  652.  
  653.  Subtraction:
  654.     c.r  =  b.r - a.r
  655.     c.i  =  b.i - a.i
  656.  
  657.  Multiplication:
  658.     c.r  =  b.r * a.r  -  b.i * a.i
  659.     c.i  =  b.r * a.i  +  b.i * a.r
  660.  
  661.  Division:
  662.     d    =  a.r * a.r  +  a.i * a.i
  663.     c.r  = (b.r * a.r  + b.i * a.i)/d
  664.     c.i  = (b.i * a.r  -  b.r * a.i)/d
  665.  
  666. =item I<cabs>: Complex absolute value
  667.  
  668.  SYNOPSIS:
  669.  
  670.  # double a, cabs();
  671.  # cmplx z;
  672.  
  673.  $z = cmplx(2, 3);    # $z = 2 + 3 i
  674.  $a = cabs( $z );
  675.  
  676.  DESCRIPTION:
  677.  
  678.  If z = x + iy
  679.  
  680.  then
  681.  
  682.        a = sqrt( x**2 + y**2 ).
  683.  
  684.  Overflow and underflow are avoided by testing the magnitudes
  685.  of x and y before squaring.  If either is outside half of
  686.  the floating point full scale range, both are rescaled.
  687.  
  688. =item I<csqrt>: Complex square root
  689.  
  690.  SYNOPSIS:
  691.  
  692.  # void csqrt();
  693.  # cmplx z, w;
  694.  
  695.  $z = cmplx(2, 3);    # $z = 2 + 3 i
  696.  $w = $z->csqrt;
  697.  print $w->{r}, '  ', $w->{i};  # prints real and imaginary parts of $w
  698.  print $w->as_string;           # prints $w as Re($w) + i Im($w)
  699.  
  700.  DESCRIPTION:
  701.  
  702.  If z = x + iy,  r = |z|, then
  703.  
  704.                        1/2
  705.  Im w  =  [ (r - x)/2 ]   ,
  706.  
  707.  Re w  =  y / 2 Im w.
  708.  
  709.  Note that -w is also a square root of z.  The root chosen
  710.  is always in the upper half plane.
  711.  
  712.  Because of the potential for cancellation error in r - x,
  713.  the result is sharpened by doing a Heron iteration
  714.  (see sqrt.c) in complex arithmetic.
  715.  
  716. =back
  717.  
  718. =head1 BUGS
  719.  
  720.  Please report any to Randy Kobes <randy@theoryx5.uwinnipeg.ca>
  721.  
  722. =head1 SEE ALSO
  723.  
  724. For the basic interface to the cephes complex number routines, see
  725. L<Math::Cephes>. See also L<Math::Complex> 
  726. for a more extensive interface to complex number routines.
  727.  
  728. =head1 COPYRIGHT
  729.  
  730. The C code for the Cephes Math Library is
  731. Copyright 1984, 1987, 1989, 2002 by Stephen L. Moshier, 
  732. and is available at http://www.netlib.org/cephes/.
  733. Direct inquiries to 30 Frost Street, Cambridge, MA 02140.
  734.  
  735. The perl interface is copyright 2000, 2002 by Randy Kobes.
  736. This library is free software; you can redistribute it and/or
  737. modify it under the same terms as Perl itself.
  738.  
  739. =cut
  740.