ComputerCraft Forums

ComputerCraft => Programs => APIs and Utilities => Topic started by: KingofGamesYami on Aug 13, 2018, 02:19 AM

Title: Matrix API
Post by: KingofGamesYami on Aug 13, 2018, 02:19 AM
Reposted from here (http://www.computercraft.info/forums2/index.php?/topic/20299-10-matrix-api/)

If you do not know what a Matrix is, I would recommend this wikipedia page (http://en.wikipedia.org/wiki/Matrix_(mathematics)).
Download:
1.0
pastebin get aE66gE86 (http://pastebin.com/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 (http://en.wikipedia.org/wiki/Matrix_(mathematics)#Basic_operations)
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...
Title: Matrix API
Post by: exerro on Aug 28, 2018, 11:16 AM
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 (https://en.wikipedia.org/wiki/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?
Title: Matrix API
Post by: KingofGamesYami on Aug 28, 2018, 03:15 PM
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.