Reduced row echelon form: Difference between revisions

m
No edit summary
 
(4 intermediate revisions by 3 users not shown)
Line 1:
Hello RosettaCode ...<br>
 
Your algorithm/function does NOT work ...<br>
 
The "SWAP" is not being done. ...<br>
 
Try this matrix to see the problem. ...<br>
 
 
//===================================<br>
 
Linear Algebra with Applications by W. Keith Nicholson, University of Calgary<br>
 
page 12 - Systems of Linear Equations<br>
 
 
Solve the following system of equations.<br>
 
3x + y − 4z = −1<br>
 
x + 10z = 5<br>
 
4x + y + 6z = 1<br>
 
 
Solution. The corresponding augmented matrix is<br>
 
3 1 −4 −1<br>
 
1 0 10 5<br>
 
4 1 6 1<br>
 
 
Create the first leading one by interchanging rows 1 and 2<br>
 
1 0 01 5<br>
 
3 1 −4 −1<br>
 
4 1 6 1<br>
 
 
Now subtract 3 times row 1 from row 2, and subtract 4 times row 1 from row 3. The result is<br>
 
1 0 10 5<br>
 
0 1 −34 −16<br>
 
0 1 −34 −19<br>
 
 
Now subtract row 2 from row 3 to obtain<br>
 
1 0 10 5<br>
 
0 1 −34 −16<br>
 
0 0 0 −3<br>
 
 
This means that the following reduced system of equations<br>
 
x + 10z = 5<br>
 
y − 34z = −16<br>
 
0 +0 +0 = −3<br>
 
 
is equivalent to the original system. In other words, the two have the same solutions. <br>
 
But this last system clearly has no solution (the last equation requires that x, y and z satisfy 0x+0y+0z = −3,<br>
 
and no such numbers exist). <br>
 
Hence the original system has no solution.<big>Big text</big>
 
//===========================
 
 
 
{{wikipedia|Rref#Pseudocode}}
Line 2,191 ⟶ 2,110:
0 1 0 1
0 0 1 -2</pre>
 
=={{header|EasyLang}}==
{{trans|Go}}
<syntaxhighlight>
proc rref . m[][] .
nrow = len m[][]
ncol = len m[1][]
lead = 1
for r to nrow
if lead > ncol
return
.
i = r
while m[i][lead] = 0
i += 1
if i > nrow
i = r
lead += 1
if lead > ncol
return
.
.
.
swap m[i][] m[r][]
m = m[r][lead]
for k to ncol
m[r][k] /= m
.
for i to nrow
if i <> r
m = m[i][lead]
for k to ncol
m[i][k] -= m * m[r][k]
.
.
.
lead += 1
.
.
test[][] = [ [ 1 2 -1 -4 ] [ 2 3 -1 -11 ] [ -2 0 -3 22 ] ]
rref test[][]
print test[][]
</syntaxhighlight>
 
=={{header|Euphoria}}==
Line 5,405 ⟶ 5,367:
{{libheader|Wren-matrix}}
The above module has a method for this built in as it's needed to implement matrix inversion using the Gauss-Jordan method. However, as in the example here, it's not just restricted to square matrices.
<syntaxhighlight lang="ecmascriptwren">import "./matrix" for Matrix
import "./fmt" for Fmt
 
var m = Matrix.new([
2,078

edits