| 1 |
PHPComplex |
| 2 |
========== |
| 3 |
|
| 4 |
--- |
| 5 |
|
| 6 |
PHP Class for handling Complex numbers |
| 7 |
|
| 8 |
Master: [](http://travis-ci.org/MarkBaker/PHPComplex](http://travis-ci.org/MarkBaker/PHPComplex](http://travis-ci.org/MarkBaker/PHPComplex) |
| 9 |
|
| 10 |
Develop: [](http://travis-ci.org/MarkBaker/PHPComplex](http://travis-ci.org/MarkBaker/PHPComplex](http://travis-ci.org/MarkBaker/PHPComplex) |
| 11 |
|
| 12 |
[](https://xkcd.com/2028/](https://xkcd.com/2028/](https://xkcd.com/2028/) |
| 13 |
|
| 14 |
--- |
| 15 |
|
| 16 |
The library currently provides the following operations: |
| 17 |
|
| 18 |
- addition |
| 19 |
- subtraction |
| 20 |
- multiplication |
| 21 |
- division |
| 22 |
- division by |
| 23 |
- division into |
| 24 |
|
| 25 |
together with functions for |
| 26 |
|
| 27 |
- theta (polar theta angle) |
| 28 |
- rho (polar distance/radius) |
| 29 |
- conjugate |
| 30 |
* negative |
| 31 |
- inverse (1 / complex) |
| 32 |
- cos (cosine) |
| 33 |
- acos (inverse cosine) |
| 34 |
- cosh (hyperbolic cosine) |
| 35 |
- acosh (inverse hyperbolic cosine) |
| 36 |
- sin (sine) |
| 37 |
- asin (inverse sine) |
| 38 |
- sinh (hyperbolic sine) |
| 39 |
- asinh (inverse hyperbolic sine) |
| 40 |
- sec (secant) |
| 41 |
- asec (inverse secant) |
| 42 |
- sech (hyperbolic secant) |
| 43 |
- asech (inverse hyperbolic secant) |
| 44 |
- csc (cosecant) |
| 45 |
- acsc (inverse cosecant) |
| 46 |
- csch (hyperbolic secant) |
| 47 |
- acsch (inverse hyperbolic secant) |
| 48 |
- tan (tangent) |
| 49 |
- atan (inverse tangent) |
| 50 |
- tanh (hyperbolic tangent) |
| 51 |
- atanh (inverse hyperbolic tangent) |
| 52 |
- cot (cotangent) |
| 53 |
- acot (inverse cotangent) |
| 54 |
- coth (hyperbolic cotangent) |
| 55 |
- acoth (inverse hyperbolic cotangent) |
| 56 |
- sqrt (square root) |
| 57 |
- exp (exponential) |
| 58 |
- ln (natural log) |
| 59 |
- log10 (base-10 log) |
| 60 |
- log2 (base-2 log) |
| 61 |
- pow (raised to the power of a real number) |
| 62 |
|
| 63 |
|
| 64 |
--- |
| 65 |
|
| 66 |
# Usage |
| 67 |
|
| 68 |
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 |
| 69 |
|
| 70 |
``` |
| 71 |
$real = 1.23; |
| 72 |
$imaginary = -4.56; |
| 73 |
$suffix = 'i'; |
| 74 |
|
| 75 |
$complexObject = new Complex\Complex($real, $imaginary, $suffix); |
| 76 |
``` |
| 77 |
or |
| 78 |
``` |
| 79 |
$real = 1.23; |
| 80 |
$imaginary = -4.56; |
| 81 |
$suffix = 'i'; |
| 82 |
|
| 83 |
$arguments = [$real, $imaginary, $suffix]; |
| 84 |
|
| 85 |
$complexObject = new Complex\Complex($arguments); |
| 86 |
``` |
| 87 |
or |
| 88 |
``` |
| 89 |
$complexString = '1.23-4.56i'; |
| 90 |
|
| 91 |
$complexObject = new Complex\Complex($complexString); |
| 92 |
``` |
| 93 |
|
| 94 |
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. |
| 95 |
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). |
| 96 |
|
| 97 |
## Performing Mathematical Operations |
| 98 |
|
| 99 |
To perform mathematical operations with Complex values, you can call the appropriate method against a complex value, passing other values as arguments |
| 100 |
|
| 101 |
``` |
| 102 |
$complexString1 = '1.23-4.56i'; |
| 103 |
$complexString2 = '2.34+5.67i'; |
| 104 |
|
| 105 |
$complexObject = new Complex\Complex($complexString1); |
| 106 |
echo $complexObject->add($complexString2); |
| 107 |
``` |
| 108 |
or pass all values to the appropriate function |
| 109 |
``` |
| 110 |
$complexString1 = '1.23-4.56i'; |
| 111 |
$complexString2 = '2.34+5.67i'; |
| 112 |
|
| 113 |
echo Complex\add($complexString1, $complexString2); |
| 114 |
``` |
| 115 |
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. |
| 116 |
|
| 117 |
You can pass these arguments as Complex objects, or as an array or string that will parse to a complex object. |
| 118 |
|
| 119 |
## Using functions |
| 120 |
|
| 121 |
When calling any of the available functions for a complex value, you can either call the relevant method for the Complex object |
| 122 |
``` |
| 123 |
$complexString = '1.23-4.56i'; |
| 124 |
|
| 125 |
$complexObject = new Complex\Complex($complexString); |
| 126 |
echo $complexObject->sinh(); |
| 127 |
``` |
| 128 |
or you can call the function as you would in procedural code, passing the Complex object as an argument |
| 129 |
``` |
| 130 |
$complexString = '1.23-4.56i'; |
| 131 |
|
| 132 |
$complexObject = new Complex\Complex($complexString); |
| 133 |
echo Complex\sinh($complexObject); |
| 134 |
``` |
| 135 |
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. |
| 136 |
``` |
| 137 |
$complexString = '1.23-4.56i'; |
| 138 |
|
| 139 |
echo Complex\sinh($complexString); |
| 140 |
``` |
| 141 |
|
| 142 |
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 |
| 143 |
|
| 144 |
``` |
| 145 |
$complexString = '1.23-4.56i'; |
| 146 |
|
| 147 |
$complexObject = new Complex\Complex($complexString); |
| 148 |
echo Complex\pow($complexObject, 2); |
| 149 |
``` |
| 150 |
or pass the additional argument when calling the method |
| 151 |
``` |
| 152 |
$complexString = '1.23-4.56i'; |
| 153 |
|
| 154 |
$complexObject = new Complex\Complex($complexString); |
| 155 |
echo $complexObject->pow(2); |
| 156 |
``` |
| 157 |
|