Main Menu

Matrix API

Started by KingofGamesYami, Aug 13, 2018, 02:19 AM

Previous topic - Next topic

KingofGamesYami

Reposted from here

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...
I'm a ComputerCraft veteran with over 3k posts on the old ComputerCraft Forum.  I'm mostly inactive in CC having moved on to bigger, more interesting projects but still contribute to the community.

exerro

It would be useful to have matrix construction functions like
* identity(dimensions)
* translate2D(x, y)
* rotate3D(xr, yr, zr)
* scale2D(x, y)
and be able to find the inverse of a matrix. I think you can use Gaussian Elimination for that (at least with square matrices).

Maybe also a vector(xs...) function that constructs a matrix with one column, or that just constructs a "vector" object. I've only used matrices in code for vector transformations.

What do the comparison operators do?

KingofGamesYami

I'm not really working on this right now, but I'll look into it if I happen to be procrastinating during exams.

The comparison operators compare the traces, except == which compares each slot individually.
I'm a ComputerCraft veteran with over 3k posts on the old ComputerCraft Forum.  I'm mostly inactive in CC having moved on to bigger, more interesting projects but still contribute to the community.