-
Notifications
You must be signed in to change notification settings - Fork 3
/
ComplexType.php
348 lines (311 loc) · 8.62 KB
/
ComplexType.php
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php
/*
* Hard type support
* For when you absolutely want to know what you are getting
*
* @author Ashley Kitson <akitson@zf4.biz>
* @copyright Ashley Kitson, UK, 2012
* @licence GPL V3 or later : http://www.gnu.org/licenses/gpl.html
*/
namespace chippyash\Type\Number\Complex;
use chippyash\Type\Number\Complex\ComplexTypeInterface;
use chippyash\Type\Number\Rational\RationalType;
use chippyash\Type\Number\Rational\RationalTypeFactory;
use chippyash\Type\Number\NumericTypeInterface;
use chippyash\Type\Exceptions\NotRealComplexException;
use chippyash\Type\Number\FloatType;
use chippyash\Type\Number\IntType;
/**
* A complex number - algabraic form
*
* A complex number is a number that can be expressed in the form a + bi,
* where a and b are real numbers and i is the imaginary unit,
* which satisfies the equation i² = −1
*
* Complex numbers use real numbers expressed as a RationalType. This allows
* for greater arithmetic stability
*
* @link http://en.wikipedia.org/wiki/Complex_number
*/
class ComplexType implements ComplexTypeInterface, NumericTypeInterface
{
/**
* Real part
* @var RationalType
*/
protected $real;
/**
* Imaginary part
* @var RationalType
*/
protected $imaginary;
public function __construct(RationalType $real, RationalType $imaginary)
{
$this->setFromTypes($real, $imaginary);
}
/**
* Return real part
* @return RationalType
*/
public function r()
{
return $this->real;
}
/**
* Return imaginary part
* @return RationalType
*/
public function i()
{
return $this->imaginary;
}
/**
* Is this number equal to zero?
* @return boolean
*/
public function isZero()
{
return ($this->real->numerator()->get() == 0 && $this->imaginary->numerator()->get() == 0);
}
/**
* Is this number Gaussian, i.e r & i are both equivelent to integers
*
* @return boolean
* @link http://en.wikipedia.org/wiki/Gaussian_integer
*/
public function isGaussian()
{
return ($this->real->denominator()->get() == 1 && $this->imaginary->denominator()->get() == 1);
}
/**
* Return conjugate of this number
* @return chippyash\Type\Number\Complex\ComplexType
*/
public function conjugate()
{
return new self($this->real, $this->imaginary->negate());
}
/**
* Return the modulus, also known as absolute value or magnitude of this number
* = sqrt(r^2 + i^2);
*
* @return \chippyash\Type\Number\Rational\RationalType
*/
public function modulus()
{
if ($this->isReal()) {
//sqrt(r^2 + 0^2) = sqrt(r^2) = abs(r)
return $this->real->abs();
}
//r^2 & i^2
$sqrR = ['n'=>pow($this->real->numerator()->get(), 2), 'd'=>pow($this->real->denominator()->get(),2)];
$sqrI = ['n'=>pow($this->imaginary->numerator()->get(), 2), 'd'=>pow($this->imaginary->denominator()->get(),2)];
//r^2 + i^2
$den = $this->lcm($sqrR['d'], $sqrI['d']);
$num = ($sqrR['n'] * $den / $sqrR['d']) +
($sqrI['n'] * $den / $sqrI['d']);
//sqrt(num/den) = sqrt(num)/sqrt(den)
//now this a fudge - we ought to be able to get a proper square root using
//factors etc but what we do instead is to do an approximation by converting
//to intermediate rationals using as much precision as we can i.e.
// rN = RationaType(sqrt(num))
// rD = RationalType(sqrt(den))
// mod = rN/1 * 1/rD
$rN = RationalTypeFactory::fromFloat(sqrt($num), 1e-17);
$rD = RationalTypeFactory::fromFloat(sqrt($den), 1e-17);
$modN = $rN->numerator()->get() * $rD->denominator()->get();
$modD = $rN->denominator()->get() * $rD->numerator()->get();
return RationalTypeFactory::create($modN, $modD);
}
/**
* Return the absolute value of the number
* Proxy to modulus
* Required for NumericTypeInterface
*
* @returns \chippyash\Type\Number\FloatType
*/
public function abs()
{
return $this->modulus();
}
/**
* Is this number a real number? i.e. is it in form n+0i
*
* @return boolean
*/
public function isReal()
{
return ($this->imaginary->numerator()->get() == 0);
}
/**
* Proxy to get()
*
* @return string
*/
public function __invoke()
{
return $this->get();
}
/**
* String representation of complex number
* If isReal() then string representation of the real part
* else r(+/-)ii
*
* @return string
*/
public function __toString()
{
$r = (string) $this->real;
if ($this->isReal()) {
return $r;
}
$op = ($this->imaginary->numerator()->get() < 0 ? '' : '+');
$i = (string) $this->imaginary;
return "{$r}{$op}{$i}i";
}
/**
* Get PHP native representation.
* Return float if this isReal() else there isn't one
* so we'll proxy to __toString
*
* @retun string
*/
public function get()
{
if ($this->isReal()) {
return $this->real->get();
}
return $this->__toString();
}
/**
* This extends the chippyash\Type\TypeInterface set method and finds the
* arguments to satisfy setFromTypes()
*
* Expected parameters
* @see setFromTypes
*
* @throws \InvalidArgumentException
*/
public function set($value)
{
if (func_num_args() !== 2) {
throw new \InvalidArgumentException('set() expects two parameters');
}
return $this->setFromTypes(func_get_arg(0), func_get_arg(1));
}
/**
* Set values for complex number
*
* @param \chippyash\Type\Number\Rational\RationalType $real real part
* @param \chippyash\Type\Number\Rational\RationalType $imaginary imaginary part
*
* @return chippyash\Type\Number\Complex\ComplexType Fluent Interface
*/
public function setFromTypes(RationalType $real, RationalType $imaginary)
{
$this->real = $real;
$this->imaginary = $imaginary;
return $this;
}
/**
* Negates the number
*
* @returns chippyash\Type\Number\Complex\ComplexType Fluent Interface
*/
public function negate()
{
$this->real->negate();
$this->imaginary->negate();
return $this;
}
/**
* if this complex number isReal() then return float equivalent
* else throw an excepton
*
* @return float
*
* @throws NotRealComplexException
*/
public function toFloat()
{
if ($this->isReal()) {
return $this->real->get();
} else {
throw new NotRealComplexException();
}
}
/**
* Return the number as a Complex number i.e. a clone of this one
* Required for NumericTypeInterface
*
* @return chippyash\Type\Number\Complex\ComplexType
*/
public function asComplex()
{
return clone $this;
}
/**
* Return number as Rational number.
* NB, numerator and denominator will be caste as IntTypes
*
* @returns chippyash\Type\Number\Rational\RationalType
*
* @throws NotRealComplexException
*/
public function asRational()
{
if ($this->isReal()) {
return clone $this->real;
} else {
throw new NotRealComplexException();
}
}
/**
* Return number as an IntType number.
* If number isReal() will return floor(r())
*
* @returns chippyash\Type\Number\IntType
*/
public function asIntType()
{
if ($this->isReal()) {
return new IntType(floor($this->real->get()));
} else {
throw new NotRealComplexException();
}
}
/**
* Return number as a FloatType number.
*
* @returns chippyash\Type\Number\FloatType
*/
public function asFloatType()
{
if ($this->isReal()) {
return new FloatType($this->real->get());
} else {
throw new NotRealComplexException();
}
}
/**
* Return Greatest Common Denominator of two numbers
*
* @param int $a
* @param int $b
* @return int
*/
private function gcd($a, $b)
{
return $b ? $this->gcd($b, $a % $b) : $a;
}
/**
* Return Least Common Multiple of two numbers
* @param int $a
* @param int $b
* @return int
*/
private function lcm($a, $b)
{
return \abs(($a * $b) / $this->gcd($a, $b));
}
}