Reduced row echelon form: Difference between revisions
Content added Content deleted
m (→{{header|J}}: fix link) |
(→{{header|J}}: Add code from addon script) |
||
Line 1,659: | Line 1,659: | ||
=={{header|J}}== |
=={{header|J}}== |
||
The reduced row echelon form of a matrix can be obtained using the <code>gauss_jordan</code> verb from the [http://www.jsoftware.com/wsvn/addons/trunk/math/misc/linear.ijs linear.ijs script], available as part of the <code>math/misc</code> addon. |
The reduced row echelon form of a matrix can be obtained using the <code>gauss_jordan</code> verb from the [http://www.jsoftware.com/wsvn/addons/trunk/math/misc/linear.ijs linear.ijs script], available as part of the <code>math/misc</code> addon. <code>gauss_jordan</code> and the verb <code>pivot</code> are shown below for completeness: |
||
⚫ | |||
'''Solution:''' |
|||
<lang j>NB.*pivot v Pivot at row, column |
|||
NB. form: (row,col) pivot M |
|||
pivot=: dyad define |
|||
'r c'=. x |
|||
col=. c{"1 y |
|||
y - (col - r = i.#y) */ (r{y) % r{col |
|||
) |
|||
NB.*gauss_jordan v Gauss-Jordan elimination (full pivoting) |
|||
NB. y is: matrix |
|||
NB. x is: optional minimum tolerance, default 1e_15. |
|||
NB. If a column below the current pivot has numbers of magnitude all |
|||
NB. less then x, it is treated as all zeros. |
|||
gauss_jordan=: verb define |
|||
⚫ | |||
: |
|||
mtx=. y |
|||
'r c'=. $mtx |
|||
rows=. i.r |
|||
i=. j=. 0 |
|||
max=. i.>./ |
|||
while. (i<r) *. j<c do. |
|||
k=. max col=. | i}. j{"1 mtx |
|||
if. 0 < x-k{col do. NB. if all col < tol, set to 0: |
|||
mtx=. 0 (<(i}.rows);j) } mtx |
|||
else. NB. otherwise sort and pivot: |
|||
if. k do. |
|||
mtx=. (<i,i+k) C. mtx |
|||
end. |
|||
mtx=. (i,j) pivot mtx |
|||
i=. >:i |
|||
end. |
|||
j=. >:j |
|||
end. |
|||
mtx |
|||
)</lang> |
|||
'''Usage:''' |
|||
⚫ | |||
⚫ | |||
1 2 _1 _4 |
1 2 _1 _4 |
||
2 3 _1 _11 |
2 3 _1 _11 |
||
_2 0 _3 22 |
_2 0 _3 22 |
||
gauss_jordan A |
|||
⚫ | |||
⚫ | |||
1 0 0 _8 |
1 0 0 _8 |
||
0 1 0 1 |
0 1 0 1 |
||
Line 1,693: | Line 1,733: | ||
And: |
And: |
||
<lang j>mat=:".;._2 |
<lang j>mat=: 0 ". ];._2 noun define |
||
1 0 0 0 0 0 1 0 0 0 0 _1 0 0 0 0 0 0 |
1 0 0 0 0 0 1 0 0 0 0 _1 0 0 0 0 0 0 |
||
1 0 0 0 0 0 0 1 0 0 0 0 _1 0 0 0 0 0 |
1 0 0 0 0 0 0 1 0 0 0 0 _1 0 0 0 0 0 |