matrix1[1][1] = "2" --converts to number
matrix1[1][1] = true --error
will be possible only with direct calls to functions. The way you want it to be is not possible in Lua, same in many languages.... but I can be wrong.matrix1 = {{1, 2, 3}, {4, 5, 6}, ["add"]=function(self, m2) end}what about
setmetatable(matrix1, {__index=matrix})
?matrix = {}
matrix.new = function(data)
local mat={}
setmetatable(mat, {__index = function(tbl, nobbc)
return data[nobbc]
end
})
return mat
end
matrix1 = matrix.new({{4}})
matrix2 = matrix.new({{7}})
print(matrix1[1][1], matrix2[1][1], mat, data) --prints 4 7 nil nil
nobbc was supposed to be key but I was having issuesclass = 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 derived
end
Matrix = class()
function Matrix:init(mat)
self.matrix = mat
end
function Matrix:set(x, y, n)
self.matrix[y][x] = n
end
matrix1 = 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 proxy
end
a = createProxy{1,2,3,4,5}
print(a[2])
a[2] = 3
A[1][2] = 6
the A[1] is found by __index not __newindex so it is likeB = A[1]
B[2] = 6
--or
(A[1])[2] = 6
the reference to A[1] is assigned to B and then B[2] is assigned 6. This means __index and __newindex have to work together to allow assignment but control what can be assigned where while protecting the reference to the actual data.
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 proxy
end
a = createProxy{{5,4,3,2,1},{3,6,9,1}}
print(a[ 2][ 3])
a[ 2][ 3] = 3
print(a[ 2][ 3])
a = matrix.new({[10000] = { [100000] = 8}})
a[2][3] = 6
print(a[2][3]+a[250][44]+a[10000][100000]) --prints 14
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 derived
end
Matrix = 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
end
function Matrix:someFunction()
self:set(whateveryouwant)
end
matrix1 = 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? O.ONope.
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}
}
or however you want to store the data. It's just a combination of tables, and you can access it using map[ x][ y].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] = 4
print(o[2], m[1][2])
m[1][2] = 5
print(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)
end
print()
m = m + 1
print(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 2
print(m)
m = matrix.insert.row(m, 2, {44, 6, 7}) --truncated
print(m)
m = matrix.insert.col(m, 2, {44, 6, 7, 8}) --truncated
print(m)
print(matrix.remove.row(m, 2))
print(m)
m = matrix.remove.row(select(1, matrix.remove.col(m, 1)))
print(m)