How to resolve the algorithm Reduced row echelon form step by step in the JavaScript programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Reduced row echelon form step by step in the JavaScript programming language

Table of Contents

Problem Statement

Show how to compute the reduced row echelon form (a.k.a. row canonical form) of a matrix. The matrix can be stored in any datatype that is convenient (for most languages, this will probably be a two-dimensional array). Built-in functions or this pseudocode (from Wikipedia) may be used: For testing purposes, the RREF of this matrix: is:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Reduced row echelon form step by step in the JavaScript programming language

  • This code is a function called toReducedRowEchelonForm that converts a matrix into reduced row echelon form.

  • The function is called on an instance of the Matrix class.

  • The function loops through the rows of the matrix, and for each row, it finds the leading coefficient, which is the first non-zero element in the row.

  • If the leading coefficient is 0, the function skips the row and moves to the next one.

  • If the leading coefficient is non-zero, the function swaps the row with the row that contains the leading coefficient.

  • The function then divides the leading coefficient by itself to make it 1.

  • The function then loops through the other rows of the matrix and subtracts the leading coefficient times the row that contains the leading coefficient from each row.

  • The function returns the reduced row echelon form of the matrix.

  • The following code creates a matrix and calls the toReducedRowEchelonForm function on it:

var m = new Matrix([
 [ 1, 2, -1, -4],
 [ 2, 3, -1,-11],
 [-2, 0, -3, 22]
]);
print(m.toReducedRowEchelonForm());
print();

m = new Matrix([
 [ 1, 2, 3, 7],
 [-4, 7,-2, 7],
 [ 3, 3, 0, 7]
]);
print(m.toReducedRowEchelonForm());
  • The output of the code is:
[
 [ 1, 0, 0, 2 ],
 [ 0, 1, 0, 3 ],
 [ 0, 0, 1, -2 ]
]

[
 [ 1, 0, 0, 1 ],
 [ 0, 1, 0, 0 ],
 [ 0, 0, 1, 2 ]
]

Source code in the javascript programming language

// modifies the matrix in-place
Matrix.prototype.toReducedRowEchelonForm = function() {
    var lead = 0;
    for (var r = 0; r < this.rows(); r++) {
        if (this.columns() <= lead) {
            return;
        }
        var i = r;
        while (this.mtx[i][lead] == 0) {
            i++;
            if (this.rows() == i) {
                i = r;
                lead++;
                if (this.columns() == lead) {
                    return;
                }
            }
        }

        var tmp = this.mtx[i];
        this.mtx[i] = this.mtx[r];
        this.mtx[r] = tmp;

        var val = this.mtx[r][lead];
        for (var j = 0; j < this.columns(); j++) {
            this.mtx[r][j] /= val;
        }

        for (var i = 0; i < this.rows(); i++) {
            if (i == r) continue;
            val = this.mtx[i][lead];
            for (var j = 0; j < this.columns(); j++) {
                this.mtx[i][j] -= val * this.mtx[r][j];
            }
        }
        lead++;
    }
    return this;
}

var m = new Matrix([
  [ 1, 2, -1, -4],
  [ 2, 3, -1,-11],
  [-2, 0, -3, 22]
]);
print(m.toReducedRowEchelonForm());
print();

m = new Matrix([
  [ 1, 2, 3, 7],
  [-4, 7,-2, 7],
  [ 3, 3, 0, 7]
]);
print(m.toReducedRowEchelonForm());


  

You may also check:How to resolve the algorithm Numerical integration/Gauss-Legendre Quadrature step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Vector products step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Bioinformatics/base count step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Trabb Pardo–Knuth algorithm step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Sum and product puzzle step by step in the JavaScript programming language