Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extension for large (>3D) vectors and segment and x/y/z/w convenience #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion lua/matrix.lua
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,11 @@ function matrix:new( rows, columns, value )
if type( rows ) == "table" then
-- check for vector
if type(rows[1]) ~= "table" then -- expect a vector
return setmetatable( {{rows[1]},{rows[2]},{rows[3]}},matrix_meta )
local o = {}
for i,v in ipairs(rows) do
o[i] = {v}
end
return setmetatable( o,matrix_meta )
end
return setmetatable( rows,matrix_meta )
end
Expand Down Expand Up @@ -189,6 +193,43 @@ setmetatable( matrix, { __call = function( ... ) return matrix.new( ... ) end }
-- but one should avoid using symbolic matrices with complex ones
-- since it is not clear which metatable then is used


local segment = {}

function segment:__index(base_row)
local o = matrix(self.size,1)
for i=1,self.size do
o[i][1] = self.matrix[base_row+i-1][1]
end
return o
end

function segment:__newindex(base_row, o)
assert(self.size == #o)
for i=1,self.size do
self.matrix[base_row+i-1] = o[i]
end
end

function matrix:segment(size)
local o = {matrix=self, size=size}
return setmetatable(o, segment)
end

function matrix:x()
return self[1][1]
end
function matrix:y()
return self[2][1]
end
function matrix:z()
return self[3][1]
end
function matrix:w()
return self[4][1]
end


--// matrix.add ( m1, m2 )
-- Add two matrices; m2 may be of bigger size than m1
function matrix.add( m1, m2 )
Expand Down