Source for file ComplexOp.php
Documentation is available at ComplexOp.php
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Jesus M. Castagnetto <jmcastagnetto@php.net> |
// +----------------------------------------------------------------------+
// $Id: ComplexOp.php 304053 2010-10-05 00:43:46Z clockwerx $
include_once 'Math/Complex.php';
include_once 'Math/TrigOp.php';
* Package with classes to represent and manipulate complex number. Contain
* definitions for basic arithmetic functions, as well as trigonometric,
* inverse trigonometric, hyperbolic, inverse hyperbolic, exponential and
* logarithms of complex numbers.
* Math_ComplexOp: static class to operate on Math_Complex objects
* Originally this class was part of NumPHP (Numeric PHP package)
* @author Jesus M. Castagnetto <jmcastagnetto@php.net>
* Checks if a given object is an instance of PEAR::Math_Complex
return is_a(&$c1, 'math_complex');
/*{{{ createFromPolar() */
* Converts a polar complex z = r*exp(theta*i) to z = a + b*i
return new Math_Complex ( $a, $b );
// methods below need a valid Math_Complex object as parameter
* Calculates the complex square root of a complex number: z = sqrt(c1)
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
return PEAR ::raiseError ('argument is not a PEAR::Math_Complex object');
$x = abs($c1->getReal ());
if ($x == 0.0 && $y == 0 ) {
if ($c1->getReal () >= 0.0 ) {
$i = $c1->getIm () / (2.0 * $w);
$i = ($c1->getIm () >= 0 ) ? $w : -1 * $w;
$r = $c1->getIm () / (2.0 * $i);
return new Math_Complex ($r, $i);
* Calculates the complex square root of a real number: z = sqrt(realnumber)
* @param float $realnum A float
$i = sqrt(-1 * $realnum);
return new Math_Complex ($r, $i);
* Calculates the exponential of a complex number: z = exp(c1)
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
return PEAR ::raiseError ('argument is not a PEAR::Math_Complex object');
$rho = exp($c1->getReal ());
return new Math_Complex ($r, $i);
* Calculates the logarithm (base 2) of a complex number: z = log(c1)
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
return PEAR ::raiseError ('argument is not a PEAR::Math_Complex object');
return new Math_Complex ($r, $i);
* Calculates the logarithm (base 10) of a complex number: z = log10(c1)
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
* Calculates the conjugate of a complex number: z = conj(c1)
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
return PEAR ::raiseError ('argument is not a PEAR::Math_Complex object');
return new Math_Complex ($c1->getReal (), -1 * $c1->getIm ());
* Calculates the negative of a complex number: z = -c1
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
return PEAR ::raiseError ('argument is not a PEAR::Math_Complex object');
return new Math_Complex (-1 * $c1->getReal (), -1 * $c1->getIm ());
* Calculates the inverse of a complex number: z = 1/c1
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
return PEAR ::raiseError ('argument is not a PEAR::Math_Complex object');
return PEAR ::raiseError ('Math_Complex object\'s norm is zero');
$temp = 1.0 / $c1->abs ();
$r = $c1->getReal () * $temp * $temp;
$i = -1 * $c1->getIm () * $temp * $temp;
return new Math_Complex ($r, $i);
* Calculates the sine of a complex number: z = sin(c1)
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
return PEAR ::raiseError ('argument is not a PEAR::Math_Complex object');
$a = $c1->getReal (); $b = $c1->getIm ();
return new Math_Complex ( $r, $i );
* Calculates the cosine of a complex number: z = cos(c1)
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
return PEAR ::raiseError ('argument is not a PEAR::Math_Complex object');
$a = $c1->getReal (); $b = $c1->getIm ();
return new Math_Complex ( $r, $i );
* Calculates the tangent of a complex number: z = tan(c1)
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
return PEAR ::raiseError ('argument is not a PEAR::Math_Complex object');
$a = $c1->getReal (); $b = $c1->getIm ();
return PEAR ::raiseError ('Division by zero while calculating Math_ComplexOp::tan()');
$r = pow(Math_TrigOp ::sech ($b),2 )* tan($a)/ $den;
$i = pow(Math_TrigOp ::sec ($a),2 )* tanh($b)/ $den;
return new Math_Complex ( $r, $i );
* Calculates the secant of a complex number: z = sec(c1)
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
return PEAR ::raiseError ('argument is not a PEAR::Math_Complex object');
return Math_ComplexOP ::inverse ($z);
* Calculates the cosecant of a complex number: z = csc(c1)
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
return PEAR ::raiseError ('argument is not a PEAR::Math_Complex object');
return Math_ComplexOP ::inverse ($z);
* Calculates the cotangent of a complex number: z = cot(c1)
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
return Math_ComplexOP ::inverse ($z);
// Inverse trigonometric methods
* Calculates the inverse sine of a complex number: z = asin(c1)
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
return PEAR ::raiseError ('argument is not a PEAR::Math_Complex object');
$v = new Math_Complex ($t->getReal () - $c1->getIm (),
$t->getIm () + $c1->getReal ());
return new Math_Complex ($z->getIm (), -1* $z->getReal ());
* Calculates the inverse sine of a complex number: z = asinAlt(c1)
* Uses an alternative algorithm
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
return PEAR ::raiseError ('argument is not a PEAR::Math_Complex object');
$ac = 1.5; $bc = 0.6417; // crossover values
$d = 0.5 * ($a + $x) * ($y2 / ($r + $x + 1 ) + ($s + (1 - $x)));
$d = 0.5 * ($ax / ($r + $x + 1 ) + $ax / ($s + ($x - 1 )));
$m = 0.5 * ($y2 / ($r + ($x + 1 )) + $y2 / ($s + (1 - $x)));
$m = 0.5 * ($y2 / ($r + ($x + 1 )) + ($s + ($x - 1 )));
$real = ($r >= 0 ) ? $real : -1* $real;
$im = ($i >= 0 ) ? $im : -1* $im;
return new Math_Complex ($real, $im);
* Calculates the complex inverse sine of a real number: z = asinReal(r):
return new Math_Complex (asin($r), 0.0 );
return new Math_Complex (-1*M_PI_2 , acosh($r));
return new Math_Complex (M_PI_2 , -1* acosh($r));
* Calculates the inverse cosine of a complex number: z = acos(c1)
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
return PEAR ::raiseError ('argument is not a PEAR::Math_Complex object');
$v = new Math_Complex ($c1->getReal () - $t->getIm (),
$c1->getIm () + $t->getReal ());
return new Math_Complex ($z->getIm (), -1* $z->getReal ());
* Calculates the inverse tangent of a complex number: z = atan(c1):
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
return PEAR ::raiseError ('argument is not a PEAR::Math_Complex object');
$u = new Math_Complex (-1* $c1->getIm (), $c1->getReal ());
$t = new Math_Complex (1 ,0 );
* Calculates the inverse secant of a complex number: z = asec(c1)
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
return PEAR ::raiseError ('argument is not a PEAR::Math_Complex object');
* Calculates the inverse cosecant of a complex number: z = acsc(c1)
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
return PEAR ::raiseError ('argument is not a PEAR::Math_Complex object');
* Calculates the inverse cotangent of a complex number: z = acot(c1)
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
return PEAR ::raiseError ('argument is not a PEAR::Math_Complex object');
* Calculates the hyperbolic sine of a complex number: z = sinh(c1)
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
return PEAR ::raiseError ('argument is not a PEAR::Math_Complex object');
* Calculates the hyperbolic cosine of a complex number: z = cosh(c1)
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
return PEAR ::raiseError ('argument is not a PEAR::Math_Complex object');
* Calculates the hyperbolic tangent of a complex number: z = tanh(c1)
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
return PEAR ::raiseError ('argument is not a PEAR::Math_Complex object');
return new Math_Complex (sinh($r) * cosh($r) / $d, 0.5 * sin(2 * $i) / $d);
* Calculates the hyperbolic secant of a complex number: z = sech(c1)
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
if (PEAR ::isError ($c2)) {
* Calculates the hyperbolic cosecant of a complex number: z = csch(c1)
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
if (PEAR ::isError ($c2)) {
* Calculates the hyperbolic cotangent of a complex number: z = coth(c1)
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
if (PEAR ::isError ($c2)) {
// Inverse hyperbolic methods
* Calculates the inverse hyperbolic sine of a complex number: z = asinh(c1)
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
return PEAR ::raiseError ('argument is not a PEAR::Math_Complex object');
* Calculates the inverse hyperbolic cosine of a complex number: z = acosh(c1)
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
return PEAR ::raiseError ('argument is not a PEAR::Math_Complex object');
* Calculates the inverse hyperbolic tangent of a complex number: z = atanh(c1)
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
return PEAR ::raiseError ('argument is not a PEAR::Math_Complex object');
if ($c1->getIm () == 0.0 ) {
if ($r > -1.0 && $r < 1.0 ) {
return Math_Complex (atanh($r), 0.0 );
return Math_Complex (atanh(1 / $r), (($a < 0 ) ? M_PI_2 : -1 * M_PI_2 ));
* Calculates the inverse hyperbolic secant of a complex number: z = asech(c1)
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
return PEAR ::raiseError ('argument is not a PEAR::Math_Complex object');
* Calculates the inverse hyperbolic cosecant of a complex number: z = acsch(c1)
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
return PEAR ::raiseError ('argument is not a PEAR::Math_Complex object');
* Calculates the inverse hyperbolic cotangent of a complex number: z = acoth(c1)
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
return PEAR ::raiseError ('argument is not a PEAR::Math_Complex object');
// functions below need 2 valid Math_Complex objects as parameters
* Determines if is c1 == c2:
* @param Math_Complex $c1
* @param Math_Complex $c2
* @return boolean|PEAR_ErrorTrue if $c1 == $c2, False if $c1 != $c2, PEAR_Error object on error
return PEAR ::raiseError ('Both arguments must be PEAR::Math_Complex objects');
$same_real = ( $c1->getReal () == $c2->getReal () );
$same_im = ( $c1->getIm () == $c2->getIm () );
return ( $same_class && $same_real && $same_im );
* Returns the sum of two complex numbers: z = c1 + c2
* @param Math_Complex $c1
* @param Math_Complex $c2
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
function &add (&$c1, &$c2)
return PEAR ::raiseError ('Both arguments must be PEAR::Math_Complex objects');
return new Math_Complex ( $c1->getReal () + $c2->getReal (), $c1->getIm () + $c2->getIm ());
* Returns the difference of two complex numbers: z = c1 - c2
* @param Math_Complex $c1
* @param Math_Complex $c2
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
function &sub (&$c1, &$c2)
if (PEAR ::isError ($nc2)) {
* Returns the product of two complex numbers: z = c1 * c2
* @param Math_Complex $c1
* @param Math_Complex $c2
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
function &mult (&$c1, &$c2)
return PEAR ::raiseError ('Both arguments must be PEAR::Math_Complex objects');
$r = ($c1->getReal () * $c2->getReal ()) - ($c1->getIm () * $c2->getIm ());
$i = ($c1->getReal () * $c2->getIm ()) + ($c2->getReal () * $c1->getIm ());
return new Math_Complex ( $r, $i );
* Returns the division of two complex numbers: z = c1 * c2
* @param Math_Complex $c1
* @param Math_Complex $c2
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
function &div (&$c1, &$c2)
return PEAR ::raiseError ('Both arguments must be PEAR::Math_Complex objects');
$a = $c1->getReal (); $b = $c1->getIm ();
$c = $c2->getReal (); $d = $c2->getIm ();
return PEAR ::raiseError ('Division by zero in Math_ComplexOp::div()');
$r = ($a* $c + $b* $d)/ $div;
$i = ($b* $c - $a* $d)/ $div;
return new Math_Complex ( $r, $i );
* Returns the complex power of two complex numbers: z = c1^c2
* @param Math_Complex $c1
* @param Math_Complex $c2
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
function &pow (&$c1, &$c2)
return PEAR ::raiseError ('Both arguments must be PEAR::Math_Complex objects');
$ar = $c1->getReal (); $ai = $c1->getIm ();
$br = $c2->getReal (); $bi = $c2->getIm ();
if ($ar == 0.0 && $ai == 0.0 ) {
$rho = exp($logr * $br - $bi * $theta);
$beta = $theta * $br + $bi * $logr;
return new Math_Complex ($r, $i);
* Returns the logarithm of base c2 of the complex number c1
* @param Math_Complex $c1
* @param Math_Complex $c2
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
return PEAR ::raiseError ('Both arguments must be PEAR::Math_Complex objects');
// these functions need a complex number and a real number
* Multiplies a complex number by a real number: z = realnumber * c1
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
return PEAR ::raiseError ('First argument is not a PEAR::Math_Complex object');
return PEAR ::raiseError ('Second argument is not valid real number');
$r = $c1->getReal () * $real;
$i = $c1->getIm () * $real;
return new Math_Complex ($r, $i);
* Returns the product of a complex number and an imaginary number
* if: x = b + c*i, y = a*i; then: z = x * y = multIm(x, a)
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
return PEAR ::raiseError ('First arguments must be PEAR::Math_Complex object');
return PEAR ::raiseError ("An imaginary coefficient is needed as second parameter");
$r = -1 * $c1->getIm () * $im;
$i = $c1->getReal () * $im;
return new Math_Complex ($r, $i);
* Returns the exponentiation of a complex numbers to a real power: z = c1^(real)
* @param Math_Complex $c1
* @return Math_Complex|PEAR_ErrorA valid Math_Complex number on success, PEAR_Error otherwise
return PEAR ::raiseError ('First arguments must be PEAR::Math_Complex object');
return PEAR ::raiseError ("An real number is needed as second parameter");
$ar = $c1->getReal (); $ai = $c1->getIm ();
if ($ar == 0 && $ai == 0 ) {
$rho = exp($logr * $real);
return new Math_Complex ($r, $i);
}/*}}} End of Math_ComplexOp*/
Documentation generated on Mon, 11 Mar 2019 15:39:24 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|