ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Complex\Complex Class Reference
+ Collaboration diagram for Complex\Complex:

Public Member Functions

 __construct ($realPart=0.0, $imaginaryPart=null, $suffix='i')
 
 getReal ()
 Gets the real part of this complex number. More...
 
 getImaginary ()
 Gets the imaginary part of this complex number. More...
 
 getSuffix ()
 Gets the suffix of this complex number. More...
 
 isReal ()
 Returns true if this is a real value, false if a complex value. More...
 
 isComplex ()
 Returns true if this is a complex value, false if a real value. More...
 
 format ()
 
 __toString ()
 
 reverse ()
 Returns the reverse of this complex number. More...
 
 invertImaginary ()
 
 invertReal ()
 
 __call ($functionName, $arguments)
 Returns the result of the function call or operation. More...
 

Static Public Member Functions

static validateComplexArgument ($complex)
 Validates whether the argument is a valid complex number, converting scalar or array values if possible. More...
 

Data Fields

const EULER = 2.7182818284590452353602874713526624977572
 Euler's Number. More...
 
const NUMBER_SPLIT_REGEXP
 Regexp to split an input string into real and imaginary components and suffix More...
 

Protected Attributes

 $realPart = 0.0
 
 $imaginaryPart = 0.0
 
 $suffix
 

Static Protected Attributes

static $functions
 
static $operations
 

Static Private Member Functions

static parseComplex ($complexNumber)
 Validates whether the argument is a valid complex number, converting scalar or array values if possible. More...
 

Detailed Description

Definition at line 60 of file Complex.php.

Constructor & Destructor Documentation

◆ __construct()

Complex\Complex::__construct (   $realPart = 0.0,
  $imaginaryPart = null,
  $suffix = 'i' 
)

Definition at line 168 of file Complex.php.

169  {
170  if ($imaginaryPart === null) {
171  if (is_array($realPart)) {
172  // We have an array of (potentially) real and imaginary parts, and any suffix
173  list ($realPart, $imaginaryPart, $suffix) = array_values($realPart) + [0.0, 0.0, 'i'];
174  } elseif ((is_string($realPart)) || (is_numeric($realPart))) {
175  // We've been given a string to parse to extract the real and imaginary parts, and any suffix
176  list($realPart, $imaginaryPart, $suffix) = self::parseComplex($realPart);
177  }
178  }
179 
180  if ($imaginaryPart != 0.0 && empty($suffix)) {
181  $suffix = 'i';
182  } elseif ($imaginaryPart == 0.0 && !empty($suffix)) {
183  $suffix = '';
184  }
185 
186  // Set parsed values in our properties
187  $this->realPart = (float) $realPart;
188  $this->imaginaryPart = (float) $imaginaryPart;
189  $this->suffix = strtolower($suffix ?? '');
190  }

Member Function Documentation

◆ __call()

Complex\Complex::__call (   $functionName,
  $arguments 
)

Returns the result of the function call or operation.

Returns
Complex|float
Exceptions
Exception|

Definition at line 374 of file Complex.php.

375  {
376  $functionName = strtolower(str_replace('_', '', $functionName));
377 
378  // Test for function calls
379  if (in_array($functionName, self::$functions, true)) {
380  $functionName = "\\" . __NAMESPACE__ . "\\{$functionName}";
381  return $functionName($this, ...$arguments);
382  }
383  // Test for operation calls
384  if (in_array($functionName, self::$operations, true)) {
385  $functionName = "\\" . __NAMESPACE__ . "\\{$functionName}";
386  return $functionName($this, ...$arguments);
387  }
388  throw new Exception('Complex Function or Operation does not exist');
389  }

◆ __toString()

Complex\Complex::__toString ( )

Definition at line 265 of file Complex.php.

265  : string
266  {
267  return $this->format();
268  }

◆ format()

Complex\Complex::format ( )

Definition at line 242 of file Complex.php.

