You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. PHPComplex
  2. ==========
  3. ---
  4. PHP Class for handling Complex numbers
  5. Master: [![Build Status](https://travis-ci.org/MarkBaker/PHPComplex.png?branch=master)](http://travis-ci.org/MarkBaker/PHPComplex)
  6. Develop: [![Build Status](https://travis-ci.org/MarkBaker/PHPComplex.png?branch=develop)](http://travis-ci.org/MarkBaker/PHPComplex)
  7. [![Complex Numbers](https://imgs.xkcd.com/comics/complex_numbers_2x.png)](https://xkcd.com/2028/)
  8. ---
  9. The library currently provides the following operations:
  10. - addition
  11. - subtraction
  12. - multiplication
  13. - division
  14. - division by
  15. - division into
  16. together with functions for
  17. - theta (polar theta angle)
  18. - rho (polar distance/radius)
  19. - conjugate
  20. * negative
  21. - inverse (1 / complex)
  22. - cos (cosine)
  23. - acos (inverse cosine)
  24. - cosh (hyperbolic cosine)
  25. - acosh (inverse hyperbolic cosine)
  26. - sin (sine)
  27. - asin (inverse sine)
  28. - sinh (hyperbolic sine)
  29. - asinh (inverse hyperbolic sine)
  30. - sec (secant)
  31. - asec (inverse secant)
  32. - sech (hyperbolic secant)
  33. - asech (inverse hyperbolic secant)
  34. - csc (cosecant)
  35. - acsc (inverse cosecant)
  36. - csch (hyperbolic secant)
  37. - acsch (inverse hyperbolic secant)
  38. - tan (tangent)
  39. - atan (inverse tangent)
  40. - tanh (hyperbolic tangent)
  41. - atanh (inverse hyperbolic tangent)
  42. - cot (cotangent)
  43. - acot (inverse cotangent)
  44. - coth (hyperbolic cotangent)
  45. - acoth (inverse hyperbolic cotangent)
  46. - sqrt (square root)
  47. - exp (exponential)
  48. - ln (natural log)
  49. - log10 (base-10 log)
  50. - log2 (base-2 log)
  51. - pow (raised to the power of a real number)
  52. ---
  53. # Usage
  54. To create a new complex object, you can provide either the real, imaginary and suffix parts as individual values, or as an array of values passed passed to the constructor; or a string representing the value. e.g
  55. ```
  56. $real = 1.23;
  57. $imaginary = -4.56;
  58. $suffix = 'i';
  59. $complexObject = new Complex\Complex($real, $imaginary, $suffix);
  60. ```
  61. or
  62. ```
  63. $real = 1.23;
  64. $imaginary = -4.56;
  65. $suffix = 'i';
  66. $arguments = [$real, $imaginary, $suffix];
  67. $complexObject = new Complex\Complex($arguments);
  68. ```
  69. or
  70. ```
  71. $complexString = '1.23-4.56i';
  72. $complexObject = new Complex\Complex($complexString);
  73. ```
  74. Complex objects are immutable: whenever you call a method or pass a complex value to a function that returns a complex value, a new Complex object will be returned, and the original will remain unchanged.
  75. This also allows you to chain multiple methods as you would for a fluent interface (as long as they are methods that will return a Complex result).
  76. ## Performing Mathematical Operations
  77. To perform mathematical operations with Complex values, you can call the appropriate method against a complex value, passing other values as arguments
  78. ```
  79. $complexString1 = '1.23-4.56i';
  80. $complexString2 = '2.34+5.67i';
  81. $complexObject = new Complex\Complex($complexString1);
  82. echo $complexObject->add($complexString2);
  83. ```
  84. or pass all values to the appropriate function
  85. ```
  86. $complexString1 = '1.23-4.56i';
  87. $complexString2 = '2.34+5.67i';
  88. echo Complex\add($complexString1, $complexString2);
  89. ```
  90. If you want to perform the same operation against multiple values (e.g. to add three or more complex numbers), then you can pass multiple arguments to any of the operations.
  91. You can pass these arguments as Complex objects, or as an array or string that will parse to a complex object.
  92. ## Using functions
  93. When calling any of the available functions for a complex value, you can either call the relevant method for the Complex object
  94. ```
  95. $complexString = '1.23-4.56i';
  96. $complexObject = new Complex\Complex($complexString);
  97. echo $complexObject->sinh();
  98. ```
  99. or you can call the function as you would in procedural code, passing the Complex object as an argument
  100. ```
  101. $complexString = '1.23-4.56i';
  102. $complexObject = new Complex\Complex($complexString);
  103. echo Complex\sinh($complexObject);
  104. ```
  105. When called procedurally using the function, you can pass in the argument as a Complex object, or as an array or string that will parse to a complex object.
  106. ```
  107. $complexString = '1.23-4.56i';
  108. echo Complex\sinh($complexString);
  109. ```
  110. In the case of the `pow()` function (the only implemented function that requires an additional argument) you need to pass both arguments when calling the function procedurally
  111. ```
  112. $complexString = '1.23-4.56i';
  113. $complexObject = new Complex\Complex($complexString);
  114. echo Complex\pow($complexObject, 2);
  115. ```
  116. or pass the additional argument when calling the method
  117. ```
  118. $complexString = '1.23-4.56i';
  119. $complexObject = new Complex\Complex($complexString);
  120. echo $complexObject->pow(2);
  121. ```