Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
PHPMatrix
2
==========
3
 
4
---
5
 
6
PHP Class for handling Matrices
7
 
8
[![Build Status](https://github.com/MarkBaker/PHPMatrix/workflows/main/badge.svg)](https://github.com/MarkBaker/PHPMatrix/actions)
9
[![Total Downloads](https://img.shields.io/packagist/dt/markbaker/matrix)](https://packagist.org/packages/markbaker/matrix)
10
[![Latest Stable Version](https://img.shields.io/github/v/release/MarkBaker/PHPMatrix)](https://packagist.org/packages/markbaker/matrix)
11
[![License](https://img.shields.io/github/license/MarkBaker/PHPMatrix)](https://packagist.org/packages/markbaker/matrix)
12
 
13
 
14
[![Matrix Transform](https://imgs.xkcd.com/comics/matrix_transform.png)](https://xkcd.com/184/)
15
 
16
Matrix Transform
17
 
18
---
19
 
20
This library currently provides the following operations:
21
 
22
 - addition
23
 - direct sum
24
 - subtraction
25
 - multiplication
26
 - division (using [A].[B]<sup>-1</sup>)
27
    - division by
28
    - division into
29
 
30
together with functions for
31
 
32
 - adjoint
33
 - antidiagonal
34
 - cofactors
35
 - determinant
36
 - diagonal
37
 - identity
38
 - inverse
39
 - minors
40
 - trace
41
 - transpose
42
 - solve
43
 
44
   Given Matrices A and B, calculate X for A.X = B
45
 
46
and classes for
47
 
48
 - Decomposition
49
   - LU Decomposition with partial row pivoting,
50
 
51
     such that [P].[A] = [L].[U] and [A] = [P]<sup>|</sup>.[L].[U]
52
   - QR Decomposition
53
 
54
     such that [A] = [Q].[R]
55
 
56
## TO DO
57
 
58
 - power() function
59
 - Decomposition
60
   - Cholesky Decomposition
61
   - EigenValue Decomposition
62
     - EigenValues
63
     - EigenVectors
64
 
65
---
66
 
67
# Installation
68
 
69
```shell
70
composer require markbaker/matrix:^3.0
71
```
72
 
73
# Important BC Note
74
 
75
If you've previously been using procedural calls to functions and operations using this library, then from version 3.0 you should use [MarkBaker/PHPMatrixFunctions](https://github.com/MarkBaker/PHPMatrixFunctions) instead (available on packagist as [markbaker/matrix-functions](https://packagist.org/packages/markbaker/matrix-functions)).
76
 
77
You'll need to replace `markbaker/matrix`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 Matrix functions in the past, so no actual code changes are required.
78
 
79
```shell
80
composer require markbaker/matrix-functions:^1.0
81
```
82
 
83
You should not reference this library (`markbaker/matrix`) in your `composer.json`, composer wil take care of that for you.
84
 
85
# Usage
86
 
87
To create a new Matrix object, provide an array as the constructor argument
88
 
89
```php
90
$grid = [
91
    [16,  3,  2, 13],
92
    [ 5, 10, 11,  8],
93
    [ 9,  6,  7, 12],
94
    [ 4, 15, 14,  1],
95
];
96
 
97
$matrix = new Matrix\Matrix($grid);
98
```
99
The `Builder` class provides helper methods for creating specific matrices, specifically an identity matrix of a specified size; or a matrix of a specified dimensions, with every cell containing a set value.
100
```php
101
$matrix = Matrix\Builder::createFilledMatrix(1, 5, 3);
102
```
103
Will create a matrix of 5 rows and 3 columns, filled with a `1` in every cell; while
104
```php
105
$matrix = Matrix\Builder::createIdentityMatrix(3);
106
```
107
will create a 3x3 identity matrix.
108
 
109
 
110
Matrix objects are immutable: whenever you call a method or pass a grid to a function that returns a matrix value, a new Matrix object will be returned, and the original will remain unchanged. 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 Matrix result).
111
 
112
## Performing Mathematical Operations
113
 
114
To perform mathematical operations with Matrices, you can call the appropriate method against a matrix value, passing other values as arguments
115
 
116
```php
117
$matrix1 = new Matrix\Matrix([
118
    [2, 7, 6],
119
    [9, 5, 1],
120
    [4, 3, 8],
121
]);
122
$matrix2 = new Matrix\Matrix([
123
    [1, 2, 3],
124
    [4, 5, 6],
125
    [7, 8, 9],
126
]);
127
 
128
var_dump($matrix1->multiply($matrix2)->toArray());
129
```
130
or pass all values to the appropriate static method
131
```php
132
$matrix1 = new Matrix\Matrix([
133
    [2, 7, 6],
134
    [9, 5, 1],
135
    [4, 3, 8],
136
]);
137
$matrix2 = new Matrix\Matrix([
138
    [1, 2, 3],
139
    [4, 5, 6],
140
    [7, 8, 9],
141
]);
142
 
143
var_dump(Matrix\Operations::multiply($matrix1, $matrix2)->toArray());
144
```
145
You can pass in the arguments as Matrix objects, or as arrays.
146
 
147
If you want to perform the same operation against multiple values (e.g. to add three or more matrices), then you can pass multiple arguments to any of the operations.
148
 
149
## Using functions
150
 
151
When calling any of the available functions for a matrix value, you can either call the relevant method for the Matrix object
152
```php
153
$grid = [
154
    [16,  3,  2, 13],
155
    [ 5, 10, 11,  8],
156
    [ 9,  6,  7, 12],
157
    [ 4, 15, 14,  1],
158
];
159
 
160
$matrix = new Matrix\Matrix($grid);
161
 
162
echo $matrix->trace();
163
```
164
or you can call the static method, passing the Matrix object or array as an argument
165
```php
166
$grid = [
167
    [16,  3,  2, 13],
168
    [ 5, 10, 11,  8],
169
    [ 9,  6,  7, 12],
170
    [ 4, 15, 14,  1],
171
];
172
 
173
$matrix = new Matrix\Matrix($grid);
174
echo Matrix\Functions::trace($matrix);
175
```
176
```php
177
$grid = [
178
    [16,  3,  2, 13],
179
    [ 5, 10, 11,  8],
180
    [ 9,  6,  7, 12],
181
    [ 4, 15, 14,  1],
182
];
183
 
184
echo Matrix\Functions::trace($grid);
185
```
186
 
187
## Decomposition
188
 
189
The library also provides classes for matrix decomposition. You can access these using
190
```php
191
$grid = [
192
    [1, 2],
193
    [3, 4],
194
];
195
 
196
$matrix = new Matrix\Matrix($grid);
197
 
198
$decomposition = new Matrix\Decomposition\QR($matrix);
199
$Q = $decomposition->getQ();
200
$R = $decomposition->getR();
201
```
202
 
203
or alternatively us the `Decomposition` factory, identifying which form of decomposition you want to use
204
```php
205
$grid = [
206
    [1, 2],
207
    [3, 4],
208
];
209
 
210
$matrix = new Matrix\Matrix($grid);
211
 
212
$decomposition = Matrix\Decomposition\Decomposition::decomposition(Matrix\Decomposition\Decomposition::QR, $matrix);
213
$Q = $decomposition->getQ();
214
$R = $decomposition->getR();
215
```