0 Members and 1 Guest are viewing this topic.

matrix1 = matrix.new({{2, 3, 4}, {3, 4, 5}})matrix2 = matrix.new({{1, 0, 5}, {2, 5, 9}})matrix1:add(matrix2) == matrix.add(matrix1, matrix2) --truematrix1[1] = {2} --errormatrix1[1][1] = 2 --no errormatrix1[1][1] = "2" --converts to numbermatrix1[1][1] = true --errortable.maxn(matrix1) == 0 --true matrix1 doesn't actually contain any values, it forwards them to a local table in the matrix.new functionmatrix1[1][300] = 2 --error wrong dimensions

matrix1[1][1] = "2" --converts to numbermatrix1[1][1] = true --error

matrix1 = {{1, 2, 3}, {4, 5, 6}, ["add"]=function(self, m2) end}

setmetatable(matrix1, {__index=matrix})

matrix = {}matrix.new = function(data)local mat={}setmetatable(mat, {__index = function(tbl, nobbc)return data[nobbc]end})return matendmatrix1 = matrix.new({{4}})matrix2 = matrix.new({{7}})print(matrix1[1][1], matrix2[1][1], mat, data) --prints 4 7 nil nil

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 derivedend

Matrix = class()function Matrix:init(mat) self.matrix = matendfunction Matrix:set(x, y, n) self.matrix[y][x] = nendmatrix1 = Matrix{{1,2,3},{1,2,3},{1,2,3}}matrix2 = Matrix{{1,2,3},{1,2,3}}matrix1:set(1,1,3)

function createProxy(tbl) local mt = {} local proxy = {} mt.__index = function (_, key) return tbl[ key] end mt.__newindex = function () print("You are not allowed to write to this table!") end setmetatable(proxy, mt) return proxyenda = createProxy{1,2,3,4,5}print(a[2])a[2] = 3

A[1][2] = 6

B = A[1]B[2] = 6--or(A[1])[2] = 6

function createProxy(tbl) local mt = {} local proxy = {} mt.__index = function (t, key) local rownumber = rawget(t, "rownumber") if rownumber then return tbl[ rownumber][ key] else return tbl[ key] end end mt.__newindex = function () print("You are not allowed to write to this table!") end for key, row in ipairs(tbl) do proxy[ key] = {} proxy[ key].rownumber = key setmetatable(proxy[ key], mt) end setmetatable(proxy, mt) return proxyenda = createProxy{{5,4,3,2,1},{3,6,9,1}}print(a[ 2][ 3])a[ 2][ 3] = 3print(a[ 2][ 3])

a = matrix.new({[10000] = { [100000] = 8}})a[2][3] = 6print(a[2][3]+a[250][44]+a[10000][100000]) --prints 14