-
Notifications
You must be signed in to change notification settings - Fork 3
/
ComplexType.php
171 lines (148 loc) · 3.78 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
<?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\FloatType;
use chippyash\Type\Number\NumericTypeInterface;
/**
* 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
*
* @link http://en.wikipedia.org/wiki/Complex_number
*/
class ComplexType implements ComplexTypeInterface, NumericTypeInterface
{
/**
* Real part
* @var float
*/
protected $real;
/**
* Imaginary part
* @var float
*/
protected $imaginary;
public function __construct(FloatType $real, FloatType $imaginary)
{
$this->setFromTypes($real, $imaginary);
}
/**
* Return real part
* @return float
*/
public function r()
{
return $this->real;
}
/**
* Return imaginary part
* @return float
*/
public function i()
{
return $this->imaginary;
}
/**
* Is this number equal to zero?
* @return boolean
*/
public function isZero()
{
return ($this->real == 0 && $this->imaginary == 0);
}
/**
* Is this number
* @return boolean
* @link http://en.wikipedia.org/wiki/Gaussian_integer
*/
public function isGaussian()
{
return (intval($this->real) == $this->real && intval($this->imaginary) == $this->imaginary);
}
/**
* Return conjugate of this number
* @return chippyash\Type\Number\Complex\ComplexType
*/
public function conjugate()
{
return new self(new FloatType($this->real), new FloatType($this->imaginary * -1));
}
/**
* Return the modulus, also known as absolute value or magnitude of this number
*
* @return \chippyash\Type\Number\FloatType
*/
public function modulus()
{
return new FloatType(sqrt(pow($this->real, 2)+pow($this->imaginary, 2)));
}
/**
* Proxy to get()
*
* @return string
*/
public function __invoke()
{
return $this->get();
}
/**
* String representation of complex number
*
* @return string
*/
public function __toString()
{
$op = ($this->imaginary < 0 ? '' : '+');
return "{$this->real}{$op}{$this->imaginary}i";
}
/**
* Get PHP native representation. As there isn't one
* we'll proxy to to __toString
*
* @retun string
*/
public function 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\FloatType $real numerator
* @param \chippyash\Type\Number\FloatType $imaginary denominator
*
* @return chippyash\Type\Number\Complex\ComplexTypeInterface Fluent Interface
*/
public function setFromTypes(FloatType $real, FloatType $imaginary)
{
$this->real = $real();
$this->imaginary = $imaginary();
return $this;
}
}