|
From: <in...@dn...> - 2006-01-20 11:10:55
|
Author: marcus
Date: 2006-01-20 06:10:32 -0500 (Fri, 20 Jan 2006)
New Revision: 180
Added:
trunk/LinearAlgebra/Decomposition/DenseSvd.cs
trunk/LinearAlgebra/Decomposition/ISvd.cs
trunk/LinearAlgebra/Decomposition/Svd.cs
Modified:
trunk/LinearAlgebra/Decomposition/Cholesky.cs
trunk/LinearAlgebra/Decomposition/DenseCholesky.cs
trunk/LinearAlgebra/Decomposition/DenseLU.cs
trunk/LinearAlgebra/Decomposition/DenseQR.cs
trunk/NumericalLibrary.csproj
Log:
minor correction to LU, Cholesky, and QR constructors.
added SVD interface and a stub for DenseSvd.
Modified: trunk/LinearAlgebra/Decomposition/Cholesky.cs
===================================================================
--- trunk/LinearAlgebra/Decomposition/Cholesky.cs 2006-01-19 09:07:07 UTC (rev 179)
+++ trunk/LinearAlgebra/Decomposition/Cholesky.cs 2006-01-20 11:10:32 UTC (rev 180)
@@ -42,7 +42,6 @@
throw new NotSquareMatrixException(Strings.MustBeSquare);
}
-
decomp = new DenseCholesky(matrix);
order = matrix.Rows;
Modified: trunk/LinearAlgebra/Decomposition/DenseCholesky.cs
===================================================================
--- trunk/LinearAlgebra/Decomposition/DenseCholesky.cs 2006-01-19 09:07:07 UTC (rev 179)
+++ trunk/LinearAlgebra/Decomposition/DenseCholesky.cs 2006-01-20 11:10:32 UTC (rev 180)
@@ -32,7 +32,7 @@
throw new NotSquareMatrixException(Strings.MustBeSquare);
}
- this.matrix = (DenseMatrix)matrix.Clone();
+ this.matrix = new DenseMatrix(matrix);
}
#endregion Constructor
Modified: trunk/LinearAlgebra/Decomposition/DenseLU.cs
===================================================================
--- trunk/LinearAlgebra/Decomposition/DenseLU.cs 2006-01-19 09:07:07 UTC (rev 179)
+++ trunk/LinearAlgebra/Decomposition/DenseLU.cs 2006-01-20 11:10:32 UTC (rev 180)
@@ -38,14 +38,17 @@
/// <param name="matrix">The matrix to use.</param>
public DenseLU(Matrix matrix)
{
- if (matrix is DenseMatrix)
+ if (matrix == null)
{
- this.matrix = (DenseMatrix)matrix.Clone();
+ throw new ArgumentNullException("matrix", Strings.NullParameterException);
}
- else
+
+ if (matrix.Rows != matrix.Columns)
{
- this.matrix = new DenseMatrix(matrix);
+ throw new NotSquareMatrixException(Strings.MustBeSquare);
}
+
+ this.matrix = new DenseMatrix(matrix);
}
#endregion Constructors
Modified: trunk/LinearAlgebra/Decomposition/DenseQR.cs
===================================================================
--- trunk/LinearAlgebra/Decomposition/DenseQR.cs 2006-01-19 09:07:07 UTC (rev 179)
+++ trunk/LinearAlgebra/Decomposition/DenseQR.cs 2006-01-20 11:10:32 UTC (rev 180)
@@ -35,7 +35,8 @@
{
throw new ArgumentNullException("matrix", Strings.NullParameterException);
}
- this.r = (DenseMatrix)matrix.Clone();
+
+ this.r = new DenseMatrix(matrix);
}
#endregion Constructor
Added: trunk/LinearAlgebra/Decomposition/DenseSvd.cs
===================================================================
--- trunk/LinearAlgebra/Decomposition/DenseSvd.cs 2006-01-19 09:07:07 UTC (rev 179)
+++ trunk/LinearAlgebra/Decomposition/DenseSvd.cs 2006-01-20 11:10:32 UTC (rev 180)
@@ -0,0 +1,210 @@
+/*
+* DenseSvd.cs
+*
+* Copyright (c) 2005, dnAnalytics. All rights reserved.
+*/
+
+#region Using Directives
+using System;
+using dnAnalytics.LinearAlgebra;
+using dnAnalytics.Math;
+using dnAnalytics.Exceptions;
+using dnAnalytics.Resources;
+#endregion
+
+namespace dnAnalytics.LinearAlgebra.Decomposition
+{
+ /// <summary>
+ /// Class for computing the Singular Value Decomposition of a <see cref="Matrix"/>.
+ /// </summary>
+ public sealed class DenseSvd : Algorithm
+ {
+ #region Fields
+ private DenseMatrix matrix;
+ #endregion Fields
+
+ #region Constructors
+ /// <summary>
+ /// Constructs an QR object for the given matrix.
+ /// </summary>
+ /// <param name="matrix">The matrix to factor.</param>
+ /// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception>
+ public DenseSvd(Matrix matrix)
+ {
+ if (matrix == null)
+ {
+ throw new ArgumentNullException("matrix", Strings.NullParameterException);
+ }
+
+ this.matrix = new DenseMatrix(matrix);
+ }
+
+ #endregion Constructors
+
+ #region Properties
+
+ ///<summary>Returns the two norm of the <see cref="Matrix"/>.</summary>
+ ///<value>The two norm of the <see cref="Matrix"/>.</value>
+ public double Norm2
+ {
+ get
+ {
+ Compute();
+ throw new NotImplementedException();
+ }
+ }
+
+ ///<summary>Returns the condition number <c>max(S) / min(S)</c>.</summary>
+ ///<value>The condition number.</value>
+ public double Condition
+ {
+ get
+ {
+ Compute();
+ throw new NotImplementedException();
+ }
+ }
+
+ ///<summary>Returns the effective numerical matrix rank.</summary>
+ ///<value>The number of non-negligible singular values.</value>
+ public int Rank
+ {
+ get
+ {
+ Compute();
+ throw new NotImplementedException();
+ }
+ }
+ #endregion Properties
+
+ #region Public Methods
+
+ ///<summary>Returns the left singular vectors as a <see cref="Matrix"/>.</summary>
+ ///<returns>The left singular vectors. The matrix will be <c>null</c>,
+ ///if <c>computeVectors</c> in the constructor is set to false.</returns>
+ public Matrix U()
+ {
+ Compute();
+ throw new NotImplementedException();
+ }
+
+ /// <summary>
+ /// Copies the left singular vectors into the <paramref name="result"/> <see cref="Matrix"/>.
+ /// </summary>
+ /// <param name="result">The <see cref="Matrix"/> to copy the left singular vectors into.</param>
+ /// <exception cref="ArgumentNullException">If <paramref name="result"/> is <c>null</c>.</exception>
+ /// <exception cref="NotConformableException">If <paramref name="result"/> doesn't have conforming dimensions.</exception>
+ /// <remarks>The method does nothing if <c>computeVectors</c> in the constructor is set to false.</remarks>
+ public void U(Matrix result)
+ {
+ if (result == null)
+ {
+ throw new ArgumentNullException("result", Strings.NullParameterException);
+ }
+ if (result.Rows != matrix.Rows || result.Columns != matrix.Columns)
+ {
+ throw new NotConformableException("result", Strings.ParameterNotConformable);
+ }
+ Compute();
+ throw new NotImplementedException();
+ }
+
+ ///<summary>Returns the right singular vectors as a <see cref="Matrix"/>.</summary>
+ ///<returns>The right singular vectors. The matrix will be <c>null</c>,
+ ///if <c>computeVectors</c> in the constructor is set to false.</returns>
+ public Matrix V()
+ {
+ Compute();
+ throw new NotImplementedException();
+ }
+
+ /// <summary>
+ /// Copies the right singular vectors into the <paramref name="result"/> <see cref="Matrix"/>.
+ /// </summary>
+ /// <param name="result">The <see cref="Matrix"/> to copy the right singular vectors into.</param>
+ /// <exception cref="ArgumentNullException">If <paramref name="result"/> is <c>null</c>.</exception>
+ /// <exception cref="NotConformableException">If <paramref name="result"/> doesn't have conforming dimensions.</exception>
+ /// <remarks>The method does nothing if <c>computeVectors</c> in the constructor is set to false.</remarks>
+ public void V(Matrix result)
+ {
+ if (result == null)
+ {
+ throw new ArgumentNullException("result", Strings.NullParameterException);
+ }
+ if (result.Rows != matrix.Rows || result.Columns != matrix.Columns)
+ {
+ throw new NotConformableException("result", Strings.ParameterNotConformable);
+ }
+ Compute();
+ throw new NotImplementedException();
+ }
+
+ ///<summary>Returns the singular values as a diagonal <see cref="Matrix"/>.</summary>
+ ///<returns>The singular values as a diagonal <see cref="Matrix"/>.</returns>
+ public Matrix W()
+ {
+ Compute();
+ throw new NotImplementedException();
+ }
+
+ /// <summary>
+ /// Resets the <paramref name="result"/> <see cref="Matrix"/> and copies the singular values onto the diagonal.
+ /// </summary>
+ /// <param name="result"><see cref="Matrix"/> to copy the singular values to.</param>
+ /// <exception cref="ArgumentNullException">If <paramref name="result"/> is <c>null</c>.</exception>
+ /// <exception cref="NotConformableException">If <paramref name="result"/> doesn't have conforming dimensions.</exception>
+ public void W(Matrix result)
+ {
+ if (result == null)
+ {
+ throw new ArgumentNullException("result", Strings.NullParameterException);
+ }
+ if (result.Rows != matrix.Rows || result.Columns != matrix.Columns)
+ {
+ throw new NotConformableException("result", Strings.ParameterNotConformable);
+ }
+ Compute();
+ throw new NotImplementedException();
+ }
+
+ ///<summary>Returns the singular values as a <see cref="Vector"/>.</summary>
+ ///<returns>the singular values as a <see cref="Vector"/>.</returns>
+ public Vector S()
+ {
+ Compute();
+ throw new NotImplementedException();
+ }
+
+ /// <summary>
+ /// Copies the singular values to the given <see cref="Vector"/>.
+ /// </summary>
+ /// <param name="result">The <see cref="Vector"/> to copy the singular values into.</param>
+ /// <exception cref="ArgumentNullException">If <paramref name="result"/> is <c>null</c>.</exception>
+ /// <exception cref="NotConformableException">If <paramref name="result"/> doesn't have conforming dimensions.</exception>
+ public void S(Vector result)
+ {
+ if (result == null)
+ {
+ throw new ArgumentNullException("result", Strings.NullParameterException);
+ }
+ //if (result.Rows != rows || result.Columns != rows)
+ //{
+ // throw new NotConformableException("result", Strings.ParameterNotConformable);
+ // }
+
+ Compute();
+ throw new NotImplementedException();
+ }
+ #endregion Public Methods
+
+ #region Protected Members
+ /// <summary>
+ /// Delegates the actual computation to the correct QR subtype.
+ /// </summary>
+ protected override void InternalCompute()
+ {
+
+ }
+ #endregion Protected Members
+ }
+}
Added: trunk/LinearAlgebra/Decomposition/ISvd.cs
===================================================================
--- trunk/LinearAlgebra/Decomposition/ISvd.cs 2006-01-19 09:07:07 UTC (rev 179)
+++ trunk/LinearAlgebra/Decomposition/ISvd.cs 2006-01-20 11:10:32 UTC (rev 180)
@@ -0,0 +1,86 @@
+/*
+* ISvd.cs
+*
+* Copyright (c) 2005, dnAnalytics. All rights reserved.
+*/
+
+#region Using Directives
+using System;
+using dnAnalytics.LinearAlgebra;
+using dnAnalytics.Math;
+using dnAnalytics.Exceptions;
+#endregion
+
+namespace dnAnalytics.LinearAlgebra.Decomposition
+{
+ /// <summary>
+ /// Interface for computing the Singular Value Decomposition of a <see cref="Matrix"/>.
+ /// </summary>
+ internal interface ISvd : IAlgorithm
+ {
+ ///<summary>Returns the left singular vectors.</summary>
+ ///<returns>The left singular vectors. The vectors will be <c>null</c>,
+ ///if <c>computeVectors</c> in the constructor is set to false.</returns>
+ Matrix U();
+
+ /// <summary>
+ /// Copies the left singular vectors into the <paramref name="result"/> <see cref="Matrix"/>.
+ /// </summary>
+ /// <param name="result">The <see cref="Matrix"/> to copy the left singular vectors into.</param>
+ /// <exception cref="ArgumentNullException">If <paramref name="result"/> is <c>null</c>.</exception>
+ /// <exception cref="NotConformableException">If <paramref name="result"/> doesn't have conforming dimensions.</exception>
+ /// <remarks>The method does nothing if <c>computeVectors</c> in the constructor is set to false.</remarks>
+ void U(Matrix result);
+
+ ///<summary>Returns the right singular vectors.</summary>
+ ///<returns>The right singular vectors. The vectors will be <c>null</c>,
+ ///if <c>computeVectors</c> in the constructor is set to false.</returns>
+ Matrix V();
+
+ /// <summary>
+ /// Copies the right singular vectors into the <paramref name="result"/> <see cref="Matrix"/>.
+ /// </summary>
+ /// <param name="result">The <see cref="Matrix"/> to copy the right singular vectors into.</param>
+ /// <exception cref="ArgumentNullException">If <paramref name="result"/> is <c>null</c>.</exception>
+ /// <exception cref="NotConformableException">If <paramref name="result"/> doesn't have conforming dimensions.</exception>
+ /// <remarks>The method does nothing if <c>computeVectors</c> in the constructor is set to false.</remarks>
+ void V(Matrix result);
+
+ ///<summary>Returns the singular values as a diagonal <see cref="Matrix"/>.</summary>
+ ///<returns>The singular values as a diagonal <see cref="Matrix"/>.</returns>
+ Matrix W();
+
+ /// <summary>
+ /// Resets the <paramref name="result"/> <see cref="Matrix"/> and copies the singular values onto the diagonal.
+ /// </summary>
+ /// <param name="result"><see cref="Matrix"/> to copy the singular values to.</param>
+ /// <exception cref="ArgumentNullException">If <paramref name="result"/> is <c>null</c>.</exception>
+ /// <exception cref="NotConformableException">If <paramref name="result"/> doesn't have conforming dimensions.</exception>
+ void W(Matrix result);
+
+ ///<summary>Returns the singular values as a <see cref="Vector"/>.</summary>
+ ///<returns>the singular values as a <see cref="Vector"/>.</returns>
+ Vector S();
+
+ /// <summary>
+ /// Copies the singular values to the given <see cref="Vector"/>.
+ /// </summary>
+ /// <param name="result">The <see cref="Vector"/> to copy the singular values into.</param>
+ /// <exception cref="ArgumentNullException">If <paramref name="result"/> is <c>null</c>.</exception>
+ /// <exception cref="NotConformableException">If <paramref name="result"/> doesn't have conforming dimensions.</exception>
+ void S(Vector result);
+
+ ///<summary>Returns the two norm of the <see cref="Matrix"/>.</summary>
+ ///<value>The two norm of the <see cref="Matrix"/>.</value>
+ double Norm2 { get; }
+
+ ///<summary>Returns the condition number <c>max(S) / min(S)</c>.</summary>
+ ///<value>The condition number.</value>
+ double Condition { get; }
+
+ ///<summary>Returns the effective numerical matrix rank.</summary>
+ ///<value>The number of non-negligible singular values.</value>
+ int Rank { get; }
+
+ }
+}
Added: trunk/LinearAlgebra/Decomposition/Svd.cs
===================================================================
--- trunk/LinearAlgebra/Decomposition/Svd.cs 2006-01-19 09:07:07 UTC (rev 179)
+++ trunk/LinearAlgebra/Decomposition/Svd.cs 2006-01-20 11:10:32 UTC (rev 180)
@@ -0,0 +1,205 @@
+/*
+* Svd.cs
+*
+* Copyright (c) 2005, dnAnalytics. All rights reserved.
+*/
+
+#region Using Directives
+using System;
+using dnAnalytics.LinearAlgebra;
+using dnAnalytics.Math;
+using dnAnalytics.Exceptions;
+using dnAnalytics.Resources;
+#endregion
+
+namespace dnAnalytics.LinearAlgebra.Decomposition
+{
+ /// <summary>
+ /// Class for computing the Singular Value Decomposition of a <see cref="Matrix"/>.
+ /// </summary>
+ public sealed class Svd : Algorithm
+ {
+ #region Fields
+ private ISvd decomp;
+ private readonly int rows;
+ private readonly int columns;
+ #endregion Fields
+
+
+ #region Constructors
+ /// <summary>
+ /// Constructs an QR object for the given matrix.
+ /// </summary>
+ /// <param name="matrix">The matrix to factor.</param>
+ /// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception>
+ public Svd(Matrix matrix)
+ {
+ if (matrix == null)
+ {
+ throw new ArgumentNullException("matrix", Strings.NullParameterException);
+ }
+
+ //decomp = new DenseSvd(matrix);
+ rows = matrix.Rows;
+ columns = matrix.Columns;
+ }
+
+ #endregion Constructors
+
+ #region Properties
+
+ ///<summary>Returns the two norm of the <see cref="Matrix"/>.</summary>
+ ///<value>The two norm of the <see cref="Matrix"/>.</value>
+ public double Norm2
+ {
+ get
+ {
+ return decomp.Norm2;
+ }
+ }
+
+ ///<summary>Returns the condition number <c>max(S) / min(S)</c>.</summary>
+ ///<value>The condition number.</value>
+ public double Condition
+ {
+ get
+ {
+ return decomp.Condition;
+ }
+ }
+
+ ///<summary>Returns the effective numerical matrix rank.</summary>
+ ///<value>The number of non-negligible singular values.</value>
+ public int Rank
+ {
+ get
+ {
+ return decomp.Rank;
+ }
+ }
+ #endregion Properties
+
+ #region Public Methods
+
+ ///<summary>Returns the left singular vectors as a <see cref="Matrix"/>.</summary>
+ ///<returns>The left singular vectors. The matrix will be <c>null</c>,
+ ///if <c>computeVectors</c> in the constructor is set to false.</returns>
+ public Matrix U()
+ {
+ return decomp.U();
+ }
+
+ /// <summary>
+ /// Copies the left singular vectors into the <paramref name="result"/> <see cref="Matrix"/>.
+ /// </summary>
+ /// <param name="result">The <see cref="Matrix"/> to copy the left singular vectors into.</param>
+ /// <exception cref="ArgumentNullException">If <paramref name="result"/> is <c>null</c>.</exception>
+ /// <exception cref="NotConformableException">If <paramref name="result"/> doesn't have conforming dimensions.</exception>
+ /// <remarks>The method does nothing if <c>computeVectors</c> in the constructor is set to false.</remarks>
+ public void U(Matrix result)
+ {
+ if (result == null)
+ {
+ throw new ArgumentNullException("result", Strings.NullParameterException);
+ }
+ if (result.Rows != rows || result.Columns != columns)
+ {
+ throw new NotConformableException("result", Strings.ParameterNotConformable);
+ }
+ decomp.U(result);
+ }
+
+ ///<summary>Returns the right singular vectors as a <see cref="Matrix"/>.</summary>
+ ///<returns>The right singular vectors. The matrix will be <c>null</c>,
+ ///if <c>computeVectors</c> in the constructor is set to false.</returns>
+ public Matrix V()
+ {
+ return decomp.V();
+ }
+
+ /// <summary>
+ /// Copies the right singular vectors into the <paramref name="result"/> <see cref="Matrix"/>.
+ /// </summary>
+ /// <param name="result">The <see cref="Matrix"/> to copy the right singular vectors into.</param>
+ /// <exception cref="ArgumentNullException">If <paramref name="result"/> is <c>null</c>.</exception>
+ /// <exception cref="NotConformableException">If <paramref name="result"/> doesn't have conforming dimensions.</exception>
+ /// <remarks>The method does nothing if <c>computeVectors</c> in the constructor is set to false.</remarks>
+ public void V(Matrix result)
+ {
+ if (result == null)
+ {
+ throw new ArgumentNullException("result", Strings.NullParameterException);
+ }
+ if (result.Rows != rows || result.Columns != columns)
+ {
+ throw new NotConformableException("result", Strings.ParameterNotConformable);
+ }
+ decomp.V(result);
+ }
+
+ ///<summary>Returns the singular values as a diagonal <see cref="Matrix"/>.</summary>
+ ///<returns>The singular values as a diagonal <see cref="Matrix"/>.</returns>
+ public Matrix W()
+ {
+ return decomp.W();
+ }
+
+ /// <summary>
+ /// Resets the <paramref name="result"/> <see cref="Matrix"/> and copies the singular values onto the diagonal.
+ /// </summary>
+ /// <param name="result"><see cref="Matrix"/> to copy the singular values to.</param>
+ /// <exception cref="ArgumentNullException">If <paramref name="result"/> is <c>null</c>.</exception>
+ /// <exception cref="NotConformableException">If <paramref name="result"/> doesn't have conforming dimensions.</exception>
+ public void W(Matrix result)
+ {
+ if (result == null)
+ {
+ throw new ArgumentNullException("result", Strings.NullParameterException);
+ }
+ if (result.Rows != rows || result.Columns != columns)
+ {
+ throw new NotConformableException("result", Strings.ParameterNotConformable);
+ }
+ decomp.W(result);
+ }
+
+ ///<summary>Returns the singular values as a <see cref="Vector"/>.</summary>
+ ///<returns>the singular values as a <see cref="Vector"/>.</returns>
+ public Vector S()
+ {
+ return decomp.S();
+ }
+
+ /// <summary>
+ /// Copies the singular values to the given <see cref="Vector"/>.
+ /// </summary>
+ /// <param name="result">The <see cref="Vector"/> to copy the singular values into.</param>
+ /// <exception cref="ArgumentNullException">If <paramref name="result"/> is <c>null</c>.</exception>
+ /// <exception cref="NotConformableException">If <paramref name="result"/> doesn't have conforming dimensions.</exception>
+ public void S(Vector result)
+ {
+ if (result == null)
+ {
+ throw new ArgumentNullException("result", Strings.NullParameterException);
+ }
+ //if (result.Rows != rows || result.Columns != rows)
+ //{
+ // throw new NotConformableException("result", Strings.ParameterNotConformable);
+ // }
+
+ decomp.S(result);
+ throw new NotImplementedException();
+ }
+ #endregion Public Methods
+
+ #region Protected Members
+ /// <summary>
+ /// Delegates the actual computation to the correct QR subtype.
+ /// </summary>
+ protected override void InternalCompute()
+ {
+ decomp.Compute();
+ }
+ #endregion Protected Members
+ }
+}
Modified: trunk/NumericalLibrary.csproj
===================================================================
--- trunk/NumericalLibrary.csproj 2006-01-19 09:07:07 UTC (rev 179)
+++ trunk/NumericalLibrary.csproj 2006-01-20 11:10:32 UTC (rev 180)
@@ -62,6 +62,8 @@
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="LinearAlgebra\Decomposition\Cholesky.cs" />
<Compile Include="LinearAlgebra\Decomposition\DenseQR.cs" />
+ <Compile Include="LinearAlgebra\Decomposition\DenseSvd.cs" />
+ <Compile Include="LinearAlgebra\Decomposition\ISvd.cs" />
<Compile Include="LinearAlgebra\Decomposition\QR.cs" />
<Compile Include="LinearAlgebra\Decomposition\Householder.cs">
<SubType>Code</SubType>
@@ -116,6 +118,7 @@
</Compile>
<Compile Include="LinearAlgebra\Range.cs" />
<Compile Include="LinearAlgebra\Solvers\ISolver.cs" />
+ <Compile Include="LinearAlgebra\Decomposition\Svd.cs" />
<Compile Include="LinearAlgebra\Vector.cs" />
<Compile Include="Math\Algorithm.cs" />
<Compile Include="Math\Complex.cs" />
|