Anonymous user
QR decomposition: Difference between revisions
→{{header|J}}: note built-in QR decomp function
(Exact and arbitrary precision solution) |
(→{{header|J}}: note built-in QR decomp function) |
||
Line 1,117:
=={{header|J}}==
'''Solution''' (built-in):<lang j> QR =: 128!:0</lang>
'''Solution''' (custom implementation): <lang j> mp=: +/ . * NB. matrix product▼
h =: +@|: NB. conjugate transpose▼
QR=: 3 : 0▼
▲<lang j>mp=: +/ . * NB. matrix product
▲h =: +@|: NB. conjugate transpose
if. 1>:n do.
A ((% {.@,) ; ]) %:(h A) mp A▼
m =.>.n%2▼
A0=.m{."1 A▼
A1=.m}."1 A▼
'Q0 R0'=.QR A0▼
'Q1 R1'=.QR A1 - Q0 mp T=.(h Q0) mp A1▼
(Q0,.Q1);(R0,.T),(-n){."1 R1▼
)</lang>▼
▲QR=: 3 : 0
12 _51 4
▲ n=.{:$A=.y
_4 24 _41
▲ A ((% {.@,) ; ]) %:(h A) mp A
QR matrix
▲ else.
+-----------------------------+----------+
▲ m =.>.n%2
| 0.857143 _0.394286 _0.331429|14 21 _14|
▲ A0=.m{."1 A
| 0.428571 0.902857 0.0342857| 0 175 _70|
▲ A1=.m}."1 A
|_0.285714 0.171429 _0.942857| 0 0 35|
▲ 'Q0 R0'=.QR A0
+-----------------------------+----------+
▲ 'Q1 R1'=.QR A1 - Q0 mp T=.(h Q0) mp A1
x:&.> QR matrix NB. Display as exact fractions
▲ (Q0,.Q1);(R0,.T),(-n){."1 R1
+--------------------+----------+
▲ end.
▲)</lang>
+--------------------+----------+</lang>
'''Example''' (polynomial fitting using QR reduction):<lang j> X=:i.# Y=:1 6 17 34 57 86 121 162 209 262 321▼
▲<lang j> QR x:12 _51 4,6 167 _68,:_4 24 _41
▲│ 6r7 _69r175 _58r175│14 21 _14│
▲│ 3r7 158r175 6r175│ 0 175 _70│
▲│_2r7 6r35 _33r35│ 0 0 35│
▲<lang j> X=:i.# Y=:1 6 17 34 57 86 121 162 209 262 321
'Q R'=: QR X ^/ i.3
R %.~(|:Q)+/ .* Y
1 2 3</lang>
'''Notes''':J offers a built-in QR decomposition function, <tt>128!:0</tt>. If J did not offer this function as a built-in, it could written in J along the lines of the second version, which is covered in more detail at From [[j:Essays/QR Decomposition]].
=={{header|Mathematica}}==
|