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