Jump to content




[1.0] Matrix API


5 replies to this topic

#1 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 10 September 2014 - 12:31 AM

If you do not know what a Matrix is, I would recommend this wikipedia page.

Download:

1.0
pastebin get aE66gE86

API Functions
matrix.create( length/tbl, width )
Creates and returns a matrix object.
If a length and width are specified, it creates a len x wid matrix full of 0s

If the length is a table, it will create an array from that table. Format for the table is as follows:

{ {1, 2, 3}, {4, 5, 6} }

1 2 3
4 5 6

Object Functions
object:insert( a, b, value )
changes the value of the specified matrix value
object:draw( x, y )
draws the matrix on screen at the specified coordinates (Note: this needs improvement)
object:len()
returns the length of the matrix
object:wid()
returns the width of the matrix
object:getValue( a, b )
returns the value within the matrix or nil
object:transpose()
see this page
object:trace()
adds all the items in the matrix together and returns them (note: this is temporary, until I figure out actual tracing

Metamethods
The following are allowed:
matrix * matrix
matrix * num
matrix / num
matrix + matrix
matrix - matrix
matrix == matrix
matrix > matrix
matrix < matrix
matrix ^ num --#this may be inaccurate, and probably shouldn't be possible
the following are prohibited:
matrix / matrix
matrix()
setmetatable( matrix, {} )
the following are edited:
getmetatabe( matrix ) --#contains __type only!

I am open to suggestions about how to improve this, many of the matrix functions on wikipedia go way over my head, I'm only in Algebra II...

#2 ardera

  • Members
  • 503 posts
  • LocationGermany

Posted 26 September 2014 - 02:00 PM

Nice!
What about adding a matrix.newIdentity function that creates a new identity matrix?
also, matrix * vector and so on would be nice

#3 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 26 September 2014 - 03:15 PM

I'll definitely add that once I understand how to do it myself. I'm currently taking Algebra II, so everything I do know about matrices is already in the API. If you can link a tutorial or an explanation I'll try to work on it in my free time.

#4 Djosjowa

  • Members
  • 3 posts

Posted 07 March 2015 - 01:59 PM

Very nice API! I'm doing a little project now were I use a lot of matrices and it was quit annoying that I couldn't do matrix algebra.
Are you still working on it? I could use some more functions, like finding the max or min value and returning that value plus the index. I've already added some stuff to it myself. It is quite handy that there is a base already to improve on.

Edited by Djosjowa, 07 March 2015 - 11:50 PM.


#5 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 08 March 2015 - 01:53 AM

View PostDjosjowa, on 07 March 2015 - 01:59 PM, said:

Very nice API! I'm doing a little project now were I use a lot of matrices and it was quit annoying that I couldn't do matrix algebra.
Are you still working on it? I could use some more functions, like finding the max or min value and returning that value plus the index. I've already added some stuff to it myself. It is quite handy that there is a base already to improve on.
Nah, I wrote this when we were doing matrices in school. Feel free to modify it however you like, post your version, etc.

#6 The Lone Wolfling

  • Members
  • 43 posts

Posted 27 March 2015 - 09:18 PM

May I suggest you add a simple matrix inverter?

Also, calculating the determinant.

Also, making a zeroed matrix.

Also, making a matrix by passing in a function that returns the value of a cell given the row/column number.

Also, raising a (square) matrix to a power using exponentiation by squaring.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users