77--
88-- @module matrix
99
10+ --- Constructors
11+ --
12+ -- @section Constructors
13+
14+ --- Constructs a new matrix of rows by columns, filling it using the provided function or scalar.
15+ --
16+ -- @tparam number rows The number of rows in the matrix
17+ -- @tparam number columns The number of columns in the matrix
18+ -- @tparam function|number|nil func A function taking (row, column) to generate values, or a scalar to fill all elements
19+ -- @treturn Matrix A new matrix
20+ -- @usage m = matrix.new(3, 3, function(r, c) return r + c end)
21+ -- @usage m = matrix.new(2, 4, 5) -- fills all elements with 5
22+ -- @usage m = matrix.new(2, 2) -- fills all elements with 1
23+ -- @export
24+ function new (rows , columns , func )
25+ expect (1 , rows , " number" , " nil" )
26+ expect (2 , columns , " number" , " nil" )
27+ expect (3 , func , " function" , " number" , " nil" )
28+
29+ local m = {}
30+ m .rows = rows or 1
31+ m .columns = columns or 1
32+ for r = 1 , rows do
33+ m [r ] = {}
34+ for c = 1 , columns do
35+ if type (func ) == " function" then
36+ m [r ][c ] = func (r , c )
37+ elseif type (func ) == " number" then
38+ m [r ][c ] = func
39+ else
40+ m [r ][c ] = 1
41+ end
42+ end
43+ end
44+ return setmetatable (m , metatable )
45+ end
46+
47+ --- Constructs a matrix from a 2D array (table of tables).
48+ --
49+ -- @tparam table arr A 2D array representing the matrix data
50+ -- @treturn Matrix A new matrix
51+ -- @usage m = matrix.from2DArray({{1, 2}, {3, 4}})
52+ -- @export
53+ function from2DArray (arr )
54+ expect (1 , arr , " table" )
55+ if getmetatable (arr ) ~= nil then
56+ error (" Invalid Argument! Takes a 2D array!" )
57+ end
58+
59+ return new (# arr , # arr [1 ], function (r , c ) return arr [r ][c ] or 0 end )
60+ end
61+
62+ --- Constructs a matrix from a vector, as either a row or column matrix.
63+ --
64+ -- @tparam table v The vector to convert
65+ -- @tparam boolean row Whether to create a row matrix (true) or column matrix (false). Defaults to true.
66+ -- @treturn Matrix A new matrix representing the vector
67+ -- @usage m = matrix.fromVector(vector.new(1, 2, 3), true) -- row matrix
68+ -- @usage m = matrix.fromVector(vector.new(1, 2, 3), false) -- column matrix
69+ -- @export
70+ function fromVector (v , row )
71+ expect (1 , v , " Vector" )
72+ expect (2 , row , " boolean" , " nil" )
73+
74+ row = row or true
75+ local m = {}
76+ if row then
77+ m [1 ] = {v .x , v .y , v .z }
78+ else
79+ m [1 ] = {v .x }
80+ m [2 ] = {v .y }
81+ m [3 ] = {v .z }
82+ end
83+ return from2DArray (m )
84+ end
85+
86+ --- Constructs a rotation matrix from a quaternion.
87+ --
88+ -- @tparam table q The quaternion to convert
89+ -- @treturn Matrix A new 3x3 rotation matrix
90+ -- @usage m = matrix.fromQuaternion(quaternion.new(1, vector.new(0, 0, 0)))
91+ -- @export
92+ -- @see quaternion
93+ function fromQuaternion (q )
94+ if not quaternion then
95+ error (" Quaternion API is not loaded!" )
96+ end
97+ expect (1 , q , " Quaternion" )
98+
99+ q = q :normalize ()
100+ local w = q .a
101+ local x = q .v .x
102+ local y = q .v .y
103+ local z = q .v .z
104+
105+ local m = {
106+ {1 - 2 * (y * y + z * z ), 2 * (x * y - w * z ), 2 * (x * z + w * y )},
107+ {2 * (x * y + w * z ), 1 - 2 * (x * x + z * z ), 2 * (y * z - w * x )},
108+ {2 * (x * z - w * y ), 2 * (y * z + w * x ), 1 - 2 * (x * x + y * y )}
109+ }
110+ return from2DArray (m )
111+ end
112+
113+ --- Constructs an identity matrix of given dimensions.
114+ --
115+ -- @tparam number rows The number of rows
116+ -- @tparam number columns The number of columns
117+ -- @treturn Matrix A new identity matrix
118+ -- @usage m = matrix.identity(3, 3)
119+ -- @export
120+ function identity (rows , columns )
121+ return new (rows , columns )
122+ end
123+
10124--- A matrix, with dimensions `rows` x `columns`.
11125--
12126-- This is suitable for representing linear transformations, systems of equations,
@@ -601,118 +715,4 @@ local metatable = {
601715 __len = matrix .length ,
602716 __tostring = matrix .tostring ,
603717 __eq = matrix .equals
604- }
605-
606- --- Constructors
607- --
608- -- @section Constructors
609-
610- --- Constructs a new matrix of rows by columns, filling it using the provided function or scalar.
611- --
612- -- @tparam number rows The number of rows in the matrix
613- -- @tparam number columns The number of columns in the matrix
614- -- @tparam function|number|nil func A function taking (row, column) to generate values, or a scalar to fill all elements
615- -- @treturn Matrix A new matrix
616- -- @usage m = matrix.new(3, 3, function(r, c) return r + c end)
617- -- @usage m = matrix.new(2, 4, 5) -- fills all elements with 5
618- -- @usage m = matrix.new(2, 2) -- fills all elements with 1
619- -- @export
620- function new (rows , columns , func )
621- expect (1 , rows , " number" , " nil" )
622- expect (2 , columns , " number" , " nil" )
623- expect (3 , func , " function" , " number" , " nil" )
624-
625- local m = {}
626- m .rows = rows or 1
627- m .columns = columns or 1
628- for r = 1 , rows do
629- m [r ] = {}
630- for c = 1 , columns do
631- if type (func ) == " function" then
632- m [r ][c ] = func (r , c )
633- elseif type (func ) == " number" then
634- m [r ][c ] = func
635- else
636- m [r ][c ] = 1
637- end
638- end
639- end
640- return setmetatable (m , metatable )
641- end
642-
643- --- Constructs a matrix from a 2D array (table of tables).
644- --
645- -- @tparam table arr A 2D array representing the matrix data
646- -- @treturn Matrix A new matrix
647- -- @usage m = matrix.from2DArray({{1, 2}, {3, 4}})
648- -- @export
649- function from2DArray (arr )
650- expect (1 , arr , " table" )
651- if getmetatable (arr ) ~= nil then
652- error (" Invalid Argument! Takes a 2D array!" )
653- end
654-
655- return new (# arr , # arr [1 ], function (r , c ) return arr [r ][c ] or 0 end )
656- end
657-
658- --- Constructs a matrix from a vector, as either a row or column matrix.
659- --
660- -- @tparam table v The vector to convert
661- -- @tparam boolean row Whether to create a row matrix (true) or column matrix (false). Defaults to true.
662- -- @treturn Matrix A new matrix representing the vector
663- -- @usage m = matrix.fromVector(vector.new(1, 2, 3), true) -- row matrix
664- -- @usage m = matrix.fromVector(vector.new(1, 2, 3), false) -- column matrix
665- -- @export
666- function fromVector (v , row )
667- expect (1 , v , " Vector" )
668- expect (2 , row , " boolean" , " nil" )
669-
670- row = row or true
671- local m = {}
672- if row then
673- m [1 ] = {v .x , v .y , v .z }
674- else
675- m [1 ] = {v .x }
676- m [2 ] = {v .y }
677- m [3 ] = {v .z }
678- end
679- return from2DArray (m )
680- end
681-
682- --- Constructs a rotation matrix from a quaternion.
683- --
684- -- @tparam table q The quaternion to convert
685- -- @treturn Matrix A new 3x3 rotation matrix
686- -- @usage m = matrix.fromQuaternion(quaternion.new(1, vector.new(0, 0, 0)))
687- -- @export
688- -- @see quaternion
689- function fromQuaternion (q )
690- if not quaternion then
691- error (" Quaternion API is not loaded!" )
692- end
693- expect (1 , q , " Quaternion" )
694-
695- q = q :normalize ()
696- local w = q .a
697- local x = q .v .x
698- local y = q .v .y
699- local z = q .v .z
700-
701- local m = {
702- {1 - 2 * (y * y + z * z ), 2 * (x * y - w * z ), 2 * (x * z + w * y )},
703- {2 * (x * y + w * z ), 1 - 2 * (x * x + z * z ), 2 * (y * z - w * x )},
704- {2 * (x * z - w * y ), 2 * (y * z + w * x ), 1 - 2 * (x * x + y * y )}
705- }
706- return from2DArray (m )
707- end
708-
709- --- Constructs an identity matrix of given dimensions.
710- --
711- -- @tparam number rows The number of rows
712- -- @tparam number columns The number of columns
713- -- @treturn Matrix A new identity matrix
714- -- @usage m = matrix.identity(3, 3)
715- -- @export
716- function identity (rows , columns )
717- return new (rows , columns )
718- end
718+ }
0 commit comments