Omnimaga: The Coders Of Tomorrow
Welcome, Guest. Please login or register.
 
Omnimaga: The Coders Of Tomorrow
18 May, 2013, 14:20:49 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   home   news downloads projects tutorials misc forums rules new posts irc about Login Register  
+-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
  Print  
Author Topic: Matrix Library -  (Read 863 times) Bookmark and Share
0 Members and 1 Guest are viewing this topic.
Jim Bauwens
Lua! Nspire! Linux!
Editor
LV10 31337 u53r (Next: 2000)
*
Offline Offline

Gender: Male
Last Login: Today at 12:49:12
Date Registered: 28 February, 2011, 22:32:12
Location: Belgium
Posts: 1733


Total Post Ratings: +180

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

Take a look at this:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

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))

What do you think ?
« Last Edit: 23 June, 2012, 22:45:25 by jimbauwens » Logged

3rik
LV3 Member (Next: 100)
***
Offline Offline

Gender: Male
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

View Profile
« 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 Offline

Gender: Male
Last Login: Today at 10:41:03
Date Registered: 25 August, 2008, 07:00:21
Location: Québec (Canada)
Posts: 50196


Total Post Ratings: +2611

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

Nspire Lua didn't have native matrix support? shocked
Logged

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

Follow me on Bandcamp|Facebook|Reverbnation|Youtube|Twitter|Myspace
Levak
LV8 Addict (Next: 1000)
********
Offline Offline

Gender: Male
Last Login: Today at 03:42:02
Date Registered: 04 April, 2010, 23:42:49
Location: France
Posts: 832


Total Post Ratings: +148

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

Nspire Lua didn't have native matrix support? shocked
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 Offline

Gender: Male
Last Login: Today at 12:49:12
Date Registered: 28 February, 2011, 22:32:12
Location: Belgium
Posts: 1733


Total Post Ratings: +180

View Profile WWW
« 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:

1
2
3
4
5
6
7
8
9
10
11
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 Offline

Gender: Male
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

View Profile
« 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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)

* Lua Matrix Library.zip (20.59 KB - downloaded 13 times.)
« Last Edit: 06 July, 2012, 02:40:12 by 3rik » Logged

Userbars
3rik
LV3 Member (Next: 100)
***
Offline Offline

Gender: Male
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

View Profile
« 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.

* Lua Matrix Library 0.9.1.zip (20.55 KB - downloaded 12 times.)
Logged

Userbars
adriweb
Editor
LV9 Veteran (Next: 1337)
*
Offline Offline

Gender: Male
Last Login: Today at 13:31:14
Date Registered: 13 April, 2011, 18:42:59
Location: South of France
Posts: 1193


Total Post Ratings: +185

View Profile WWW
« 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 Tongue)
« Last Edit: 16 July, 2012, 12:59:35 by adriweb » Logged


TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation
3rik
LV3 Member (Next: 100)
***
Offline Offline

Gender: Male
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

View Profile
« 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.

* Lua Matrix Library 0.9.2.zip (21.82 KB - downloaded 8 times.)
Logged

Userbars
Pages: 1 [2]   Go Up
  Print  
 
Jump to:  

Powered by EzPortal
Powered by MySQL Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Powered by PHP
Page created in 0.602 seconds with 31 queries.
Skin by DJ Omnimaga edited from SMF default theme with the help of tr1p1ea.
All programs, games and songs avaliable on this website are property of their respective owners.
Best viewed in Opera, Firefox, Chrome and Safari with a resolution of 1024x768 or above.