| Recommend this page to a friend! |
| Packages of Caleb | PHP Common Class Library | _docs/Matrix.md | Download |
|
|||||||||||||||||||||
Documentation for the "Matrix" class.Facilitates the generation of multidimensional arrays to an arbitrarily specified depth and number of elements, and facilitates iteration through those multidimensional arrays in any direction (whether up and down a particular array, across different depths, etc) via arbitrary callables and closures. How to use:
Matrix property.The Matrix property will be populated with a multidimensional array (the "matrix") when createMatrix is called, and could be fairly described as the instance's main concern. Various methods are provided for accessing and manipulating this multidimensional array, and in case one would prefer to access or manipulate this data directly at the implementation, the property is defined as public.
Dimensions property.The Dimensions property is an integer which describes the number of dimensions the matrix has. It is populated when createMatrix is called, and is defined as public.
Magnitude property.The Magnitude property is an integer, or an array of integers, which describes the magnitude of each dimension of the matrix. It is populated when createMatrix is called, and is defined as public. When populated with an integer, that integer determines the magnitude of all dimensions. When populated with an array of integers, each element of the array corresponds to each dimension of the matrix respectively (i.e., the first element determines the magnitude of the first dimension, the second element of the second dimension and so on).
Data property.The Data property can be any type of data supported by PHP, is populated when createMatrix is called, and is defined as public. It will be used to populate each coordinate of the matrix (i.e., when populating the Matrix property, the contents of the Data property will be used to populate each element of the deepest vector of the matrix).
createMatrix method.The createMatrix method is used to create a new matrix for the instance. It accepts three parameters. The first parameter,
populateVector method.The populateVector method is invoked by the createMatrix method, and recursively by itself, and is used to populate each vector when creating a new matrix for the instance. It accepts two parameters. The first parameter,
iterateCallback method.The iterateCallback method allows applying a callback function over some specified coordinates. It accepts a minimum of one parameter. The first parameter,
iterateCallbackGenerator method.The iterateCallbackGenerator method is a generator invoked by the iterateCallback method, and serves as a simple wrapper for the recursively invoked iterateCallbackGeneratorInner method. It accepts three parameters. The first parameter,
iterateCallbackGeneratorInner method.The iterateCallbackGeneratorInner method is a generator invoked by the iterateCallbackGenerator method, and recursively by itself, and is the main mechanism by which the iterateCallback method is able to apply a callback function to some specified coordinates, and the main mechanism by which return values can be firstly yielded from the callback function, and then subsequently returned by the iterateCallback method to the implementation. It accepts six parameters. The first parameter,
Callback functions.An iterateCallback callback function can optionally support up to eight parameters. The callback function's return value is entirely up to you. It could be the value of the current coordinate, if the intention of your callback function is to fetch the value of the current coordinate, or the return value may be omitted entirely, if your intention is to just perform some changes to its value without necessarily returning anything. These eight parameters are as follows:
By this point in the documentation, the first six of those parameters are relatively self-explanatory. In short: Whenever iterating over any particular coordinate, as well as for the coordinate in question, it is also possible to access and manipulate the values of the immediately preceding and immediately succeeding coordinates. It should be noted that in order to actually change any coordinate values, the parameters corresponding to those values must be passed by reference (as per those denoted above). Passing by value instead of by reference will result in the failure of the callback function to affect any changes to the matrix. The seventh parameter ( The eighth parameter ( Examples.An example for generating a simple two-dimensional matrix, whereby each vector has a magnitude of three, and applying a simple callback function to it:
Produces:
Because that matrix has exactly two dimensions, its values could be described in a table like so: 1 | 2 | 3 ---|---|--- 4 | 5 | 6 7 | 8 | 9 The "dimensions", in this example, could be described as the columns and rows of the table, or as "up" and "down", with each individual cell in the table described as a "coordinate". Each dimension ("up" or "down"), in combination with the specified "magnitude" (in this case, three) forms a "vector", which could also be regarded as the values of a particular set of coordinates (e.g.; 1, 2, 3; 4, 5, 6; 7, 8, 9; 1, 4, 7; 2, 5, 8; 3, 6, 9; etc). However, the Matrix class isn't aware of any particular notion of "rows" and "columns", of "up" and "down", of "left" and "right", of "forward" and "backward" or of anything else like that; such terms are useful as a means of describing a particular vector to a human, but for the Matrix class, which can tolerate any arbitrary number of dimensions and any arbitrary magnitude (available RAM, processor cycles, and time permitting), such words are generally meaningless, and it may be easier to simply describe vectors in terms of belonging to the first dimension, the second dimension, the third dimension and so on. You may also notice that, despite having a magnitude of three, for the coordinates to which the callback function should be applied, the example specifies Last Updated: 2 July 2025 (2025.07.02). |