blob: d075479586128dbac92d44af0c607fbbc88d242f (
plain) (
blame)
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
|
<?php
/**
*
* Class for the creating "special" Matrices
*
* @copyright Copyright (c) 2018 Mark Baker (https://github.com/MarkBaker/PHPMatrix)
* @license https://opensource.org/licenses/MIT MIT
*/
namespace Matrix;
/**
* Matrix Builder class.
*
* @package Matrix
*/
class Builder
{
/**
* Create a new matrix of specified dimensions, and filled with a specified value
* If the column argument isn't provided, then a square matrix will be created
*
* @param mixed $fillValue
* @param int $rows
* @param int|null $columns
* @return Matrix
* @throws Exception
*/
public static function createFilledMatrix($fillValue, $rows, $columns = null)
{
if ($columns === null) {
$columns = $rows;
}
$rows = Matrix::validateRow($rows);
$columns = Matrix::validateColumn($columns);
return new Matrix(
array_fill(
0,
$rows,
array_fill(
0,
$columns,
$fillValue
)
)
);
}
/**
* Create a new identity matrix of specified dimensions
* This will always be a square matrix, with the number of rows and columns matching the provided dimension
*
* @param int $dimensions
* @return Matrix
* @throws Exception
*/
public static function createIdentityMatrix($dimensions, $fillValue = null)
{
$grid = static::createFilledMatrix($fillValue, $dimensions)->toArray();
for ($x = 0; $x < $dimensions; ++$x) {
$grid[$x][$x] = 1;
}
return new Matrix($grid);
}
}
|