Member Functions
LibLinAlg.genMat
- .setSize(n, m, toZero, keepMem)
Sets the size of the matrix to n x m. This is the only way to change the shape. If toZero is false, elements will be initialized to nil. If keepMem is false, the original matrix elements will be overwritten.
Returns the shape of the matrix.
Sets every matrix element to the given value. Can be any object, but be aware of references if an object is passed.
Returns a deep copy of the matrix.
Sends a string containing a clean printout of the matrix to stdout.
Returns the transposed matrix, i.e. flips the matrix across it's diagonal such that A(i,j) -> A(j, i).
- .foreach(sendIndices, f, ...)
Applies the function f with parameters ... to every matrix element. If sendIndices is true, the first two arguments passed to f will be i and j, i.e. the matrix indices.
Special operators
Allows usage like e.g. A == B. True if all the elements match, else false.
Allows for elements to accessed like e.g. A(1,2). Does not work on assigment. Equivalent to A.at[1][2].
Allows usage like e.g. C = A .. B, where the result will be a matrix where the columns of B have been added to the columns of A.
Example of usage:
Code:
LLA = Library.LibLinAlg
--Create two matrices of size 2x2 and 2x3
A = LLA.genMat(2,2)
B = LLA.genMat(2,3)
n, m = A.getShape() --Extract 2 and 2 from A
--Fill A with only 2s and B with only 3s
A.fill(2); B.fill(3)
--Dump the result to stdout
A.dump(); B.dump()
> [genMat 2x2]
> 2 2 |
> 2 2 |
> [genMat 2x3]
> 3 3 3 |
> 3 3 3 |
--Generate the 2 x 5 concatenated matrix
C = A .. B ; C.dump()
> [genMat 2x5]
> 2 2 3 3 3 |
> 2 2 3 3 3 |
--Get the transposed matrix
C = C.transpose(); C.dump()
> [genMat 5x2]
> 2 2 |
> 2 2 |
> 3 3 |
> 3 3 |
> 3 3 |
--Resize C to a 3x2 matrix, keeping the available memory
C.setSize(3, 2); C.dump()
> [genMat 3x2]
> 2 2 |
> 2 2 |
> 3 3 |
--Make a deep copy of A and set it to B
B = A.copy()
--Test is the matrices are equal
print(A == B)
> true
--Using foreach to apply a string operator on every element
B.fill("hey!")
B.foreach(true, functon(i, j) return string.upper(B(i, j)) end)
B.dump()
> [genMat 3x2]
> HEY! HEY! |
> HEY! HEY! |
> HEY! HEY! |
Bookmarks