function pleac_Using_Complex_Numbers() { // PHP offers no native support for complex numbers. However, a 'Math_Complex' class // is available for download from PEAR: [http://pear.php.net/package/Math_Complex]. // Note the following 'include' directives are required: // // include_once('Math/Complex.php'); // include_once('Math/TrigOp.php'); // include_once('Math/ComplexOp.php'); $a = new Math_Complex(3, 5); $b = new Math_Complex(2, -2); $c = Math_ComplexOp::mult($a, $b); echo $c->toString() . "\n"; // ---------------------------- $d = new Math_Complex(3, 4); $r = Math_ComplexOp::sqrt($d); echo $r->toString() . "\n"; }
/** * Calculates the inverse cosine of a complex number: z = acos(c1) * * @param Math_Complex $c1 * @return Math_Complex A valid Math_Complex number on success * @throws InvalidArgumentException */ public static function acos(Math_Complex $c1) { if (!Math_ComplexOp::isComplex($c1)) { throw new InvalidArgumentException('argument is not a Math_Complex object'); } $t = Math_ComplexOp::mult($c1, $c1); $v = Math_ComplexOp::sub(new Math_Complex(1, 0), $t); $t = Math_ComplexOp::sqrt($v); $v = new Math_Complex($c1->getReal() - $t->getIm(), $c1->getIm() + $t->getReal()); $z = Math_ComplexOp::log($v); return new Math_Complex($z->getIm(), -1 * $z->getReal()); }
function testSqrt() { /*{{{*/ $tmp = Math_ComplexOp::sqrt($this->cnum1); $this->assertEquals('0.664490477541 + 0.3762281153i', $tmp->toString()); }
<?php /* * To test Math_Complex and Math_ComplexOp * $Id$ */ require_once 'Math/ComplexOp.php'; $a = new Math_Complex(0.3, 0.5); $b = new Math_Complex(1.0, -M_PI_2); $im = -1.2; echo "a = " . $a->toString() . "\n"; echo "b = " . $b->toString() . "\n"; echo "im = {$im}i\n"; $z = Math_ComplexOp::createFromPolar(0.022, -0.223); echo "from polar, z = " . $z->toString() . "\n"; $z = Math_ComplexOp::sqrt($a); echo "sqrt(a) = " . $z->toString() . "\n"; $z = Math_ComplexOp::sqrtReal(-2.3); echo "sqrtReal(a) = " . $z->toString() . "\n"; $z = Math_ComplexOp::exp($a); echo "exp(a) = " . $z->toString() . "\n"; $z = Math_ComplexOp::log($a); echo "log(a) = " . $z->toString() . "\n"; $z = Math_ComplexOp::log10($a); echo "log10(a) = " . $z->toString() . "\n"; $z = Math_ComplexOp::conjugate($a); echo "conjugate(a) = " . $z->toString() . "\n"; $z = Math_ComplexOp::negative($a); echo "negative(a) = " . $z->toString() . "\n"; $z = Math_ComplexOp::inverse($a); echo "inverse(a) = " . $z->toString() . "\n";