|
libmatrix
|
A matrix library, in C.
It use sparse matrix representation under the hood.
Before you can do anything with a matrix, you need to declare it. This is done with the DECLARE_MATRIX and MATRIX macro.
This declares a matrix type named MyMatrix with a double as the data type and an int64_t as the index type.
Then, you can use all the functions that are defined for MyMatrix:
MatrixType_newMatrixType_freeMatrixType_from_1dMatrixType_from_2dMatrixType_to_1dMatrixType_to_2dMatrixType_identityMatrixType_getMatrixType_setMatrixType_findMatrixType_reshapeMatrixType_renameMatrixType_scaleMatrixType_transposeMatrixType_addMatrixType_multiplyMatrixType_hadamardMatrixType_expMatrixType_submatrixYou can simply create a matrix with MatrixType_new:
This creates a 3x3 matrix.
Tip: Use
MatrixType_identityto create an identity matrix.
You can also create a matrix from a 1D array:
Or from a 2D array:
Tip: Use
MatrixType_to_1dandMatrixType_to_2dto convert a matrix to a 1D or 2D array.
No matter how you create a matrix, the matrix will get a random name of 4 characters.
You can change the name of the matrix with MatrixType_rename:
The name string will be copied, so you can free it after calling MatrixType_rename.
If a matrix is no longer needed, you can free it with MatrixType_free:
This will free the matrix with all its elements and its name.
You can set the value of a matrix at a specific index with MatrixType_set:
The above sets the value at the first row and first column to 1.0.
You can also get the value of a matrix at a specific index with MatrixType_get:
Sometimes, you want to find the index of underlying data of a matrix. You can do this with MatrixType_find:
This will find the index of the sparse matrix element at the first row and second column.
You can resize a matrix with MatrixType_reshape:
By calling MatrixType_reshape, the matrix will be resized to a new size. If the new size is smaller than the old size, the matrix will be truncated. If the new size is larger than the old size, the only change will be the size of the matrix in the underlying data array.
All the below functions will return a new matrix. The original matrix will not be modified.
You can scale a matrix with MatrixType_scale:
This will return a new matrix with all the elements of the original matrix multiplied by 2.0.
Tip: Use
MatrixType_scaleto copy a matrix by passing1.0as the scale factor.
You can transpose a matrix with MatrixType_transpose:
This function uses fast transpose algorithm to transpose the matrix.
You can add two matrices with MatrixType_add:
This will return a new matrix with all the elements of the two matrices added together.
You can multiply two matrices with MatrixType_multiply:
This will return a new matrix with all the elements of the two matrices multiplied together.
You can perform element-wise product of two matrices with MatrixType_hadamard:
This will return a new matrix with all the elements of the two matrices multiplied together.
Notice: Before using addition, multiplication, or element-wise product, you need to make sure that the matrices are compatible and the dimensions are correct.
You can exponentiate a matrix with MatrixType_exp:
Notice: The exponentiation function is only available for square matrices.
You can get a submatrix of a matrix with MatrixType_submatrix:
The second and third arguments are boolean arrays that indicate which rows and columns to include in the submatrix.
The above code will return a new matrix sub with the following elements:
1.8.17