Today I would like to continue the story about a wonderful library for working with numeric arrays in php
numphp . Earlier, I already did a brief review on it
here . Since then, the library has acquired functionality, and, most importantly, has learned how to work with multidimensional arrays or matrices. About them, basically, there will be a speech.
Without unnecessary introductions immediately an example of how you can create a 3 by 4 matrix, using the capabilities of numphp.
$matrix = new np_array([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]);
As a result, we get the np_array object. Moreover, each row of the matrix is also an object of this type. This allows universal samples to be made.
For example, the standard logic can be used to access the second line.
')
$row = $matrix[1];
But it would be uninteresting =)
Due to the fact that any slice of the matrix is np_array, we can at one stroke know, for example, the average value for the entire third row.
$avg = $matrix[2]->mean();
Moreover, using comparison operators, one can select all matrix elements that satisfy the condition, for example, more than 5
$result = $matrix[$matrix->gt(5)]
And get their amount
$result = $matrix[$matrix['> 5']]->sum()
As I mentioned above, we can use the full power of the slices implemented in numphp, applicable to matrices too. For example, take the second and third line
$result = $matrix['1:3'];
A more detailed description of the features can be found in the
library documentation.
Mathematical operations
At the moment, the same operations can be applied to matrices as to vector arrays. The operation will be applied to each element of the original matrix.
$result = $matrix->mul(5);
You can also sum the matrix and vector. In this case, the vector will be added to each row of the matrix.
$result = $matrix->add([1, 2, 3, 4]); [[ 1, 3, 5, 7], [ 5, 7, 9, 11], [ 9, 11, 13, 15]]
Or even sum up two matrices. Here you will also see one of the ways how to generate a matrix of the desired size.
$diffMartrix = Generator::ones([3, 4]);
Thus it is possible to significantly simplify and speed up the work with matrices and vectors.
Resizing
Each np_array object can easily change its shape. By shape, I mean the dimension of a particular object. For example
$list = new np_array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); $list->shape
Simply, we can assume that the first number means the number of rows, the second - the number of columns. But, it is important to understand that the library allows you to work with n-dimensional arrays.
If we want to take our matrix from the example and make it, say, an array of points on a coordinate board, we can turn it into a 6x2 matrix
$newMatrix = $matrix->reshape([6, 2]);
If you want to present an n-dimensional array as 1-dimensional, you can use the flatten method
$result = $matrix->flatten();
This approach is convenient for quickly generating some kind of test data set of the desired form.
$result = Generator::arange(1, 15)->reshape([2, 7]);
The last thing I would like to describe here, but not the last thing the library can do is work with diagonal slices of matrices.
$matrix = Generator::arange(16)->reshape([4, 4]);
You can generate a matrix with a given diagonal in almost the same way.
$matrix = Generator::diagonal([5, 3, 1]);
In fact, the library includes many more pleasant and useful things for working with numeric arrays and matrices of any dimension. By combining them, you can very quickly and simply do calculations based on data. I would like to realize much more in the plans, so work on the library continues.
Additional features and documentation can be found
here .
Also, if you want to contribute to the development - I will be glad to discuss any issues.