Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local a = {
- len = function( self )
- return self.length
- end,
- wid = function( self )
- return self.width
- end,
- insert = function( self, x, y, value )
- if not self[ x ] then
- error( "x value out of bounds", 2 )
- end
- if not self[ x ][ y ] then
- error( "y value out of bounds", 2 )
- end
- self[ x ][ y ] = value
- end,
- getValue = function( self, x, y )
- return self[ x ] and self[ x ][ y ] or nil
- end,
- draw = function( self, a, b )
- term.setCursorPos( a, b )
- local maxNum = 0
- local maxDec = 0
- for x = 1, self:len() do
- for y = 1, self:wid() do
- local v = tostring( self:getValue( x, y ) )
- if #v > maxNum then
- maxNum = #v
- end
- end
- end
- maxNum = maxNum + 1
- for x = 1, self:len() do
- for y = 1, self:wid() do
- local value = self:getValue( x, y )
- term.write( value .. string.rep( " ", maxNum ) )
- end
- local _, b = term.getCursorPos()
- term.setCursorPos( a, b + 1 )
- end
- end,
- transpose = function( self )
- local array = create( self:wid(), self:len() )
- for pos = 1, self:len() do
- for column = 1, array:len() do
- array:insert( column, pos, self:getValue( pos, column ) )
- end
- end
- return array
- end,
- trace = function( self )
- local v = 0
- for x = 1, self:len() do
- for y = 1, self:wid() do
- v = v + self:getValue( x, y )
- end
- end
- return v
- end,
- }
- local mt = {
- __metatable = { __type = "matrix" },
- __index = a,
- __add = function( lhs, rhs )
- if type( rhs ) ~= "table" then
- error( "attempt to add matrix and " .. type( rns ), 2 )
- elseif getmetatable( rhs ).__type ~= "matrix" then
- error( "attempt to add matrix and table", 2 )
- elseif lhs:len() ~= rhs:len() or lhs:wid() ~= rhs:wid() then
- error( "matrices' dimensions are not equal", 2 )
- end
- local array = create( lhs:len(), lhs:wid() )
- for x, tbl in ipairs( lhs ) do
- for y, value in ipairs( rhs ) do
- array:insert( x, y, rhs:getValue( x, y ) + lhs:getValue( x, y ) )
- end
- end
- return array
- end,
- __sub = function( lhs, rhs )
- if type( rhs ) ~= "table" and type( rhs ) ~= "number" then
- error( "attempt to subtract matrix and " .. type( rhs ), 2 )
- elseif getmetatable( rhs ).__type ~= "matrix" then
- error( "attempt to subtract matrix and table", 2 )
- elseif lhs:len() ~= rhs:len() or lhs:wid() ~= rhs:wid() then
- error( "matrices' dimensions are not equal", 2 )
- end
- local array = create( lhs:len(), lhs:wid() )
- for x, tbl in ipairs( lhs ) do
- for y, value in ipairs( rhs ) do
- array:insert( x, y, rhs:getValue( x, y ) - lhs:getValue( x, y ) )
- end
- end
- return array
- end,
- __mul = function( lhs, rhs )
- if type( rhs ) ~= "table" and type( rhs ) ~= "number" then
- error( "attempt to multiply matrix and " .. type( rns ), 2 )
- elseif type( rhs ) == "table" and lhs:len() ~= rhs:wid() then
- error( 'attempt to multiply ' .. lhs:len() .. "x" .. lys:wid() .. " matrix with " .. rhs:len() .. "x" .. rhs:wid() .. " matrix", 2 )
- elseif type( rhs ) == "number" then
- local array = create( lhs:len(), lhs:wid() )
- for x = 1, lhs:len() do
- for y = 1, lhs:wid() do
- array:insert( x, y, lhs:getValue( x, y ) * rhs )
- end
- end
- return array
- elseif getmetatable( rhs ).__type ~= "matrix" then
- error( "attempt to multiply matrix and table", 2 )
- end
- local array = create( lhs:len(), rhs:wid() )
- for pos = 1, lhs:len() do
- for column = 1, rhs:len() do
- local value = 0
- for x = 1, lhs:wid() do
- value = value + lhs:getValue( pos, x ) * rhs:getValue( column, x )
- end
- array:insert( pos, column, value )
- end
- end
- return array
- end,
- __eq = function( o1, o2 )
- if o1:len() ~= o2:len() or o1:wid() ~= o2:wid() then
- return false
- end
- for x = 1, o1:len() do
- for y = 1, o1:wid() do
- if o1:getValue( x, y ) ~= o2:getValue( x, y ) then
- return false
- end
- end
- end
- return true
- end,
- __lt = function( o1, o2 )
- return o1:trace() < o2:trace()
- end,
- __le = function( o1, o2 )
- return o1:trace() > o2:trace()
- end,
- __pow = function( lhs, rhs )
- if type( rhs ) ~= "number" then
- error( "attempt to perform arithmatic __pow on matrix and " .. type( rhs ), 2 )
- end
- local array = create( lhs:len(), lhs:wid() )
- for x = 1, lhs:len() do
- for y = 1, lhs:wid() do
- array:insert( x, y, lhs:getValue( x, y ) ^ rhs )
- end
- end
- return array
- end,
- __div = function( lhs, rhs )
- if type( rhs ) ~= "number" and type( rhs ) ~= "table" then
- error( "Attempt to divide matrix by " .. type( rhs ), 2 )
- elseif type( rhs ) == "table" and getmetatable( rhs ).__type == "matrix" then
- error( "Attempt to divide matrix by matrix", 2 )
- end
- local array = create( lhs:len(), lhs:wid() )
- for x = 1, lhs:len() do
- for y = 1, lhs:wid() do
- array:insert( x, y, lhs:getValue( x, y ) / rhs )
- end
- end
- return array
- end,
- __call = function()
- error( "attempt to call matrix", 2 )
- end,
- }
- function create( length, width )
- if length and width then
- local array = { length = length, width = width }
- for x = 1, length do
- array[ x ] = {}
- for y = 1, width do
- array[ x ][ y ] = 0
- end
- end
- setmetatable( array, mt )
- return array
- end
- length.length = #length
- length.width = #length[ 1 ]
- setmetatable( length, mt )
- return length
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement