Copy link to clipboard
Copied
Hi
Does anyone know if there are AS classes or functions to solve systems of linear equations? On the form A * x = c. A is a square matrix, x is an unknown vector, and c are constant.
In other words, a system of equations with some number of unkowns (1-5), and an equal number of equations.
here's an implementation of gaussian elimination you can use. there's no error checking included:
package com.kglad{
public class Gaussian_Elimination {
public function solveF(a:Array):Array{
for(var pivotCol:int=0;pivotCol<a[0].length-1;pivotCol++){
a = pivotF(a,pivotCol);
var factor:Number;
for(var row:int=pivotCol+1;row<a.length;row++){
factor = a[row][pivotCol];
for(var _col:int=pivotC
...Copy link to clipboard
Copied
here's an implementation of gaussian elimination you can use. there's no error checking included:
package com.kglad{
public class Gaussian_Elimination {
public function solveF(a:Array):Array{
for(var pivotCol:int=0;pivotCol<a[0].length-1;pivotCol++){
a = pivotF(a,pivotCol);
var factor:Number;
for(var row:int=pivotCol+1;row<a.length;row++){
factor = a[row][pivotCol];
for(var _col:int=pivotCol;_col<a[0].length;_col++){
a[row][_col] -= a[pivotCol][_col]*factor;
}
}
}
return echelonF(a);
}
private function pivotF(a:Array,pivotCol:int):Array{
var maxAbs:Number = 0;
var maxRow:int;
for(var row:int=0;row<a.length;row++){
if(Math.abs(a[row][pivotCol])>maxAbs){
maxRow = row;
maxAbs = Math.abs(a[row][pivotCol])
}
}
// swap row pivotCol with maxRow
var tempA:Array = a[pivotCol];
// normalize maxRow
var max:Number = a[maxRow][pivotCol];
for(var i:int=0;i<a[maxRow].length;i++){
a[maxRow] /= max;
}
a[pivotCol] = a[maxRow];
a[maxRow] = tempA;
return a;
}
private function echelonF(a:Array):Array{
var ans:Array = [];
var last:int = a[0].length-1;
for(var row:int=0;row<a.length;row++){
var term:Number = 0;
for(var num:int=0;num<row;num++){
term-=a[row][last-num-1]*ans[num];
}
term+=a[row][last];
ans.push(term/a[row][last-row-1]);
}
return ans.reverse();
}
}
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now