Example: QR Factorization of a Matrix

The QR Factorization of a Matrix is performed. A linear system is then solved using the factorization. The rank of the input matrix is also computed.
using System;
using Imsl.Math;

public class QREx1
{
    public static void  Main(String[] args)
    {
        double[,] a = {
            {1, 2, 4},
            {1, 4, 16}, 
            {1, 6, 36}, 
            {1, 8, 64}
        };
        double[] b = new double[]{16.99, 57.01, 120.99, 209.01};
        
        // Compute the QR factorization of A
        QR qr = new QR(a);
        
        // Solve Ax = b
        double[] x = qr.Solve(b);
        new PrintMatrix("x").Print(x);
        
        // Print Q and R.
        new PrintMatrix("Q").Print(qr.GetQ());
        new PrintMatrix("R").Print(qr.GetR());
        
        // Find the rank of A.
        int rank = qr.GetRank();
        Console.Out.WriteLine("rank = " + rank);
    }
}

Output

          x
           0          
0  0.990000000000019  
1  2.00199999999999   
2  3                  

                                         Q
            0                   1                   2                   3           
0  -0.0531494003452735  -0.54217094609664    0.808223859120487  -0.22360679774998   
1  -0.212597601381094   -0.657435635424271  -0.269407953040162   0.670820393249937  
2  -0.478344603107461   -0.345794067982896  -0.449013255066938  -0.670820393249936  
3  -0.850390405524374    0.392753756227487   0.269407953040163   0.223606797749979  

                              R
           0                  1                   2           
0  -75.2595508889071  -10.6298800690547   -1.5944820103582    
1    0                 -2.64681879196785  -1.15264689327632   
2    0                  0                  0.359210604053549  
3    0                  0                  0                  

rank = 3

Link to C# source.