Example 1: Bounded Variable Least Squares

The following example solves a linear least squares problem with bounds on the variables and compares the result to its unbounded solution. The normal of the residuals is 0.0 for the exact solution.
using System;
using Imsl.Math;

public class BoundedVariableLeastSquaresEx1
{
    public static void  Main(String[] args)
    {
        double[,] a = {{1, - 3, 2}, {- 3, 10, - 5},{2, - 5, 6}};
        double[] b = {27, - 78, 64};
        double[] xlb = {- 1, - 1, - 1};
        double[] xub = {5, 5, 5};
        double[] xl_ub = {
            Double.NegativeInfinity, 
            Double.NegativeInfinity, 
            Double.NegativeInfinity
        };
        double[] xu_ub = {
            Double.PositiveInfinity, 
            Double.PositiveInfinity, 
            Double.PositiveInfinity
        };
        
        // compute the bounded solution
        BoundedVariableLeastSquares bvls = 
            new BoundedVariableLeastSquares(a, b, xlb, xub);
        bvls.Solve();
        double[] x_bounded = bvls.GetSolution();
        new PrintMatrix("Bounded Solution").Print(x_bounded);
        Console.WriteLine("Norm of the Residuals = " + 
            bvls.ResidualNorm);
        
        // compute the unbounded solution
        bvls = new BoundedVariableLeastSquares(a, b, xl_ub, xu_ub);
        bvls.Solve();
        double[] x_unbounded = bvls.GetSolution();
        new PrintMatrix("\nUnbounded Solution").Print(x_unbounded);
        Console.WriteLine("Norm of the Residuals = " + 
            bvls.ResidualNorm);
    }
}

Output

Bounded Solution
   0   
0   5  
1  -1  
2   5  

Norm of the Residuals = 35.0142828000232
 
Unbounded Solution
           0          
0   1.00000000000004  
1  -3.99999999999999  
2   7                 

Norm of the Residuals = 0

Link to C# source.