QR decomposition: Difference between revisions

m
Line 2,667:
 
=={{header|Standard ML}}==
{{trans|Axiom}}
We first define a signature for a radical category joined with a field. We then define a functor with (a) structures to define operators and functions for Array and Array2, and (b) functions for the QR decomposition (adapted from Axiom):
<lang sml>signature RADCATFIELD = sig
type real
Line 2,676 ⟶ 2,677:
val * : real * real -> real
val / : real * real -> real
val toString : real -> string
val sign : real -> real
val sqrt : real -> real
Line 2,731:
fun updateSubMatrix(h,i,j,s) =
tabulate RowMajor (nRows s, nCols s, fn (a,b) => update(h,Int.+(i,a),Int.+(j,b),sub(s,a,b)))
fun print a =
let
val (m,n) = dimensions a
fun loop(i,j,x) =
(if i=0 andalso j=0 then TextIO.print "Array2.fromList([" else ();
if j=0 andalso Int.>(i,0) then TextIO.print "," else ();
if j=0 then TextIO.print "[" else ();
if Int.>(j,0) then TextIO.print "," else ();
(TextIO.print o F.toString) x;
if j=Int.-(n,1) then TextIO.print "]" else ();
if i=Int.-(m,1) andalso j=Int.-(n,1) then TextIO.print "])\n" else ())
in
appi RowMajor loop {base=a,nrows=SOME m,ncols=SOME n,row=0,col=0}
end
end
end
fun printtoList a =
List.tabulate(Array2.nRows a, fn i => List.tabulate(Array2.nCols a, fn j => Array2.sub(a,i,j)))
fun householder a =
let open Array
Line 2,815 ⟶ 2,803:
end
 
structure Q = QR(RealFieldRealRadicalCategoryField);
 
let
Line 2,821 ⟶ 2,809:
val {q,r} = Q.qr(mat)
in
({q=Q.M.printtoList q; r=Q.M.printtoList r)}
end;
(* output *)
val it =
Array2.fromList([[~0.857142857143,0.394285714286,0.331428571429],
{q=[[~0.428571428571857142857143,~0.902857142857394285714286,~0.0342857142857331428571429],
[~0.285714285714428571428571,~0.171428571429902857142857,~0.9428571428570342857142857]]),
[0.285714285714,~0.171428571429,0.942857142857]],
Array2.fromList([[~14.0,~21.0,14.0],
r=[[~14.0,~21.0,14.0],[5.97812397875E~18,~175.0,70.0],
[4.47505280695E~16,0.0,~35.0]])} : {q:real list list, r:real list list}
 
let open Array
136

edits