How to resolve the algorithm Reduced row echelon form step by step in the Sidef programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Reduced row echelon form step by step in the Sidef 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 Sidef programming language
Source code in the sidef programming language
func rref (M) {
var (j, rows, cols) = (0, M.len, M[0].len)
for r in (^rows) {
j < cols || return M
var i = r
while (!M[i][j]) {
++i == rows || next
i = r
++j == cols && return M
}
M[i, r] = M[r, i] if (r != i)
M[r] = (M[r] »/» M[r][j])
for n in (^rows) {
next if (n == r)
M[n] = (M[n] »-« (M[r] »*» M[n][j]))
}
++j
}
return M
}
func say_it (message, array) {
say "\n#{message}";
array.each { |row|
say row.map { |n| " %5s" % n.as_rat }.join
}
}
var M = [
[ # base test case
[ 1, 2, -1, -4 ],
[ 2, 3, -1, -11 ],
[ -2, 0, -3, 22 ],
],
[ # mix of number styles
[ 3, 0, -3, 1 ],
[ .5, 3/2, -3, -2 ],
[ .2, 4/5, -1.6, .3 ],
],
[ # degenerate case
[ 1, 2, 3, 4, 3, 1],
[ 2, 4, 6, 2, 6, 2],
[ 3, 6, 18, 9, 9, -6],
[ 4, 8, 12, 10, 12, 4],
[ 5, 10, 24, 11, 15, -4],
],
];
M.each { |matrix|
say_it('Original Matrix', matrix);
say_it('Reduced Row Echelon Form Matrix', rref(matrix));
say '';
}
You may also check:How to resolve the algorithm Largest int from concatenated ints step by step in the Prolog programming language
You may also check:How to resolve the algorithm Compound data type step by step in the Scheme programming language
You may also check:How to resolve the algorithm Jaro-Winkler distance step by step in the Python programming language
You may also check:How to resolve the algorithm Sort using a custom comparator step by step in the R programming language
You may also check:How to resolve the algorithm Count in factors step by step in the Visual Basic .NET programming language