No Description

Complex.php 3.5KB

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