0 Members and 1 Guest are viewing this topic.
class = function(prototype)local derived={} if prototype then derived.__proto = prototype function derived.__index(t,key) return rawget(derived,key) or prototype[nobbc] end else function derived.__index(t,key) return rawget(derived,key) end end function derived.__call(proto,...) local instance={} setmetatable(instance,proto) instance.__obj = true local init=instance.init if init then init(instance,...) end return instance end setmetatable(derived,derived) return derivedendMatrix = class()function Matrix:init(mat) self.get = function(self, y, x) assert(type(y) == "number" and type(x) == "number", "Invalid input to Matrix:get!") return mat[y][x] end self.set = function(self, y, x, n) local nn = tonumber(n) assert(type(y) == "number" and type(x) == "number" and nn, "Invalid input to Matrix:set!") mat[y][x] = nn end endfunction Matrix:someFunction() self:set(whateveryouwant)endmatrix1 = Matrix{{1,2,3},{4,5,6}}matrix1:set(2,1, "9")print(matrix1:get(2,1))
Nspire Lua didn't have native matrix support?
map = { {1,1,1,1,1,1,1,1}, {1,1,0,0,0,0,0,1} {1,0,0,1,1,1,1,1} {1,0,1,1,1,1,1,1} {1,0,0,0,0,0,0,1} {1,1,0,0,0,1,0,1} {1,0,0,1,0,0,0,1} {1,1,1,1,1,1,1,1}}
m = matrix.new({{2, 5, 7}, {5, 7}, [4] = {5, 6, 7, 3}})print(m)n = matrix.new({{2, false, "7"}, {true, 7}, {5, 6, 0, 3}, {}})print(n)o = m[1]print(o[1], o[2], o[3], o[4], "\n")print(o[2], m[1][2])o[2] = 4print(o[2], m[1][2])m[1][2] = 5print(o[2], m[1][2])m[3] = {4, [3] = 5}print(m)print(tostring(-m))p = m:copy()print("\n", "\n", m, "\n", p, "\n", "\n")print(m:isequal(p))print(m + p == p:ebemult(3) - m)print(p:ebemult(m:ebediv(5)))print(m:totable())print(unpack(m:totable()[1]))print()for row, col, val in p:mpairs() do print(row, col, val)endprint()m = m + 1print(p*m)print(m*p)print(m:trans()*p)print(m.dim[1], m.dim[2], m.dim.rows, m.dim.cols)print(matrix.gen.zero(4)+m == m)print(matrix.gen.random(5))print(matrix.gen.random(5, 2))print(matrix.gen.random(5, 2, 3))print(matrix.gen.random(5, 2, 3, 10))m = m:submatrix(2, 3, 2, 3) --now a 2 x 2print(m)m = matrix.insert.row(m, 2, {44, 6, 7}) --truncatedprint(m)m = matrix.insert.col(m, 2, {44, 6, 7, 8}) --truncatedprint(m)print(matrix.remove.row(m, 2))print(m)m = matrix.remove.row(select(1, matrix.remove.col(m, 1)))print(m)