Add Matrix type (2D array) in numojo.core.mat module. It is like numpy.matrix type. It provides:
Matrix type (2D array) with initialization and basic manipulation.
Functioon to construct matrix from other data objects, e.g., List, NDArray, String, and numpy array.
Matrix slicing.
Arithmetic functions for item-wise calculation and broadcasting.
Matrix mutiplication, inverse of matrix, solve of linear system, Ordinary Least Square...
Auxilliary types, e.g., MatrixIter.
Because the number of dimensions is known at the compile time, the Matrix type gains advantages in the running speed compared to the NDArray type when the users only want to deal with the matrices manipulation, and it can also be more consistent with numpy.matrix, for example:
For getitem, inputting two Int returns a scalar.
We do not need auxillary types NDArrayShape and NDArrayStrides as the shape and strides information is fixed in length Tuple[Int,Int].
TODO: In future, we can also make use of the trait ArrayLike to align the behavior of NDArray type and the Matrix type.
import numojo as nm
from numojo import mat
from numojo.prelude import *
fn main() raises:
var A = mat.rand(shape=(4, 4))
var B = mat.rand(shape=(4, 4))
var C = mat.rand(shape=(4, 1))
print(A + B)
print(A - B)
print(A * B)
print(A @ B)
print(C.T())
print(mat.solve(A, B))
print(mat.inv(A))
print(mat.lstsq(A, C))
print(A[1,1])
print(A[2])
for i in A:
print(i)
Add
Matrix
type (2D array) innumojo.core.mat
module. It is likenumpy.matrix
type. It provides:Matrix
type (2D array) with initialization and basic manipulation.MatrixIter
.Because the number of dimensions is known at the compile time, the Matrix type gains advantages in the running speed compared to the NDArray type when the users only want to deal with the matrices manipulation, and it can also be more consistent with
numpy.matrix
, for example:TODO: In future, we can also make use of the trait ArrayLike to align the behavior of NDArray type and the Matrix type.
Example slicing:
Examples broadcasting:
Examples calculation: