1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hotrush\AngularSweep; |
4
|
|
|
|
5
|
|
|
class ComplexNumber |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* The real part of the complex number |
9
|
|
|
* |
10
|
|
|
* @var float |
11
|
|
|
* @access private |
12
|
|
|
*/ |
13
|
|
|
private $_real; |
14
|
|
|
/** |
15
|
|
|
* The imaginary part of the complex number |
16
|
|
|
* |
17
|
|
|
* @var float |
18
|
|
|
* @access private |
19
|
|
|
*/ |
20
|
|
|
private $_im; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Constructor for ComplexNumberPoint |
24
|
|
|
* |
25
|
|
|
* @param float $real Real part of the number |
26
|
|
|
* @param float $im Imaginary part of the number |
27
|
|
|
*/ |
28
|
|
|
public function __construct($real, $im) |
29
|
|
|
{ |
30
|
|
|
$this->_real = floatval($real); |
31
|
|
|
$this->_im = floatval($im); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Simple string representation of the number. |
36
|
|
|
* |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
public function toString() |
40
|
|
|
{ |
41
|
|
|
$r = $this->getReal(); |
42
|
|
|
$i = $this->getIm(); |
43
|
|
|
$str = $r; |
44
|
|
|
$str .= ($i < 0) ? ' - ' : ' + '; |
45
|
|
|
$str .= abs($i) . 'i'; |
46
|
|
|
return $str; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Returns the square of the magnitude of the number. |
51
|
|
|
* |
52
|
|
|
* @return float |
53
|
|
|
*/ |
54
|
|
|
public function abs2() |
55
|
|
|
{ |
56
|
|
|
return ($this->_real * $this->_real + $this->_im * $this->_im); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns the magnitude (also referred as norm) of the number. |
61
|
|
|
* |
62
|
|
|
* @return float |
63
|
|
|
*/ |
64
|
|
|
public function abs() |
65
|
|
|
{ |
66
|
|
|
return sqrt($this->abs2()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns the argument of the complex number. |
71
|
|
|
* |
72
|
|
|
* @return float A floating point number on success |
73
|
|
|
* @throws \Exception |
74
|
|
|
*/ |
75
|
|
|
public function arg() |
76
|
|
|
{ |
77
|
|
|
$arg = atan2($this->_im, $this->_real); |
78
|
|
|
if (M_PI < $arg || $arg < -1 * M_PI) { |
79
|
|
|
throw new \Exception('Argument has an impossible value'); |
80
|
|
|
} else { |
81
|
|
|
return $arg; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns the angle (argument) associated with the complex number. |
87
|
|
|
* |
88
|
|
|
* @return mixed A float on success |
89
|
|
|
* @throws \Exception |
90
|
|
|
*/ |
91
|
|
|
public function angle() |
92
|
|
|
{ |
93
|
|
|
return $this->arg(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns the real part of the complex number. |
98
|
|
|
* |
99
|
|
|
* @return float |
100
|
|
|
*/ |
101
|
|
|
public function getReal() |
102
|
|
|
{ |
103
|
|
|
return $this->_real; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns the imaginary part of the complex number. |
108
|
|
|
* @return float |
109
|
|
|
*/ |
110
|
|
|
public function getIm() |
111
|
|
|
{ |
112
|
|
|
return $this->_im; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Subtract complex number. |
117
|
|
|
* |
118
|
|
|
* @param ComplexNumber $number |
119
|
|
|
* @return ComplexNumber |
120
|
|
|
*/ |
121
|
|
|
public function subtract(ComplexNumber $number) |
122
|
|
|
{ |
123
|
|
|
return new ComplexNumber( |
124
|
|
|
$this->getReal() - $number->getReal(), |
125
|
|
|
$this->getIm() - $number->getIm() |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
} |