Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
PHPComplex
2
==========
3
 
4
---
5
 
6
PHP Class Library for working with Complex numbers
7
 
8
[![Build Status](https://github.com/MarkBaker/PHPComplex/workflows/main/badge.svg)](https://github.com/MarkBaker/PHPComplex/actions)
9
[![Total Downloads](https://img.shields.io/packagist/dt/markbaker/complex)](https://packagist.org/packages/markbaker/complex)
10
[![Latest Stable Version](https://img.shields.io/github/v/release/MarkBaker/PHPComplex)](https://packagist.org/packages/markbaker/complex)
11
[![License](https://img.shields.io/github/license/MarkBaker/PHPComplex)](https://packagist.org/packages/markbaker/complex)
12
 
13
 
14
[![Complex Numbers](https://imgs.xkcd.com/comics/complex_numbers_2x.png)](https://xkcd.com/2028/)
15
 
16
---
17
 
18
The library currently provides the following operations:
19
 
20
 - addition
21
 - subtraction
22
 - multiplication
23
 - division
24
    - division by
25
    - division into
26
 
27
together with functions for
28
 
29
 - theta (polar theta angle)
30
 - rho (polar distance/radius)
31
 - conjugate
32
 * negative
33
 - inverse (1 / complex)
34
 - cos (cosine)
35
 - acos (inverse cosine)
36
 - cosh (hyperbolic cosine)
37
 - acosh (inverse hyperbolic cosine)
38
 - sin (sine)
39
 - asin (inverse sine)
40
 - sinh (hyperbolic sine)
41
 - asinh (inverse hyperbolic sine)
42
 - sec (secant)
43
 - asec (inverse secant)
44
 - sech (hyperbolic secant)
45
 - asech (inverse hyperbolic secant)
46
 - csc (cosecant)
47
 - acsc (inverse cosecant)
48
 - csch (hyperbolic secant)
49
 - acsch (inverse hyperbolic secant)
50
 - tan (tangent)
51
 - atan (inverse tangent)
52
 - tanh (hyperbolic tangent)
53
 - atanh (inverse hyperbolic tangent)
54
 - cot (cotangent)
55
 - acot (inverse cotangent)
56
 - coth (hyperbolic cotangent)
57
 - acoth (inverse hyperbolic cotangent)
58
 - sqrt (square root)
59
 - exp (exponential)
60
 - ln (natural log)
61
 - log10 (base-10 log)
62
 - log2 (base-2 log)
63
 - pow (raised to the power of a real number)
64
 
65
 
66
---
67
 
68
# Installation
69
 
70
```shell
71
composer require markbaker/complex:^1.0
72
```
73
 
74
# Important BC Note
75
 
76
If you've previously been using procedural calls to functions and operations using this library, then from version 3.0 you should use [MarkBaker/PHPComplexFunctions](https://github.com/MarkBaker/PHPComplexFunctions) instead (available on packagist as [markbaker/complex-functions](https://packagist.org/packages/markbaker/complex-functions)).
77
 
78
You'll need to replace `markbaker/complex`in your `composer.json` file with the new library, but otherwise there should be no difference in the namespacing, or in the way that you have called the Complex functions in the past, so no actual code changes are required.
79
 
80
```shell
81
composer require markbaker/complex-functions:^3.0
82
```
83
 
84
You should not reference this library (`markbaker/complex`) in your `composer.json`, composer wil take care of that for you.
85
 
86
# Usage
87
 
88
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
89
 
90
```php
91
$real = 1.23;
92
$imaginary = -4.56;
93
$suffix = 'i';
94
 
95
$complexObject = new Complex\Complex($real, $imaginary, $suffix);
96
```
97
or as an array
98
```php
99
$real = 1.23;
100
$imaginary = -4.56;
101
$suffix = 'i';
102
 
103
$arguments = [$real, $imaginary, $suffix];
104
 
105
$complexObject = new Complex\Complex($arguments);
106
```
107
or as a string
108
```php
109
$complexString = '1.23-4.56i';
110
 
111
$complexObject = new Complex\Complex($complexString);
112
```
113
 
114
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.
115
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).
116
 
117
## Performing Mathematical Operations
118
 
119
To perform mathematical operations with Complex values, you can call the appropriate method against a complex value, passing other values as arguments
120
 
121
```php
122
$complexString1 = '1.23-4.56i';
123
$complexString2 = '2.34+5.67i';
124
 
125
$complexObject = new Complex\Complex($complexString1);
126
echo $complexObject->add($complexString2);
127
```
128
 
129
or use the static Operation methods
130
```php
131
$complexString1 = '1.23-4.56i';
132
$complexString2 = '2.34+5.67i';
133
 
134
echo Complex\Operations::add($complexString1, $complexString2);
135
```
136
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.
137
 
138
You can pass these arguments as Complex objects, or as an array, or string that will parse to a complex object.
139
 
140
## Using functions
141
 
142
When calling any of the available functions for a complex value, you can either call the relevant method for the Complex object
143
```php
144
$complexString = '1.23-4.56i';
145
 
146
$complexObject = new Complex\Complex($complexString);
147
echo $complexObject->sinh();
148
```
149
 
150
or use the static Functions methods
151
```php
152
$complexString = '1.23-4.56i';
153
 
154
echo Complex\Functions::sinh($complexString);
155
```
156
As with operations, you can pass these arguments as Complex objects, or as an array or string that will parse to a complex object.
157
 
158
 
159
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
160
 
161
```php
162
$complexString = '1.23-4.56i';
163
 
164
$complexObject = new Complex\Complex($complexString);
165
echo Complex\Functions::pow($complexObject, 2);
166
```
167
or pass the additional argument when calling the method
168
```php
169
$complexString = '1.23-4.56i';
170
 
171
$complexObject = new Complex\Complex($complexString);
172
echo $complexObject->pow(2);
173
```