﻿ Matrix Library
25 May, 2013, 21:41:52
 OmnomIRC You must Register, be logged in and have at least 40 posts to use this shout-box! If it still doesn't show up afterward, it might be that OmnomIRC is disabled for your group or under maintenance.Note: You can also use an IRC client like mIRC, X-Chat or Mibbit to connect to an EFnet server and #omnimaga.

 Pages: 1 [2]   Go Down
 Author Topic: Matrix Library -  (Read 873 times) 0 Members and 1 Guest are viewing this topic.
Jim Bauwens
Lua! Nspire! Linux!
Editor
LV10 31337 u53r (Next: 2000)

Offline

Gender:
Date Registered: 28 February, 2011, 22:32:12
Location: Belgium
Posts: 1733

Total Post Ratings: +180

 « Reply #15 on: 23 June, 2012, 22:44:58 » 0

Take a look at this:

 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 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: 23 June, 2012, 22:45:25 by jimbauwens » Logged

3rik
LV3 Member (Next: 100)

Offline

Gender:
Last Login: 02 April, 2013, 22:45:02
Date Registered: 09 August, 2011, 04:04:54
Location: Right over there.
Posts: 92

Topic starter
Total Post Ratings: +8

 « Reply #16 on: 23 June, 2012, 22:50:53 » 0

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.
 Logged

Userbars
DJ Omnimaga
Retired Omnimaga founder (Site issues must be PM'ed to Netham45, Eeems, Shmibs, Deep Thought and AngelFish, not me.)
Editor
LV15 Omnimagician (Next: --)

Offline

Gender:
Date Registered: 25 August, 2008, 07:00:21
Posts: 50237

Total Post Ratings: +2615

 « Reply #17 on: 24 June, 2012, 06:58:20 » 0

Nspire Lua didn't have native matrix support?
 Logged

Retired 83+ coder, Omnimaga/TIMGUL founder. Now doing power metal music (formerly did electronica)

Levak

Offline

Gender:
Date Registered: 04 April, 2010, 23:42:49
Location: France
Posts: 844

Total Post Ratings: +148

 « Reply #18 on: 24 June, 2012, 07:11:01 » 0

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.
 Logged

Human always wants to survive and that's why he will fall one day.
My website - TI-Planet - iNspired-Lua
Jim Bauwens
Lua! Nspire! Linux!
Editor
LV10 31337 u53r (Next: 2000)

Offline

Gender:
Date Registered: 28 February, 2011, 22:32:12
Location: Belgium
Posts: 1733

Total Post Ratings: +180

 « Reply #19 on: 24 June, 2012, 12:23:26 » 0

Depends what you mean with a Matrix.

If you just want a matrix for a map, it's as simple as:
 1234567891011 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: 24 June, 2012, 12:23:55 by jimbauwens » Logged

3rik
LV3 Member (Next: 100)

Offline

Gender:
Last Login: 02 April, 2013, 22:45:02
Date Registered: 09 August, 2011, 04:04:54
Location: Right over there.
Posts: 92

Topic starter
Total Post Ratings: +8

 « Reply #20 on: 06 July, 2012, 02:33:41 » 0

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.
 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 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)
 Lua Matrix Library.zip (20.59 KB - downloaded 14 times.) « Last Edit: 06 July, 2012, 02:40:12 by 3rik » Logged

Userbars
3rik
LV3 Member (Next: 100)

Offline

Gender:
Last Login: 02 April, 2013, 22:45:02
Date Registered: 09 August, 2011, 04:04:54
Location: Right over there.
Posts: 92

Topic starter
Total Post Ratings: +8

 « Reply #21 on: 09 July, 2012, 07:44:08 » 0

I fixed a few bugs, and changed all the asserts to errors so it's easier to find mistakes.

Userbars
Editor
LV9 Veteran (Next: 1337)

Offline

Gender:
Last Login: 23 May, 2013, 12:02:44
Date Registered: 13 April, 2011, 18:42:59
Location: South of France
Posts: 1197

Total Post Ratings: +185

 « Reply #22 on: 16 July, 2012, 12:57:22 » 0

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: 16 July, 2012, 12:59:35 by adriweb » Logged

TI-Nspire Lua programming : Tutorials  |  API Documentation
3rik
LV3 Member (Next: 100)

Offline

Gender:
Last Login: 02 April, 2013, 22:45:02
Date Registered: 09 August, 2011, 04:04:54
Location: Right over there.
Posts: 92

Topic starter
Total Post Ratings: +8

 « Reply #23 on: 16 July, 2012, 21:03:53 » 0

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.