Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Systems of linear equations

Guest
Jan 30, 2013 Jan 30, 2013

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.

TOPICS
ActionScript
349
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 30, 2013 Jan 30, 2013

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

...
Translate
Community Expert ,
Jan 30, 2013 Jan 30, 2013
LATEST

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();

        }

    }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines