Reduced row echelon form: Difference between revisions

→‎{{header|J}}: Add code from addon script
m (→‎{{header|J}}: fix link)
(→‎{{header|J}}: Add code from addon script)
Line 1,659:
 
=={{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. <code>gauss_jordan</code> and the verb <code>pivot</code> are shown below for completeness:
 
<lang j> ]mymatrix=: _4]\ 1 2 _1 _4 2 3 _1 _11 _2 0 _3 22
'''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
1e_15 gauss_jordan mymatrixy
:
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:'''
<lang j> require 'math/misc/linear'
<lang j> ]mymatrixA=: _4]\ 1 2 _1 _4 , 2 3 _1 _11 ,: _2 0 _3 22
1 2 _1 _4
2 3 _1 _11
_2 0 _3 22
 
gauss_jordan A
require 'math/misc/linear'
gauss_jordan mymatrix
1 0 0 _8
0 1 0 1
Line 1,693 ⟶ 1,733:
And:
 
<lang j>mat=: 0 ". ];._2]0 :0noun 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 0 1 0 0 0 0 _1 0 0 0 0 0
892

edits