Reduced row echelon form: Difference between revisions
Content added Content deleted
(Added Algol W) |
(add FreeBASIC) |
||
Line 1,345: | Line 1,345: | ||
end program prg_test</lang> |
end program prg_test</lang> |
||
=={{header|FreeBASIC}}== |
|||
Include the code from [[Matrix multiplication#FreeBASIC]] because this function uses the matrix type defined there and I don't want to reproduce it all here. |
|||
<lang freebasic>#include once "matmult.bas" |
|||
sub rowswap( byval M as Matrix, i as uinteger, j as uinteger ) |
|||
dim as integer k |
|||
for k = 0 to ubound(M.m, 2) |
|||
swap M.m(j, k), M.m(i, k) |
|||
next k |
|||
end sub |
|||
function rowech(byval M as Matrix) as Matrix |
|||
dim as uinteger lead = 0, rowCount = 1+ubound(M.m, 1), colCount = 1+ubound(M.m, 2) |
|||
dim as uinteger r, i, j |
|||
dim as double K |
|||
for r = 0 to rowCount-1 |
|||
if lead >= colCount then exit for |
|||
i = r |
|||
while M.m(i, lead) = 0 |
|||
i += 1 |
|||
if i = rowCount then |
|||
i = r |
|||
lead += 1 |
|||
if lead = colCount then exit for |
|||
endif |
|||
wend |
|||
rowswap M, r, i |
|||
K = M.m(r,lead) |
|||
if K <> 0 then |
|||
for j = 0 to colCount-1 |
|||
M.m(r,j) /= K |
|||
next j |
|||
endif |
|||
for i = 0 to rowCount-1 |
|||
if i <> r then |
|||
K = M.m(i, lead) |
|||
for j = 0 to colCount-1 |
|||
M.m(i,j) -= M.m(r,j) * K |
|||
next j |
|||
endif |
|||
next i |
|||
lead += 1 |
|||
next r |
|||
return M |
|||
end function |
|||
dim as Matrix M = Matrix (3, 4) |
|||
dim as Matrix N |
|||
M.m(0,0) = 1 : M.m(0,1) = 2 : M.m(0,2) = -1 : M.M(0,3) = -4 |
|||
M.m(1,0) = 2 : M.m(1,1) = 3 : M.m(1,2) = -1 : M.m(1,3) = -11 |
|||
M.m(2,0) = -2: M.m(2,1) = 0 : M.m(2,2) = -3 : M.m(2,3) = 22 |
|||
dim as integer i, j |
|||
N = rowech(M) |
|||
for i=0 to 2 |
|||
for j = 0 to 3 |
|||
print N.m(i, j), |
|||
next j |
|||
print |
|||
next i</lang> |
|||
{{out}} |
|||
<pre> |
|||
1 0 0 -8 |
|||
-0 1 0 1 |
|||
-0 -0 1 -2 |
|||
</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |