MatrixQRTest.frink

Download or view MatrixQRTest.frink in plain text format


/** This class tests QR decomposition and least-squares routines in
    Matrix.frink.

    This tests the solution of a system of equations represented by matrices
    A * X = B

    where A and B are given and X is solved for.
*/


use Matrix.frink

A = new Matrix[[[2, 3],
                [5, 7]]]

B = new Matrix[[[12],
                [29]]]

println["Original matrix is:\n" + A.formatMatrix[]]
sol = A.leastSquares[B].roundToInt[]
println[]
println["Least-squares solution is:"]
println[sol.formatMatrix[]]

reverse = A.multiply[sol]
println["Verifying solution:"]
println[formatTable[[[reverse.formatMatrix[], "=", B.formatMatrix[]]]]]


A = new Matrix[[[1, 2],
                [1, 3]]]

B = new Matrix[[[1],
                [2]]]

println["\n\n"]
println["Original matrix is:\n" + A.formatMatrix[]]
[Q, R] = A.QRDecompose[]
println["Q and R are:"]
println[formatTable[[[Q.formatMatrix[], "*", R.formatMatrix[]]]]]
println["Testing decomposition.  This should equal original matrix: "]
println[Q.multiply[R].roundToInt[].formatMatrix[]]

sol = A.leastSquares[B].roundToInt[]
println[]
println["Least-squares solution is:"]
println[sol.formatMatrix[]]

reverse = A.multiply[sol]
println["Verifying solution:"]
println[formatTable[[[reverse.formatMatrix[], "=", B.formatMatrix[]]]]]


/*
// Symbolic test.  Produces big symbolic results!
symbolicMode[true]
A = new Matrix[[[f1_1, f2_1],
                [f2_1, f2_2]]]

B = new Matrix[[[y1],
                [y2]]]

println["\n\n"]
println["Original matrix is:\n" + A.formatMatrix[]]
sol = A.leastSquares[B]
println[]
println["Least-squares solution is:"]
println[sol.formatMatrix[]]

reverse = A.multiply[sol]
println["Verifying solution:"]
println[formatTable[[[reverse.formatMatrix[], "=", B.formatMatrix[]]]]]
*/


Download or view MatrixQRTest.frink in plain text format


This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen was born 19966 days, 16 hours, 16 minutes ago.