ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Complex.php
Go to the documentation of this file.
1 <?php
2 
3 class Complex {
4 
5  private $realPart = 0;
6  private $imaginaryPart = 0;
7  private $suffix = NULL;
8 
9  public static function _parseComplex($complexNumber)
10  {
11  // Test for real number, with no imaginary part
12  if (is_numeric($complexNumber))
13  return array( $complexNumber, 0, NULL );
14 
15  // Fix silly human errors
16  if (strpos($complexNumber,'+-') !== FALSE)
17  $complexNumber = str_replace('+-','-',$complexNumber);
18  if (strpos($complexNumber,'++') !== FALSE)
19  $complexNumber = str_replace('++','+',$complexNumber);
20  if (strpos($complexNumber,'--') !== FALSE)
21  $complexNumber = str_replace('--','-',$complexNumber);
22 
23  // Basic validation of string, to parse out real and imaginary parts, and any suffix
24  $validComplex = preg_match('/^([\-\+]?(\d+\.?\d*|\d*\.?\d+)([Ee][\-\+]?[0-2]?\d{1,3})?)([\-\+]?(\d+\.?\d*|\d*\.?\d+)([Ee][\-\+]?[0-2]?\d{1,3})?)?(([\-\+]?)([ij]?))$/ui',$complexNumber,$complexParts);
25 
26  if (!$validComplex) {
27  // Neither real nor imaginary part, so test to see if we actually have a suffix
28  $validComplex = preg_match('/^([\-\+]?)([ij])$/ui',$complexNumber,$complexParts);
29  if (!$validComplex) {
30  throw new Exception('COMPLEX: Invalid complex number');
31  }
32  // We have a suffix, so set the real to 0, the imaginary to either 1 or -1 (as defined by the sign)
33  $imaginary = 1;
34  if ($complexParts[1] === '-') {
35  $imaginary = 0 - $imaginary;
36  }
37  return array(0, $imaginary, $complexParts[2]);
38  }
39 
40  // If we don't have an imaginary part, identify whether it should be +1 or -1...
41  if (($complexParts[4] === '') && ($complexParts[9] !== '')) {
42  if ($complexParts[7] !== $complexParts[9]) {
43  $complexParts[4] = 1;
44  if ($complexParts[8] === '-') {
45  $complexParts[4] = -1;
46  }
47  // ... or if we have only the real and no imaginary part (in which case our real should be the imaginary)
48  } else {
49  $complexParts[4] = $complexParts[1];
50  $complexParts[1] = 0;
51  }
52  }
53 
54  // Return real and imaginary parts and suffix as an array, and set a default suffix if user input lazily
55  return array( $complexParts[1],
56  $complexParts[4],
57  !empty($complexParts[9]) ? $complexParts[9] : 'i'
58  );
59  } // function _parseComplex()
60 
61 
62  public function __construct($realPart, $imaginaryPart = null, $suffix = 'i')
63  {
64  if ($imaginaryPart === null) {
65  if (is_array($realPart)) {
66  // We have an array of (potentially) real and imaginary parts, and any suffix
67  list ($realPart, $imaginaryPart, $suffix) = array_values($realPart) + array(0.0, 0.0, 'i');
68  } elseif((is_string($realPart)) || (is_numeric($realPart))) {
69  // We've been given a string to parse to extract the real and imaginary parts, and any suffix
70  list ($realPart, $imaginaryPart, $suffix) = self::_parseComplex($realPart);
71  }
72  }
73 
74  // Set parsed values in our properties
75  $this->realPart = (float) $realPart;
76  $this->imaginaryPart = (float) $imaginaryPart;
77  $this->suffix = strtolower($suffix);
78  }
79 
80  public function getReal()
81  {
82  return $this->realPart;
83  }
84 
85  public function getImaginary()
86  {
87  return $this->imaginaryPart;
88  }
89 
90  public function getSuffix()
91  {
92  return $this->suffix;
93  }
94 
95  public function __toString() {
96  $str = "";
97  if ($this->imaginaryPart != 0.0) {
98  if (abs($this->imaginaryPart) != 1.0) {
99  $str .= $this->imaginaryPart . $this->suffix;
100  } else {
101  $str .= (($this->imaginaryPart < 0.0) ? '-' : ''). $this->suffix;
102  }
103  }
104  if ($this->realPart != 0.0) {
105  if (($str) && ($this->imaginaryPart > 0.0))
106  $str = "+" . $str;
107  $str = $this->realPart . $str;
108  }
109  if (!$str)
110  $str = "0.0";
111  return $str;
112  }
113 
114 }
$imaginaryPart
Definition: Complex.php:6
getSuffix()
Definition: Complex.php:90
__toString()
Definition: Complex.php:95
$suffix
Definition: Complex.php:7
getImaginary()
Definition: Complex.php:85
Create styles array
The data for the language used.
static _parseComplex($complexNumber)
Definition: Complex.php:9
__construct($realPart, $imaginaryPart=null, $suffix='i')
Definition: Complex.php:62
getReal()
Definition: Complex.php:80
$realPart
Definition: Complex.php:5