ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
Complex Class Reference
+ Collaboration diagram for Complex:

Public Member Functions

 __construct ($realPart, $imaginaryPart=null, $suffix='i')
 
 getReal ()
 
 getImaginary ()
 
 getSuffix ()
 
 __toString ()
 

Static Public Member Functions

static _parseComplex ($complexNumber)
 

Private Attributes

 $realPart = 0
 
 $imaginaryPart = 0
 
 $suffix = NULL
 

Detailed Description

Definition at line 3 of file Complex.php.

Constructor & Destructor Documentation

◆ __construct()

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

Definition at line 62 of file Complex.php.

References $imaginaryPart, $realPart, $suffix, and array.

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  }
$imaginaryPart
Definition: Complex.php:6
$suffix
Definition: Complex.php:7
Create styles array
The data for the language used.
$realPart
Definition: Complex.php:5

Member Function Documentation

◆ __toString()

Complex::__toString ( )

Definition at line 95 of file Complex.php.

References $suffix.

95  {
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  }
$suffix
Definition: Complex.php:7

◆ _parseComplex()

static Complex::_parseComplex (   $complexNumber)
static

Definition at line 9 of file Complex.php.

References array.

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()
Create styles array
The data for the language used.

◆ getImaginary()

Complex::getImaginary ( )

Definition at line 85 of file Complex.php.

References $imaginaryPart.

86  {
87  return $this->imaginaryPart;
88  }
$imaginaryPart
Definition: Complex.php:6

◆ getReal()

Complex::getReal ( )

Definition at line 80 of file Complex.php.

References $realPart.

81  {
82  return $this->realPart;
83  }
$realPart
Definition: Complex.php:5

◆ getSuffix()

Complex::getSuffix ( )

Definition at line 90 of file Complex.php.

References $suffix.

91  {
92  return $this->suffix;
93  }
$suffix
Definition: Complex.php:7

Field Documentation

◆ $imaginaryPart

Complex::$imaginaryPart = 0
private

Definition at line 6 of file Complex.php.

Referenced by __construct(), and getImaginary().

◆ $realPart

Complex::$realPart = 0
private

Definition at line 5 of file Complex.php.

Referenced by __construct(), and getReal().

◆ $suffix

Complex::$suffix = NULL
private

Definition at line 7 of file Complex.php.

Referenced by __construct(), __toString(), and getSuffix().


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