Example 2: Solve a Small Linear System with User Supplied Inner Product

A solution to a small linear system is found. The coefficient matrix is stored as a full matrix and no preconditioning is used. Typically, preconditioning is required to achieve convergence in a reasonable number of iterations. The user supplies a function to compute the inner product and norm within the Gram-Schmidt implementation.
using System;
using Imsl.Math;
using IMSLException = Imsl.IMSLException;

public class GenMinResEx2 : GenMinRes.IFunction, GenMinRes.IVectorProducts
{
    private static double[,] a = {
                    {33.0, 16.0, 72.0},
                    {-24.0, -10.0, -57.0},
                    {18.0, -11.0, 7.0}
                                 };
    private static double[] b = {129.0, -96.0, 8.5};
    // If A were to be read in from some outside source the   //
    // code to read the matrix could reside in a constructor. //
    
    public void  Amultp(double[] p, double[] z)
    {
        double[] result;
        result = Matrix.Multiply(a, p);
        Array.Copy(result, 0, z, 0, z.Length);
    }
    
    public double Innerproduct(double[] x, double[] y)
    {
        int n = x.Length;
        double tmp = 0.0;
        for (int i = 0; i < n; i++)
        {
            tmp += x[i] * y[i];
        }
        return tmp;
    }
    
    public double Norm(double[] x)
    {
        int n = x.Length;
        double tmp = 0.0;
        for (int i = 0; i < n; i++)
        {
            tmp += x[i] * x[i];
        }
        return System.Math.Sqrt(tmp);
    }
    
    public static void  Main(String[] args)
    {
        int n = 3;
        
        GenMinResEx2 atp = new GenMinResEx2();
        
        // Construct a GenMinRes object
        GenMinRes gnmnrs = new GenMinRes(n, atp);
        gnmnrs.SetVectorProducts(atp);
        
        // Solve Ax = b
        new PrintMatrix("x").Print(gnmnrs.Solve(b));
    }
}

Output

          x
           0          
0  0.999999999999999  
1  1.5                
2  1                  


Link to C# source.