Math_Complex
[ class tree: Math_Complex ] [ index: Math_Complex ] [ all elements ]

Source for file ComplexOp.php

Documentation is available at ComplexOp.php

  1. <?php
  2.  
  3. //
  4. // +----------------------------------------------------------------------+
  5. // | PHP Version 4                                                        |
  6. // +----------------------------------------------------------------------+
  7. // | Copyright (c) 1997-2003 The PHP Group                                |
  8. // +----------------------------------------------------------------------+
  9. // | This source file is subject to version 2.0 of the PHP license,       |
  10. // | that is bundled with this package in the file LICENSE, and is        |
  11. // | available at through the world-wide-web at                           |
  12. // | http://www.php.net/license/2_02.txt.                                 |
  13. // | If you did not receive a copy of the PHP license and are unable to   |
  14. // | obtain it through the world-wide-web, please send a note to          |
  15. // | license@php.net so we can mail you a copy immediately.               |
  16. // +----------------------------------------------------------------------+
  17. // | Authors: Jesus M. Castagnetto <jmcastagnetto@php.net>                |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: ComplexOp.php 304053 2010-10-05 00:43:46Z clockwerx $
  21. //
  22.  
  23. include_once 'PEAR.php';
  24. include_once 'Math/Complex.php';
  25. include_once 'Math/TrigOp.php';
  26.  
  27. /**
  28.  * Package with classes to represent and manipulate complex number. Contain
  29.  * definitions for basic arithmetic functions, as well as trigonometric,
  30.  * inverse trigonometric, hyperbolic, inverse hyperbolic, exponential and
  31.  * logarithms of complex numbers.
  32.  *
  33.  * @package Math_Complex
  34.  */
  35.  
  36. /**
  37.  * Math_ComplexOp: static class to operate on Math_Complex objects
  38.  *
  39.  * Originally this class was part of NumPHP (Numeric PHP package)
  40.  *
  41.  * @author  Jesus M. Castagnetto <jmcastagnetto@php.net>
  42.  * @version 0.8
  43.  * @access  public
  44.  * @package Math_Complex
  45.  */
  46. class Math_ComplexOp {/*{{{*/
  47.  
  48.     /*{{{ isComplex() */
  49.     /**
  50.      * Checks if a given object is an instance of PEAR::Math_Complex
  51.      *
  52.      * @static
  53.      * @return boolean 
  54.      * @access public
  55.      */
  56.     function isComplex(&$c1)
  57.     {
  58.         if (function_exists('is_a')) {
  59.             return is_a(&$c1'math_complex');
  60.         else {
  61.             return (strtolower(get_class($c1)) == 'math_complex' 
  62.                     || is_subclass_of($c1'math_complex'));
  63.         }
  64.     }/*}}}*/
  65.  
  66.     /*{{{ createFromPolar() */
  67.     /**
  68.      * Converts a polar complex z = r*exp(theta*i) to z = a + b*i
  69.      *
  70.      * @static
  71.      * @param float $r 
  72.      * @param float $theta 
  73.      * @return Math_Complex 
  74.      * @access public
  75.      */
  76.     function &createFromPolar ($r$theta)
  77.     {
  78.         $r floatval($r);
  79.         $theta floatval($theta);
  80.         $a $r cos($theta);
  81.         $b $r sin($theta);
  82.         return new Math_Complex $a$b );
  83.     /*}}}*/
  84.  
  85.     // methods below need a valid Math_Complex object as parameter
  86.  
  87.     /*{{{ sqrt() */
  88.     /**
  89.      * Calculates the complex square root of a complex number: z = sqrt(c1)
  90.      *
  91.      * @static
  92.      * @param Math_Complex $c1 
  93.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  94.      * @access public
  95.      */
  96.     function &sqrt (&$c1
  97.     {
  98.         if (!Math_ComplexOp::isComplex($c1)) {
  99.             return PEAR::raiseError('argument is not a PEAR::Math_Complex object');
  100.         }
  101.         $x abs($c1->getReal());
  102.         $y abs($c1->getIm());
  103.         if ($x == 0.0  && $y == 0{
  104.             $r $i = 0.0;
  105.         else {
  106.             if ($x >= $y{
  107.                 $t $y $x;
  108.                 $w sqrt($xsqrt(0.5 * (1.0 + sqrt(1.0 + $t*$t)));
  109.             else {
  110.                 $t $x $y;
  111.                 $w sqrt($ysqrt(0.5 * ($t sqrt(1.0 + $t*$t)));
  112.             }
  113.  
  114.             if ($c1->getReal(>= 0.0{
  115.                 $r $w;
  116.                 $i $c1->getIm((2.0 * $w);
  117.             else {
  118.                 $i ($c1->getIm(>= 0$w : -1 * $w;
  119.                 $r $c1->getIm((2.0 * $i);
  120.             }
  121.         }
  122.         return new Math_Complex ($r$i);
  123.     }/*}}}*/
  124.  
  125.     /*{{{ sqrtReal() */
  126.     /**
  127.      * Calculates the complex square root of a real number: z = sqrt(realnumber)
  128.      *
  129.      * @static
  130.      * @param float $realnum A float
  131.      * @return Math_Complex 
  132.      * @access public
  133.      */
  134.     function &sqrtReal ($realnum
  135.     {
  136.         if ($realnum >= 0{
  137.             $r sqrt($realnum);
  138.             $i = 0.0;
  139.         else {
  140.             $r = 0.0;
  141.             $i sqrt(-1 * $realnum);
  142.         }
  143.         return new Math_Complex($r$i);
  144.     }/*}}}*/
  145.  
  146.     /*{{{ exp() */
  147.     /**
  148.      * Calculates the exponential of a complex number: z = exp(c1)
  149.      *
  150.      * @static
  151.      * @param Math_Complex $c1 
  152.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  153.      * @access public
  154.      */
  155.     function &exp (&$c1
  156.     {
  157.         if (!Math_ComplexOp::isComplex($c1))
  158.             return PEAR::raiseError('argument is not a PEAR::Math_Complex object');
  159.         $rho exp($c1->getReal());
  160.         $theta $c1->getIm();
  161.  
  162.         $r $rho cos($theta);
  163.         $i $rho sin($theta);
  164.         return new Math_Complex($r$i);
  165.     }/*}}}*/
  166.  
  167.     /*{{{ log() */
  168.     /**
  169.      * Calculates the logarithm (base 2) of a complex number: z = log(c1)
  170.      *
  171.      * @static
  172.      * @param Math_Complex $c1 
  173.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  174.      * @access public
  175.      */
  176.     function &log (&$c1
  177.     {
  178.         if (!Math_ComplexOp::isComplex($c1))
  179.             return PEAR::raiseError('argument is not a PEAR::Math_Complex object');
  180.         $r log($c1->abs());
  181.         $i $c1->arg();
  182.         return new Math_Complex($r$i);
  183.     }/*}}}*/
  184.  
  185.     /*{{{ log10() */
  186.     /**
  187.      * Calculates the logarithm (base 10) of a complex number: z = log10(c1)
  188.      *
  189.      * @static
  190.      * @param Math_Complex $c1 
  191.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  192.      * @access public
  193.      */
  194.     function &log10 (&$c1
  195.     {
  196.         $log Math_ComplexOp::log($c1);
  197.         if (PEAR::isError($log))
  198.             return $log;
  199.         return Math_ComplexOp::multReal ($log1/log(10));
  200.     }/*}}}*/
  201.  
  202.     /*{{{ conjugate() */
  203.     /**
  204.      * Calculates the conjugate of a complex number: z = conj(c1)
  205.      *
  206.      * @static
  207.      * @param Math_Complex $c1 
  208.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  209.      * @access public
  210.      */
  211.     function &conjugate (&$c1
  212.     {
  213.         if (!Math_ComplexOp::isComplex($c1))
  214.             return PEAR::raiseError('argument is not a PEAR::Math_Complex object');
  215.         return new Math_Complex($c1->getReal()-1 * $c1->getIm());
  216.     }/*}}}*/
  217.  
  218.     /*{{{ negative() */
  219.     /**
  220.      * Calculates the negative of a complex number: z = -c1
  221.      *
  222.      * @static
  223.      * @param Math_Complex $c1 
  224.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  225.      * @access public
  226.      */
  227.     function &negative (&$c1
  228.     {
  229.         if (!Math_ComplexOp::isComplex($c1)) {
  230.             return PEAR::raiseError('argument is not a PEAR::Math_Complex object');
  231.         else {
  232.             return new Math_Complex(-1 * $c1->getReal()-1 * $c1->getIm());
  233.         }
  234.     }/*}}}*/
  235.  
  236.     /*{{{ inverse() */
  237.     /**
  238.      * Calculates the inverse of a complex number: z = 1/c1
  239.      *
  240.      * @static
  241.      * @param Math_Complex $c1 
  242.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  243.      * @access public
  244.      */
  245.     function &inverse (&$c1
  246.     {
  247.         if (!Math_ComplexOp::isComplex($c1))
  248.             return PEAR::raiseError('argument is not a PEAR::Math_Complex object');
  249.         $abs $c1->abs();
  250.         if ($abs == 0.0)
  251.             return PEAR::raiseError('Math_Complex object\'s norm is zero');
  252.         $temp = 1.0 / $c1->abs();
  253.         $r $c1->getReal($temp $temp;
  254.         $i = -1 * $c1->getIm($temp $temp;
  255.         return new Math_Complex ($r$i);
  256.     }/*}}}*/
  257.  
  258.     // Trigonometric methods
  259.  
  260.     /*{{{ sin() */
  261.     /**
  262.      * Calculates the sine of a complex number: z = sin(c1)
  263.      *
  264.      * @static
  265.      * @param Math_Complex $c1 
  266.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  267.      * @access public
  268.      */
  269.     function &sin (&$c1
  270.     {
  271.         if (!Math_ComplexOp::isComplex($c1))
  272.             return PEAR::raiseError('argument is not a PEAR::Math_Complex object');
  273.         $a $c1->getReal()$b $c1->getIm();
  274.         $r sin($a)*cosh($b);
  275.         $i cos($a)*sinh($b);
  276.         return new Math_Complex$r$i );
  277.     }/*}}}*/
  278.  
  279.     /*{{{ cos() */
  280.     /**
  281.      * Calculates the cosine of a complex number: z = cos(c1)
  282.      *
  283.      * @static
  284.      * @param Math_Complex $c1 
  285.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  286.      * @access public
  287.      */
  288.     function &cos (&$c1
  289.     {
  290.         if (!Math_ComplexOp::isComplex($c1))
  291.             return PEAR::raiseError('argument is not a PEAR::Math_Complex object');
  292.         $a $c1->getReal()$b $c1->getIm();
  293.         $r cos($a)*cosh($b);
  294.         $i sin($a)*sinh($b);
  295.         return new Math_Complex$r$i );
  296.     }/*}}}*/
  297.  
  298.     /*{{{ tan() */
  299.     /**
  300.      * Calculates the tangent of a complex number: z = tan(c1)
  301.      *
  302.      * @static
  303.      * @param Math_Complex $c1 
  304.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  305.      * @access public
  306.      */
  307.     function &tan (&$c1
  308.     {
  309.         if (!Math_ComplexOp::isComplex($c1))
  310.             return PEAR::raiseError('argument is not a PEAR::Math_Complex object');
  311.         $a $c1->getReal()$b $c1->getIm();
  312.         $den = 1 + pow(tan($a),2)*pow(tanh($b),2);
  313.         if ($den == 0.0)
  314.             return PEAR::raiseError('Division by zero while calculating Math_ComplexOp::tan()');
  315.         $r pow(Math_TrigOp::sech($b),2)*tan($a)/$den;
  316.         $i pow(Math_TrigOp::sec($a),2)*tanh($b)/$den;
  317.         return new Math_Complex$r$i );
  318.     }/*}}}*/
  319.  
  320.     /*{{{ sec() */
  321.     /**
  322.      * Calculates the secant of a complex number: z = sec(c1)
  323.      *
  324.      * @static
  325.      * @param Math_Complex $c1 
  326.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  327.      * @access public
  328.      */
  329.     function &sec (&$c1
  330.     {
  331.         if (!Math_ComplexOp::isComplex($c1))
  332.             return PEAR::raiseError('argument is not a PEAR::Math_Complex object');
  333.         $z Math_ComplexOp::cos($c1);  
  334.         return Math_ComplexOP::inverse($z);
  335.     }/*}}}*/
  336.  
  337.     /*{{{ csc() */
  338.     /**
  339.      * Calculates the cosecant of a complex number: z = csc(c1)
  340.      *
  341.      * @static
  342.      * @param Math_Complex $c1 
  343.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  344.      * @access public
  345.      */
  346.     function &csc (&$c1
  347.     {
  348.         if (!Math_ComplexOp::isComplex($c1))
  349.             return PEAR::raiseError('argument is not a PEAR::Math_Complex object');
  350.         $z Math_ComplexOp::sin($c1);  
  351.         return Math_ComplexOP::inverse($z);
  352.     }/*}}}*/
  353.  
  354.     /*{{{ cot() */
  355.     /**
  356.      * Calculates the cotangent of a complex number: z = cot(c1)
  357.      *
  358.      * @static
  359.      * @param Math_Complex $c1 
  360.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  361.      * @access public
  362.      */
  363.     function &cot (&$c1
  364.     {
  365.         $z Math_ComplexOp::tan($c1);
  366.         if (PEAR::isError($z)) {
  367.             return $z;
  368.         else {
  369.             return Math_ComplexOP::inverse($z);
  370.         }
  371.     }/*}}}*/
  372.  
  373.     // Inverse trigonometric methods
  374.     
  375.     /*{{{ asin() */
  376.     /**
  377.      * Calculates the inverse sine of a complex number: z = asin(c1)
  378.      *
  379.      * @static
  380.      * @param Math_Complex $c1 
  381.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  382.      * @access public
  383.      */
  384.     function &asin (&$c1
  385.     {
  386.         if (!Math_ComplexOp::isComplex($c1))
  387.             return PEAR::raiseError('argument is not a PEAR::Math_Complex object');
  388.         $t Math_ComplexOp::mult($c1$c1);
  389.         $v Math_ComplexOp::sub(new Math_Complex(1,0)$t);
  390.         $t Math_ComplexOp::sqrt($v);
  391.         $v = new Math_Complex($t->getReal($c1->getIm(),
  392.                               $t->getIm($c1->getReal());
  393.         $z Math_ComplexOp::log($v);
  394.         return new Math_Complex($z->getIm()-1*$z->getReal());
  395.     }/*}}}*/
  396.  
  397.     // alternative method
  398.     /*{{{ asinAlt() */
  399.     /**
  400.      * Calculates the inverse sine of a complex number: z = asinAlt(c1)
  401.      * Uses an alternative algorithm
  402.      *
  403.      * @static
  404.      * @param Math_Complex $c1 
  405.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  406.      * @access public
  407.      */
  408.     function &asinAlt (&$c1
  409.     {
  410.         if (!Math_ComplexOp::isComplex($c1))
  411.             return PEAR::raiseError('argument is not a PEAR::Math_Complex object');
  412.         $r $c1->getReal();
  413.         $i $c1->getIm();
  414.         if ($i == 0{
  415.             return Math_ComplexOp::asinReal($r);
  416.         else {
  417.             $x abs($r)$y abs($i);
  418.             $r hypot($x + 1$y)$s hypot($x - 1$y);
  419.             $a ($r $s)/2;
  420.             $b $x $a;
  421.             $y2 $y $y;
  422.             $ac = 1.5; $bc = 0.6417; // crossover values
  423.  
  424.             if ($b <= $bc{
  425.                 $real asin($b);
  426.             else {
  427.                 if ($x <= 1{
  428.                     $d = 0.5 * ($a $x($y2 ($r $x + 1($s (1 - $x)));
  429.                     $real atan2($xsqrt($d));
  430.                 else {
  431.                     $ax $a $x;
  432.                     $d = 0.5 * ($ax ($r $x + 1$ax ($s ($x - 1)));
  433.                     $real atan2($x$y sqrt($d));
  434.                 }
  435.             }
  436.  
  437.             if ($a <= $ac{
  438.                 if ($x < 1{
  439.                     $m = 0.5 * ($y2 ($r ($x + 1)) $y2 ($s (1 - $x)));
  440.                 else {
  441.                     $m = 0.5 * ($y2 ($r ($x + 1)) ($s ($x - 1)));
  442.                 }
  443.                 $im log1p($m sqrt($m ($a + 1)));
  444.             else {
  445.                 $im log($a sqrt($a $a - 1));
  446.             }
  447.             $real ($r >= 0$real : -1*$real;
  448.             $im ($i >= 0$im : -1*$im;
  449.             return new Math_Complex($real$im);
  450.         }
  451.     }/*}}}*/
  452.  
  453.     /*{{{ asinReal() */
  454.     /**
  455.      * Calculates the complex inverse sine of a real number: z = asinReal(r):
  456.      * 
  457.      * @static
  458.      * @param float $r 
  459.      * @return Math_Complex 
  460.      * @access public
  461.      */
  462.     function &asinReal($r
  463.     {
  464.         $r floatval($r);
  465.         if (abs($r<= 1.0{
  466.             return new Math_Complex(asin($r)0.0);
  467.         else {
  468.             if ($r < 0.0{
  469.                 return new Math_Complex(-1*M_PI_2acosh($r));
  470.             else {
  471.                 return new Math_Complex(M_PI_2-1*acosh($r));
  472.             }
  473.         }
  474.     }/*}}}*/
  475.  
  476.     /*{{{ acos() */
  477.     /**
  478.      * Calculates the inverse cosine of a complex number: z = acos(c1)
  479.      *
  480.      * @static
  481.      * @param Math_Complex $c1 
  482.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  483.      * @access public
  484.      */
  485.     function &acos (&$c1
  486.     {
  487.         if (!Math_ComplexOp::isComplex($c1))
  488.             return PEAR::raiseError('argument is not a PEAR::Math_Complex object');
  489.         $t Math_ComplexOp::mult($c1$c1);
  490.         $v Math_ComplexOp::sub(new Math_Complex(1,0)$t);
  491.         $t Math_ComplexOp::sqrt($v);
  492.         $v = new Math_Complex($c1->getReal($t->getIm(),
  493.                               $c1->getIm($t->getReal());
  494.         $z Math_ComplexOp::log($v);
  495.         return new Math_Complex($z->getIm()-1*$z->getReal());
  496.     }/*}}}*/
  497.     
  498.     /*{{{ atan() */
  499.     /**
  500.      * Calculates the inverse tangent of a complex number: z = atan(c1):
  501.      *
  502.      * @static
  503.      * @param Math_Complex $c1 
  504.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  505.      * @access public
  506.      */
  507.     function &atan (&$c1
  508.     {
  509.         if (!Math_ComplexOp::isComplex($c1))
  510.             return PEAR::raiseError('argument is not a PEAR::Math_Complex object');
  511.         $u = new Math_Complex(-1*$c1->getIm()$c1->getReal());
  512.         $t = new Math_Complex(1,0);
  513.         $d1 Math_ComplexOp::sub($t$u);
  514.         $d2 Math_ComplexOp::add($t$u);
  515.         $u Math_ComplexOp::div($d1$d2);
  516.         if (PEAR::isError($u)) {
  517.             return $u;
  518.         else {
  519.             return Math_ComplexOp::multIm(Math_ComplexOp::log($u)0.5);
  520.         }
  521.     }/*}}}*/
  522.  
  523.     /*{{{ asec() */
  524.     /**
  525.      * Calculates the inverse secant of a complex number: z = asec(c1)
  526.      *
  527.      * @static
  528.      * @param Math_Complex $c1 
  529.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  530.      * @access public
  531.      */
  532.     function &asec (&$c1
  533.     {
  534.         if (!Math_ComplexOp::isComplex($c1))
  535.             return PEAR::raiseError('argument is not a PEAR::Math_Complex object');
  536.         $z Math_ComplexOp::inverse($c1)// get the cosine
  537.         if (PEAR::isError($z)) {
  538.             return $z;
  539.         else {
  540.             return Math_ComplexOp::acos($z);
  541.         }
  542.     }/*}}}*/
  543.  
  544.     /*{{{ acsc() */
  545.     /**
  546.      * Calculates the inverse cosecant of a complex number: z = acsc(c1)
  547.      *
  548.      * @static
  549.      * @param Math_Complex $c1 
  550.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  551.      * @access public
  552.      */
  553.     function &acsc (&$c1
  554.     {
  555.         if (!Math_ComplexOp::isComplex($c1))
  556.             return PEAR::raiseError('argument is not a PEAR::Math_Complex object');
  557.         $z Math_ComplexOp::inverse($c1)// get the sine
  558.         if (PEAR::isError($z)) {
  559.             return $z;
  560.         else {
  561.             return Math_ComplexOp::asin($z);
  562.         }
  563.     }/*}}}*/
  564.  
  565.     /*{{{ acot() */
  566.     /**
  567.      * Calculates the inverse cotangent of a complex number: z = acot(c1)
  568.      *
  569.      * @static
  570.      * @param Math_Complex $c1 
  571.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  572.      * @access public
  573.      */
  574.     function &acot (&$c1
  575.     {
  576.         if (!Math_ComplexOp::isComplex($c1))
  577.             return PEAR::raiseError('argument is not a PEAR::Math_Complex object');
  578.         $z Math_ComplexOp::inverse($c1)// get the tangent
  579.         if (PEAR::isError($z)) {
  580.             return $z;
  581.         else {
  582.             return Math_ComplexOp::atan($z);
  583.         }
  584.     }/*}}}*/
  585.  
  586.     // Hyperbolic methods
  587.     
  588.     /*{{{ sinh() */
  589.     /**
  590.      * Calculates the hyperbolic sine of a complex number: z = sinh(c1)
  591.      *
  592.      * @static
  593.      * @param Math_Complex $c1 
  594.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  595.      * @access public
  596.      */
  597.     function &sinh (&$c1
  598.     {
  599.         if (!Math_ComplexOp::isComplex($c1)) {
  600.             return PEAR::raiseError('argument is not a PEAR::Math_Complex object');
  601.         }
  602.         $r $c1->getReal();
  603.         $i $c1->getIm();
  604.         return new Math_Complex(sinh($rcos($i)cosh($rsin($i));
  605.     }/*}}}*/
  606.  
  607.     /*{{{ cosh() */
  608.     /**
  609.      * Calculates the hyperbolic cosine of a complex number: z = cosh(c1)
  610.      *
  611.      * @static
  612.      * @param Math_Complex $c1 
  613.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  614.      * @access public
  615.      */
  616.     function &cosh (&$c1
  617.     {
  618.         if (!Math_ComplexOp::isComplex($c1)) {
  619.             return PEAR::raiseError('argument is not a PEAR::Math_Complex object');
  620.         }
  621.         $r $c1->getReal();
  622.         $i $c1->getIm();
  623.         return new Math_Complex(cosh($rcos($i)sinh($rsin($i));
  624.     }/*}}}*/
  625.  
  626.     /*{{{ tanh() */
  627.     /**
  628.      * Calculates the hyperbolic tangent of a complex number: z = tanh(c1)
  629.      *
  630.      * @static
  631.      * @param Math_Complex $c1 
  632.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  633.      * @access public
  634.      */
  635.     function &tanh (&$c1
  636.     {
  637.         if (!Math_ComplexOp::isComplex($c1)) {
  638.             return PEAR::raiseError('argument is not a PEAR::Math_Complex object');
  639.         }
  640.         $r $c1->getReal();
  641.         $i $c1->getIm();
  642.         $d cos($icos($isinh($rsinh($r);
  643.         return new Math_Complex(sinh($rcosh($r$d0.5 * sin(2 * $i$d);
  644.     }/*}}}*/
  645.  
  646.     /*{{{ sech() */
  647.     /**
  648.      * Calculates the hyperbolic secant of a complex number: z = sech(c1)
  649.      *
  650.      * @static
  651.      * @param Math_Complex $c1 
  652.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  653.      * @access public
  654.      */
  655.     function &sech (&$c1
  656.     {
  657.         $c2 Math_ComplexOp::cosh($c1);
  658.         if (PEAR::isError($c2)) {
  659.             return $c2;
  660.         else {
  661.             return Math_ComplexOp::inverse($c2);
  662.         }
  663.     }/*}}}*/
  664.  
  665.     /*{{{ csch() */
  666.     /**
  667.      * Calculates the hyperbolic cosecant of a complex number: z = csch(c1)
  668.      *
  669.      * @static
  670.      * @param Math_Complex $c1 
  671.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  672.      * @access public
  673.      */
  674.     function &csch (&$c1
  675.     {
  676.         $c2 Math_ComplexOp::sinh($c1);
  677.         if (PEAR::isError($c2)) {
  678.             return $c2;
  679.         else {
  680.             return Math_ComplexOp::inverse($c2);
  681.         }
  682.     }/*}}}*/
  683.  
  684.     /*{{{ coth() */
  685.     /**
  686.      * Calculates the hyperbolic cotangent of a complex number: z = coth(c1)
  687.      *
  688.      * @static
  689.      * @param Math_Complex $c1 
  690.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  691.      * @access public
  692.      */
  693.     function &coth (&$c1
  694.     {
  695.         $c2 Math_ComplexOp::tanh($c1);
  696.         if (PEAR::isError($c2)) {
  697.             return $c2;
  698.         else {
  699.             return Math_ComplexOp::inverse($c2);
  700.         }
  701.     }/*}}}*/
  702.  
  703.     // Inverse hyperbolic methods
  704.     
  705.     /*{{{ asinh() */
  706.     /**
  707.      * Calculates the inverse hyperbolic sine of a complex number: z = asinh(c1)
  708.      *
  709.      * @static
  710.      * @param Math_Complex $c1 
  711.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  712.      * @access public
  713.      */
  714.     function &asinh (&$c1
  715.     {
  716.         if (!Math_ComplexOp::isComplex($c1)) {
  717.             return PEAR::raiseError('argument is not a PEAR::Math_Complex object');
  718.         }
  719.         $z Math_ComplexOp::multIm($c11.0);
  720.         $z Math_ComplexOp::asin($z);
  721.         if (PEAR::isError($z)) {
  722.             return $z;
  723.         else {
  724.             return Math_ComplexOp::multIm($z-1.0);
  725.         }
  726.     }/*}}}*/
  727.  
  728.     /*{{{ acosh() */
  729.     /**
  730.      * Calculates the inverse hyperbolic cosine of a complex number: z = acosh(c1)
  731.      *
  732.      * @static
  733.      * @param Math_Complex $c1 
  734.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  735.      * @access public
  736.      */
  737.     function &acosh (&$c1
  738.     {
  739.         if (!Math_ComplexOp::isComplex($c1)) {
  740.             return PEAR::raiseError('argument is not a PEAR::Math_Complex object');
  741.         }
  742.         $z Math_ComplexOp::acos($c1);
  743.         if (PEAR::isError($z)) {
  744.             return $z;
  745.         else {
  746.             return Math_ComplexOp::multIm($z(($z->getIm(> 0? 1.0 : -1.0));
  747.         }
  748.     }/*}}}*/
  749.  
  750.     /*{{{ atanh() */
  751.     /**
  752.      * Calculates the inverse hyperbolic tangent of a complex number: z = atanh(c1)
  753.      *
  754.      * @static
  755.      * @param Math_Complex $c1 
  756.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  757.      * @access public
  758.      */
  759.     function &atanh (&$c1
  760.     {
  761.         if (!Math_ComplexOp::isComplex($c1)) {
  762.             return PEAR::raiseError('argument is not a PEAR::Math_Complex object');
  763.         }
  764.         if ($c1->getIm(== 0.0{
  765.             $r $c1->getReal();
  766.             if ($r > -1.0 && $r < 1.0{
  767.                 return Math_Complex(atanh($r)0.0);
  768.             else {
  769.                 return Math_Complex(atanh(1 / $r)(($a < 0? M_PI_2 : -1 * M_PI_2));
  770.             }
  771.         else {
  772.             $z Math_ComplexOp::multIm($c11.0);
  773.             $z Math_ComplexOp::atan($z);
  774.             return Math_ComplexOp::multIm($z-1.0);
  775.         }
  776.     }/*}}}*/
  777.  
  778.     /*{{{ asech() */
  779.     /**
  780.      * Calculates the inverse hyperbolic secant of a complex number: z = asech(c1)
  781.      *
  782.      * @static
  783.      * @param Math_Complex $c1 
  784.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  785.      * @access public
  786.      */
  787.     function &asech (&$c1
  788.     {
  789.         if (!Math_ComplexOp::isComplex($c1)) {
  790.             return PEAR::raiseError('argument is not a PEAR::Math_Complex object');
  791.         }
  792.         $z Math_ComplexOp::inverse($c1);
  793.         return Math_ComplexOp::acosh($z);
  794.     }/*}}}*/
  795.  
  796.     /*{{{ acsch() */
  797.     /**
  798.      * Calculates the inverse hyperbolic cosecant of a complex number: z = acsch(c1)
  799.      *
  800.      * @static
  801.      * @param Math_Complex $c1 
  802.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  803.      * @access public
  804.      */
  805.     function &acsch (&$c1
  806.     {
  807.         if (!Math_ComplexOp::isComplex($c1)) {
  808.             return PEAR::raiseError('argument is not a PEAR::Math_Complex object');
  809.         }
  810.         $z Math_ComplexOp::inverse($c1);
  811.         return Math_ComplexOp::asinh($z);
  812.     }/*}}}*/
  813.  
  814.     /*{{{ acoth() */
  815.     /**
  816.      * Calculates the inverse hyperbolic cotangent of a complex number: z = acoth(c1)
  817.      *
  818.      * @static
  819.      * @param Math_Complex $c1 
  820.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  821.      * @access public
  822.      */
  823.     function &acoth (&$c1
  824.     {
  825.         if (!Math_ComplexOp::isComplex($c1)) {
  826.             return PEAR::raiseError('argument is not a PEAR::Math_Complex object');
  827.         }
  828.         $z Math_ComplexOp::inverse($c1);
  829.         return Math_ComplexOp::atanh($z);
  830.     }/*}}}*/
  831.  
  832.     // functions below need 2 valid Math_Complex objects as parameters
  833.  
  834.     /*{{{ areEqual() */
  835.     /**
  836.      * Determines if is c1 == c2:
  837.      *
  838.      * @static
  839.      * @param Math_Complex $c1 
  840.      * @param Math_Complex $c2 
  841.      * @return boolean|PEAR_ErrorTrue if $c1 == $c2, False if $c1 != $c2, PEAR_Error object on error
  842.      * @access public
  843.      */
  844.     function &areEqual (&$c1&$c2
  845.     {
  846.         if (!Math_ComplexOp::isComplex($c1
  847.             || !Math_ComplexOp::isComplex($c2)) {
  848.             return PEAR::raiseError('Both arguments must be PEAR::Math_Complex objects');
  849.         else {
  850.             $same_class strtolower(get_class($c1)) == strtolower(get_class($c2)) );
  851.             $same_real $c1->getReal(== $c2->getReal() );
  852.             $same_im $c1->getIm(== $c2->getIm() );
  853.             return $same_class && $same_real && $same_im );
  854.         }
  855.     }/*}}}*/
  856.  
  857.     /*{{{ add() */
  858.     /**
  859.      * Returns the sum of two complex numbers: z = c1 + c2
  860.      *
  861.      * @static
  862.      * @param Math_Complex $c1 
  863.      * @param Math_Complex $c2 
  864.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  865.      * @access public
  866.      */
  867.     function &add (&$c1&$c2
  868.     {
  869.         if (!Math_ComplexOp::isComplex($c1
  870.             || !Math_ComplexOp::isComplex($c2)) {
  871.             return PEAR::raiseError('Both arguments must be PEAR::Math_Complex objects');
  872.         else {
  873.             return new Math_Complex$c1->getReal($c2->getReal()$c1->getIm($c2->getIm());
  874.         }
  875.     }/*}}}*/
  876.  
  877.     /*{{{ sub() */
  878.     /**
  879.      * Returns the difference of two complex numbers: z = c1 - c2
  880.      *
  881.      * @static
  882.      * @param Math_Complex $c1 
  883.      * @param Math_Complex $c2 
  884.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  885.      * @access public
  886.      */
  887.     function &sub (&$c1&$c2
  888.     {
  889.         $nc2 Math_ComplexOp::negative($c2);
  890.         if (PEAR::isError($nc2)) {
  891.             return $nc2;
  892.         else {
  893.             return Math_ComplexOp::add($c1$nc2);
  894.         }
  895.     }/*}}}*/
  896.  
  897.     /*{{{ mult() */
  898.     /**
  899.      * Returns the product of two complex numbers: z = c1 * c2
  900.      *
  901.      * @static
  902.      * @param Math_Complex $c1 
  903.      * @param Math_Complex $c2 
  904.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  905.      * @access public
  906.      */
  907.     function &mult (&$c1&$c2
  908.     {
  909.         if (!Math_ComplexOp::isComplex($c1
  910.             || !Math_ComplexOp::isComplex($c2)) {
  911.             return PEAR::raiseError('Both arguments must be PEAR::Math_Complex objects');
  912.         else {
  913.             $r ($c1->getReal($c2->getReal()) ($c1->getIm($c2->getIm());
  914.             $i ($c1->getReal($c2->getIm()) ($c2->getReal($c1->getIm());
  915.             return new Math_Complex$r$i );
  916.         }
  917.     }/*}}}*/
  918.  
  919.     /*{{{ div */
  920.     /**
  921.      * Returns the division of two complex numbers: z = c1 * c2
  922.      *
  923.      * @static
  924.      * @param Math_Complex $c1 
  925.      * @param Math_Complex $c2 
  926.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  927.      * @access public
  928.      */
  929.     function &div (&$c1&$c2
  930.     {
  931.         if (!Math_ComplexOp::isComplex($c1
  932.             || !Math_ComplexOp::isComplex($c2)) {
  933.             return PEAR::raiseError('Both arguments must be PEAR::Math_Complex objects');
  934.         else {
  935.             $a $c1->getReal()$b $c1->getIm();
  936.             $c $c2->getReal()$d $c2->getIm();
  937.             $div $c*$c $d*$d;
  938.             if ($div == 0.0{
  939.                 return PEAR::raiseError('Division by zero in Math_ComplexOp::div()');
  940.             else {
  941.                 $r ($a*$c $b*$d)/$div;
  942.                 $i ($b*$c $a*$d)/$div;
  943.                 return new Math_Complex$r$i );
  944.             }
  945.         }
  946.     }/*}}}*/
  947.  
  948.     /*{{{ pow() */
  949.     /**
  950.      * Returns the complex power of two complex numbers: z = c1^c2
  951.      *
  952.      * @static
  953.      * @param Math_Complex $c1 
  954.      * @param Math_Complex $c2 
  955.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  956.      * @access public
  957.      */
  958.     function &pow (&$c1&$c2
  959.     {
  960.         if (!Math_ComplexOp::isComplex($c1
  961.             || !Math_ComplexOp::isComplex($c2)) {
  962.             return PEAR::raiseError('Both arguments must be PEAR::Math_Complex objects');
  963.         else {
  964.             $ar $c1->getReal()$ai $c1->getIm();
  965.             $br $c2->getReal()$bi $c2->getIm();
  966.  
  967.             if ($ar == 0.0 && $ai == 0.0{
  968.                 $r $i = 0.0;
  969.             else {
  970.                 $logr log($c1->abs());
  971.                 $theta $c1->arg();
  972.                 $rho exp($logr $br $bi $theta);
  973.                 $beta $theta $br $bi $logr;
  974.                 $r $rho cos($beta);
  975.                 $i $rho sin($beta);
  976.             }
  977.             return new Math_Complex($r$i);
  978.         }
  979.     }/*}}}*/
  980.  
  981.     /*{{{ logBase() */
  982.     /**
  983.      * Returns the logarithm of base c2 of the complex number c1
  984.      *
  985.      * @static
  986.      * @param Math_Complex $c1 
  987.      * @param Math_Complex $c2 
  988.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  989.      * @access public
  990.      */
  991.     function &logBase (&$c1&$c2
  992.     {
  993.         if (!Math_ComplexOp::isComplex($c1
  994.             || !Math_ComplexOp::isComplex($c2)) {
  995.             return PEAR::raiseError('Both arguments must be PEAR::Math_Complex objects');
  996.         else {
  997.             return Math_ComplexOp::div(Math_ComplexOp::log($c1)Math_ComplexOp::log($c2));
  998.         }
  999.     }/*}}}*/
  1000.  
  1001.     // these functions need a complex number and a real number
  1002.  
  1003.     /*{{{ multReal() */
  1004.     /**
  1005.      * Multiplies a complex number by a real number: z = realnumber * c1
  1006.      *
  1007.      * @static
  1008.      * @param Math_Complex $c1 
  1009.      * @param float $real 
  1010.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  1011.      * @access public
  1012.      */
  1013.     function &multReal (&$c1$real
  1014.     {
  1015.         if (!Math_ComplexOp::isComplex($c1)) {
  1016.             return PEAR::raiseError('First argument is not a PEAR::Math_Complex object');
  1017.         }
  1018.         if (!is_numeric($real)) {
  1019.             return PEAR::raiseError('Second argument is not valid real number');
  1020.         }
  1021.         $r $c1->getReal($real;
  1022.         $i $c1->getIm($real;
  1023.         return new Math_Complex($r$i);
  1024.     }/*}}}*/
  1025.  
  1026.     /*{{{ multIm() */
  1027.     /**
  1028.      * Returns the product of a complex number and an imaginary number
  1029.      * if: x = b + c*i, y = a*i; then: z = x * y = multIm(x, a)
  1030.      *
  1031.      * @static
  1032.      * @param Math_Complex $c1 
  1033.      * @param float $im 
  1034.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  1035.      * @access public
  1036.      */
  1037.     function &multIm ($c1$im
  1038.     {
  1039.         if (!Math_ComplexOp::isComplex($c1)) {
  1040.             return PEAR::raiseError('First arguments must be PEAR::Math_Complex object');
  1041.         elseif (!is_numeric($im)) {
  1042.             return PEAR::raiseError("An imaginary coefficient is needed as second parameter");
  1043.         else {
  1044.             $r = -1 * $c1->getIm($im;
  1045.             $i $c1->getReal($im;
  1046.             return new Math_Complex($r$i);
  1047.         }
  1048.     }/*}}}*/
  1049.  
  1050.     /*{{{ powReal() */
  1051.     /**
  1052.      * Returns the exponentiation of a complex numbers to a real power: z = c1^(real)
  1053.      *
  1054.      * @static
  1055.      * @param Math_Complex $c1 
  1056.      * @param float $real 
  1057.      * @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
  1058.      * @access public
  1059.      */
  1060.     function &powReal ($c1$real
  1061.     {
  1062.         if (!Math_ComplexOp::isComplex($c1)) {
  1063.             return PEAR::raiseError('First arguments must be PEAR::Math_Complex object');
  1064.         elseif (!is_numeric($real)) {
  1065.             return PEAR::raiseError("An real number is needed as second parameter");
  1066.         else {
  1067.             $ar $c1->getReal()$ai $c1->getIm();
  1068.             if ($ar == 0 && $ai == 0{
  1069.                 $r $i = 0.0;
  1070.             else {
  1071.                 $logr log($c1->abs());
  1072.                 $theta $c1->arg();
  1073.                 $rho exp($logr $real);
  1074.                 $beta $theta $real;
  1075.                 $r $rho cos($beta);
  1076.                 $i $rho sin($beta);
  1077.             }
  1078.             return new Math_Complex($r$i);
  1079.         }
  1080.     }/*}}}*/
  1081.  
  1082.  
  1083. }/*}}} End of Math_ComplexOp*/
  1084. ?>

Documentation generated on Mon, 11 Mar 2019 15:39:24 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.