Reduced row echelon form: Difference between revisions

m
Added Sidef
m (→‎{{header|Perl 6}}: Updated to work with latest Rakudo, minor updates)
m (Added Sidef)
Line 3,133:
Original source: [http://seed7.sourceforge.net/algorith/math.htm#toReducedRowEchelonForm]
 
=={{header|Sidef}}==
{{trans|Perl 6}}
<lang ruby>func rref (Array m) {
m.is_empty && return;
var (lead, rows, cols) = (0, m.len, m[0].len);
 
rows.range.each { |r|
lead >= cols && return m;
var i = r;
 
while (!m[i][lead]) {
++i == rows || next;
i = r;
++lead == cols && return m;
}
 
m[i, r] = m[r, i];
var lv = m[r][lead];
m[r] = (m[r] »/» lv);
 
rows.range.each { |n|
n == r && next;
m[n] = (m[n] »-« (m[r] «*« m[n][lead]))
}
++lead;
}
return m
}
 
func say_it (message, array) {
say "\n#{message}";
array.each { |row|
say row.map { |n| " %5s" % n }.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|
var rat_matrix = matrix.map{.map{.to_r}};
say_it('Original Matrix', rat_matrix);
say_it('Reduced Row Echelon Form Matrix', rref(rat_matrix));
say '';
}</lang>
{{out}}
<pre>
Original Matrix
1 2 -1 -4
2 3 -1 -11
-2 0 -3 22
 
Reduced Row Echelon Form Matrix
1 0 0 -8
0 1 0 1
0 0 1 -2
 
 
Original Matrix
3 0 -3 1
1/2 3/2 -3 -2
1/5 4/5 -8/5 3/10
 
Reduced Row Echelon Form Matrix
1 0 0 -41/2
0 1 0 -217/6
0 0 1 -125/6
 
 
Original Matrix
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
 
Reduced Row Echelon Form Matrix
1 2 0 0 3 4
0 0 1 0 0 -1
0 0 0 1 0 0
0 0 0 0 0 0
0 0 0 0 0 0
</pre>
 
=={{header|Swift}}==
2,747

edits