Blame view
Complex.class.php
4.04 KB
cf76164e6 20190709 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
<?php /** * @author Michele Andreoli <michi.andreoli[at]gmail.com> * @name Complex.class.php * @version 0.3 updated 09-05-2010 * @license http://opensource.org/licenses/gpl-license-php GNU Public License * @package FFT */ /** * Class that implements a complex data number */ class Complex { private $real; private $imag; /** * Create a complex datatype * @param double $real * @param double $imag */ public function __construct($real, $imag) { $this->real = $real; $this->imag = $imag; } /** * Return the multiplication of two complex number * @param complex $a * @param complex $b * @return complex */ static public function Cmul($a, $b) { $c = new Complex(0, 0); $c->setReal($a->getReal() * $b->getReal() - $a->getImag() * $b->getImag()); $c->setImag($a->getImag() * $b->getReal() + $a->getReal() * $b->getImag()); return $c; } /** * Return the sum of two complex number * @param complex $a * @param complex $b * @return complex */ static public function Cadd($a, $b) { $c = new Complex(0, 0); $c->setReal($a->getReal() + $b->getReal()); $c->setImag($a->getImag() + $b->getImag()); return $c; } /** * Return the difference of two complex number * @param complex $a * @param complex $b * @return complex */ static public function Csub($a, $b) { $c = new Complex(0, 0); $c->setReal($a->getReal() - $b->getReal()); $c->setImag($a->getImag() - $b->getImag()); return $c; } /** * Return the absolute value of a complex number * @param complex $z * @return double */ static public function Cabs($z) { $x = abs($z->getReal()); $y = abs($z->getImag()); if ($x == 0.0) $ans = $y; else if ($y == 0.0) $ans = $x; else if ($x > $y) { $temp = $y / $x; $ans = $x * sqrt(1.0 + $temp * $temp); } else { $temp = $x / $y; $ans = $y * sqrt(1.0 + $temp * $temp); } return $ans; } /** * Return the radix of two complex number * @param complex $z * @return complex */ static public function Csqrt($z) { if (($z->getReal() == 0.0) && ($z->getImag() == 0.0)) { $c = new Complex(0, 0); return $c; } else { $x = abs($z->getReal()); $y = abs($z->getImag()); if ($x >= $y) { $r = $y / $x; $w = sqrt($x) * sqrt(0.5 * (1.0 + sqrt(1.0 + $r * $r))); } else { $r = $x / $y; $w = sqrt($y) * sqrt(0.5 * ($r + sqrt(1.0 + $r * $r))); } $c = new Complex(0, 0); if ($z->getReal() >= 0.0) { $c->setReal($w); $c->setImag($z->getImag() / (2.0 * $w)); } else { if ($z->getImag() >= 0) $c->setImag($w); else $c->setImag(-$w); $c->setReal($z->getReal() / (2.0 * $c->getImag())); } return $c; } } /** * Return the inverse of a complex number * @param complex $z * @return complex */ static public function Cinv($z) { $c = new Complex(0, 0); $c->setReal($z->getReal()); $c->setImag(-$z->getImag()); return $c; } /** * Return the multiplication of a complex number with a scalar value * @param complex $n * @param complex $z * @return complex */ static public function RCmul($n, $z) { $c = new Complex(0, 0); $c->setReal($z->getReal() * $n); $c->setImag(-$z->getImag() * $n); return $c; } /** * Getters and setters */ public function setReal($real) { $this->real = $real; } public function setImag($imag) { $this->imag = $imag; } public function getReal() { return $this->real; } public function getImag() { return $this->imag; } } ?> |