### Author Topic: Matrix Library  (Read 5139 times)

0 Members and 1 Guest are viewing this topic.

#### Jim Bauwens

• Lua! Nspire! Linux!
• Editor
• LV10 31337 u53r (Next: 2000)
• Posts: 1881
• Rating: +206/-7
• Linux!
##### Re: Matrix Library
« Reply #15 on: June 23, 2012, 04:44:58 pm »
Take a look at this:

Code: [Select]
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))
What do you think ?
« Last Edit: June 23, 2012, 04:45:25 pm by jimbauwens »

#### 3rik

• LV3 Member (Next: 100)
• Posts: 92
• Rating: +8/-0
• My TI-84+ SE
##### Re: Matrix Library
« Reply #16 on: June 23, 2012, 04:50:53 pm »
Yeah this type of thing would work and it would be far easier to manage. I think in the long run it will probably be shorter and faster to do some thing like this, rather than basically making a new type out of an old one.
Thanks.
Userbars

#### DJ Omnimaga

• Former TI programmer
• CoT Emeritus
• LV15 Omnimagician (Next: --)
• Posts: 55833
• Rating: +3151/-232
• CodeWalrus founder & retired Omnimaga founder
##### Re: Matrix Library
« Reply #17 on: June 24, 2012, 12:58:20 am »
Nspire Lua didn't have native matrix support?
In case you are wondering where I went, I left Omni back in 2015 to form CodeWalrus due to various reasons explained back then, but I stopped calc dev in 2016 and am now mostly active on the CW Discord server at https://discord.gg/cuZcfcF

#### Levak

• LV9 Veteran (Next: 1337)
• Posts: 1002
• Rating: +208/-39
##### Re: Matrix Library
« Reply #18 on: June 24, 2012, 01:11:01 am »
Nspire Lua didn't have native matrix support?
Nope.
Everything is table in Lua. So it has to be made either in a library or by the programmer with some offset tricks.
I do not get mad at people, I just want them to learn the way I learnt.
My website - TI-Planet - iNspired-Lua

#### Jim Bauwens

• Lua! Nspire! Linux!
• Editor
• LV10 31337 u53r (Next: 2000)
• Posts: 1881
• Rating: +206/-7
• Linux!
##### Re: Matrix Library
« Reply #19 on: June 24, 2012, 06:23:26 am »
Depends what you mean with a Matrix.

If you just want a matrix for a map, it's as simple as:
Code: [Select]
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].

If you are talking about mathematical matrices, no Lua does not provide support for the math of it. However, Nspire-Lua does. You can access Matrices in the document using var.recall, var.recallAt and math.eval. You can write to the matrix using math.eval, var.store or var.storeAt. Performing (mathematical) operations is done using math.eval.

So: You can easily have normal data and mathematical matrices in Nspire-Lua. But mathematical matrixes in normal (computer) lua is not possible, hence why 3rik is writting this lib.
« Last Edit: June 24, 2012, 06:23:55 am by jimbauwens »

#### 3rik

• LV3 Member (Next: 100)
• Posts: 92
• Rating: +8/-0
• My TI-84+ SE
##### Re: Matrix Library
« Reply #20 on: July 05, 2012, 08:33:41 pm »
I've finished all the basic stuff.

I'll attach it here and update the first post.

Edit:

The documentation is included in the source.

Here's some code to test the library out.
Code: [Select]
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)
« Last Edit: July 05, 2012, 08:40:12 pm by 3rik »
Userbars

#### 3rik

• LV3 Member (Next: 100)
• Posts: 92
• Rating: +8/-0
• My TI-84+ SE
##### Re: Matrix Library
« Reply #21 on: July 09, 2012, 01:44:08 am »
I fixed a few bugs, and changed all the asserts to errors so it's easier to find mistakes.
Userbars

• Editor
• LV10 31337 u53r (Next: 2000)
• Posts: 1708
• Rating: +229/-17
##### Re: Matrix Library
« Reply #22 on: July 16, 2012, 06:57:22 am »
I just found something, maybe it can help if ever needed ?
https://github.com/smeschia/gsl-lua/blob/master/src/gsl.lua

(edit : eh, maybe not... looks like just a binding )
« Last Edit: July 16, 2012, 06:59:35 am by adriweb »
My calculator programs
TI-Nspire Lua programming : Tutorials  |  API Documentation

#### 3rik

• LV3 Member (Next: 100)
• Posts: 92
• Rating: +8/-0
• My TI-84+ SE
##### Re: Matrix Library
« Reply #23 on: July 16, 2012, 03:03:53 pm »
I think I'm okay for now. I'm sort of taking a crash course in linear algebra before I finish up the next version. I do have a lot of new things added, so I'll post what I have so far and update the first post.
Userbars