242  : string
243  {
244  $str = "";
245  if ($this->imaginaryPart != 0.0) {
246  if (\abs($this->imaginaryPart) != 1.0) {
247  $str .= $this->imaginaryPart . $this->suffix;
248  } else {
249  $str .= (($this->imaginaryPart < 0.0) ? '-' : '') . $this->suffix;
250  }
251  }
252  if ($this->realPart != 0.0) {
253  if (($str) && ($this->imaginaryPart > 0.0)) {
254  $str = "+" . $str;
255  }
256  $str = $this->realPart . $str;
257  }
258  if (!$str) {
259  $str = "0.0";
260  }
261 
262  return $str;
263  }

◆ getImaginary()

Complex\Complex::getImaginary ( )

Gets the imaginary part of this complex number.

Returns
Float

Definition at line 207 of file Complex.php.

207  : float
208  {
209  return $this->imaginaryPart;
210  }

◆ getReal()

Complex\Complex::getReal ( )

Gets the real part of this complex number.

Returns
Float

Definition at line 197 of file Complex.php.

197  : float
198  {
199  return $this->realPart;
200  }

◆ getSuffix()

Complex\Complex::getSuffix ( )

Gets the suffix of this complex number.

Returns
String

Definition at line 217 of file Complex.php.

217  : string
218  {
219  return $this->suffix;
220  }

◆ invertImaginary()

Complex\Complex::invertImaginary ( )

Definition at line 302 of file Complex.php.

302  : Complex
303  {
304  return new Complex(
305  $this->realPart,
306  $this->imaginaryPart * -1,
307  ($this->imaginaryPart == 0.0) ? null : $this->suffix
308  );
309  }

◆ invertReal()

Complex\Complex::invertReal ( )

Definition at line 311 of file Complex.php.

311  : Complex
312  {
313  return new Complex(
314  $this->realPart * -1,
315  $this->imaginaryPart,
316  ($this->imaginaryPart == 0.0) ? null : $this->suffix
317  );
318  }

◆ isComplex()

Complex\Complex::isComplex ( )

Returns true if this is a complex value, false if a real value.

Returns
Bool

Definition at line 237 of file Complex.php.

237  : bool
238  {
239  return !$this->isReal();
240  }
isReal()
Returns true if this is a real value, false if a complex value.
Definition: Complex.php:227

◆ isReal()

Complex\Complex::isReal ( )

Returns true if this is a real value, false if a complex value.

Returns
Bool

Definition at line 227 of file Complex.php.

227  : bool
228  {
229  return $this->imaginaryPart == 0.0;
230  }

◆ parseComplex()

static Complex\Complex::parseComplex (   $complexNumber)
staticprivate

Validates whether the argument is a valid complex number, converting scalar or array values if possible.

Parameters
mixed$complexNumberThe value to parse
Returns
array
Exceptions
ExceptionIf the argument isn't a Complex number or cannot be converted to one

Definition at line 109 of file Complex.php.

