Results 1 to 6 of 6
Thread: AS3 Matrix class matrix math
-
April 2nd, 2012, 05:58 AM #158Registered User
postsAS3 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.
here's a link:
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 ) ); } } }
-
April 3rd, 2012, 09:08 PM #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.
Proud Montanadian
We tolerate living and breathing. And niches.
Name Brand Watches
Maybe getTimer() or TweenMax is the answer to your problem . . .
-
April 4th, 2012, 02:32 AM #358Registered User
postsYeah... no. XD
I might implement a 4x4, but that's as high as it's going.
-
April 4th, 2012, 04:03 AM #4
Just a suggestion for something to do. Maybe it's only me who finds doing math for no reason fun.
Proud Montanadian
We tolerate living and breathing. And niches.
Name Brand Watches
Maybe getTimer() or TweenMax is the answer to your problem . . .
-
April 4th, 2012, 09:43 AM #558Registered User
postsYou 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!
-
April 4th, 2012, 03:23 PM #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)Proud Montanadian
We tolerate living and breathing. And niches.
Name Brand Watches
Maybe getTimer() or TweenMax is the answer to your problem . . .

Reply With Quote

Bookmarks