| | #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 SingularValueDecomposition
|
| | {
|
| |
|
| | #region Fields
|
| |
|
| | DGESVD _dgesvd;
|
| |
|
| | #endregion
|
| |
|
| |
|
| |
|
| |
|
| | public SingularValueDecomposition()
|
| | {
|
| |
|
| |
|
| |
|
| | }
|
| |
|
| | #region Public Metods
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | public void ComputeSVD(Matrix A, out Matrix S)
|
| | {
|
| |
|
| | Vector singularValues;
|
| |
|
| | S = new Matrix(A.RowCount, A.ColumnCount);
|
| |
|
| | this.ComputeSVD(A, out singularValues);
|
| |
|
| | for (int i = 0; i < singularValues.Length; i++)
|
| | {
|
| | S[i, i] = singularValues[i];
|
| | }
|
| | }
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | public void ComputeSVD(Matrix A, out Vector S)
|
| | {
|
| |
|
| | if (this._dgesvd == null) this._dgesvd = new DGESVD();
|
| |
|
| | Matrix ACopy = A.Clone();
|
| | double[] ACopyData = ACopy.Data;
|
| | S = new Vector(Math.Min(A.RowCount, A.ColumnCount));
|
| | double[] SingularValuesData = S.Data;
|
| | Matrix U = new Matrix(1, 1);
|
| | double[] UData = U.Data;
|
| | Matrix VT = new Matrix(1, 1);
|
| | double[] VTData = VT.Data;
|
| |
|
| | double[] Work = new double[1];
|
| | int LWork = -1;
|
| | int Info = 0;
|
| |
|
| |
|
| | this._dgesvd.Run("N", "N", A.RowCount, A.ColumnCount, ref ACopyData, 0, A.RowCount, ref SingularValuesData, 0, ref UData, 0, U.RowCount, ref VTData, 0, VT.RowCount, ref Work, 0, LWork, ref Info);
|
| |
|
| | LWork = Convert.ToInt32(Work[0]);
|
| | if (LWork > 0)
|
| | {
|
| | Work = new double[LWork];
|
| | _dgesvd.Run("N", "N", A.RowCount, A.ColumnCount, ref ACopyData, 0, A.RowCount, ref SingularValuesData, 0, ref UData, 0, U.RowCount, ref VTData, 0, VT.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("DBDSQR did not converge.");
|
| | }
|
| |
|
| | #endregion
|
| |
|
| | }
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | public void ComputeSVD(Matrix A, out Matrix S, out Matrix U, out Matrix VT)
|
| | {
|
| |
|
| | if (this._dgesvd == null) this._dgesvd = new DGESVD();
|
| |
|
| | Matrix ACopy = A.Clone();
|
| | double[] ACopyData = ACopy.Data;
|
| | S = new Matrix(A.RowCount, A.ColumnCount);
|
| | double[] SingularValuesData = new double[Math.Min(A.RowCount, A.ColumnCount)];
|
| | U = new Matrix(A.RowCount, A.RowCount);
|
| | double[] UData = U.Data;
|
| | VT = new Matrix(A.ColumnCount, A.ColumnCount);
|
| | double[] VTData = VT.Data;
|
| |
|
| | double[] Work = new double[1];
|
| | int LWork = -1;
|
| | int Info = 0;
|
| |
|
| |
|
| | this._dgesvd.Run("A", "A", A.RowCount, A.ColumnCount, ref ACopyData, 0, A.RowCount, ref SingularValuesData, 0, ref UData, 0, U.RowCount, ref VTData, 0, VT.RowCount, ref Work, 0, LWork, ref Info);
|
| |
|
| | LWork = Convert.ToInt32(Work[0]);
|
| | if (LWork > 0)
|
| | {
|
| | Work = new double[LWork];
|
| | _dgesvd.Run("A", "A", A.RowCount, A.ColumnCount, ref ACopyData, 0, A.RowCount, ref SingularValuesData, 0, ref UData, 0, U.RowCount, ref VTData, 0, VT.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("DBDSQR did not converge.");
|
| | }
|
| |
|
| | #endregion
|
| |
|
| | for (int i = 0; i < SingularValuesData.Length; i++)
|
| | {
|
| | S[i, i] = SingularValuesData[i];
|
| | }
|
| |
|
| | }
|
| |
|
| | #endregion
|
| |
|
| | }
|
| | }
|
| |
|