The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.

# Thread: AS3 Matrix class matrix math

1. 59
posts
Registered User

## AS3 Matrix class matrix math

Let me make this clear first:
this has almost nothing to do with transformation matrixes, except that they follow similar patters, and thus, can be interchanged.
Alright! onto the good part:
I wanted to make some way to do matrix math in AS3, so i built a simple class for it, and thought i might as well share. it supports the common matrix operations, addition, subtraction, multiplication, and inverses of 1x1, 2x2, and 3x3 matrices. This can be used for solving linear systems of equations, which may be useful.
http://pastebin.com/DSrfj3Lz
A small script i wrote up for testing is below. It solves a 3 variable system of equations with fairly good accuracy. ( To within math errors )
Code:
```package
{

import ASUtil.Math.matrix;

import flash.display.Sprite;

public class Main extends Sprite
{

/*
1x - 3y + 3z = -4
2x + 3y - 1z = 15
4x - 3y - 1z = 19

The solution is ( 5, 1, -2 )

We solve this using an inverse matrix:

[ 1 -3  3 ]   [ x ]   [ -4 ]
[ 2  3 -1 ] * [ y ] = [ 15 ]
[ 4 -3 -1 ]   [ z ]   [ 19 ]

Gets turned into

[ 1 -3  3 ] ^ -1   [ -4 ]   [ x ]
[ 2  3 -1 ]      * [ 15 ] = [ y ]
[ 4 -3 -1 ]        [ 19 ]   [ z ]

Which should result in about

[  5 ]
[  1 ]
[ -2 ]

---------------------------------------------------------------
[ -4 ]
[ 15 ]
[ 19 ]
*/
private var m:matrix = new matrix ( 1, 3, -4, 15, 19 );

/*
[ 1 -3  3 ]
[ 2  3 -1 ]
[ 4 -3 -1 ]
*/
private var m2:matrix = new matrix ( 3, 3, 1, -3, 3, 2, 3, -1, 4, -3, -1 );

public function Main()
{

trace ( m2 );
trace ( m );
trace ( m2.inverse ().mulMatrix ( m ) );

}

}

}```
:)

2. Cool man, I've been working on something similar here and there. You should implement an algorithm to find the inverse of nxn matrices.

3. 59
posts
Registered User
Yeah... no. XD
I might implement a 4x4, but that's as high as it's going.

4. Just a suggestion for something to do. Maybe it's only me who finds doing math for no reason fun.

5. 59
posts
Registered User
You have me wrong here! I love math a great deal, but As far as I've found, there is no static way to solve an nxn matrix. ( Besides, i'm only just at that point in math, cause they wouldn't let me take algebra 2 over the summer ) It requires some pretty dynamic code that I haven't even begun to contemplate. Not to mention it would be extremely slow. I'm already reaching at quite a bit of power to solve the 4x4 matrices, and that's statically solved. Maybe when they add workers to the runtime, but that's not happened yet. If you would like to write an inversion method, be my guest!

6. Of course there's a way to solve it! The most straight forward method would be gauss-jordan elimination. Another would be cofactor expansion, although both methods seem like they would be very slow for large n. For probably a majority of math problems, someone has probably already created an efficient computer algorithm to solve it and normally I just search for one to implement, but I have been unable to find one for this that works for all cases. I have seen a few that recommend gauss-jordan elimination though, so that might in fact be the best way.

I plan to implement a method though for the Matrix class that I'm making. (eventually)

##### Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•