forked from jorgecasas/php-ml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrixTest.php
191 lines (160 loc) · 4.55 KB
/
MatrixTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
declare(strict_types=1);
namespace tests\Phpml\Math;
use Phpml\Math\Matrix;
use PHPUnit\Framework\TestCase;
class MatrixTest extends TestCase
{
/**
* @expectedException \Phpml\Exception\InvalidArgumentException
*/
public function testThrowExceptionOnInvalidMatrixSupplied()
{
new Matrix([[1, 2], [3]]);
}
public function testCreateMatrixFromFlatArray()
{
$flatArray = [1, 2, 3, 4];
$matrix = Matrix::fromFlatArray($flatArray);
$this->assertInstanceOf(Matrix::class, $matrix);
$this->assertEquals([[1], [2], [3], [4]], $matrix->toArray());
$this->assertEquals(4, $matrix->getRows());
$this->assertEquals(1, $matrix->getColumns());
$this->assertEquals($flatArray, $matrix->getColumnValues(0));
}
/**
* @expectedException \Phpml\Exception\MatrixException
*/
public function testThrowExceptionOnInvalidColumnNumber()
{
$matrix = new Matrix([[1, 2, 3], [4, 5, 6]]);
$matrix->getColumnValues(4);
}
/**
* @expectedException \Phpml\Exception\MatrixException
*/
public function testThrowExceptionOnGetDeterminantIfArrayIsNotSquare()
{
$matrix = new Matrix([[1, 2, 3], [4, 5, 6]]);
$matrix->getDeterminant();
}
public function testGetMatrixDeterminant()
{
//http://matrix.reshish.com/determinant.php
$matrix = new Matrix([
[3, 3, 3],
[4, 2, 1],
[5, 6, 7],
]);
$this->assertEquals(-3, $matrix->getDeterminant());
$matrix = new Matrix([
[1, 2, 3, 3, 2, 1],
[1 / 2, 5, 6, 7, 1, 1],
[3 / 2, 7 / 2, 2, 0, 6, 8],
[1, 8, 10, 1, 2, 2],
[1 / 4, 4, 1, 0, 2, 3 / 7],
[1, 8, 7, 5, 4, 4 / 5],
]);
$this->assertEquals(1116.5035, $matrix->getDeterminant(), '', $delta = 0.0001);
}
public function testMatrixTranspose()
{
$matrix = new Matrix([
[3, 3, 3],
[4, 2, 1],
[5, 6, 7],
]);
$transposedMatrix = [
[3, 4, 5],
[3, 2, 6],
[3, 1, 7],
];
$this->assertEquals($transposedMatrix, $matrix->transpose()->toArray());
}
/**
* @expectedException \Phpml\Exception\InvalidArgumentException
*/
public function testThrowExceptionOnMultiplyWhenInconsistentMatrixSupplied()
{
$matrix1 = new Matrix([[1, 2, 3], [4, 5, 6]]);
$matrix2 = new Matrix([[3, 2, 1], [6, 5, 4]]);
$matrix1->multiply($matrix2);
}
public function testMatrixMultiplyByMatrix()
{
$matrix1 = new Matrix([
[1, 2, 3],
[4, 5, 6],
]);
$matrix2 = new Matrix([
[7, 8],
[9, 10],
[11, 12],
]);
$product = [
[58, 64],
[139, 154],
];
$this->assertEquals($product, $matrix1->multiply($matrix2)->toArray());
}
public function testDivideByScalar()
{
$matrix = new Matrix([
[4, 6, 8],
[2, 10, 20],
]);
$quotient = [
[2, 3, 4],
[1, 5, 10],
];
$this->assertEquals($quotient, $matrix->divideByScalar(2)->toArray());
}
/**
* @expectedException \Phpml\Exception\MatrixException
*/
public function testThrowExceptionWhenInverseIfArrayIsNotSquare()
{
$matrix = new Matrix([[1, 2, 3], [4, 5, 6]]);
$matrix->inverse();
}
/**
* @expectedException \Phpml\Exception\MatrixException
*/
public function testThrowExceptionWhenInverseIfMatrixIsSingular()
{
$matrix = new Matrix([
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
]);
$matrix->inverse();
}
public function testInverseMatrix()
{
//http://ncalculators.com/matrix/inverse-matrix.htm
$matrix = new Matrix([
[3, 4, 2],
[4, 5, 5],
[1, 1, 1],
]);
$inverseMatrix = [
[0, -1, 5],
[1 / 2, 1 / 2, -7 / 2],
[-1 / 2, 1 / 2, -1 / 2],
];
$this->assertEquals($inverseMatrix, $matrix->inverse()->toArray(), '', $delta = 0.0001);
}
public function testCrossOutMatrix()
{
$matrix = new Matrix([
[3, 4, 2],
[4, 5, 5],
[1, 1, 1],
]);
$crossOuted = [
[3, 2],
[1, 1],
];
$this->assertEquals($crossOuted, $matrix->crossOut(1, 1)->toArray());
}
}