This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
KingofGamesYami's profile picture

[1.0] Matrix API

Started by KingofGamesYami, 09 September 2014 - 11:31 PM
KingofGamesYami #1
Posted 10 September 2014 - 01: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…
ardera #2
Posted 26 September 2014 - 03: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
KingofGamesYami #3
Posted 26 September 2014 - 04: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.
Djosjowa #4
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 on 07 March 2015 - 10:50 PM
KingofGamesYami #5
Posted 08 March 2015 - 01:53 AM
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.
The Lone Wolfling #6
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.