| #region Copyright � 2009, De Santiago-Castillo JA. All rights reserved.
|
|
|
|
|
|
|
|
|
|
|
| #endregion
|
|
|
| using System;
|
| using System.Collections.Generic;
|
| using System.Text;
|
| using System.ComponentModel;
|
| using DotNumerics.LinearAlgebra.CSLapack;
|
|
|
| namespace DotNumerics.LinearAlgebra
|
| {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| public sealed class LinearLeastSquares
|
| {
|
|
|
| #region Fields
|
|
|
| DGELS _dgels;
|
| DGELSD _dgelsd;
|
| DGELSY _dgelsy;
|
|
|
| #endregion
|
|
|
|
|
|
|
|
|
| public LinearLeastSquares()
|
| {
|
|
|
| }
|
|
|
| #region Public Methods
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| #endregion
|
|
|
| #region Private Metods
|
|
|
| #region QR or LQ factorization
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| public Matrix QRorLQSolve(Matrix A, Matrix B)
|
| {
|
|
|
| if (this._dgels == null) this._dgels = new DGELS();
|
|
|
| Matrix AClon = A.Clone();
|
| double[] AClonData = AClon.Data;
|
|
|
| Matrix ClonB = new Matrix(Math.Max(A.RowCount, A.ColumnCount), B.ColumnCount);
|
|
|
| double[] ClonData = ClonB.Data;
|
|
|
| for (int j = 0; j < ClonB.ColumnCount; j++)
|
| {
|
| for (int i = 0; i < B.RowCount; i++)
|
| {
|
| ClonB[i, j] = B[i, j];
|
| }
|
| }
|
|
|
|
|
| int Info = 0;
|
|
|
| double[] Work = new double[1];
|
| int LWork = -1;
|
|
|
| _dgels.Run("N", A.RowCount, A.ColumnCount, B.ColumnCount, ref AClonData, 0, A.RowCount, ref ClonData, 0, ClonB.RowCount, ref Work, 0, LWork, ref Info);
|
| LWork = Convert.ToInt32(Work[0]);
|
| if (LWork > 0)
|
| {
|
| Work = new double[LWork];
|
| _dgels.Run("N", A.RowCount, A.ColumnCount, B.ColumnCount, ref AClonData, 0, A.RowCount, ref ClonData, 0, ClonB.RowCount, ref Work, 0, LWork, ref Info);
|
| }
|
| else
|
| {
|
|
|
|
|
| }
|
|
|
|
|
| #region Error
|
|
|
|
|
|
|
|
|
|
|
|
|
| if (Info < 0)
|
| {
|
| string infoSTg = Math.Abs(Info).ToString();
|
| throw new ArgumentException("the " + infoSTg + " -th argument had an illegal value");
|
| }
|
| else if (Info > 0)
|
| {
|
| string infoSTg = Math.Abs(Info).ToString();
|
| throw new Exception("The matrix A does not have full rank; the least squares solution could not be computed. You can use SVDSolve or SVDdcSolve");
|
| }
|
|
|
| #endregion
|
|
|
| Matrix Solution = new Matrix(A.ColumnCount, B.ColumnCount);
|
|
|
| for (int j = 0; j < Solution.ColumnCount; j++)
|
| {
|
| for (int i = 0; i < Solution.RowCount; i++)
|
| {
|
| Solution[i, j] = ClonB[i, j];
|
| }
|
| }
|
|
|
| return Solution ;
|
| }
|
|
|
| #endregion
|
|
|
| #region Complete orthogonal factorization
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| public Matrix COFSolve(Matrix A, Matrix B)
|
| {
|
|
|
|
|
|
|
| DLAMCH dch= new DLAMCH();
|
| double RCOND = 100*dch.Run("Epsilon");
|
|
|
| return this.COFSolve(A, B, RCOND);
|
|
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| public Matrix COFSolve(Matrix A, Matrix B, double rcond)
|
| {
|
|
|
| if (this._dgelsy == null) this._dgelsy = new DGELSY();
|
|
|
| Matrix AClon = A.Clone();
|
| double[] AClonData = AClon.Data;
|
|
|
| Matrix ClonB = new Matrix(Math.Max(A.RowCount, A.ColumnCount), B.ColumnCount);
|
|
|
| double[] ClonData = ClonB.Data;
|
|
|
| for (int j = 0; j < ClonB.ColumnCount; j++)
|
| {
|
| for (int i = 0; i < B.RowCount; i++)
|
| {
|
| ClonB[i, j] = B[i, j];
|
| }
|
| }
|
|
|
| int Info = 0;
|
|
|
| double[] Work = new double[1];
|
| int[] JPVT = new int[A.ColumnCount];
|
|
|
| int RANK = 1;
|
| int LWork = -1;
|
|
|
| this._dgelsy.Run(A.RowCount, A.ColumnCount, B.ColumnCount, ref AClonData, 0, A.RowCount, ref ClonData, 0, ClonB.RowCount, ref JPVT, 0, rcond, ref RANK, ref Work, 0, LWork, ref Info);
|
| LWork = Convert.ToInt32(Work[0]);
|
| if (LWork > 0)
|
| {
|
| Work = new double[LWork];
|
| this._dgelsy.Run(A.RowCount, A.ColumnCount, B.ColumnCount, ref AClonData, 0, A.RowCount, ref ClonData, 0, ClonB.RowCount, ref JPVT, 0, rcond, ref RANK, ref Work, 0, LWork, ref Info);
|
| }
|
| else
|
| {
|
|
|
|
|
| }
|
|
|
|
|
| #region Error
|
|
|
|
|
|
|
|
|
| if (Info < 0)
|
| {
|
| string infoSTg = Math.Abs(Info).ToString();
|
| throw new ArgumentException("the " + infoSTg + " -th argument had an illegal value");
|
| }
|
| else if (Info > 0)
|
| {
|
|
|
|
|
| string infoSTg = Math.Abs(Info).ToString();
|
| throw new Exception("Error The algorithm ... ");
|
| }
|
| #endregion
|
|
|
| Matrix Solution = new Matrix(A.ColumnCount, B.ColumnCount);
|
|
|
| for (int j = 0; j < Solution.ColumnCount; j++)
|
| {
|
| for (int i = 0; i < Solution.RowCount; i++)
|
| {
|
| Solution[i, j] = ClonB[i, j];
|
| }
|
| }
|
|
|
| return Solution;
|
|
|
|
|
| }
|
|
|
| #endregion
|
|
|
| #region Singular value decomposition (SVD) of A.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| public Matrix SVDdcSolve(Matrix A, Matrix B)
|
| {
|
|
|
|
|
|
|
|
|
|
|
| double RCond = -1.0;
|
|
|
| return this.SVDdcSolve(A, B, RCond);
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| public Matrix SVDdcSolve(Matrix A, Matrix B, double rcond)
|
| {
|
|
|
| if (this._dgelsd == null) this._dgelsd = new DGELSD();
|
|
|
| Matrix AClon = A.Clone();
|
| double[] AClonData = AClon.Data;
|
| Matrix ClonB = new Matrix(Math.Max(A.RowCount, A.ColumnCount), B.ColumnCount);
|
| double[] ClonData = ClonB.Data;
|
|
|
| for (int j = 0; j < ClonB.ColumnCount; j++)
|
| {
|
| for (int i = 0; i < B.RowCount; i++)
|
| {
|
| ClonB[i, j] = B[i, j];
|
| }
|
| }
|
|
|
|
|
|
|
|
|
| double[] S = new double[Math.Min(A.RowCount, A.ColumnCount)];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| int Rank = 0;
|
| int Info = 0;
|
|
|
| double[] Work = new double[1];
|
| int LWork = -1;
|
| int LIWork = this.CalculateLIWORK(A.RowCount, A.ColumnCount);
|
| int[] IWork = new int[LIWork];
|
|
|
|
|
| this._dgelsd.Run(A.RowCount, A.ColumnCount, B.ColumnCount, ref AClonData, 0, A.RowCount, ref ClonData, 0, ClonB.RowCount, ref S, 0, rcond, ref Rank, ref Work, 0, LWork, ref IWork, 0, ref Info);
|
| LWork = Convert.ToInt32(Work[0]);
|
| if (LWork > 0)
|
| {
|
| Work = new double[LWork];
|
| this._dgelsd.Run(A.RowCount, A.ColumnCount, B.ColumnCount, ref AClonData, 0, A.RowCount, ref ClonData, 0, ClonB.RowCount, ref S, 0, rcond, ref Rank, ref Work, 0, LWork, ref IWork, 0, ref Info);
|
| }
|
| else
|
| {
|
|
|
|
|
| }
|
|
|
|
|
| #region Error
|
|
|
|
|
|
|
|
|
|
|
| if (Info < 0)
|
| {
|
| string infoSTg = Math.Abs(Info).ToString();
|
| throw new ArgumentException("the " + infoSTg + " -th argument had an illegal value");
|
| }
|
| else if (Info > 0)
|
| {
|
| string infoSTg = Math.Abs(Info).ToString();
|
| throw new Exception("The algorithm for computing the SVD failed to converge.");
|
| }
|
|
|
| #endregion
|
|
|
|
|
|
|
|
|
| Matrix Solution = new Matrix(A.ColumnCount, B.ColumnCount);
|
|
|
| for (int j = 0; j < Solution.ColumnCount; j++)
|
| {
|
| for (int i = 0; i < Solution.RowCount; i++)
|
| {
|
| Solution[i, j] = ClonB[i, j];
|
| }
|
| }
|
|
|
| return Solution;
|
|
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| private int CalculateLIWORK(int ARows, int AColumns)
|
| {
|
| int LIwork = 0;
|
|
|
| double TWO = 2.0;
|
| ILAENV SubILAENV = new ILAENV();
|
|
|
| int MinMN = Math.Min(ARows, AColumns);
|
| MinMN = Math.Min(1, MinMN);
|
|
|
| int SMLSIZ = SubILAENV.Run(9, "DGELSD", " ", 0, 0, 0, 0);
|
|
|
| int NLVL = Math.Max(Convert.ToInt32(Math.Truncate(Math.Log(Convert.ToDouble(MinMN) / Convert.ToDouble(SMLSIZ + 1)) / Math.Log(TWO))) + 1, 0);
|
|
|
| LIwork = 3 * MinMN * NLVL + 11 + MinMN;
|
|
|
| return LIwork;
|
| }
|
|
|
|
|
| #endregion
|
|
|
|
|
|
|
|
|
| #endregion
|
|
|
| }
|
|
|
|
|
| }
|
|
|