@@ -23,7 +23,7 @@ local matrix = {
2323 -- Supports broadcasting with row vectors and column vectors.
2424 --
2525 -- @tparam Matrix self The first matrix to add
26- -- @tparam Matrix, number other The second matrix, scalar, or vector to add
26+ -- @tparam Matrix| number other The second matrix, scalar, or vector to add
2727 -- @treturn Matrix The resulting matrix
2828 -- @usage m1:add(m2)
2929 -- @usage m1 + m2
@@ -62,7 +62,7 @@ local matrix = {
6262 --- Subtracts two matrices, or subtracts a scalar from all elements.
6363 --
6464 -- @tparam Matrix self The matrix to subtract from
65- -- @tparam Matrix, number other The matrix or scalar to subtract
65+ -- @tparam Matrix| number other The matrix or scalar to subtract
6666 -- @treturn Matrix The resulting matrix
6767 -- @usage m1:sub(m2)
6868 -- @usage m1 - m2
@@ -73,7 +73,7 @@ local matrix = {
7373 --- Multiplies a matrix by a scalar or performs matrix multiplication.
7474 --
7575 -- @tparam Matrix self The matrix to multiply
76- -- @tparam Matrix, number other The scalar or matrix to multiply with
76+ -- @tparam Matrix| number other The scalar or matrix to multiply with
7777 -- @treturn Matrix The resulting matrix
7878 -- Note: For matrix multiplication, the number of columns in self must equal the number of rows in other
7979 -- @usage m:mul(3)
@@ -110,7 +110,7 @@ local matrix = {
110110 --- Divides a matrix by a scalar or another matrix.
111111 --
112112 -- @tparam Matrix self The matrix to divide
113- -- @tparam Matrix, number other The scalar or matrix to divide by
113+ -- @tparam Matrix| number other The scalar or matrix to divide by
114114 -- @treturn Matrix The resulting matrix
115115 -- Note: Division by a matrix is performed by multiplying by its inverse
116116 -- @usage m:div(2)
@@ -595,6 +595,15 @@ local metatable = {
595595 __eq = matrix .equals
596596}
597597
598+ --- Constructs a new matrix of rows by columns, filling it using the provided function or scalar.
599+ --
600+ -- @tparam number rows The number of rows in the matrix
601+ -- @tparam number columns The number of columns in the matrix
602+ -- @tparam function|number|nil func A function taking (row, column) to generate values, or a scalar to fill all elements
603+ -- @treturn Matrix A new matrix
604+ -- @usage m = matrix.new(3, 3, function(r, c) return r + c end)
605+ -- @usage m = matrix.new(2, 4, 5) -- fills all elements with 5
606+ -- @usage m = matrix.new(2, 2) -- fills all elements with 1
598607function new (rows , columns , func )
599608 expect (1 , rows , " number" , " nil" )
600609 expect (2 , columns , " number" , " nil" )
@@ -618,6 +627,11 @@ function new(rows, columns, func)
618627 return setmetatable (m , metatable )
619628end
620629
630+ --- Constructs a matrix from a 2D array (table of tables).
631+ --
632+ -- @tparam table arr A 2D array representing the matrix data
633+ -- @treturn Matrix A new matrix
634+ -- @usage m = matrix.from2DArray({{1, 2}, {3, 4}})
621635function from2DArray (arr )
622636 expect (1 , arr , " table" )
623637 if getmetatable (arr ) ~= nil then
@@ -627,6 +641,13 @@ function from2DArray(arr)
627641 return new (# arr , # arr [1 ], function (r , c ) return arr [r ][c ] or 0 end )
628642end
629643
644+ --- Constructs a matrix from a vector, as either a row or column matrix.
645+ --
646+ -- @tparam table v The vector to convert
647+ -- @tparam boolean row Whether to create a row matrix (true) or column matrix (false). Defaults to true.
648+ -- @treturn Matrix A new matrix representing the vector
649+ -- @usage m = matrix.fromVector(vector.new(1, 2, 3), true) -- row matrix
650+ -- @usage m = matrix.fromVector(vector.new(1, 2, 3), false) -- column matrix
630651function fromVector (v , row )
631652 expect (1 , v , " table" )
632653 if getmetatable (v ).__index ~= getmetatable (vector .new ()).__index then
@@ -649,6 +670,11 @@ function fromVector(v, row)
649670 return from2DArray (m )
650671end
651672
673+ --- Constructs a rotation matrix from a quaternion.
674+ --
675+ -- @tparam table q The quaternion to convert
676+ -- @treturn Matrix A new 3x3 rotation matrix
677+ -- @usage m = matrix.fromQuaternion(quaternion.new(1, vector.new(0, 0, 0)))
652678function fromQuaternion (q )
653679 expect (1 , q , " table" )
654680 if getmetatable (q ).__index ~= getmetatable (quaternion .new ()).__index then
@@ -669,6 +695,12 @@ function fromQuaternion(q)
669695 return from2DArray (m )
670696end
671697
698+ --- Constructs an identity matrix of given dimensions.
699+ --
700+ -- @tparam number rows The number of rows
701+ -- @tparam number columns The number of columns
702+ -- @treturn Matrix A new identity matrix
703+ -- @usage m = matrix.identity(3, 3)
672704function identity (rows , columns )
673705 return new (rows , columns )
674706end
0 commit comments