Reduced row echelon form: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
|||
Line 798: | Line 798: | ||
MtxDisplay(m1); |
MtxDisplay(m1); |
||
return 0; |
return 0; |
||
}</lang> |
|||
=={{header|C sharp|C#}}== |
|||
<lang csharp>using System; |
|||
namespace rref |
|||
{ |
|||
class Program |
|||
{ |
|||
static void Main(string[] args) |
|||
{ |
|||
int[,] matrix = new int[3, 4]{ |
|||
{ 1, 2, -1, -4 }, |
|||
{ 2, 3, -1, -11 }, |
|||
{ -2, 0, -3, 22 } |
|||
}; |
|||
matrix = rref(matrix); |
|||
} |
|||
private static int[,] rref(int[,] matrix) |
|||
{ |
|||
int lead = 0, rowCount = matrix.GetLength(0), columnCount = matrix.GetLength(1); |
|||
for (int r = 0; r < rowCount; r++) |
|||
{ |
|||
if (columnCount <= lead) break; |
|||
int i = r; |
|||
while (matrix[i, lead] == 0) |
|||
{ |
|||
i++; |
|||
if (i == rowCount) |
|||
{ |
|||
i = r; |
|||
lead++; |
|||
if (columnCount == lead) |
|||
{ |
|||
lead--; |
|||
break; |
|||
} |
|||
} |
|||
} |
|||
for (int j = 0; j < columnCount; j++) |
|||
{ |
|||
int temp = matrix[r, j]; |
|||
matrix[r, j] = matrix[i, j]; |
|||
matrix[i, j] = temp; |
|||
} |
|||
int div = matrix[r, lead]; |
|||
if(div != 0) |
|||
for (int j = 0; j < columnCount; j++) matrix[r, j] /= div; |
|||
for (int j = 0; j < rowCount; j++) |
|||
{ |
|||
if (j != r) |
|||
{ |
|||
int sub = matrix[j, lead]; |
|||
for (int k = 0; k < columnCount; k++) matrix[j, k] -= (sub * matrix[r, k]); |
|||
} |
|||
} |
|||
lead++; |
|||
} |
|||
return matrix; |
|||
} |
|||
} |
|||
}</lang> |
}</lang> |
||
Line 998: | Line 1,060: | ||
-0 -0 1 -2 |
-0 -0 1 -2 |
||
</pre> |
</pre> |
||
=={{header|C sharp|C#}}== |
|||
<lang csharp>using System; |
|||
namespace rref |
|||
{ |
|||
class Program |
|||
{ |
|||
static void Main(string[] args) |
|||
{ |
|||
int[,] matrix = new int[3, 4]{ |
|||
{ 1, 2, -1, -4 }, |
|||
{ 2, 3, -1, -11 }, |
|||
{ -2, 0, -3, 22 } |
|||
}; |
|||
matrix = rref(matrix); |
|||
} |
|||
private static int[,] rref(int[,] matrix) |
|||
{ |
|||
int lead = 0, rowCount = matrix.GetLength(0), columnCount = matrix.GetLength(1); |
|||
for (int r = 0; r < rowCount; r++) |
|||
{ |
|||
if (columnCount <= lead) break; |
|||
int i = r; |
|||
while (matrix[i, lead] == 0) |
|||
{ |
|||
i++; |
|||
if (i == rowCount) |
|||
{ |
|||
i = r; |
|||
lead++; |
|||
if (columnCount == lead) |
|||
{ |
|||
lead--; |
|||
break; |
|||
} |
|||
} |
|||
} |
|||
for (int j = 0; j < columnCount; j++) |
|||
{ |
|||
int temp = matrix[r, j]; |
|||
matrix[r, j] = matrix[i, j]; |
|||
matrix[i, j] = temp; |
|||
} |
|||
int div = matrix[r, lead]; |
|||
if(div != 0) |
|||
for (int j = 0; j < columnCount; j++) matrix[r, j] /= div; |
|||
for (int j = 0; j < rowCount; j++) |
|||
{ |
|||
if (j != r) |
|||
{ |
|||
int sub = matrix[j, lead]; |
|||
for (int k = 0; k < columnCount; k++) matrix[j, k] -= (sub * matrix[r, k]); |
|||
} |
|||
} |
|||
lead++; |
|||
} |
|||
return matrix; |
|||
} |
|||
} |
|||
}</lang> |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
Line 2,095: | Line 2,095: | ||
0,1,0,1.666666666666667 |
0,1,0,1.666666666666667 |
||
0,0,1,1</pre> |
0,0,1,1</pre> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
Line 2,374: | Line 2,373: | ||
base0 |
base0 |
||
</lang> |
</lang> |
||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
Line 2,639: | Line 2,636: | ||
0 0 1 -2</pre> |
0 0 1 -2</pre> |
||
=={{header| |
=={{header|Phix}}== |
||
{{Trans|Euphoria}} |
|||
<lang Phix>function ToReducedRowEchelonForm(sequence M) |
|||
integer lead = 1, |
|||
rowCount = length(M), |
|||
columnCount = length(M[1]), |
|||
i |
|||
for r=1 to rowCount do |
|||
if lead>=columnCount then exit end if |
|||
i = r |
|||
while M[i][lead]=0 do |
|||
i += 1 |
|||
if i=rowCount then |
|||
i = r |
|||
lead += 1 |
|||
if lead=columnCount then exit end if |
|||
end if |
|||
end while |
|||
-- nb M[i] is assigned before M[r], which matters when i=r: |
|||
{M[r],M[i]} = {sq_div(M[i],M[i][lead]),M[r]} |
|||
for j=1 to rowCount do |
|||
if j!=r then |
|||
M[j] = sq_sub(M[j],sq_mul(M[j][lead],M[r])) |
|||
end if |
|||
end for |
|||
lead += 1 |
|||
end for |
|||
return M |
|||
end function |
|||
? ToReducedRowEchelonForm( |
|||
{ { 1, 2, -1, -4 }, |
|||
{ 2, 3, -1, -11 }, |
|||
{ -2, 0, -3, 22 } })</lang> |
|||
{{out}} |
|||
<pre> |
|||
{{1,0,0,-8},{0,1,0,1},{0,0,1,-2}} |
|||
</pre> |
|||
=={{header|PHP}}== |
|||
{{works with|PHP|5.x}} |
|||
{{trans|Java}} |
|||
<lang php><?php |
|||
function rref($matrix) |
|||
{ |
|||
$lead = 0; |
|||
$rowCount = count($matrix); |
|||
if ($rowCount == 0) |
|||
return $matrix; |
|||
$columnCount = 0; |
|||
if (isset($matrix[0])) { |
|||
$columnCount = count($matrix[0]); |
|||
} |
|||
for ($r = 0; $r < $rowCount; $r++) { |
|||
if ($lead >= $columnCount) |
|||
break; { |
|||
$i = $r; |
|||
while ($matrix[$i][$lead] == 0) { |
|||
$i++; |
|||
if ($i == $rowCount) { |
|||
$i = $r; |
|||
$lead++; |
|||
if ($lead == $columnCount) |
|||
return $matrix; |
|||
} |
|||
} |
|||
$temp = $matrix[$r]; |
|||
$matrix[$r] = $matrix[$i]; |
|||
$matrix[$i] = $temp; |
|||
} { |
|||
$lv = $matrix[$r][$lead]; |
|||
for ($j = 0; $j < $columnCount; $j++) { |
|||
$matrix[$r][$j] = $matrix[$r][$j] / $lv; |
|||
} |
|||
} |
|||
for ($i = 0; $i < $rowCount; $i++) { |
|||
if ($i != $r) { |
|||
$lv = $matrix[$i][$lead]; |
|||
for ($j = 0; $j < $columnCount; $j++) { |
|||
$matrix[$i][$j] -= $lv * $matrix[$r][$j]; |
|||
} |
|||
} |
|||
} |
|||
$lead++; |
|||
} |
|||
return $matrix; |
|||
} |
|||
?></lang> |
|||
=={{header|PicoLisp}}== |
|||
<lang PicoLisp>(de reducedRowEchelonForm (Mat) |
|||
(let (Lead 1 Cols (length (car Mat))) |
|||
(for (X Mat X (cdr X)) |
|||
(NIL |
|||
(loop |
|||
(T (seek '((R) (n0 (get R 1 Lead))) X) |
|||
@ ) |
|||
(T (> (inc 'Lead) Cols)) ) ) |
|||
(xchg @ X) |
|||
(let D (get X 1 Lead) |
|||
(map |
|||
'((R) (set R (/ (car R) D))) |
|||
(car X) ) ) |
|||
(for Y Mat |
|||
(unless (== Y (car X)) |
|||
(let N (- (get Y Lead)) |
|||
(map |
|||
'((Dst Src) |
|||
(inc Dst (* N (car Src))) ) |
|||
Y |
|||
(car X) ) ) ) ) |
|||
(T (> (inc 'Lead) Cols)) ) ) |
|||
Mat )</lang> |
|||
{{out}} |
|||
<pre>(reducedRowEchelonForm |
|||
'(( 1 2 -1 -4) ( 2 3 -1 -11) (-2 0 -3 22)) ) |
|||
-> ((1 0 0 -8) (0 1 0 1) (0 0 1 -2))</pre> |
|||
=={{header|Python}}== |
|||
<lang python>def ToReducedRowEchelonForm( M): |
|||
if not M: return |
|||
lead = 0 |
|||
rowCount = len(M) |
|||
columnCount = len(M[0]) |
|||
for r in range(rowCount): |
|||
if lead >= columnCount: |
|||
return |
|||
i = r |
|||
while M[i][lead] == 0: |
|||
i += 1 |
|||
if i == rowCount: |
|||
i = r |
|||
lead += 1 |
|||
if columnCount == lead: |
|||
return |
|||
M[i],M[r] = M[r],M[i] |
|||
lv = M[r][lead] |
|||
M[r] = [ mrx / float(lv) for mrx in M[r]] |
|||
for i in range(rowCount): |
|||
if i != r: |
|||
lv = M[i][lead] |
|||
M[i] = [ iv - lv*rv for rv,iv in zip(M[r],M[i])] |
|||
lead += 1 |
|||
mtx = [ |
|||
[ 1, 2, -1, -4], |
|||
[ 2, 3, -1, -11], |
|||
[-2, 0, -3, 22],] |
|||
ToReducedRowEchelonForm( mtx ) |
|||
for rw in mtx: |
|||
print ', '.join( (str(rv) for rv in rw) )</lang> |
|||
=={{header|R}}== |
|||
{{trans|Fortran}} |
|||
<lang rsplus>rref <- function(m) { |
|||
pivot <- 1 |
|||
norow <- nrow(m) |
|||
nocolumn <- ncol(m) |
|||
for(r in 1:norow) { |
|||
if ( nocolumn <= pivot ) break; |
|||
i <- r |
|||
while( m[i,pivot] == 0 ) { |
|||
i <- i + 1 |
|||
if ( norow == i ) { |
|||
i <- r |
|||
pivot <- pivot + 1 |
|||
if ( nocolumn == pivot ) return(m) |
|||
} |
|||
} |
|||
trow <- m[i, ] |
|||
m[i, ] <- m[r, ] |
|||
m[r, ] <- trow |
|||
m[r, ] <- m[r, ] / m[r, pivot] |
|||
for(i in 1:norow) { |
|||
if ( i != r ) |
|||
m[i, ] <- m[i, ] - m[r, ] * m[i, pivot] |
|||
} |
|||
pivot <- pivot + 1 |
|||
} |
|||
return(m) |
|||
} |
|||
m <- matrix(c(1, 2, -1, -4, |
|||
2, 3, -1, -11, |
|||
-2, 0, -3, 22), 3, 4, byrow=TRUE) |
|||
print(m) |
|||
print(rref(m))</lang> |
|||
=={{header|Racket}}== |
|||
<lang racket> |
|||
#lang racket |
|||
(require math) |
|||
(define (reduced-echelon M) |
|||
(matrix-row-echelon M #t #t)) |
|||
(reduced-echelon |
|||
(matrix [[1 2 -1 -4] |
|||
[2 3 -1 -11] |
|||
[-2 0 -3 22]])) |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
(mutable-array |
|||
#[#[1 0 0 -8] |
|||
#[0 1 0 1] |
|||
#[0 0 1 -2]]) |
|||
</pre> |
|||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
{{trans|Perl}} |
{{trans|Perl}} |
||
{{works with|Rakudo|2018.03}} |
{{works with|Rakudo|2018.03}} |
||
Line 2,898: | Line 3,109: | ||
Note that the negative zeros in the output are innocuous, |
Note that the negative zeros in the output are innocuous, |
||
and also occur in the Perl 5 version. |
and also occur in the Perl 5 version. |
||
=={{header|Phix}}== |
|||
{{Trans|Euphoria}} |
|||
<lang Phix>function ToReducedRowEchelonForm(sequence M) |
|||
integer lead = 1, |
|||
rowCount = length(M), |
|||
columnCount = length(M[1]), |
|||
i |
|||
for r=1 to rowCount do |
|||
if lead>=columnCount then exit end if |
|||
i = r |
|||
while M[i][lead]=0 do |
|||
i += 1 |
|||
if i=rowCount then |
|||
i = r |
|||
lead += 1 |
|||
if lead=columnCount then exit end if |
|||
end if |
|||
end while |
|||
-- nb M[i] is assigned before M[r], which matters when i=r: |
|||
{M[r],M[i]} = {sq_div(M[i],M[i][lead]),M[r]} |
|||
for j=1 to rowCount do |
|||
if j!=r then |
|||
M[j] = sq_sub(M[j],sq_mul(M[j][lead],M[r])) |
|||
end if |
|||
end for |
|||
lead += 1 |
|||
end for |
|||
return M |
|||
end function |
|||
? ToReducedRowEchelonForm( |
|||
{ { 1, 2, -1, -4 }, |
|||
{ 2, 3, -1, -11 }, |
|||
{ -2, 0, -3, 22 } })</lang> |
|||
{{out}} |
|||
<pre> |
|||
{{1,0,0,-8},{0,1,0,1},{0,0,1,-2}} |
|||
</pre> |
|||
=={{header|PHP}}== |
|||
{{works with|PHP|5.x}} |
|||
{{trans|Java}} |
|||
<lang php><?php |
|||
function rref($matrix) |
|||
{ |
|||
$lead = 0; |
|||
$rowCount = count($matrix); |
|||
if ($rowCount == 0) |
|||
return $matrix; |
|||
$columnCount = 0; |
|||
if (isset($matrix[0])) { |
|||
$columnCount = count($matrix[0]); |
|||
} |
|||
for ($r = 0; $r < $rowCount; $r++) { |
|||
if ($lead >= $columnCount) |
|||
break; { |
|||
$i = $r; |
|||
while ($matrix[$i][$lead] == 0) { |
|||
$i++; |
|||
if ($i == $rowCount) { |
|||
$i = $r; |
|||
$lead++; |
|||
if ($lead == $columnCount) |
|||
return $matrix; |
|||
} |
|||
} |
|||
$temp = $matrix[$r]; |
|||
$matrix[$r] = $matrix[$i]; |
|||
$matrix[$i] = $temp; |
|||
} { |
|||
$lv = $matrix[$r][$lead]; |
|||
for ($j = 0; $j < $columnCount; $j++) { |
|||
$matrix[$r][$j] = $matrix[$r][$j] / $lv; |
|||
} |
|||
} |
|||
for ($i = 0; $i < $rowCount; $i++) { |
|||
if ($i != $r) { |
|||
$lv = $matrix[$i][$lead]; |
|||
for ($j = 0; $j < $columnCount; $j++) { |
|||
$matrix[$i][$j] -= $lv * $matrix[$r][$j]; |
|||
} |
|||
} |
|||
} |
|||
$lead++; |
|||
} |
|||
return $matrix; |
|||
} |
|||
?></lang> |
|||
=={{header|PicoLisp}}== |
|||
<lang PicoLisp>(de reducedRowEchelonForm (Mat) |
|||
(let (Lead 1 Cols (length (car Mat))) |
|||
(for (X Mat X (cdr X)) |
|||
(NIL |
|||
(loop |
|||
(T (seek '((R) (n0 (get R 1 Lead))) X) |
|||
@ ) |
|||
(T (> (inc 'Lead) Cols)) ) ) |
|||
(xchg @ X) |
|||
(let D (get X 1 Lead) |
|||
(map |
|||
'((R) (set R (/ (car R) D))) |
|||
(car X) ) ) |
|||
(for Y Mat |
|||
(unless (== Y (car X)) |
|||
(let N (- (get Y Lead)) |
|||
(map |
|||
'((Dst Src) |
|||
(inc Dst (* N (car Src))) ) |
|||
Y |
|||
(car X) ) ) ) ) |
|||
(T (> (inc 'Lead) Cols)) ) ) |
|||
Mat )</lang> |
|||
{{out}} |
|||
<pre>(reducedRowEchelonForm |
|||
'(( 1 2 -1 -4) ( 2 3 -1 -11) (-2 0 -3 22)) ) |
|||
-> ((1 0 0 -8) (0 1 0 1) (0 0 1 -2))</pre> |
|||
=={{header|Python}}== |
|||
<lang python>def ToReducedRowEchelonForm( M): |
|||
if not M: return |
|||
lead = 0 |
|||
rowCount = len(M) |
|||
columnCount = len(M[0]) |
|||
for r in range(rowCount): |
|||
if lead >= columnCount: |
|||
return |
|||
i = r |
|||
while M[i][lead] == 0: |
|||
i += 1 |
|||
if i == rowCount: |
|||
i = r |
|||
lead += 1 |
|||
if columnCount == lead: |
|||
return |
|||
M[i],M[r] = M[r],M[i] |
|||
lv = M[r][lead] |
|||
M[r] = [ mrx / float(lv) for mrx in M[r]] |
|||
for i in range(rowCount): |
|||
if i != r: |
|||
lv = M[i][lead] |
|||
M[i] = [ iv - lv*rv for rv,iv in zip(M[r],M[i])] |
|||
lead += 1 |
|||
mtx = [ |
|||
[ 1, 2, -1, -4], |
|||
[ 2, 3, -1, -11], |
|||
[-2, 0, -3, 22],] |
|||
ToReducedRowEchelonForm( mtx ) |
|||
for rw in mtx: |
|||
print ', '.join( (str(rv) for rv in rw) )</lang> |
|||
=={{header|R}}== |
|||
{{trans|Fortran}} |
|||
<lang rsplus>rref <- function(m) { |
|||
pivot <- 1 |
|||
norow <- nrow(m) |
|||
nocolumn <- ncol(m) |
|||
for(r in 1:norow) { |
|||
if ( nocolumn <= pivot ) break; |
|||
i <- r |
|||
while( m[i,pivot] == 0 ) { |
|||
i <- i + 1 |
|||
if ( norow == i ) { |
|||
i <- r |
|||
pivot <- pivot + 1 |
|||
if ( nocolumn == pivot ) return(m) |
|||
} |
|||
} |
|||
trow <- m[i, ] |
|||
m[i, ] <- m[r, ] |
|||
m[r, ] <- trow |
|||
m[r, ] <- m[r, ] / m[r, pivot] |
|||
for(i in 1:norow) { |
|||
if ( i != r ) |
|||
m[i, ] <- m[i, ] - m[r, ] * m[i, pivot] |
|||
} |
|||
pivot <- pivot + 1 |
|||
} |
|||
return(m) |
|||
} |
|||
m <- matrix(c(1, 2, -1, -4, |
|||
2, 3, -1, -11, |
|||
-2, 0, -3, 22), 3, 4, byrow=TRUE) |
|||
print(m) |
|||
print(rref(m))</lang> |
|||
=={{header|Racket}}== |
|||
<lang racket> |
|||
#lang racket |
|||
(require math) |
|||
(define (reduced-echelon M) |
|||
(matrix-row-echelon M #t #t)) |
|||
(reduced-echelon |
|||
(matrix [[1 2 -1 -4] |
|||
[2 3 -1 -11] |
|||
[-2 0 -3 22]])) |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
(mutable-array |
|||
#[#[1 0 0 -8] |
|||
#[0 1 0 1] |
|||
#[0 0 1 -2]]) |
|||
</pre> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 3,663: | Line 3,661: | ||
[0 0 1 -2]] |
[0 0 1 -2]] |
||
</pre> |
</pre> |
||
=={{header|TI-89 BASIC}}== |
=={{header|TI-89 BASIC}}== |