110  {
111  // Test for real number, with no imaginary part
112  if (is_numeric($complexNumber)) {
113  return [$complexNumber, 0, null];
114  }
115 
116  // Fix silly human errors
117  $complexNumber = str_replace(
118  ['+-', '-+', '++', '--'],
119  ['-', '-', '+', '+'],
120  $complexNumber
121  );
122 
123  // Basic validation of string, to parse out real and imaginary parts, and any suffix
124  $validComplex = preg_match(
125  self::NUMBER_SPLIT_REGEXP,
126  $complexNumber,
127  $complexParts
128  );
129 
130  if (!$validComplex) {
131  // Neither real nor imaginary part, so test to see if we actually have a suffix
132  $validComplex = preg_match('/^([\-\+]?)([ij])$/ui', $complexNumber, $complexParts);
133  if (!$validComplex) {
134  throw new Exception('Invalid complex number');
135  }
136  // We have a suffix, so set the real to 0, the imaginary to either 1 or -1 (as defined by the sign)
137  $imaginary = 1;
138  if ($complexParts[1] === '-') {
139  $imaginary = 0 - $imaginary;
140  }
141  return [0, $imaginary, $complexParts[2]];
142  }
143 
144  // If we don't have an imaginary part, identify whether it should be +1 or -1...
145  if (($complexParts[4] === '') && ($complexParts[9] !== '')) {
146  if ($complexParts[7] !== $complexParts[9]) {
147  $complexParts[4] = 1;
148  if ($complexParts[8] === '-') {
149  $complexParts[4] = -1;
150  }
151  } else {
152  // ... or if we have only the real and no imaginary part
153  // (in which case our real should be the imaginary)
154  $complexParts[4] = $complexParts[1];
155  $complexParts[1] = 0;
156  }
157  }
158 
159  // Return real and imaginary parts and suffix as an array, and set a default suffix if user input lazily
160  return [
161  $complexParts[1],
162  $complexParts[4],
163  !empty($complexParts[9]) ? $complexParts[9] : 'i'
164  ];
165  }

◆ reverse()

Complex\Complex::reverse ( )

Returns the reverse of this complex number.

Returns
Complex

Definition at line 293 of file Complex.php.

293  : Complex
294  {
295  return new Complex(
296  $this->imaginaryPart,
297  $this->realPart,
298  ($this->realPart == 0.0) ? null : $this->suffix
299  );
300  }

◆ validateComplexArgument()

static Complex\Complex::validateComplexArgument (   $complex)
static

Validates whether the argument is a valid complex number, converting scalar or array values if possible.

Parameters
mixed$complexThe value to validate
Returns
Complex
Exceptions
ExceptionIf the argument isn't a Complex number or cannot be converted to one

Definition at line 277 of file Complex.php.

277  : Complex
278  {
279  if (is_scalar($complex) || is_array($complex)) {
280  $complex = new Complex($complex);
281  } elseif (!is_object($complex) || !($complex instanceof Complex)) {
282  throw new Exception('Value is not a valid complex number');
283  }
284 
285  return $complex;
286  }

Field Documentation

◆ $functions

Complex\Complex::$functions
staticprotected

Definition at line 320 of file Complex.php.

◆ $imaginaryPart

Complex\Complex::$imaginaryPart = 0.0
protected

Definition at line 94 of file Complex.php.

◆ $operations

Complex\Complex::$operations
staticprotected
Initial value:
= [
'add',
'subtract',
'multiply',
'divideby',
'divideinto',
]

Definition at line 360 of file Complex.php.

◆ $realPart

Complex\Complex::$realPart = 0.0
protected

Definition at line 89 of file Complex.php.

◆ $suffix

Complex\Complex::$suffix
protected

Definition at line 99 of file Complex.php.

◆ EULER

const Complex\Complex::EULER = 2.7182818284590452353602874713526624977572

Euler's Number.

Definition at line 65 of file Complex.php.

◆ NUMBER_SPLIT_REGEXP

const Complex\Complex::NUMBER_SPLIT_REGEXP
Initial value:
=
'` ^
( # Real part
[-+]?(\d+\.?\d*|\d*\.?\d+) # Real value (integer or float)
([Ee][-+]?[0-2]?\d{1,3})? # Optional real exponent for scientific format
)
( # Imaginary part
[-+]?(\d+\.?\d*|\d*\.?\d+) # Imaginary value (integer or float)
([Ee][-+]?[0-2]?\d{1,3})? # Optional imaginary exponent for scientific format
)?
( # Imaginary part is optional
([-+]?) # Imaginary (implicit 1 or -1) only
([ij]?) # Imaginary i or j - depending on whether mathematical or engineering
)
$`uix'

Regexp to split an input string into real and imaginary components and suffix

Definition at line 70 of file Complex.php.


The documentation for this class was generated from the following file: