Browse Source

LA: Simplify QR decomposition architecture

optimization-1
Christoph Ruegg 13 years ago
parent
commit
5f66808724
  1. 2
      src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs
  2. 4
      src/Numerics/LinearAlgebra/Complex/Factorization/DenseGramSchmidt.cs
  3. 78
      src/Numerics/LinearAlgebra/Complex/Factorization/DenseQR.cs
  4. 12
      src/Numerics/LinearAlgebra/Complex/Factorization/GramSchmidt.cs
  5. 28
      src/Numerics/LinearAlgebra/Complex/Factorization/QR.cs
  6. 12
      src/Numerics/LinearAlgebra/Complex/Factorization/UserGramSchmidt.cs
  7. 145
      src/Numerics/LinearAlgebra/Complex/Factorization/UserQR.cs
  8. 2
      src/Numerics/LinearAlgebra/Complex/Matrix.cs
  9. 2
      src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs
  10. 4
      src/Numerics/LinearAlgebra/Complex32/Factorization/DenseGramSchmidt.cs
  11. 77
      src/Numerics/LinearAlgebra/Complex32/Factorization/DenseQR.cs
  12. 12
      src/Numerics/LinearAlgebra/Complex32/Factorization/GramSchmidt.cs
  13. 27
      src/Numerics/LinearAlgebra/Complex32/Factorization/QR.cs
  14. 12
      src/Numerics/LinearAlgebra/Complex32/Factorization/UserGramSchmidt.cs
  15. 146
      src/Numerics/LinearAlgebra/Complex32/Factorization/UserQR.cs
  16. 2
      src/Numerics/LinearAlgebra/Complex32/Matrix.cs
  17. 2
      src/Numerics/LinearAlgebra/Double/DenseMatrix.cs
  18. 4
      src/Numerics/LinearAlgebra/Double/Factorization/DenseGramSchmidt.cs
  19. 80
      src/Numerics/LinearAlgebra/Double/Factorization/DenseQR.cs
  20. 12
      src/Numerics/LinearAlgebra/Double/Factorization/GramSchmidt.cs
  21. 28
      src/Numerics/LinearAlgebra/Double/Factorization/QR.cs
  22. 12
      src/Numerics/LinearAlgebra/Double/Factorization/UserGramSchmidt.cs
  23. 151
      src/Numerics/LinearAlgebra/Double/Factorization/UserQR.cs
  24. 2
      src/Numerics/LinearAlgebra/Double/Matrix.cs
  25. 3
      src/Numerics/LinearAlgebra/Factorization/GramSchmidt.cs
  26. 52
      src/Numerics/LinearAlgebra/Factorization/QR.cs
  27. 2
      src/Numerics/LinearAlgebra/Single/DenseMatrix.cs
  28. 4
      src/Numerics/LinearAlgebra/Single/Factorization/DenseGramSchmidt.cs
  29. 69
      src/Numerics/LinearAlgebra/Single/Factorization/DenseQR.cs
  30. 12
      src/Numerics/LinearAlgebra/Single/Factorization/GramSchmidt.cs
  31. 28
      src/Numerics/LinearAlgebra/Single/Factorization/QR.cs
  32. 12
      src/Numerics/LinearAlgebra/Single/Factorization/UserGramSchmidt.cs
  33. 148
      src/Numerics/LinearAlgebra/Single/Factorization/UserQR.cs
  34. 2
      src/Numerics/LinearAlgebra/Single/Matrix.cs
  35. 13
      src/UnitTests/LinearAlgebraTests/Complex/Factorization/QRTests.cs
  36. 13
      src/UnitTests/LinearAlgebraTests/Complex/Factorization/UserQRTests.cs
  37. 13
      src/UnitTests/LinearAlgebraTests/Complex32/Factorization/QRTests.cs
  38. 13
      src/UnitTests/LinearAlgebraTests/Complex32/Factorization/UserQRTests.cs
  39. 13
      src/UnitTests/LinearAlgebraTests/Double/Factorization/QRTests.cs
  40. 13
      src/UnitTests/LinearAlgebraTests/Double/Factorization/UserQRTests.cs
  41. 13
      src/UnitTests/LinearAlgebraTests/Single/Factorization/QRTests.cs
  42. 13
      src/UnitTests/LinearAlgebraTests/Single/Factorization/UserQRTests.cs

2
src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs

@ -1021,7 +1021,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
public override QR<Complex> QR(QRMethod method = QRMethod.Thin) public override QR<Complex> QR(QRMethod method = QRMethod.Thin)
{ {
return new DenseQR(this, method); return DenseQR.Create(this, method);
} }
public override GramSchmidt<Complex> GramSchmidt() public override GramSchmidt<Complex> GramSchmidt()

4
src/Numerics/LinearAlgebra/Complex/Factorization/DenseGramSchmidt.cs

@ -164,7 +164,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
throw new NotSupportedException("Can only do GramSchmidt factorization for dense matrices at the moment."); throw new NotSupportedException("Can only do GramSchmidt factorization for dense matrices at the moment.");
} }
Control.LinearAlgebraProvider.QRSolveFactored(((DenseMatrix)Q).Values, ((DenseMatrix)MatrixR).Values, Q.RowCount, MatrixR.ColumnCount, null, dinput.Values, input.ColumnCount, dresult.Values, QRMethod.Thin); Control.LinearAlgebraProvider.QRSolveFactored(((DenseMatrix)Q).Values, ((DenseMatrix)FullR).Values, Q.RowCount, FullR.ColumnCount, null, dinput.Values, input.ColumnCount, dresult.Values, QRMethod.Thin);
} }
/// <summary> /// <summary>
@ -199,7 +199,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
throw new NotSupportedException("Can only do GramSchmidt factorization for dense vectors at the moment."); throw new NotSupportedException("Can only do GramSchmidt factorization for dense vectors at the moment.");
} }
Control.LinearAlgebraProvider.QRSolveFactored(((DenseMatrix)Q).Values, ((DenseMatrix)MatrixR).Values, Q.RowCount, MatrixR.ColumnCount, null, dinput.Values, 1, dresult.Values, QRMethod.Thin); Control.LinearAlgebraProvider.QRSolveFactored(((DenseMatrix)Q).Values, ((DenseMatrix)FullR).Values, Q.RowCount, FullR.ColumnCount, null, dinput.Values, 1, dresult.Values, QRMethod.Thin);
} }
} }
} }

78
src/Numerics/LinearAlgebra/Complex/Factorization/DenseQR.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// //
// Copyright (c) 2009-2010 Math.NET // Copyright (c) 2009-2013 Math.NET
// //
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
@ -28,9 +28,9 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Factorization;
using MathNet.Numerics.Properties; using MathNet.Numerics.Properties;
using System;
namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
{ {
@ -39,6 +39,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
using Numerics; using Numerics;
#else #else
using System.Numerics; using System.Numerics;
#endif #endif
/// <summary> /// <summary>
@ -50,16 +51,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
/// <remarks> /// <remarks>
/// The computation of the QR decomposition is done at construction time by Householder transformation. /// The computation of the QR decomposition is done at construction time by Householder transformation.
/// </remarks> /// </remarks>
public class DenseQR : QR public sealed class DenseQR : QR
{ {
/// <summary> /// <summary>
/// Gets or sets Tau vector. Contains additional information on Q - used for native solver. /// Gets or sets Tau vector. Contains additional information on Q - used for native solver.
/// </summary> /// </summary>
public Complex[] Tau Complex[] Tau { get; set; }
{
get;
set;
}
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="DenseQR"/> class. This object will compute the /// Initializes a new instance of the <see cref="DenseQR"/> class. This object will compute the
@ -69,35 +66,37 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
/// <param name="method">The type of QR factorization to perform.</param> /// <param name="method">The type of QR factorization to perform.</param>
/// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">If <paramref name="matrix"/> row count is less then column count</exception> /// <exception cref="ArgumentException">If <paramref name="matrix"/> row count is less then column count</exception>
public DenseQR(DenseMatrix matrix, QRMethod method = QRMethod.Full) public static DenseQR Create(DenseMatrix matrix, QRMethod method = QRMethod.Full)
{ {
if (matrix == null)
{
throw new ArgumentNullException("matrix");
}
if (matrix.RowCount < matrix.ColumnCount) if (matrix.RowCount < matrix.ColumnCount)
{ {
throw Matrix.DimensionsDontMatch<ArgumentException>(matrix); throw Matrix.DimensionsDontMatch<ArgumentException>(matrix);
} }
QrMethod = method; var tau = new Complex[Math.Min(matrix.RowCount, matrix.ColumnCount)];
Tau = new Complex[Math.Min(matrix.RowCount, matrix.ColumnCount)]; Matrix<Complex> q;
Matrix<Complex> r;
if (method == QRMethod.Full) if (method == QRMethod.Full)
{ {
MatrixR = matrix.Clone(); r = matrix.Clone();
Q = new DenseMatrix(matrix.RowCount); q = new DenseMatrix(matrix.RowCount);
Control.LinearAlgebraProvider.QRFactor(((DenseMatrix)MatrixR).Values, matrix.RowCount, matrix.ColumnCount, Control.LinearAlgebraProvider.QRFactor(((DenseMatrix) r).Values, matrix.RowCount, matrix.ColumnCount, ((DenseMatrix) q).Values, tau);
((DenseMatrix)Q).Values, Tau);
} }
else else
{ {
Q = matrix.Clone(); q = matrix.Clone();
MatrixR = new DenseMatrix(matrix.ColumnCount); r = new DenseMatrix(matrix.ColumnCount);
Control.LinearAlgebraProvider.ThinQRFactor(((DenseMatrix)Q).Values, matrix.RowCount, matrix.ColumnCount, Control.LinearAlgebraProvider.ThinQRFactor(((DenseMatrix) q).Values, matrix.RowCount, matrix.ColumnCount, ((DenseMatrix) r).Values, tau);
((DenseMatrix)MatrixR).Values, Tau);
} }
return new DenseQR(q, r, method, tau);
}
DenseQR(Matrix<Complex> q, Matrix<Complex> rFull, QRMethod method, Complex[] tau)
: base(q, rFull, method)
{
Tau = tau;
} }
/// <summary> /// <summary>
@ -107,17 +106,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
/// <param name="result">The left hand side <see cref="Matrix{T}"/>, <b>X</b>.</param> /// <param name="result">The left hand side <see cref="Matrix{T}"/>, <b>X</b>.</param>
public override void Solve(Matrix<Complex> input, Matrix<Complex> result) public override void Solve(Matrix<Complex> input, Matrix<Complex> result)
{ {
// Check for proper arguments.
if (input == null)
{
throw new ArgumentNullException("input");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
// The solution X should have the same number of columns as B // The solution X should have the same number of columns as B
if (input.ColumnCount != result.ColumnCount) if (input.ColumnCount != result.ColumnCount)
{ {
@ -131,7 +119,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
} }
// The solution X row dimension is equal to the column dimension of A // The solution X row dimension is equal to the column dimension of A
if (MatrixR.ColumnCount != result.RowCount) if (FullR.ColumnCount != result.RowCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension); throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension);
} }
@ -148,7 +136,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
throw new NotSupportedException("Can only do QR factorization for dense matrices at the moment."); throw new NotSupportedException("Can only do QR factorization for dense matrices at the moment.");
} }
Control.LinearAlgebraProvider.QRSolveFactored(((DenseMatrix)Q).Values, ((DenseMatrix)MatrixR).Values, Q.RowCount, MatrixR.ColumnCount, Tau, dinput.Values, input.ColumnCount, dresult.Values, QrMethod); Control.LinearAlgebraProvider.QRSolveFactored(((DenseMatrix) Q).Values, ((DenseMatrix) FullR).Values, Q.RowCount, FullR.ColumnCount, Tau, dinput.Values, input.ColumnCount, dresult.Values, Method);
} }
/// <summary> /// <summary>
@ -158,16 +146,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
/// <param name="result">The left hand side <see cref="Matrix{T}"/>, <b>x</b>.</param> /// <param name="result">The left hand side <see cref="Matrix{T}"/>, <b>x</b>.</param>
public override void Solve(Vector<Complex> input, Vector<Complex> result) public override void Solve(Vector<Complex> input, Vector<Complex> result)
{ {
if (input == null)
{
throw new ArgumentNullException("input");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
// Ax=b where A is an m x n matrix // Ax=b where A is an m x n matrix
// Check that b is a column vector with m entries // Check that b is a column vector with m entries
if (Q.RowCount != input.Count) if (Q.RowCount != input.Count)
@ -176,9 +154,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
} }
// Check that x is a column vector with n entries // Check that x is a column vector with n entries
if (MatrixR.ColumnCount != result.Count) if (FullR.ColumnCount != result.Count)
{ {
throw Matrix.DimensionsDontMatch<ArgumentException>(MatrixR, result); throw Matrix.DimensionsDontMatch<ArgumentException>(FullR, result);
} }
var dinput = input as DenseVector; var dinput = input as DenseVector;
@ -193,7 +171,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
throw new NotSupportedException("Can only do QR factorization for dense vectors at the moment."); throw new NotSupportedException("Can only do QR factorization for dense vectors at the moment.");
} }
Control.LinearAlgebraProvider.QRSolveFactored(((DenseMatrix)Q).Values, ((DenseMatrix)MatrixR).Values, Q.RowCount, MatrixR.ColumnCount, Tau, dinput.Values, 1, dresult.Values, QrMethod); Control.LinearAlgebraProvider.QRSolveFactored(((DenseMatrix) Q).Values, ((DenseMatrix) FullR).Values, Q.RowCount, FullR.ColumnCount, Tau, dinput.Values, 1, dresult.Values, Method);
} }
} }
} }

12
src/Numerics/LinearAlgebra/Complex/Factorization/GramSchmidt.cs

@ -62,16 +62,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
{ {
get get
{ {
if (MatrixR.RowCount != MatrixR.ColumnCount) if (FullR.RowCount != FullR.ColumnCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixSquare); throw new ArgumentException(Resources.ArgumentMatrixSquare);
} }
var det = Complex.One; var det = Complex.One;
for (var i = 0; i < MatrixR.ColumnCount; i++) for (var i = 0; i < FullR.ColumnCount; i++)
{ {
det *= MatrixR.At(i, i); det *= FullR.At(i, i);
if (MatrixR.At(i, i).Magnitude.AlmostEqual(0.0)) if (FullR.At(i, i).Magnitude.AlmostEqual(0.0))
{ {
return 0; return 0;
} }
@ -89,9 +89,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
{ {
get get
{ {
for (var i = 0; i < MatrixR.ColumnCount; i++) for (var i = 0; i < FullR.ColumnCount; i++)
{ {
if (MatrixR.At(i, i).Magnitude.AlmostEqual(0.0)) if (FullR.At(i, i).Magnitude.AlmostEqual(0.0))
{ {
return false; return false;
} }

28
src/Numerics/LinearAlgebra/Complex/Factorization/QR.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET //
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -24,17 +28,18 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Factorization;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
{ {
using System;
using Properties;
#if NOSYSNUMERICS #if NOSYSNUMERICS
using Complex = Numerics.Complex; using Complex = Numerics.Complex;
#else #else
using Complex = System.Numerics.Complex; using Complex = System.Numerics.Complex;
#endif #endif
/// <summary> /// <summary>
@ -51,6 +56,11 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
/// </remarks> /// </remarks>
public abstract class QR : QR<Complex> public abstract class QR : QR<Complex>
{ {
protected QR(Matrix<Complex> q, Matrix<Complex> rFull, QRMethod method)
: base(q, rFull, method)
{
}
/// <summary> /// <summary>
/// Gets the absolute determinant value of the matrix for which the QR matrix was computed. /// Gets the absolute determinant value of the matrix for which the QR matrix was computed.
/// </summary> /// </summary>
@ -58,16 +68,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
{ {
get get
{ {
if (MatrixR.RowCount != MatrixR.ColumnCount) if (FullR.RowCount != FullR.ColumnCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixSquare); throw new ArgumentException(Resources.ArgumentMatrixSquare);
} }
var det = Complex.One; var det = Complex.One;
for (var i = 0; i < MatrixR.ColumnCount; i++) for (var i = 0; i < FullR.ColumnCount; i++)
{ {
det *= MatrixR.At(i, i); det *= FullR.At(i, i);
if (MatrixR.At(i, i).Magnitude.AlmostEqual(0.0)) if (FullR.At(i, i).Magnitude.AlmostEqual(0.0))
{ {
return 0; return 0;
} }
@ -85,9 +95,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
{ {
get get
{ {
for (var i = 0; i < MatrixR.ColumnCount; i++) for (var i = 0; i < FullR.ColumnCount; i++)
{ {
if (MatrixR.At(i, i).Magnitude.AlmostEqual(0.0)) if (FullR.At(i, i).Magnitude.AlmostEqual(0.0))
{ {
return false; return false;
} }

12
src/Numerics/LinearAlgebra/Complex/Factorization/UserGramSchmidt.cs

@ -159,19 +159,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
{ {
for (var j = 0; j < input.ColumnCount; j++) for (var j = 0; j < input.ColumnCount; j++)
{ {
inputCopy.At(k, j, inputCopy.At(k, j) / MatrixR.At(k, k)); inputCopy.At(k, j, inputCopy.At(k, j) / FullR.At(k, k));
} }
for (var i = 0; i < k; i++) for (var i = 0; i < k; i++)
{ {
for (var j = 0; j < input.ColumnCount; j++) for (var j = 0; j < input.ColumnCount; j++)
{ {
inputCopy.At(i, j, inputCopy.At(i, j) - (inputCopy.At(k, j) * MatrixR.At(i, k))); inputCopy.At(i, j, inputCopy.At(i, j) - (inputCopy.At(k, j) * FullR.At(i, k)));
} }
} }
} }
for (var i = 0; i < MatrixR.ColumnCount; i++) for (var i = 0; i < FullR.ColumnCount; i++)
{ {
for (var j = 0; j < input.ColumnCount; j++) for (var j = 0; j < input.ColumnCount; j++)
{ {
@ -223,14 +223,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
// Solve R*X = Y; // Solve R*X = Y;
for (var k = Q.ColumnCount - 1; k >= 0; k--) for (var k = Q.ColumnCount - 1; k >= 0; k--)
{ {
inputCopy[k] /= MatrixR.At(k, k); inputCopy[k] /= FullR.At(k, k);
for (var i = 0; i < k; i++) for (var i = 0; i < k; i++)
{ {
inputCopy[i] -= inputCopy[k] * MatrixR.At(i, k); inputCopy[i] -= inputCopy[k] * FullR.At(i, k);
} }
} }
for (var i = 0; i < MatrixR.ColumnCount; i++) for (var i = 0; i < FullR.ColumnCount; i++)
{ {
result[i] = inputCopy[i]; result[i] = inputCopy[i];
} }

145
src/Numerics/LinearAlgebra/Complex/Factorization/UserQR.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// //
// Copyright (c) 2009-2010 Math.NET // Copyright (c) 2009-2013 Math.NET
// //
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
@ -28,11 +28,11 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Linq;
using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Factorization;
using MathNet.Numerics.Properties; using MathNet.Numerics.Properties;
using MathNet.Numerics.Threading; using MathNet.Numerics.Threading;
using System;
using System.Linq;
namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
{ {
@ -41,6 +41,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
using Numerics; using Numerics;
#else #else
using System.Numerics; using System.Numerics;
#endif #endif
/// <summary> /// <summary>
@ -52,7 +53,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
/// <remarks> /// <remarks>
/// The computation of the QR decomposition is done at construction time by Householder transformation. /// The computation of the QR decomposition is done at construction time by Householder transformation.
/// </remarks> /// </remarks>
public class UserQR : QR public sealed class UserQR : QR
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="UserQR"/> class. This object will compute the /// Initializes a new instance of the <see cref="UserQR"/> class. This object will compute the
@ -61,71 +62,70 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
/// <param name="matrix">The matrix to factor.</param> /// <param name="matrix">The matrix to factor.</param>
/// <param name="method">The QR factorization method to use.</param> /// <param name="method">The QR factorization method to use.</param>
/// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception>
public UserQR(Matrix<Complex> matrix, QRMethod method = QRMethod.Full) public static UserQR Create(Matrix<Complex> matrix, QRMethod method = QRMethod.Full)
{ {
if (matrix == null)
{
throw new ArgumentNullException("matrix");
}
if (matrix.RowCount < matrix.ColumnCount) if (matrix.RowCount < matrix.ColumnCount)
{ {
throw Matrix.DimensionsDontMatch<ArgumentException>(matrix); throw Matrix.DimensionsDontMatch<ArgumentException>(matrix);
} }
QrMethod = method; Matrix<Complex> q;
Matrix<Complex> r;
var minmn = Math.Min(matrix.RowCount, matrix.ColumnCount); var minmn = Math.Min(matrix.RowCount, matrix.ColumnCount);
var u = new Complex[minmn][]; var u = new Complex[minmn][];
if (method == QRMethod.Full) if (method == QRMethod.Full)
{ {
MatrixR = matrix.Clone(); r = matrix.Clone();
Q = matrix.CreateMatrix(matrix.RowCount, matrix.RowCount); q = matrix.CreateMatrix(matrix.RowCount, matrix.RowCount);
for (var i = 0; i < matrix.RowCount; i++) for (var i = 0; i < matrix.RowCount; i++)
{ {
Q.At(i, i, 1.0f); q.At(i, i, 1.0f);
} }
for (var i = 0; i < minmn; i++) for (var i = 0; i < minmn; i++)
{ {
u[i] = GenerateColumn(MatrixR, i, i); u[i] = GenerateColumn(r, i, i);
ComputeQR(u[i], MatrixR, i, matrix.RowCount, i + 1, matrix.ColumnCount, ComputeQR(u[i], r, i, matrix.RowCount, i + 1, matrix.ColumnCount, Control.NumberOfParallelWorkerThreads);
Control.NumberOfParallelWorkerThreads);
} }
for (var i = minmn - 1; i >= 0; i--) for (var i = minmn - 1; i >= 0; i--)
{ {
ComputeQR(u[i], Q, i, matrix.RowCount, i, matrix.RowCount, ComputeQR(u[i], q, i, matrix.RowCount, i, matrix.RowCount, Control.NumberOfParallelWorkerThreads);
Control.NumberOfParallelWorkerThreads);
} }
} }
else else
{ {
MatrixR = matrix.CreateMatrix(matrix.ColumnCount, matrix.ColumnCount); q = matrix.Clone();
Q = matrix.Clone();
for (var i = 0; i < minmn; i++) for (var i = 0; i < minmn; i++)
{ {
u[i] = GenerateColumn(Q, i, i); u[i] = GenerateColumn(q, i, i);
ComputeQR(u[i], Q, i, matrix.RowCount, i + 1, matrix.ColumnCount, ComputeQR(u[i], q, i, matrix.RowCount, i + 1, matrix.ColumnCount, Control.NumberOfParallelWorkerThreads);
Control.NumberOfParallelWorkerThreads);
} }
MatrixR = Q.SubMatrix(0, matrix.ColumnCount, 0, matrix.ColumnCount); r = q.SubMatrix(0, matrix.ColumnCount, 0, matrix.ColumnCount);
Q.Clear(); q.Clear();
for (var i = 0; i < matrix.ColumnCount; i++) for (var i = 0; i < matrix.ColumnCount; i++)
{ {
Q.At(i, i, 1.0f); q.At(i, i, 1.0f);
} }
for (var i = minmn - 1; i >= 0; i--) for (var i = minmn - 1; i >= 0; i--)
{ {
ComputeQR(u[i], Q, i, matrix.RowCount, i, matrix.ColumnCount, ComputeQR(u[i], q, i, matrix.RowCount, i, matrix.ColumnCount, Control.NumberOfParallelWorkerThreads);
Control.NumberOfParallelWorkerThreads);
} }
} }
return new UserQR(q, r, method);
}
UserQR(Matrix<Complex> q, Matrix<Complex> rFull, QRMethod method)
: base(q, rFull, method)
{
} }
/// <summary> /// <summary>
@ -135,7 +135,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
/// <param name="row">The first row</param> /// <param name="row">The first row</param>
/// <param name="column">Column index</param> /// <param name="column">Column index</param>
/// <returns>Generated vector</returns> /// <returns>Generated vector</returns>
private static Complex[] GenerateColumn(Matrix<Complex> a, int row, int column) static Complex[] GenerateColumn(Matrix<Complex> a, int row, int column)
{ {
var ru = a.RowCount - row; var ru = a.RowCount - row;
var u = new Complex[ru]; var u = new Complex[ru];
@ -146,7 +146,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
a.At(i, column, 0.0); a.At(i, column, 0.0);
} }
var norm = u.Aggregate(Complex.Zero, (current, t) => current + (t.Magnitude * t.Magnitude)); var norm = u.Aggregate(Complex.Zero, (current, t) => current + (t.Magnitude*t.Magnitude));
norm = norm.SquareRoot(); norm = norm.SquareRoot();
if (row == a.RowCount - 1 || norm.Magnitude == 0) if (row == a.RowCount - 1 || norm.Magnitude == 0)
@ -158,7 +158,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
if (u[0].Magnitude != 0.0) if (u[0].Magnitude != 0.0)
{ {
norm = norm.Magnitude * (u[0] / u[0].Magnitude); norm = norm.Magnitude*(u[0]/u[0].Magnitude);
} }
a.At(row, column, -norm); a.At(row, column, -norm);
@ -170,10 +170,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
u[0] += 1.0; u[0] += 1.0;
var s = (1.0 / u[0]).SquareRoot(); var s = (1.0/u[0]).SquareRoot();
for (var i = 0; i < ru; i++) for (var i = 0; i < ru; i++)
{ {
u[i] = u[i].Conjugate() * s; u[i] = u[i].Conjugate()*s;
} }
return u; return u;
@ -189,7 +189,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
/// <param name="columnStart">The first column</param> /// <param name="columnStart">The first column</param>
/// <param name="columnDim">The last column</param> /// <param name="columnDim">The last column</param>
/// <param name="availableCores">Number of available CPUs</param> /// <param name="availableCores">Number of available CPUs</param>
private static void ComputeQR(Complex[] u, Matrix<Complex> a, int rowStart, int rowDim, int columnStart, int columnDim, int availableCores) static void ComputeQR(Complex[] u, Matrix<Complex> a, int rowStart, int rowDim, int columnStart, int columnDim, int availableCores)
{ {
if (rowDim < rowStart || columnDim < columnStart) if (rowDim < rowStart || columnDim < columnStart)
{ {
@ -200,8 +200,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
if ((availableCores > 1) && (tmpColCount > 200)) if ((availableCores > 1) && (tmpColCount > 200))
{ {
var tmpSplit = columnStart + (tmpColCount / 2); var tmpSplit = columnStart + (tmpColCount/2);
var tmpCores = availableCores / 2; var tmpCores = availableCores/2;
CommonParallel.Invoke( CommonParallel.Invoke(
() => ComputeQR(u, a, rowStart, rowDim, columnStart, tmpSplit, tmpCores), () => ComputeQR(u, a, rowStart, rowDim, columnStart, tmpSplit, tmpCores),
@ -214,12 +214,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
var scale = Complex.Zero; var scale = Complex.Zero;
for (var i = rowStart; i < rowDim; i++) for (var i = rowStart; i < rowDim; i++)
{ {
scale += u[i - rowStart] * a.At(i, j); scale += u[i - rowStart]*a.At(i, j);
} }
for (var i = rowStart; i < rowDim; i++) for (var i = rowStart; i < rowDim; i++)
{ {
a.At(i, j, a.At(i, j) - (u[i - rowStart].Conjugate() * scale)); a.At(i, j, a.At(i, j) - (u[i - rowStart].Conjugate()*scale));
} }
} }
} }
@ -232,17 +232,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
/// <param name="result">The left hand side <see cref="Matrix{T}"/>, <b>X</b>.</param> /// <param name="result">The left hand side <see cref="Matrix{T}"/>, <b>X</b>.</param>
public override void Solve(Matrix<Complex> input, Matrix<Complex> result) public override void Solve(Matrix<Complex> input, Matrix<Complex> result)
{ {
// Check for proper arguments.
if (input == null)
{
throw new ArgumentNullException("input");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
// The solution X should have the same number of columns as B // The solution X should have the same number of columns as B
if (input.ColumnCount != result.ColumnCount) if (input.ColumnCount != result.ColumnCount)
{ {
@ -250,13 +239,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
} }
// The dimension compatibility conditions for X = A\B require the two matrices A and B to have the same number of rows // The dimension compatibility conditions for X = A\B require the two matrices A and B to have the same number of rows
if (MatrixR.RowCount != input.RowCount) if (FullR.RowCount != input.RowCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension); throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension);
} }
// The solution X row dimension is equal to the column dimension of A // The solution X row dimension is equal to the column dimension of A
if (MatrixR.ColumnCount != result.RowCount) if (FullR.ColumnCount != result.RowCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension); throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension);
} }
@ -264,20 +253,20 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
var inputCopy = input.Clone(); var inputCopy = input.Clone();
// Compute Y = transpose(Q)*B // Compute Y = transpose(Q)*B
var column = new Complex[MatrixR.RowCount]; var column = new Complex[FullR.RowCount];
for (var j = 0; j < input.ColumnCount; j++) for (var j = 0; j < input.ColumnCount; j++)
{ {
for (var k = 0; k < MatrixR.RowCount; k++) for (var k = 0; k < FullR.RowCount; k++)
{ {
column[k] = inputCopy.At(k, j); column[k] = inputCopy.At(k, j);
} }
for (var i = 0; i < MatrixR.RowCount; i++) for (var i = 0; i < FullR.RowCount; i++)
{ {
var s = Complex.Zero; var s = Complex.Zero;
for (var k = 0; k < MatrixR.RowCount; k++) for (var k = 0; k < FullR.RowCount; k++)
{ {
s += Q.At(k, i).Conjugate() * column[k]; s += Q.At(k, i).Conjugate()*column[k];
} }
inputCopy.At(i, j, s); inputCopy.At(i, j, s);
@ -285,23 +274,23 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
} }
// Solve R*X = Y; // Solve R*X = Y;
for (var k = MatrixR.ColumnCount - 1; k >= 0; k--) for (var k = FullR.ColumnCount - 1; k >= 0; k--)
{ {
for (var j = 0; j < input.ColumnCount; j++) for (var j = 0; j < input.ColumnCount; j++)
{ {
inputCopy.At(k, j, inputCopy.At(k, j) / MatrixR.At(k, k)); inputCopy.At(k, j, inputCopy.At(k, j)/FullR.At(k, k));
} }
for (var i = 0; i < k; i++) for (var i = 0; i < k; i++)
{ {
for (var j = 0; j < input.ColumnCount; j++) for (var j = 0; j < input.ColumnCount; j++)
{ {
inputCopy.At(i, j, inputCopy.At(i, j) - (inputCopy.At(k, j) * MatrixR.At(i, k))); inputCopy.At(i, j, inputCopy.At(i, j) - (inputCopy.At(k, j)*FullR.At(i, k)));
} }
} }
} }
for (var i = 0; i < MatrixR.ColumnCount; i++) for (var i = 0; i < FullR.ColumnCount; i++)
{ {
for (var j = 0; j < inputCopy.ColumnCount; j++) for (var j = 0; j < inputCopy.ColumnCount; j++)
{ {
@ -317,60 +306,50 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
/// <param name="result">The left hand side <see cref="Matrix{T}"/>, <b>x</b>.</param> /// <param name="result">The left hand side <see cref="Matrix{T}"/>, <b>x</b>.</param>
public override void Solve(Vector<Complex> input, Vector<Complex> result) public override void Solve(Vector<Complex> input, Vector<Complex> result)
{ {
if (input == null)
{
throw new ArgumentNullException("input");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
// Ax=b where A is an m x n matrix // Ax=b where A is an m x n matrix
// Check that b is a column vector with m entries // Check that b is a column vector with m entries
if (MatrixR.RowCount != input.Count) if (FullR.RowCount != input.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength); throw new ArgumentException(Resources.ArgumentVectorsSameLength);
} }
// Check that x is a column vector with n entries // Check that x is a column vector with n entries
if (MatrixR.ColumnCount != result.Count) if (FullR.ColumnCount != result.Count)
{ {
throw Matrix.DimensionsDontMatch<ArgumentException>(MatrixR, result); throw Matrix.DimensionsDontMatch<ArgumentException>(FullR, result);
} }
var inputCopy = input.Clone(); var inputCopy = input.Clone();
// Compute Y = transpose(Q)*B // Compute Y = transpose(Q)*B
var column = new Complex[MatrixR.RowCount]; var column = new Complex[FullR.RowCount];
for (var k = 0; k < MatrixR.RowCount; k++) for (var k = 0; k < FullR.RowCount; k++)
{ {
column[k] = inputCopy[k]; column[k] = inputCopy[k];
} }
for (var i = 0; i < MatrixR.RowCount; i++) for (var i = 0; i < FullR.RowCount; i++)
{ {
var s = Complex.Zero; var s = Complex.Zero;
for (var k = 0; k < MatrixR.RowCount; k++) for (var k = 0; k < FullR.RowCount; k++)
{ {
s += Q.At(k, i).Conjugate() * column[k]; s += Q.At(k, i).Conjugate()*column[k];
} }
inputCopy[i] = s; inputCopy[i] = s;
} }
// Solve R*X = Y; // Solve R*X = Y;
for (var k = MatrixR.ColumnCount - 1; k >= 0; k--) for (var k = FullR.ColumnCount - 1; k >= 0; k--)
{ {
inputCopy[k] /= MatrixR.At(k, k); inputCopy[k] /= FullR.At(k, k);
for (var i = 0; i < k; i++) for (var i = 0; i < k; i++)
{ {
inputCopy[i] -= inputCopy[k] * MatrixR.At(i, k); inputCopy[i] -= inputCopy[k]*FullR.At(i, k);
} }
} }
for (var i = 0; i < MatrixR.ColumnCount; i++) for (var i = 0; i < FullR.ColumnCount; i++)
{ {
result[i] = inputCopy[i]; result[i] = inputCopy[i];
} }

2
src/Numerics/LinearAlgebra/Complex/Matrix.cs

@ -470,7 +470,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
public override QR<Complex> QR(QRMethod method = QRMethod.Thin) public override QR<Complex> QR(QRMethod method = QRMethod.Thin)
{ {
return new UserQR(this, method); return UserQR.Create(this, method);
} }
public override GramSchmidt<Complex> GramSchmidt() public override GramSchmidt<Complex> GramSchmidt()

2
src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs

@ -1016,7 +1016,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
public override QR<Complex32> QR(QRMethod method = QRMethod.Thin) public override QR<Complex32> QR(QRMethod method = QRMethod.Thin)
{ {
return new DenseQR(this, method); return DenseQR.Create(this, method);
} }
public override GramSchmidt<Complex32> GramSchmidt() public override GramSchmidt<Complex32> GramSchmidt()

4
src/Numerics/LinearAlgebra/Complex32/Factorization/DenseGramSchmidt.cs

@ -159,7 +159,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
throw new NotSupportedException("Can only do GramSchmidt factorization for dense matrices at the moment."); throw new NotSupportedException("Can only do GramSchmidt factorization for dense matrices at the moment.");
} }
Control.LinearAlgebraProvider.QRSolveFactored(((DenseMatrix)Q).Values, ((DenseMatrix)MatrixR).Values, Q.RowCount, MatrixR.ColumnCount, null, dinput.Values, input.ColumnCount, dresult.Values, QRMethod.Thin); Control.LinearAlgebraProvider.QRSolveFactored(((DenseMatrix)Q).Values, ((DenseMatrix)FullR).Values, Q.RowCount, FullR.ColumnCount, null, dinput.Values, input.ColumnCount, dresult.Values, QRMethod.Thin);
} }
/// <summary> /// <summary>
@ -194,7 +194,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
throw new NotSupportedException("Can only do GramSchmidt factorization for dense vectors at the moment."); throw new NotSupportedException("Can only do GramSchmidt factorization for dense vectors at the moment.");
} }
Control.LinearAlgebraProvider.QRSolveFactored(((DenseMatrix)Q).Values, ((DenseMatrix)MatrixR).Values, Q.RowCount, MatrixR.ColumnCount, null, dinput.Values, 1, dresult.Values, QRMethod.Thin); Control.LinearAlgebraProvider.QRSolveFactored(((DenseMatrix)Q).Values, ((DenseMatrix)FullR).Values, Q.RowCount, FullR.ColumnCount, null, dinput.Values, 1, dresult.Values, QRMethod.Thin);
} }
} }
} }

77
src/Numerics/LinearAlgebra/Complex32/Factorization/DenseQR.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// //
// Copyright (c) 2009-2010 Math.NET // Copyright (c) 2009-2013 Math.NET
// //
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
@ -28,9 +28,9 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Factorization;
using MathNet.Numerics.Properties; using MathNet.Numerics.Properties;
using System;
namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
{ {
@ -45,16 +45,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
/// <remarks> /// <remarks>
/// The computation of the QR decomposition is done at construction time by Householder transformation. /// The computation of the QR decomposition is done at construction time by Householder transformation.
/// </remarks> /// </remarks>
public class DenseQR : QR public sealed class DenseQR : QR
{ {
/// <summary> /// <summary>
/// Gets or sets Tau vector. Contains additional information on Q - used for native solver. /// Gets or sets Tau vector. Contains additional information on Q - used for native solver.
/// </summary> /// </summary>
public Complex32[] Tau Complex32[] Tau { get; set; }
{
get;
set;
}
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="DenseQR"/> class. This object will compute the /// Initializes a new instance of the <see cref="DenseQR"/> class. This object will compute the
@ -64,35 +60,37 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
/// <param name="method">The QR factorization method to use.</param> /// <param name="method">The QR factorization method to use.</param>
/// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">If <paramref name="matrix"/> row count is less then column count</exception> /// <exception cref="ArgumentException">If <paramref name="matrix"/> row count is less then column count</exception>
public DenseQR(DenseMatrix matrix, QRMethod method = QRMethod.Full) public static DenseQR Create(DenseMatrix matrix, QRMethod method = QRMethod.Full)
{ {
if (matrix == null)
{
throw new ArgumentNullException("matrix");
}
if (matrix.RowCount < matrix.ColumnCount) if (matrix.RowCount < matrix.ColumnCount)
{ {
throw Matrix.DimensionsDontMatch<ArgumentException>(matrix); throw Matrix.DimensionsDontMatch<ArgumentException>(matrix);
} }
QrMethod = method; var tau = new Complex32[Math.Min(matrix.RowCount, matrix.ColumnCount)];
Tau = new Complex32[Math.Min(matrix.RowCount, matrix.ColumnCount)]; Matrix<Complex32> q;
Matrix<Complex32> r;
if (method == QRMethod.Full) if (method == QRMethod.Full)
{ {
MatrixR = matrix.Clone(); r = matrix.Clone();
Q = new DenseMatrix(matrix.RowCount); q = new DenseMatrix(matrix.RowCount);
Control.LinearAlgebraProvider.QRFactor(((DenseMatrix)MatrixR).Values, matrix.RowCount, matrix.ColumnCount, Control.LinearAlgebraProvider.QRFactor(((DenseMatrix) r).Values, matrix.RowCount, matrix.ColumnCount, ((DenseMatrix) q).Values, tau);
((DenseMatrix)Q).Values, Tau);
} }
else else
{ {
Q = matrix.Clone(); q = matrix.Clone();
MatrixR = new DenseMatrix(matrix.ColumnCount); r = new DenseMatrix(matrix.ColumnCount);
Control.LinearAlgebraProvider.ThinQRFactor(((DenseMatrix)Q).Values, matrix.RowCount, matrix.ColumnCount, Control.LinearAlgebraProvider.ThinQRFactor(((DenseMatrix) q).Values, matrix.RowCount, matrix.ColumnCount, ((DenseMatrix) r).Values, tau);
((DenseMatrix)MatrixR).Values, Tau);
} }
return new DenseQR(q, r, method, tau);
}
DenseQR(Matrix<Complex32> q, Matrix<Complex32> rFull, QRMethod method, Complex32[] tau)
: base(q, rFull, method)
{
Tau = tau;
} }
/// <summary> /// <summary>
@ -102,17 +100,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
/// <param name="result">The left hand side <see cref="Matrix{T}"/>, <b>X</b>.</param> /// <param name="result">The left hand side <see cref="Matrix{T}"/>, <b>X</b>.</param>
public override void Solve(Matrix<Complex32> input, Matrix<Complex32> result) public override void Solve(Matrix<Complex32> input, Matrix<Complex32> result)
{ {
// Check for proper arguments.
if (input == null)
{
throw new ArgumentNullException("input");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
// The solution X should have the same number of columns as B // The solution X should have the same number of columns as B
if (input.ColumnCount != result.ColumnCount) if (input.ColumnCount != result.ColumnCount)
{ {
@ -126,7 +113,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
} }
// The solution X row dimension is equal to the column dimension of A // The solution X row dimension is equal to the column dimension of A
if (MatrixR.ColumnCount != result.RowCount) if (FullR.ColumnCount != result.RowCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension); throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension);
} }
@ -143,7 +130,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
throw new NotSupportedException("Can only do QR factorization for dense matrices at the moment."); throw new NotSupportedException("Can only do QR factorization for dense matrices at the moment.");
} }
Control.LinearAlgebraProvider.QRSolveFactored(((DenseMatrix)Q).Values, ((DenseMatrix)MatrixR).Values, Q.RowCount, MatrixR.ColumnCount, Tau, dinput.Values, input.ColumnCount, dresult.Values, QrMethod); Control.LinearAlgebraProvider.QRSolveFactored(((DenseMatrix) Q).Values, ((DenseMatrix) FullR).Values, Q.RowCount, FullR.ColumnCount, Tau, dinput.Values, input.ColumnCount, dresult.Values, Method);
} }
/// <summary> /// <summary>
@ -153,16 +140,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
/// <param name="result">The left hand side <see cref="Matrix{T}"/>, <b>x</b>.</param> /// <param name="result">The left hand side <see cref="Matrix{T}"/>, <b>x</b>.</param>
public override void Solve(Vector<Complex32> input, Vector<Complex32> result) public override void Solve(Vector<Complex32> input, Vector<Complex32> result)
{ {
if (input == null)
{
throw new ArgumentNullException("input");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
// Ax=b where A is an m x n matrix // Ax=b where A is an m x n matrix
// Check that b is a column vector with m entries // Check that b is a column vector with m entries
if (Q.RowCount != input.Count) if (Q.RowCount != input.Count)
@ -171,9 +148,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
} }
// Check that x is a column vector with n entries // Check that x is a column vector with n entries
if (MatrixR.ColumnCount != result.Count) if (FullR.ColumnCount != result.Count)
{ {
throw Matrix.DimensionsDontMatch<ArgumentException>(MatrixR, result); throw Matrix.DimensionsDontMatch<ArgumentException>(FullR, result);
} }
var dinput = input as DenseVector; var dinput = input as DenseVector;
@ -188,7 +165,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
throw new NotSupportedException("Can only do QR factorization for dense vectors at the moment."); throw new NotSupportedException("Can only do QR factorization for dense vectors at the moment.");
} }
Control.LinearAlgebraProvider.QRSolveFactored(((DenseMatrix)Q).Values, ((DenseMatrix)MatrixR).Values, Q.RowCount, MatrixR.ColumnCount, Tau, dinput.Values, 1, dresult.Values, QrMethod); Control.LinearAlgebraProvider.QRSolveFactored(((DenseMatrix) Q).Values, ((DenseMatrix) FullR).Values, Q.RowCount, FullR.ColumnCount, Tau, dinput.Values, 1, dresult.Values, Method);
} }
} }
} }

12
src/Numerics/LinearAlgebra/Complex32/Factorization/GramSchmidt.cs

@ -57,16 +57,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
{ {
get get
{ {
if (MatrixR.RowCount != MatrixR.ColumnCount) if (FullR.RowCount != FullR.ColumnCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixSquare); throw new ArgumentException(Resources.ArgumentMatrixSquare);
} }
var det = Complex32.One; var det = Complex32.One;
for (var i = 0; i < MatrixR.ColumnCount; i++) for (var i = 0; i < FullR.ColumnCount; i++)
{ {
det *= MatrixR.At(i, i); det *= FullR.At(i, i);
if (MatrixR.At(i, i).Magnitude.AlmostEqual(0.0f)) if (FullR.At(i, i).Magnitude.AlmostEqual(0.0f))
{ {
return 0; return 0;
} }
@ -84,9 +84,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
{ {
get get
{ {
for (var i = 0; i < MatrixR.ColumnCount; i++) for (var i = 0; i < FullR.ColumnCount; i++)
{ {
if (MatrixR.At(i, i).Magnitude.AlmostEqual(0.0f)) if (FullR.At(i, i).Magnitude.AlmostEqual(0.0f))
{ {
return false; return false;
} }

27
src/Numerics/LinearAlgebra/Complex32/Factorization/QR.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET //
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -24,13 +28,13 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Factorization;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
{ {
using System;
using Numerics; using Numerics;
using Properties;
/// <summary> /// <summary>
/// <para>A class which encapsulates the functionality of the QR decomposition.</para> /// <para>A class which encapsulates the functionality of the QR decomposition.</para>
@ -46,6 +50,11 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
/// </remarks> /// </remarks>
public abstract class QR : QR<Complex32> public abstract class QR : QR<Complex32>
{ {
protected QR(Matrix<Complex32> q, Matrix<Complex32> rFull, QRMethod method)
: base(q, rFull, method)
{
}
/// <summary> /// <summary>
/// Gets the absolute determinant value of the matrix for which the QR matrix was computed. /// Gets the absolute determinant value of the matrix for which the QR matrix was computed.
/// </summary> /// </summary>
@ -53,16 +62,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
{ {
get get
{ {
if (MatrixR.RowCount != MatrixR.ColumnCount) if (FullR.RowCount != FullR.ColumnCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixSquare); throw new ArgumentException(Resources.ArgumentMatrixSquare);
} }
var det = Complex32.One; var det = Complex32.One;
for (var i = 0; i < MatrixR.ColumnCount; i++) for (var i = 0; i < FullR.ColumnCount; i++)
{ {
det *= MatrixR.At(i, i); det *= FullR.At(i, i);
if (MatrixR.At(i, i).Magnitude.AlmostEqual(0.0f)) if (FullR.At(i, i).Magnitude.AlmostEqual(0.0f))
{ {
return 0; return 0;
} }
@ -80,9 +89,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
{ {
get get
{ {
for (var i = 0; i < MatrixR.ColumnCount; i++) for (var i = 0; i < FullR.ColumnCount; i++)
{ {
if (MatrixR.At(i, i).Magnitude.AlmostEqual(0.0f)) if (FullR.At(i, i).Magnitude.AlmostEqual(0.0f))
{ {
return false; return false;
} }

12
src/Numerics/LinearAlgebra/Complex32/Factorization/UserGramSchmidt.cs

@ -154,19 +154,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
{ {
for (var j = 0; j < input.ColumnCount; j++) for (var j = 0; j < input.ColumnCount; j++)
{ {
inputCopy.At(k, j, inputCopy.At(k, j) / MatrixR.At(k, k)); inputCopy.At(k, j, inputCopy.At(k, j) / FullR.At(k, k));
} }
for (var i = 0; i < k; i++) for (var i = 0; i < k; i++)
{ {
for (var j = 0; j < input.ColumnCount; j++) for (var j = 0; j < input.ColumnCount; j++)
{ {
inputCopy.At(i, j, inputCopy.At(i, j) - (inputCopy.At(k, j) * MatrixR.At(i, k))); inputCopy.At(i, j, inputCopy.At(i, j) - (inputCopy.At(k, j) * FullR.At(i, k)));
} }
} }
} }
for (var i = 0; i < MatrixR.ColumnCount; i++) for (var i = 0; i < FullR.ColumnCount; i++)
{ {
for (var j = 0; j < input.ColumnCount; j++) for (var j = 0; j < input.ColumnCount; j++)
{ {
@ -218,14 +218,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
// Solve R*X = Y; // Solve R*X = Y;
for (var k = Q.ColumnCount - 1; k >= 0; k--) for (var k = Q.ColumnCount - 1; k >= 0; k--)
{ {
inputCopy[k] /= MatrixR.At(k, k); inputCopy[k] /= FullR.At(k, k);
for (var i = 0; i < k; i++) for (var i = 0; i < k; i++)
{ {
inputCopy[i] -= inputCopy[k] * MatrixR.At(i, k); inputCopy[i] -= inputCopy[k] * FullR.At(i, k);
} }
} }
for (var i = 0; i < MatrixR.ColumnCount; i++) for (var i = 0; i < FullR.ColumnCount; i++)
{ {
result[i] = inputCopy[i]; result[i] = inputCopy[i];
} }

146
src/Numerics/LinearAlgebra/Complex32/Factorization/UserQR.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// //
// Copyright (c) 2009-2010 Math.NET // Copyright (c) 2009-2013 Math.NET
// //
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
@ -28,11 +28,11 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Linq;
using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Factorization;
using MathNet.Numerics.Properties; using MathNet.Numerics.Properties;
using MathNet.Numerics.Threading; using MathNet.Numerics.Threading;
using System;
using System.Linq;
namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
{ {
@ -47,7 +47,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
/// <remarks> /// <remarks>
/// The computation of the QR decomposition is done at construction time by Householder transformation. /// The computation of the QR decomposition is done at construction time by Householder transformation.
/// </remarks> /// </remarks>
public class UserQR : QR public sealed class UserQR : QR
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="UserQR"/> class. This object will compute the /// Initializes a new instance of the <see cref="UserQR"/> class. This object will compute the
@ -56,71 +56,70 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
/// <param name="matrix">The matrix to factor.</param> /// <param name="matrix">The matrix to factor.</param>
/// <param name="method">The QR factorization method to use.</param> /// <param name="method">The QR factorization method to use.</param>
/// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception>
public UserQR(Matrix<Complex32> matrix, QRMethod method = QRMethod.Full) public static UserQR Create(Matrix<Complex32> matrix, QRMethod method = QRMethod.Full)
{ {
if (matrix == null)
{
throw new ArgumentNullException("matrix");
}
if (matrix.RowCount < matrix.ColumnCount) if (matrix.RowCount < matrix.ColumnCount)
{ {
throw Matrix.DimensionsDontMatch<ArgumentException>(matrix); throw Matrix.DimensionsDontMatch<ArgumentException>(matrix);
} }
QrMethod = method; Matrix<Complex32> q;
Matrix<Complex32> r;
var minmn = Math.Min(matrix.RowCount, matrix.ColumnCount); var minmn = Math.Min(matrix.RowCount, matrix.ColumnCount);
var u = new Complex32[minmn][]; var u = new Complex32[minmn][];
if (method == QRMethod.Full) if (method == QRMethod.Full)
{ {
MatrixR = matrix.Clone(); r = matrix.Clone();
Q = matrix.CreateMatrix(matrix.RowCount, matrix.RowCount); q = matrix.CreateMatrix(matrix.RowCount, matrix.RowCount);
for (var i = 0; i < matrix.RowCount; i++) for (var i = 0; i < matrix.RowCount; i++)
{ {
Q.At(i, i, 1.0f); q.At(i, i, 1.0f);
} }
for (var i = 0; i < minmn; i++) for (var i = 0; i < minmn; i++)
{ {
u[i] = GenerateColumn(MatrixR, i, i); u[i] = GenerateColumn(r, i, i);
ComputeQR(u[i], MatrixR, i, matrix.RowCount, i + 1, matrix.ColumnCount, ComputeQR(u[i], r, i, matrix.RowCount, i + 1, matrix.ColumnCount, Control.NumberOfParallelWorkerThreads);
Control.NumberOfParallelWorkerThreads);
} }
for (var i = minmn - 1; i >= 0; i--) for (var i = minmn - 1; i >= 0; i--)
{ {
ComputeQR(u[i], Q, i, matrix.RowCount, i, matrix.RowCount, ComputeQR(u[i], q, i, matrix.RowCount, i, matrix.RowCount, Control.NumberOfParallelWorkerThreads);
Control.NumberOfParallelWorkerThreads);
} }
} }
else else
{ {
MatrixR = matrix.CreateMatrix(matrix.ColumnCount, matrix.ColumnCount); q = matrix.Clone();
Q = matrix.Clone();
for (var i = 0; i < minmn; i++) for (var i = 0; i < minmn; i++)
{ {
u[i] = GenerateColumn(Q, i, i); u[i] = GenerateColumn(q, i, i);
ComputeQR(u[i], Q, i, matrix.RowCount, i + 1, matrix.ColumnCount, ComputeQR(u[i], q, i, matrix.RowCount, i + 1, matrix.ColumnCount, Control.NumberOfParallelWorkerThreads);
Control.NumberOfParallelWorkerThreads);
} }
MatrixR = Q.SubMatrix(0, matrix.ColumnCount, 0, matrix.ColumnCount); r = q.SubMatrix(0, matrix.ColumnCount, 0, matrix.ColumnCount);
Q.Clear(); q.Clear();
for (var i = 0; i < matrix.ColumnCount; i++) for (var i = 0; i < matrix.ColumnCount; i++)
{ {
Q.At(i, i, 1.0f); q.At(i, i, 1.0f);
} }
for (var i = minmn - 1; i >= 0; i--) for (var i = minmn - 1; i >= 0; i--)
{ {
ComputeQR(u[i], Q, i, matrix.RowCount, i, matrix.ColumnCount, ComputeQR(u[i], q, i, matrix.RowCount, i, matrix.ColumnCount, Control.NumberOfParallelWorkerThreads);
Control.NumberOfParallelWorkerThreads);
} }
} }
return new UserQR(q, r, method);
}
UserQR(Matrix<Complex32> q, Matrix<Complex32> rFull, QRMethod method)
: base(q, rFull, method)
{
} }
/// <summary> /// <summary>
@ -130,7 +129,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
/// <param name="row">The first row</param> /// <param name="row">The first row</param>
/// <param name="column">Column index</param> /// <param name="column">Column index</param>
/// <returns>Generated vector</returns> /// <returns>Generated vector</returns>
private static Complex32[] GenerateColumn(Matrix<Complex32> a, int row, int column) static Complex32[] GenerateColumn(Matrix<Complex32> a, int row, int column)
{ {
var ru = a.RowCount - row; var ru = a.RowCount - row;
var u = new Complex32[ru]; var u = new Complex32[ru];
@ -141,19 +140,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
a.At(i, column, 0.0f); a.At(i, column, 0.0f);
} }
var norm = u.Aggregate(Complex32.Zero, (current, t) => current + (t.Magnitude * t.Magnitude)); var norm = u.Aggregate(Complex32.Zero, (current, t) => current + (t.Magnitude*t.Magnitude));
norm = norm.SquareRoot(); norm = norm.SquareRoot();
if (row == a.RowCount - 1 || norm.Magnitude == 0) if (row == a.RowCount - 1 || norm.Magnitude == 0)
{ {
a.At(row, column, -u[0]); a.At(row, column, -u[0]);
u[0] = (float)Constants.Sqrt2; u[0] = (float) Constants.Sqrt2;
return u; return u;
} }
if (u[0].Magnitude != 0.0f) if (u[0].Magnitude != 0.0f)
{ {
norm = norm.Magnitude * (u[0] / u[0].Magnitude); norm = norm.Magnitude*(u[0]/u[0].Magnitude);
} }
a.At(row, column, -norm); a.At(row, column, -norm);
@ -165,10 +164,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
u[0] += 1.0f; u[0] += 1.0f;
var s = (1.0f / u[0]).SquareRoot(); var s = (1.0f/u[0]).SquareRoot();
for (var i = 0; i < ru; i++) for (var i = 0; i < ru; i++)
{ {
u[i] = u[i].Conjugate() * s; u[i] = u[i].Conjugate()*s;
} }
return u; return u;
@ -184,7 +183,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
/// <param name="columnStart">The first column</param> /// <param name="columnStart">The first column</param>
/// <param name="columnDim">The last column</param> /// <param name="columnDim">The last column</param>
/// <param name="availableCores">Number of available CPUs</param> /// <param name="availableCores">Number of available CPUs</param>
private static void ComputeQR(Complex32[] u, Matrix<Complex32> a, int rowStart, int rowDim, int columnStart, int columnDim, int availableCores) static void ComputeQR(Complex32[] u, Matrix<Complex32> a, int rowStart, int rowDim, int columnStart, int columnDim, int availableCores)
{ {
if (rowDim < rowStart || columnDim < columnStart) if (rowDim < rowStart || columnDim < columnStart)
{ {
@ -195,8 +194,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
if ((availableCores > 1) && (tmpColCount > 200)) if ((availableCores > 1) && (tmpColCount > 200))
{ {
var tmpSplit = columnStart + (tmpColCount / 2); var tmpSplit = columnStart + (tmpColCount/2);
var tmpCores = availableCores / 2; var tmpCores = availableCores/2;
CommonParallel.Invoke( CommonParallel.Invoke(
() => ComputeQR(u, a, rowStart, rowDim, columnStart, tmpSplit, tmpCores), () => ComputeQR(u, a, rowStart, rowDim, columnStart, tmpSplit, tmpCores),
@ -209,12 +208,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
var scale = Complex32.Zero; var scale = Complex32.Zero;
for (var i = rowStart; i < rowDim; i++) for (var i = rowStart; i < rowDim; i++)
{ {
scale += u[i - rowStart] * a.At(i, j); scale += u[i - rowStart]*a.At(i, j);
} }
for (var i = rowStart; i < rowDim; i++) for (var i = rowStart; i < rowDim; i++)
{ {
a.At(i, j, a.At(i, j) - (u[i - rowStart].Conjugate() * scale)); a.At(i, j, a.At(i, j) - (u[i - rowStart].Conjugate()*scale));
} }
} }
} }
@ -227,17 +226,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
/// <param name="result">The left hand side <see cref="Matrix{T}"/>, <b>X</b>.</param> /// <param name="result">The left hand side <see cref="Matrix{T}"/>, <b>X</b>.</param>
public override void Solve(Matrix<Complex32> input, Matrix<Complex32> result) public override void Solve(Matrix<Complex32> input, Matrix<Complex32> result)
{ {
// Check for proper arguments.
if (input == null)
{
throw new ArgumentNullException("input");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
// The solution X should have the same number of columns as B // The solution X should have the same number of columns as B
if (input.ColumnCount != result.ColumnCount) if (input.ColumnCount != result.ColumnCount)
{ {
@ -245,13 +233,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
} }
// The dimension compatibility conditions for X = A\B require the two matrices A and B to have the same number of rows // The dimension compatibility conditions for X = A\B require the two matrices A and B to have the same number of rows
if (MatrixR.RowCount != input.RowCount) if (FullR.RowCount != input.RowCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension); throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension);
} }
// The solution X row dimension is equal to the column dimension of A // The solution X row dimension is equal to the column dimension of A
if (MatrixR.ColumnCount != result.RowCount) if (FullR.ColumnCount != result.RowCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension); throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension);
} }
@ -259,20 +247,20 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
var inputCopy = input.Clone(); var inputCopy = input.Clone();
// Compute Y = transpose(Q)*B // Compute Y = transpose(Q)*B
var column = new Complex32[MatrixR.RowCount]; var column = new Complex32[FullR.RowCount];
for (var j = 0; j < input.ColumnCount; j++) for (var j = 0; j < input.ColumnCount; j++)
{ {
for (var k = 0; k < MatrixR.RowCount; k++) for (var k = 0; k < FullR.RowCount; k++)
{ {
column[k] = inputCopy.At(k, j); column[k] = inputCopy.At(k, j);
} }
for (var i = 0; i < MatrixR.RowCount; i++) for (var i = 0; i < FullR.RowCount; i++)
{ {
var s = Complex32.Zero; var s = Complex32.Zero;
for (var k = 0; k < MatrixR.RowCount; k++) for (var k = 0; k < FullR.RowCount; k++)
{ {
s += Q.At(k, i).Conjugate() * column[k]; s += Q.At(k, i).Conjugate()*column[k];
} }
inputCopy.At(i, j, s); inputCopy.At(i, j, s);
@ -280,23 +268,23 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
} }
// Solve R*X = Y; // Solve R*X = Y;
for (var k = MatrixR.ColumnCount - 1; k >= 0; k--) for (var k = FullR.ColumnCount - 1; k >= 0; k--)
{ {
for (var j = 0; j < input.ColumnCount; j++) for (var j = 0; j < input.ColumnCount; j++)
{ {
inputCopy.At(k, j, inputCopy.At(k, j) / MatrixR.At(k, k)); inputCopy.At(k, j, inputCopy.At(k, j)/FullR.At(k, k));
} }
for (var i = 0; i < k; i++) for (var i = 0; i < k; i++)
{ {
for (var j = 0; j < input.ColumnCount; j++) for (var j = 0; j < input.ColumnCount; j++)
{ {
inputCopy.At(i, j, inputCopy.At(i, j) - (inputCopy.At(k, j) * MatrixR.At(i, k))); inputCopy.At(i, j, inputCopy.At(i, j) - (inputCopy.At(k, j)*FullR.At(i, k)));
} }
} }
} }
for (var i = 0; i < MatrixR.ColumnCount; i++) for (var i = 0; i < FullR.ColumnCount; i++)
{ {
for (var j = 0; j < inputCopy.ColumnCount; j++) for (var j = 0; j < inputCopy.ColumnCount; j++)
{ {
@ -312,60 +300,50 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
/// <param name="result">The left hand side <see cref="Matrix{T}"/>, <b>x</b>.</param> /// <param name="result">The left hand side <see cref="Matrix{T}"/>, <b>x</b>.</param>
public override void Solve(Vector<Complex32> input, Vector<Complex32> result) public override void Solve(Vector<Complex32> input, Vector<Complex32> result)
{ {
if (input == null)
{
throw new ArgumentNullException("input");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
// Ax=b where A is an m x n matrix // Ax=b where A is an m x n matrix
// Check that b is a column vector with m entries // Check that b is a column vector with m entries
if (MatrixR.RowCount != input.Count) if (FullR.RowCount != input.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength); throw new ArgumentException(Resources.ArgumentVectorsSameLength);
} }
// Check that x is a column vector with n entries // Check that x is a column vector with n entries
if (MatrixR.ColumnCount != result.Count) if (FullR.ColumnCount != result.Count)
{ {
throw Matrix.DimensionsDontMatch<ArgumentException>(MatrixR, result); throw Matrix.DimensionsDontMatch<ArgumentException>(FullR, result);
} }
var inputCopy = input.Clone(); var inputCopy = input.Clone();
// Compute Y = transpose(Q)*B // Compute Y = transpose(Q)*B
var column = new Complex32[MatrixR.RowCount]; var column = new Complex32[FullR.RowCount];
for (var k = 0; k < MatrixR.RowCount; k++) for (var k = 0; k < FullR.RowCount; k++)
{ {
column[k] = inputCopy[k]; column[k] = inputCopy[k];
} }
for (var i = 0; i < MatrixR.RowCount; i++) for (var i = 0; i < FullR.RowCount; i++)
{ {
var s = Complex32.Zero; var s = Complex32.Zero;
for (var k = 0; k < MatrixR.RowCount; k++) for (var k = 0; k < FullR.RowCount; k++)
{ {
s += Q.At(k, i).Conjugate() * column[k]; s += Q.At(k, i).Conjugate()*column[k];
} }
inputCopy[i] = s; inputCopy[i] = s;
} }
// Solve R*X = Y; // Solve R*X = Y;
for (var k = MatrixR.ColumnCount - 1; k >= 0; k--) for (var k = FullR.ColumnCount - 1; k >= 0; k--)
{ {
inputCopy[k] /= MatrixR.At(k, k); inputCopy[k] /= FullR.At(k, k);
for (var i = 0; i < k; i++) for (var i = 0; i < k; i++)
{ {
inputCopy[i] -= inputCopy[k] * MatrixR.At(i, k); inputCopy[i] -= inputCopy[k]*FullR.At(i, k);
} }
} }
for (var i = 0; i < MatrixR.ColumnCount; i++) for (var i = 0; i < FullR.ColumnCount; i++)
{ {
result[i] = inputCopy[i]; result[i] = inputCopy[i];
} }

2
src/Numerics/LinearAlgebra/Complex32/Matrix.cs

@ -465,7 +465,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
public override QR<Complex32> QR(QRMethod method = QRMethod.Thin) public override QR<Complex32> QR(QRMethod method = QRMethod.Thin)
{ {
return new UserQR(this, method); return UserQR.Create(this, method);
} }
public override GramSchmidt<Complex32> GramSchmidt() public override GramSchmidt<Complex32> GramSchmidt()

2
src/Numerics/LinearAlgebra/Double/DenseMatrix.cs

@ -1047,7 +1047,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public override QR<double> QR(QRMethod method = QRMethod.Thin) public override QR<double> QR(QRMethod method = QRMethod.Thin)
{ {
return new DenseQR(this, method); return DenseQR.Create(this, method);
} }
public override GramSchmidt<double> GramSchmidt() public override GramSchmidt<double> GramSchmidt()

4
src/Numerics/LinearAlgebra/Double/Factorization/DenseGramSchmidt.cs

@ -157,7 +157,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
throw new NotSupportedException("Can only do GramSchmidt factorization for dense matrices at the moment."); throw new NotSupportedException("Can only do GramSchmidt factorization for dense matrices at the moment.");
} }
Control.LinearAlgebraProvider.QRSolveFactored(((DenseMatrix)Q).Values, ((DenseMatrix)MatrixR).Values, Q.RowCount, MatrixR.ColumnCount, null, dinput.Values, input.ColumnCount, dresult.Values, QRMethod.Thin); Control.LinearAlgebraProvider.QRSolveFactored(((DenseMatrix)Q).Values, ((DenseMatrix)FullR).Values, Q.RowCount, FullR.ColumnCount, null, dinput.Values, input.ColumnCount, dresult.Values, QRMethod.Thin);
} }
/// <summary> /// <summary>
@ -192,7 +192,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
throw new NotSupportedException("Can only do GramSchmidt factorization for dense vectors at the moment."); throw new NotSupportedException("Can only do GramSchmidt factorization for dense vectors at the moment.");
} }
Control.LinearAlgebraProvider.QRSolveFactored(((DenseMatrix)Q).Values, ((DenseMatrix)MatrixR).Values, Q.RowCount, MatrixR.ColumnCount, null, dinput.Values, 1, dresult.Values, QRMethod.Thin); Control.LinearAlgebraProvider.QRSolveFactored(((DenseMatrix)Q).Values, ((DenseMatrix)FullR).Values, Q.RowCount, FullR.ColumnCount, null, dinput.Values, 1, dresult.Values, QRMethod.Thin);
} }
} }
} }

80
src/Numerics/LinearAlgebra/Double/Factorization/DenseQR.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// //
// Copyright (c) 2009-2010 Math.NET // Copyright (c) 2009-2013 Math.NET
// //
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
@ -28,9 +28,9 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Factorization;
using MathNet.Numerics.Properties; using MathNet.Numerics.Properties;
using System;
namespace MathNet.Numerics.LinearAlgebra.Double.Factorization namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
{ {
@ -43,17 +43,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
/// <remarks> /// <remarks>
/// The computation of the QR decomposition is done at construction time by Householder transformation. /// The computation of the QR decomposition is done at construction time by Householder transformation.
/// </remarks> /// </remarks>
public class DenseQR : QR public sealed class DenseQR : QR
{ {
/// <summary> /// <summary>
/// Gets or sets Tau vector. Contains additional information on Q - used for native solver. /// Gets or sets Tau vector. Contains additional information on Q - used for native solver.
/// </summary> /// </summary>
public double[] Tau double[] Tau { get; set; }
{
get;
set;
}
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="DenseQR"/> class. This object will compute the /// Initializes a new instance of the <see cref="DenseQR"/> class. This object will compute the
/// QR factorization when the constructor is called and cache it's factorization. /// QR factorization when the constructor is called and cache it's factorization.
@ -62,36 +58,37 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
/// <param name="method">The type of QR factorization to perform.</param> /// <param name="method">The type of QR factorization to perform.</param>
/// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">If <paramref name="matrix"/> row count is less then column count</exception> /// <exception cref="ArgumentException">If <paramref name="matrix"/> row count is less then column count</exception>
public DenseQR(DenseMatrix matrix, QRMethod method = QRMethod.Full) public static DenseQR Create(DenseMatrix matrix, QRMethod method = QRMethod.Full)
{ {
if (matrix == null)
{
throw new ArgumentNullException("matrix");
}
if (matrix.RowCount < matrix.ColumnCount) if (matrix.RowCount < matrix.ColumnCount)
{ {
throw Matrix.DimensionsDontMatch<ArgumentException>(matrix); throw Matrix.DimensionsDontMatch<ArgumentException>(matrix);
} }
QrMethod = method; var tau = new double[Math.Min(matrix.RowCount, matrix.ColumnCount)];
Tau = new double[Math.Min(matrix.RowCount, matrix.ColumnCount)]; Matrix<double> q;
Matrix<double> r;
if (method == QRMethod.Full) if (method == QRMethod.Full)
{ {
MatrixR = matrix.Clone(); r = matrix.Clone();
Q = new DenseMatrix(matrix.RowCount); q = new DenseMatrix(matrix.RowCount);
Control.LinearAlgebraProvider.QRFactor(((DenseMatrix)MatrixR).Values, matrix.RowCount, matrix.ColumnCount, Control.LinearAlgebraProvider.QRFactor(((DenseMatrix) r).Values, matrix.RowCount, matrix.ColumnCount, ((DenseMatrix) q).Values, tau);
((DenseMatrix)Q).Values, Tau);
} }
else else
{ {
Q = matrix.Clone(); q = matrix.Clone();
MatrixR = new DenseMatrix(matrix.ColumnCount); r = new DenseMatrix(matrix.ColumnCount);
Control.LinearAlgebraProvider.ThinQRFactor(((DenseMatrix) Q).Values, matrix.RowCount, Control.LinearAlgebraProvider.ThinQRFactor(((DenseMatrix) q).Values, matrix.RowCount, matrix.ColumnCount, ((DenseMatrix) r).Values, tau);
matrix.ColumnCount,
((DenseMatrix) MatrixR).Values, Tau);
} }
return new DenseQR(q, r, method, tau);
}
DenseQR(Matrix<double> q, Matrix<double> rFull, QRMethod method, double[] tau)
: base(q, rFull, method)
{
Tau = tau;
} }
/// <summary> /// <summary>
@ -101,17 +98,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
/// <param name="result">The left hand side <see cref="Matrix{T}"/>, <b>X</b>.</param> /// <param name="result">The left hand side <see cref="Matrix{T}"/>, <b>X</b>.</param>
public override void Solve(Matrix<double> input, Matrix<double> result) public override void Solve(Matrix<double> input, Matrix<double> result)
{ {
// Check for proper arguments.
if (input == null)
{
throw new ArgumentNullException("input");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
// The solution X should have the same number of columns as B // The solution X should have the same number of columns as B
if (input.ColumnCount != result.ColumnCount) if (input.ColumnCount != result.ColumnCount)
{ {
@ -125,7 +111,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
} }
// The solution X row dimension is equal to the column dimension of A // The solution X row dimension is equal to the column dimension of A
if (MatrixR.ColumnCount != result.RowCount) if (FullR.ColumnCount != result.RowCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension); throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension);
} }
@ -142,7 +128,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
throw new NotSupportedException("Can only do QR factorization for dense matrices at the moment."); throw new NotSupportedException("Can only do QR factorization for dense matrices at the moment.");
} }
Control.LinearAlgebraProvider.QRSolveFactored(((DenseMatrix)Q).Values, ((DenseMatrix)MatrixR).Values, Q.RowCount, MatrixR.ColumnCount, Tau, dinput.Values, input.ColumnCount, dresult.Values, QrMethod); Control.LinearAlgebraProvider.QRSolveFactored(((DenseMatrix) Q).Values, ((DenseMatrix) FullR).Values, Q.RowCount, FullR.ColumnCount, Tau, dinput.Values, input.ColumnCount, dresult.Values, Method);
} }
/// <summary> /// <summary>
@ -152,16 +138,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
/// <param name="result">The left hand side <see cref="Matrix{T}"/>, <b>x</b>.</param> /// <param name="result">The left hand side <see cref="Matrix{T}"/>, <b>x</b>.</param>
public override void Solve(Vector<double> input, Vector<double> result) public override void Solve(Vector<double> input, Vector<double> result)
{ {
if (input == null)
{
throw new ArgumentNullException("input");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
// Ax=b where A is an m x n matrix // Ax=b where A is an m x n matrix
// Check that b is a column vector with m entries // Check that b is a column vector with m entries
if (Q.RowCount != input.Count) if (Q.RowCount != input.Count)
@ -170,9 +146,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
} }
// Check that x is a column vector with n entries // Check that x is a column vector with n entries
if (MatrixR.ColumnCount != result.Count) if (FullR.ColumnCount != result.Count)
{ {
throw Matrix.DimensionsDontMatch<ArgumentException>(MatrixR, result); throw Matrix.DimensionsDontMatch<ArgumentException>(FullR, result);
} }
var dinput = input as DenseVector; var dinput = input as DenseVector;
@ -187,7 +163,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
throw new NotSupportedException("Can only do QR factorization for dense vectors at the moment."); throw new NotSupportedException("Can only do QR factorization for dense vectors at the moment.");
} }
Control.LinearAlgebraProvider.QRSolveFactored(((DenseMatrix)Q).Values, ((DenseMatrix)MatrixR).Values, Q.RowCount, MatrixR.ColumnCount, Tau, dinput.Values, 1, dresult.Values, QrMethod); Control.LinearAlgebraProvider.QRSolveFactored(((DenseMatrix) Q).Values, ((DenseMatrix) FullR).Values, Q.RowCount, FullR.ColumnCount, Tau, dinput.Values, 1, dresult.Values, Method);
} }
} }
} }

12
src/Numerics/LinearAlgebra/Double/Factorization/GramSchmidt.cs

@ -55,16 +55,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
{ {
get get
{ {
if (MatrixR.RowCount != MatrixR.ColumnCount) if (FullR.RowCount != FullR.ColumnCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixSquare); throw new ArgumentException(Resources.ArgumentMatrixSquare);
} }
var det = 1.0; var det = 1.0;
for (var i = 0; i < MatrixR.ColumnCount; i++) for (var i = 0; i < FullR.ColumnCount; i++)
{ {
det *= MatrixR.At(i, i); det *= FullR.At(i, i);
if (Math.Abs(MatrixR.At(i, i)).AlmostEqual(0.0)) if (Math.Abs(FullR.At(i, i)).AlmostEqual(0.0))
{ {
return 0; return 0;
} }
@ -82,9 +82,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
{ {
get get
{ {
for (var i = 0; i < MatrixR.ColumnCount; i++) for (var i = 0; i < FullR.ColumnCount; i++)
{ {
if (Math.Abs(MatrixR.At(i, i)).AlmostEqual(0.0)) if (Math.Abs(FullR.At(i, i)).AlmostEqual(0.0))
{ {
return false; return false;
} }

28
src/Numerics/LinearAlgebra/Double/Factorization/QR.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET //
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -24,13 +28,12 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Factorization;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.LinearAlgebra.Double.Factorization namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
{ {
using System;
using Properties;
/// <summary> /// <summary>
/// <para>A class which encapsulates the functionality of the QR decomposition.</para> /// <para>A class which encapsulates the functionality of the QR decomposition.</para>
/// <para>Any real square matrix A (m x n) may be decomposed as A = QR where Q is an orthogonal matrix /// <para>Any real square matrix A (m x n) may be decomposed as A = QR where Q is an orthogonal matrix
@ -45,6 +48,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
/// </remarks> /// </remarks>
public abstract class QR : QR<double> public abstract class QR : QR<double>
{ {
protected QR(Matrix<double> q, Matrix<double> rFull, QRMethod method)
: base(q, rFull, method)
{
}
/// <summary> /// <summary>
/// Gets the absolute determinant value of the matrix for which the QR matrix was computed. /// Gets the absolute determinant value of the matrix for which the QR matrix was computed.
/// </summary> /// </summary>
@ -52,16 +60,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
{ {
get get
{ {
if (MatrixR.RowCount != MatrixR.ColumnCount) if (FullR.RowCount != FullR.ColumnCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixSquare); throw new ArgumentException(Resources.ArgumentMatrixSquare);
} }
var det = 1.0; var det = 1.0;
for (var i = 0; i < MatrixR.ColumnCount; i++) for (var i = 0; i < FullR.ColumnCount; i++)
{ {
det *= MatrixR.At(i, i); det *= FullR.At(i, i);
if (Math.Abs(MatrixR.At(i, i)).AlmostEqual(0.0)) if (Math.Abs(FullR.At(i, i)).AlmostEqual(0.0))
{ {
return 0; return 0;
} }
@ -79,9 +87,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
{ {
get get
{ {
for (var i = 0; i < MatrixR.ColumnCount; i++) for (var i = 0; i < FullR.ColumnCount; i++)
{ {
if (Math.Abs(MatrixR.At(i, i)).AlmostEqual(0.0)) if (Math.Abs(FullR.At(i, i)).AlmostEqual(0.0))
{ {
return false; return false;
} }

12
src/Numerics/LinearAlgebra/Double/Factorization/UserGramSchmidt.cs

@ -147,19 +147,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
{ {
for (var j = 0; j < input.ColumnCount; j++) for (var j = 0; j < input.ColumnCount; j++)
{ {
inputCopy.At(k, j, inputCopy.At(k, j) / MatrixR.At(k, k)); inputCopy.At(k, j, inputCopy.At(k, j) / FullR.At(k, k));
} }
for (var i = 0; i < k; i++) for (var i = 0; i < k; i++)
{ {
for (var j = 0; j < input.ColumnCount; j++) for (var j = 0; j < input.ColumnCount; j++)
{ {
inputCopy.At(i, j, inputCopy.At(i, j) - (inputCopy.At(k, j) * MatrixR.At(i, k))); inputCopy.At(i, j, inputCopy.At(i, j) - (inputCopy.At(k, j) * FullR.At(i, k)));
} }
} }
} }
for (var i = 0; i < MatrixR.ColumnCount; i++) for (var i = 0; i < FullR.ColumnCount; i++)
{ {
for (var j = 0; j < input.ColumnCount; j++) for (var j = 0; j < input.ColumnCount; j++)
{ {
@ -211,14 +211,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
// Solve R*X = Y; // Solve R*X = Y;
for (var k = Q.ColumnCount - 1; k >= 0; k--) for (var k = Q.ColumnCount - 1; k >= 0; k--)
{ {
inputCopy[k] /= MatrixR.At(k, k); inputCopy[k] /= FullR.At(k, k);
for (var i = 0; i < k; i++) for (var i = 0; i < k; i++)
{ {
inputCopy[i] -= inputCopy[k] * MatrixR.At(i, k); inputCopy[i] -= inputCopy[k] * FullR.At(i, k);
} }
} }
for (var i = 0; i < MatrixR.ColumnCount; i++) for (var i = 0; i < FullR.ColumnCount; i++)
{ {
result[i] = inputCopy[i]; result[i] = inputCopy[i];
} }

151
src/Numerics/LinearAlgebra/Double/Factorization/UserQR.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// //
// Copyright (c) 2009-2010 Math.NET // Copyright (c) 2009-2013 Math.NET
// //
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
@ -28,11 +28,11 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Linq;
using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Factorization;
using MathNet.Numerics.Properties; using MathNet.Numerics.Properties;
using MathNet.Numerics.Threading; using MathNet.Numerics.Threading;
using System;
using System.Linq;
namespace MathNet.Numerics.LinearAlgebra.Double.Factorization namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
{ {
@ -45,7 +45,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
/// <remarks> /// <remarks>
/// The computation of the QR decomposition is done at construction time by Householder transformation. /// The computation of the QR decomposition is done at construction time by Householder transformation.
/// </remarks> /// </remarks>
public class UserQR : QR public sealed class UserQR : QR
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="UserQR"/> class. This object will compute the /// Initializes a new instance of the <see cref="UserQR"/> class. This object will compute the
@ -54,74 +54,72 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
/// <param name="matrix">The matrix to factor.</param> /// <param name="matrix">The matrix to factor.</param>
/// <param name="method">The QR factorization method to use.</param> /// <param name="method">The QR factorization method to use.</param>
/// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception>
public UserQR(Matrix<double> matrix, QRMethod method = QRMethod.Full) public static UserQR Create(Matrix<double> matrix, QRMethod method = QRMethod.Full)
{ {
if (matrix == null)
{
throw new ArgumentNullException("matrix");
}
if (matrix.RowCount < matrix.ColumnCount) if (matrix.RowCount < matrix.ColumnCount)
{ {
throw Matrix.DimensionsDontMatch<ArgumentException>(matrix); throw Matrix.DimensionsDontMatch<ArgumentException>(matrix);
} }
QrMethod = method; Matrix<double> q;
Matrix<double> r;
var minmn = Math.Min(matrix.RowCount, matrix.ColumnCount); var minmn = Math.Min(matrix.RowCount, matrix.ColumnCount);
var u = new double[minmn][]; var u = new double[minmn][];
if (method == QRMethod.Full) if (method == QRMethod.Full)
{ {
MatrixR = matrix.Clone(); r = matrix.Clone();
Q = matrix.CreateMatrix(matrix.RowCount, matrix.RowCount); q = matrix.CreateMatrix(matrix.RowCount, matrix.RowCount);
for (var i = 0; i < matrix.RowCount; i++) for (var i = 0; i < matrix.RowCount; i++)
{ {
Q.At(i, i, 1.0); q.At(i, i, 1.0);
} }
for (var i = 0; i < minmn; i++) for (var i = 0; i < minmn; i++)
{ {
u[i] = GenerateColumn(MatrixR, i, i); u[i] = GenerateColumn(r, i, i);
ComputeQR(u[i], MatrixR, i, matrix.RowCount, i + 1, matrix.ColumnCount, ComputeQR(u[i], r, i, matrix.RowCount, i + 1, matrix.ColumnCount, Control.NumberOfParallelWorkerThreads);
Control.NumberOfParallelWorkerThreads);
} }
for (var i = minmn - 1; i >= 0; i--) for (var i = minmn - 1; i >= 0; i--)
{ {
ComputeQR(u[i], Q, i, matrix.RowCount, i, matrix.RowCount, ComputeQR(u[i], q, i, matrix.RowCount, i, matrix.RowCount, Control.NumberOfParallelWorkerThreads);
Control.NumberOfParallelWorkerThreads);
} }
} }
else else
{ {
MatrixR = matrix.CreateMatrix(matrix.ColumnCount, matrix.ColumnCount); q = matrix.Clone();
Q = matrix.Clone();
for (var i = 0; i < minmn; i++) for (var i = 0; i < minmn; i++)
{ {
u[i] = GenerateColumn(Q, i, i); u[i] = GenerateColumn(q, i, i);
ComputeQR(u[i], Q, i, matrix.RowCount, i + 1, matrix.ColumnCount, ComputeQR(u[i], q, i, matrix.RowCount, i + 1, matrix.ColumnCount, Control.NumberOfParallelWorkerThreads);
Control.NumberOfParallelWorkerThreads);
} }
MatrixR = Q.SubMatrix(0, matrix.ColumnCount, 0, matrix.ColumnCount); r = q.SubMatrix(0, matrix.ColumnCount, 0, matrix.ColumnCount);
Q.Clear(); q.Clear();
for (var i = 0; i < matrix.ColumnCount; i++) for (var i = 0; i < matrix.ColumnCount; i++)
{ {
Q.At(i, i, 1.0); q.At(i, i, 1.0);
} }
for (var i = minmn - 1; i >= 0; i--) for (var i = minmn - 1; i >= 0; i--)
{ {
ComputeQR(u[i], Q, i, matrix.RowCount, i, matrix.ColumnCount, ComputeQR(u[i], q, i, matrix.RowCount, i, matrix.ColumnCount, Control.NumberOfParallelWorkerThreads);
Control.NumberOfParallelWorkerThreads);
} }
} }
return new UserQR(q, r, method);
}
UserQR(Matrix<double> q, Matrix<double> rFull, QRMethod method)
: base(q, rFull, method)
{
} }
/// <summary> /// <summary>
/// Generate column from initial matrix to work array /// Generate column from initial matrix to work array
/// </summary> /// </summary>
@ -129,7 +127,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
/// <param name="row">The first row</param> /// <param name="row">The first row</param>
/// <param name="column">Column index</param> /// <param name="column">Column index</param>
/// <returns>Generated vector</returns> /// <returns>Generated vector</returns>
private static double[] GenerateColumn(Matrix<double> a, int row, int column) static double[] GenerateColumn(Matrix<double> a, int row, int column)
{ {
var ru = a.RowCount - row; var ru = a.RowCount - row;
var u = new double[ru]; var u = new double[ru];
@ -140,7 +138,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
a.At(i, row, 0.0); a.At(i, row, 0.0);
} }
var norm = u.Sum(t => t * t); var norm = u.Sum(t => t*t);
norm = Math.Sqrt(norm); norm = Math.Sqrt(norm);
if (row == a.RowCount - 1 || norm == 0) if (row == a.RowCount - 1 || norm == 0)
@ -150,13 +148,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
return u; return u;
} }
var scale = 1.0 / norm; var scale = 1.0/norm;
if (u[0] < 0.0) if (u[0] < 0.0)
{ {
scale *= -1.0; scale *= -1.0;
} }
a.At(row, column, -1.0 / scale); a.At(row, column, -1.0/scale);
for (var i = 0; i < ru; i++) for (var i = 0; i < ru; i++)
{ {
@ -164,7 +162,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
} }
u[0] += 1.0; u[0] += 1.0;
var s = Math.Sqrt(1.0 / u[0]); var s = Math.Sqrt(1.0/u[0]);
for (var i = 0; i < ru; i++) for (var i = 0; i < ru; i++)
{ {
@ -184,7 +182,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
/// <param name="columnStart">The first column</param> /// <param name="columnStart">The first column</param>
/// <param name="columnDim">The last column</param> /// <param name="columnDim">The last column</param>
/// <param name="availableCores">Number of available CPUs</param> /// <param name="availableCores">Number of available CPUs</param>
private static void ComputeQR(double[] u, Matrix<double> a, int rowStart, int rowDim, int columnStart, int columnDim, int availableCores) static void ComputeQR(double[] u, Matrix<double> a, int rowStart, int rowDim, int columnStart, int columnDim, int availableCores)
{ {
if (rowDim < rowStart || columnDim < columnStart) if (rowDim < rowStart || columnDim < columnStart)
{ {
@ -195,8 +193,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
if ((availableCores > 1) && (tmpColCount > 200)) if ((availableCores > 1) && (tmpColCount > 200))
{ {
var tmpSplit = columnStart + (tmpColCount / 2); var tmpSplit = columnStart + (tmpColCount/2);
var tmpCores = availableCores / 2; var tmpCores = availableCores/2;
CommonParallel.Invoke( CommonParallel.Invoke(
() => ComputeQR(u, a, rowStart, rowDim, columnStart, tmpSplit, tmpCores), () => ComputeQR(u, a, rowStart, rowDim, columnStart, tmpSplit, tmpCores),
@ -209,12 +207,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
var scale = 0.0; var scale = 0.0;
for (var i = rowStart; i < rowDim; i++) for (var i = rowStart; i < rowDim; i++)
{ {
scale += u[i - rowStart] * a.At(i, j); scale += u[i - rowStart]*a.At(i, j);
} }
for (var i = rowStart; i < rowDim; i++) for (var i = rowStart; i < rowDim; i++)
{ {
a.At(i, j, a.At(i, j) - (u[i - rowStart] * scale)); a.At(i, j, a.At(i, j) - (u[i - rowStart]*scale));
} }
} }
} }
@ -227,17 +225,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
/// <param name="result">The left hand side <see cref="Matrix{T}"/>, <b>X</b>.</param> /// <param name="result">The left hand side <see cref="Matrix{T}"/>, <b>X</b>.</param>
public override void Solve(Matrix<double> input, Matrix<double> result) public override void Solve(Matrix<double> input, Matrix<double> result)
{ {
// Check for proper arguments.
if (input == null)
{
throw new ArgumentNullException("input");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
// The solution X should have the same number of columns as B // The solution X should have the same number of columns as B
if (input.ColumnCount != result.ColumnCount) if (input.ColumnCount != result.ColumnCount)
{ {
@ -245,13 +232,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
} }
// The dimension compatibility conditions for X = A\B require the two matrices A and B to have the same number of rows // The dimension compatibility conditions for X = A\B require the two matrices A and B to have the same number of rows
if (MatrixR.RowCount != input.RowCount) if (FullR.RowCount != input.RowCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension); throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension);
} }
// The solution X row dimension is equal to the column dimension of A // The solution X row dimension is equal to the column dimension of A
if (MatrixR.ColumnCount != result.RowCount) if (FullR.ColumnCount != result.RowCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension); throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension);
} }
@ -259,20 +246,20 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
var inputCopy = input.Clone(); var inputCopy = input.Clone();
// Compute Y = transpose(Q)*B // Compute Y = transpose(Q)*B
var column = new double[MatrixR.RowCount]; var column = new double[FullR.RowCount];
for (var j = 0; j < input.ColumnCount; j++) for (var j = 0; j < input.ColumnCount; j++)
{ {
for (var k = 0; k < MatrixR.RowCount; k++) for (var k = 0; k < FullR.RowCount; k++)
{ {
column[k] = inputCopy.At(k, j); column[k] = inputCopy.At(k, j);
} }
for (var i = 0; i < MatrixR.RowCount; i++) for (var i = 0; i < FullR.RowCount; i++)
{ {
double s = 0; double s = 0;
for (var k = 0; k < MatrixR.RowCount; k++) for (var k = 0; k < FullR.RowCount; k++)
{ {
s += Q.At(k, i) * column[k]; s += Q.At(k, i)*column[k];
} }
inputCopy.At(i, j, s); inputCopy.At(i, j, s);
@ -280,23 +267,23 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
} }
// Solve R*X = Y; // Solve R*X = Y;
for (var k = MatrixR.ColumnCount - 1; k >= 0; k--) for (var k = FullR.ColumnCount - 1; k >= 0; k--)
{ {
for (var j = 0; j < input.ColumnCount; j++) for (var j = 0; j < input.ColumnCount; j++)
{ {
inputCopy.At(k, j, inputCopy.At(k, j) / MatrixR.At(k, k)); inputCopy.At(k, j, inputCopy.At(k, j)/FullR.At(k, k));
} }
for (var i = 0; i < k; i++) for (var i = 0; i < k; i++)
{ {
for (var j = 0; j < input.ColumnCount; j++) for (var j = 0; j < input.ColumnCount; j++)
{ {
inputCopy.At(i, j, inputCopy.At(i, j) - (inputCopy.At(k, j) * MatrixR.At(i, k))); inputCopy.At(i, j, inputCopy.At(i, j) - (inputCopy.At(k, j)*FullR.At(i, k)));
} }
} }
} }
for (var i = 0; i < MatrixR.ColumnCount; i++) for (var i = 0; i < FullR.ColumnCount; i++)
{ {
for (var j = 0; j < inputCopy.ColumnCount; j++) for (var j = 0; j < inputCopy.ColumnCount; j++)
{ {
@ -312,60 +299,50 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
/// <param name="result">The left hand side <see cref="Matrix{T}"/>, <b>x</b>.</param> /// <param name="result">The left hand side <see cref="Matrix{T}"/>, <b>x</b>.</param>
public override void Solve(Vector<double> input, Vector<double> result) public override void Solve(Vector<double> input, Vector<double> result)
{ {
if (input == null)
{
throw new ArgumentNullException("input");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
// Ax=b where A is an m x n matrix // Ax=b where A is an m x n matrix
// Check that b is a column vector with m entries // Check that b is a column vector with m entries
if (MatrixR.RowCount != input.Count) if (FullR.RowCount != input.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength); throw new ArgumentException(Resources.ArgumentVectorsSameLength);
} }
// Check that x is a column vector with n entries // Check that x is a column vector with n entries
if (MatrixR.ColumnCount != result.Count) if (FullR.ColumnCount != result.Count)
{ {
throw Matrix.DimensionsDontMatch<ArgumentException>(MatrixR, result); throw Matrix.DimensionsDontMatch<ArgumentException>(FullR, result);
} }
var inputCopy = input.Clone(); var inputCopy = input.Clone();
// Compute Y = transpose(Q)*B // Compute Y = transpose(Q)*B
var column = new double[MatrixR.RowCount]; var column = new double[FullR.RowCount];
for (var k = 0; k < MatrixR.RowCount; k++) for (var k = 0; k < FullR.RowCount; k++)
{ {
column[k] = inputCopy[k]; column[k] = inputCopy[k];
} }
for (var i = 0; i < MatrixR.RowCount; i++) for (var i = 0; i < FullR.RowCount; i++)
{ {
double s = 0; double s = 0;
for (var k = 0; k < MatrixR.RowCount; k++) for (var k = 0; k < FullR.RowCount; k++)
{ {
s += Q.At(k, i) * column[k]; s += Q.At(k, i)*column[k];
} }
inputCopy[i] = s; inputCopy[i] = s;
} }
// Solve R*X = Y; // Solve R*X = Y;
for (var k = MatrixR.ColumnCount - 1; k >= 0; k--) for (var k = FullR.ColumnCount - 1; k >= 0; k--)
{ {
inputCopy[k] /= MatrixR.At(k, k); inputCopy[k] /= FullR.At(k, k);
for (var i = 0; i < k; i++) for (var i = 0; i < k; i++)
{ {
inputCopy[i] -= inputCopy[k] * MatrixR.At(i, k); inputCopy[i] -= inputCopy[k]*FullR.At(i, k);
} }
} }
for (var i = 0; i < MatrixR.ColumnCount; i++) for (var i = 0; i < FullR.ColumnCount; i++)
{ {
result[i] = inputCopy[i]; result[i] = inputCopy[i];
} }

2
src/Numerics/LinearAlgebra/Double/Matrix.cs

@ -471,7 +471,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public override QR<double> QR(QRMethod method = QRMethod.Thin) public override QR<double> QR(QRMethod method = QRMethod.Thin)
{ {
return new UserQR(this, method); return UserQR.Create(this, method);
} }
public override GramSchmidt<double> GramSchmidt() public override GramSchmidt<double> GramSchmidt()

3
src/Numerics/LinearAlgebra/Factorization/GramSchmidt.cs

@ -44,9 +44,8 @@ namespace MathNet.Numerics.LinearAlgebra.Factorization
where T : struct, IEquatable<T>, IFormattable where T : struct, IEquatable<T>, IFormattable
{ {
protected GramSchmidt(Matrix<T> q, Matrix<T> rFull) protected GramSchmidt(Matrix<T> q, Matrix<T> rFull)
: base(q, rFull, QRMethod.Full)
{ {
Q = q;
MatrixR = rFull;
} }
} }
} }

52
src/Numerics/LinearAlgebra/Factorization/QR.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET //
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -58,29 +62,26 @@ namespace MathNet.Numerics.LinearAlgebra.Factorization
/// </remarks> /// </remarks>
/// <typeparam name="T">Supported data types are double, single, <see cref="Complex"/>, and <see cref="Complex32"/>.</typeparam> /// <typeparam name="T">Supported data types are double, single, <see cref="Complex"/>, and <see cref="Complex32"/>.</typeparam>
public abstract class QR<T> : ISolver<T> public abstract class QR<T> : ISolver<T>
where T : struct, IEquatable<T>, IFormattable where T : struct, IEquatable<T>, IFormattable
{ {
readonly Lazy<Matrix<T>> _lazyR; readonly Lazy<Matrix<T>> _lazyR;
protected QR() protected readonly Matrix<T> FullR;
protected readonly QRMethod Method;
protected QR(Matrix<T> q, Matrix<T> rFull, QRMethod method)
{ {
_lazyR = new Lazy<Matrix<T>>(ComputeR); Q = q;
FullR = rFull;
Method = method;
_lazyR = new Lazy<Matrix<T>>(FullR.UpperTriangle);
} }
/// <summary> /// <summary>
/// Gets or sets orthogonal Q matrix /// Gets or sets orthogonal Q matrix
/// </summary> /// </summary>
public Matrix<T> Q { get; protected set; } public Matrix<T> Q { get; private set; }
/// <summary>
/// Gets or sets upper triangular factor R
/// </summary>
protected Matrix<T> MatrixR { get; set; }
/// <summary>
/// The QR factorization method.
/// </summary>
protected QRMethod QrMethod { get; set; }
/// <summary> /// <summary>
/// Gets the upper triangular factor R. /// Gets the upper triangular factor R.
@ -101,11 +102,6 @@ namespace MathNet.Numerics.LinearAlgebra.Factorization
/// <value><c>true</c> if the matrix is full rank; otherwise <c>false</c>.</value> /// <value><c>true</c> if the matrix is full rank; otherwise <c>false</c>.</value>
public abstract bool IsFullRank { get; } public abstract bool IsFullRank { get; }
private Matrix<T> ComputeR()
{
return MatrixR.UpperTriangle();
}
/// <summary> /// <summary>
/// Solves a system of linear equations, <b>AX = B</b>, with A QR factorized. /// Solves a system of linear equations, <b>AX = B</b>, with A QR factorized.
/// </summary> /// </summary>
@ -113,13 +109,7 @@ namespace MathNet.Numerics.LinearAlgebra.Factorization
/// <returns>The left hand side <see cref="Matrix{T}"/>, <b>X</b>.</returns> /// <returns>The left hand side <see cref="Matrix{T}"/>, <b>X</b>.</returns>
public virtual Matrix<T> Solve(Matrix<T> input) public virtual Matrix<T> Solve(Matrix<T> input)
{ {
// Check for proper arguments. var matrixX = input.CreateMatrix(FullR.ColumnCount, input.ColumnCount);
if (input == null)
{
throw new ArgumentNullException("input");
}
var matrixX = input.CreateMatrix(MatrixR.ColumnCount, input.ColumnCount);
Solve(input, matrixX); Solve(input, matrixX);
return matrixX; return matrixX;
} }
@ -138,13 +128,7 @@ namespace MathNet.Numerics.LinearAlgebra.Factorization
/// <returns>The left hand side <see cref="Vector{T}"/>, <b>x</b>.</returns> /// <returns>The left hand side <see cref="Vector{T}"/>, <b>x</b>.</returns>
public virtual Vector<T> Solve(Vector<T> input) public virtual Vector<T> Solve(Vector<T> input)
{ {
// Check for proper arguments. var x = input.CreateVector(FullR.ColumnCount);
if (input == null)
{
throw new ArgumentNullException("input");
}
var x = input.CreateVector(MatrixR.ColumnCount);
Solve(input, x); Solve(input, x);
return x; return x;
} }

2
src/Numerics/LinearAlgebra/Single/DenseMatrix.cs

@ -1047,7 +1047,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
public override QR<float> QR(QRMethod method = QRMethod.Thin) public override QR<float> QR(QRMethod method = QRMethod.Thin)
{ {
return new DenseQR(this, method); return DenseQR.Create(this, method);
} }
public override GramSchmidt<float> GramSchmidt() public override GramSchmidt<float> GramSchmidt()

4
src/Numerics/LinearAlgebra/Single/Factorization/DenseGramSchmidt.cs

@ -157,7 +157,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
throw new NotSupportedException("Can only do GramSchmidt factorization for dense matrices at the moment."); throw new NotSupportedException("Can only do GramSchmidt factorization for dense matrices at the moment.");
} }
Control.LinearAlgebraProvider.QRSolveFactored(((DenseMatrix)Q).Values, ((DenseMatrix)MatrixR).Values, Q.RowCount, MatrixR.ColumnCount, null, dinput.Values, input.ColumnCount, dresult.Values, QRMethod.Thin); Control.LinearAlgebraProvider.QRSolveFactored(((DenseMatrix)Q).Values, ((DenseMatrix)FullR).Values, Q.RowCount, FullR.ColumnCount, null, dinput.Values, input.ColumnCount, dresult.Values, QRMethod.Thin);
} }
/// <summary> /// <summary>
@ -192,7 +192,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
throw new NotSupportedException("Can only do GramSchmidt factorization for dense vectors at the moment."); throw new NotSupportedException("Can only do GramSchmidt factorization for dense vectors at the moment.");
} }
Control.LinearAlgebraProvider.QRSolveFactored(((DenseMatrix)Q).Values, ((DenseMatrix)MatrixR).Values, Q.RowCount, MatrixR.ColumnCount, null, dinput.Values, 1, dresult.Values, QRMethod.Thin); Control.LinearAlgebraProvider.QRSolveFactored(((DenseMatrix)Q).Values, ((DenseMatrix)FullR).Values, Q.RowCount, FullR.ColumnCount, null, dinput.Values, 1, dresult.Values, QRMethod.Thin);
} }
} }
} }

69
src/Numerics/LinearAlgebra/Single/Factorization/DenseQR.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// //
// Copyright (c) 2009-2010 Math.NET // Copyright (c) 2009-2013 Math.NET
// //
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
@ -28,9 +28,9 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Factorization;
using MathNet.Numerics.Properties; using MathNet.Numerics.Properties;
using System;
namespace MathNet.Numerics.LinearAlgebra.Single.Factorization namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
{ {
@ -43,16 +43,12 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
/// <remarks> /// <remarks>
/// The computation of the QR decomposition is done at construction time by Householder transformation. /// The computation of the QR decomposition is done at construction time by Householder transformation.
/// </remarks> /// </remarks>
public class DenseQR : QR public sealed class DenseQR : QR
{ {
/// <summary> /// <summary>
/// Gets or sets Tau vector. Contains additional information on Q - used for native solver. /// Gets or sets Tau vector. Contains additional information on Q - used for native solver.
/// </summary> /// </summary>
internal float[] Tau float[] Tau { get; set; }
{
get;
set;
}
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="DenseQR"/> class. This object will compute the /// Initializes a new instance of the <see cref="DenseQR"/> class. This object will compute the
@ -62,35 +58,37 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
/// <param name="method">The QR factorization method to use.</param> /// <param name="method">The QR factorization method to use.</param>
/// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">If <paramref name="matrix"/> row count is less then column count</exception> /// <exception cref="ArgumentException">If <paramref name="matrix"/> row count is less then column count</exception>
public DenseQR(DenseMatrix matrix, QRMethod method = QRMethod.Full) public static DenseQR Create(DenseMatrix matrix, QRMethod method = QRMethod.Full)
{ {
if (matrix == null)
{
throw new ArgumentNullException("matrix");
}
if (matrix.RowCount < matrix.ColumnCount) if (matrix.RowCount < matrix.ColumnCount)
{ {
throw Matrix.DimensionsDontMatch<ArgumentException>(matrix); throw Matrix.DimensionsDontMatch<ArgumentException>(matrix);
} }
QrMethod = method; var Tau = new float[Math.Min(matrix.RowCount, matrix.ColumnCount)];
Tau = new float[Math.Min(matrix.RowCount, matrix.ColumnCount)]; Matrix<float> Q;
Matrix<float> MatrixR;
if (method == QRMethod.Full) if (method == QRMethod.Full)
{ {
MatrixR = matrix.Clone(); MatrixR = matrix.Clone();
Q = new DenseMatrix(matrix.RowCount); Q = new DenseMatrix(matrix.RowCount);
Control.LinearAlgebraProvider.QRFactor(((DenseMatrix)MatrixR).Values, matrix.RowCount, matrix.ColumnCount, Control.LinearAlgebraProvider.QRFactor(((DenseMatrix) MatrixR).Values, matrix.RowCount, matrix.ColumnCount, ((DenseMatrix) Q).Values, Tau);
((DenseMatrix)Q).Values, Tau);
} }
else else
{ {
Q = matrix.Clone(); Q = matrix.Clone();
MatrixR = new DenseMatrix(matrix.ColumnCount); MatrixR = new DenseMatrix(matrix.ColumnCount);
Control.LinearAlgebraProvider.ThinQRFactor(((DenseMatrix)Q).Values, matrix.RowCount, matrix.ColumnCount, Control.LinearAlgebraProvider.ThinQRFactor(((DenseMatrix) Q).Values, matrix.RowCount, matrix.ColumnCount, ((DenseMatrix) MatrixR).Values, Tau);
((DenseMatrix)MatrixR).Values, Tau);
} }
return new DenseQR(Q, MatrixR, method, Tau);
}
DenseQR(Matrix<float> q, Matrix<float> rFull, QRMethod method, float[] tau)
: base(q, rFull, method)
{
Tau = tau;
} }
/// <summary> /// <summary>
@ -100,17 +98,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
/// <param name="result">The left hand side <see cref="Matrix{T}"/>, <b>X</b>.</param> /// <param name="result">The left hand side <see cref="Matrix{T}"/>, <b>X</b>.</param>
public override void Solve(Matrix<float> input, Matrix<float> result) public override void Solve(Matrix<float> input, Matrix<float> result)
{ {
// Check for proper arguments.
if (input == null)
{
throw new ArgumentNullException("input");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
// The solution X should have the same number of columns as B // The solution X should have the same number of columns as B
if (input.ColumnCount != result.ColumnCount) if (input.ColumnCount != result.ColumnCount)
{ {
@ -124,7 +111,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
} }
// The solution X row dimension is equal to the column dimension of A // The solution X row dimension is equal to the column dimension of A
if (MatrixR.ColumnCount != result.RowCount) if (FullR.ColumnCount != result.RowCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension); throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension);
} }
@ -141,7 +128,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
throw new NotSupportedException("Can only do QR factorization for dense matrices at the moment."); throw new NotSupportedException("Can only do QR factorization for dense matrices at the moment.");
} }
Control.LinearAlgebraProvider.QRSolveFactored(((DenseMatrix)Q).Values, ((DenseMatrix)MatrixR).Values, Q.RowCount, MatrixR.ColumnCount, Tau, dinput.Values, input.ColumnCount, dresult.Values, QrMethod); Control.LinearAlgebraProvider.QRSolveFactored(((DenseMatrix) Q).Values, ((DenseMatrix) FullR).Values, Q.RowCount, FullR.ColumnCount, Tau, dinput.Values, input.ColumnCount, dresult.Values, Method);
} }
/// <summary> /// <summary>
@ -151,16 +138,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
/// <param name="result">The left hand side <see cref="Matrix{T}"/>, <b>x</b>.</param> /// <param name="result">The left hand side <see cref="Matrix{T}"/>, <b>x</b>.</param>
public override void Solve(Vector<float> input, Vector<float> result) public override void Solve(Vector<float> input, Vector<float> result)
{ {
if (input == null)
{
throw new ArgumentNullException("input");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
// Ax=b where A is an m x n matrix // Ax=b where A is an m x n matrix
// Check that b is a column vector with m entries // Check that b is a column vector with m entries
if (Q.RowCount != input.Count) if (Q.RowCount != input.Count)
@ -169,9 +146,9 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
} }
// Check that x is a column vector with n entries // Check that x is a column vector with n entries
if (MatrixR.ColumnCount != result.Count) if (FullR.ColumnCount != result.Count)
{ {
throw Matrix.DimensionsDontMatch<ArgumentException>(MatrixR, result); throw Matrix.DimensionsDontMatch<ArgumentException>(FullR, result);
} }
var dinput = input as DenseVector; var dinput = input as DenseVector;
@ -186,7 +163,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
throw new NotSupportedException("Can only do QR factorization for dense vectors at the moment."); throw new NotSupportedException("Can only do QR factorization for dense vectors at the moment.");
} }
Control.LinearAlgebraProvider.QRSolveFactored(((DenseMatrix)Q).Values, ((DenseMatrix)MatrixR).Values, Q.RowCount, MatrixR.ColumnCount, Tau, dinput.Values, 1, dresult.Values, QrMethod); Control.LinearAlgebraProvider.QRSolveFactored(((DenseMatrix) Q).Values, ((DenseMatrix) FullR).Values, Q.RowCount, FullR.ColumnCount, Tau, dinput.Values, 1, dresult.Values, Method);
} }
} }
} }

12
src/Numerics/LinearAlgebra/Single/Factorization/GramSchmidt.cs

@ -55,16 +55,16 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
{ {
get get
{ {
if (MatrixR.RowCount != MatrixR.ColumnCount) if (FullR.RowCount != FullR.ColumnCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixSquare); throw new ArgumentException(Resources.ArgumentMatrixSquare);
} }
var det = 1.0; var det = 1.0;
for (var i = 0; i < MatrixR.ColumnCount; i++) for (var i = 0; i < FullR.ColumnCount; i++)
{ {
det *= MatrixR.At(i, i); det *= FullR.At(i, i);
if (Math.Abs(MatrixR.At(i, i)).AlmostEqual(0.0f)) if (Math.Abs(FullR.At(i, i)).AlmostEqual(0.0f))
{ {
return 0; return 0;
} }
@ -82,9 +82,9 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
{ {
get get
{ {
for (var i = 0; i < MatrixR.ColumnCount; i++) for (var i = 0; i < FullR.ColumnCount; i++)
{ {
if (Math.Abs(MatrixR.At(i, i)).AlmostEqual(0.0f)) if (Math.Abs(FullR.At(i, i)).AlmostEqual(0.0f))
{ {
return false; return false;
} }

28
src/Numerics/LinearAlgebra/Single/Factorization/QR.cs

@ -3,7 +3,9 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// Copyright (c) 2009-2010 Math.NET //
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -12,8 +14,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -24,13 +28,12 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Factorization;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.LinearAlgebra.Single.Factorization namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
{ {
using System;
using Properties;
/// <summary> /// <summary>
/// <para>A class which encapsulates the functionality of the QR decomposition.</para> /// <para>A class which encapsulates the functionality of the QR decomposition.</para>
/// <para>Any real square matrix A (m x n) may be decomposed as A = QR where Q is an orthogonal matrix /// <para>Any real square matrix A (m x n) may be decomposed as A = QR where Q is an orthogonal matrix
@ -45,6 +48,11 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
/// </remarks> /// </remarks>
public abstract class QR : QR<float> public abstract class QR : QR<float>
{ {
protected QR(Matrix<float> q, Matrix<float> rFull, QRMethod method)
: base(q, rFull, method)
{
}
/// <summary> /// <summary>
/// Gets the absolute determinant value of the matrix for which the QR matrix was computed. /// Gets the absolute determinant value of the matrix for which the QR matrix was computed.
/// </summary> /// </summary>
@ -52,16 +60,16 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
{ {
get get
{ {
if (MatrixR.RowCount != MatrixR.ColumnCount) if (FullR.RowCount != FullR.ColumnCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixSquare); throw new ArgumentException(Resources.ArgumentMatrixSquare);
} }
var det = 1.0; var det = 1.0;
for (var i = 0; i < MatrixR.ColumnCount; i++) for (var i = 0; i < FullR.ColumnCount; i++)
{ {
det *= MatrixR.At(i, i); det *= FullR.At(i, i);
if (Math.Abs(MatrixR.At(i, i)).AlmostEqual(0.0f)) if (Math.Abs(FullR.At(i, i)).AlmostEqual(0.0f))
{ {
return 0; return 0;
} }
@ -79,9 +87,9 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
{ {
get get
{ {
for (var i = 0; i < MatrixR.ColumnCount; i++) for (var i = 0; i < FullR.ColumnCount; i++)
{ {
if (Math.Abs(MatrixR.At(i, i)).AlmostEqual(0.0f)) if (Math.Abs(FullR.At(i, i)).AlmostEqual(0.0f))
{ {
return false; return false;
} }

12
src/Numerics/LinearAlgebra/Single/Factorization/UserGramSchmidt.cs

@ -147,19 +147,19 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
{ {
for (var j = 0; j < input.ColumnCount; j++) for (var j = 0; j < input.ColumnCount; j++)
{ {
inputCopy.At(k, j, inputCopy.At(k, j) / MatrixR.At(k, k)); inputCopy.At(k, j, inputCopy.At(k, j) / FullR.At(k, k));
} }
for (var i = 0; i < k; i++) for (var i = 0; i < k; i++)
{ {
for (var j = 0; j < input.ColumnCount; j++) for (var j = 0; j < input.ColumnCount; j++)
{ {
inputCopy.At(i, j, inputCopy.At(i, j) - (inputCopy.At(k, j) * MatrixR.At(i, k))); inputCopy.At(i, j, inputCopy.At(i, j) - (inputCopy.At(k, j) * FullR.At(i, k)));
} }
} }
} }
for (var i = 0; i < MatrixR.ColumnCount; i++) for (var i = 0; i < FullR.ColumnCount; i++)
{ {
for (var j = 0; j < input.ColumnCount; j++) for (var j = 0; j < input.ColumnCount; j++)
{ {
@ -211,14 +211,14 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
// Solve R*X = Y; // Solve R*X = Y;
for (var k = Q.ColumnCount - 1; k >= 0; k--) for (var k = Q.ColumnCount - 1; k >= 0; k--)
{ {
inputCopy[k] /= MatrixR.At(k, k); inputCopy[k] /= FullR.At(k, k);
for (var i = 0; i < k; i++) for (var i = 0; i < k; i++)
{ {
inputCopy[i] -= inputCopy[k] * MatrixR.At(i, k); inputCopy[i] -= inputCopy[k] * FullR.At(i, k);
} }
} }
for (var i = 0; i < MatrixR.ColumnCount; i++) for (var i = 0; i < FullR.ColumnCount; i++)
{ {
result[i] = inputCopy[i]; result[i] = inputCopy[i];
} }

148
src/Numerics/LinearAlgebra/Single/Factorization/UserQR.cs

@ -4,7 +4,7 @@
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
// //
// Copyright (c) 2009-2010 Math.NET // Copyright (c) 2009-2013 Math.NET
// //
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
@ -28,11 +28,11 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using System.Linq;
using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Factorization;
using MathNet.Numerics.Properties; using MathNet.Numerics.Properties;
using MathNet.Numerics.Threading; using MathNet.Numerics.Threading;
using System;
using System.Linq;
namespace MathNet.Numerics.LinearAlgebra.Single.Factorization namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
{ {
@ -45,7 +45,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
/// <remarks> /// <remarks>
/// The computation of the QR decomposition is done at construction time by Householder transformation. /// The computation of the QR decomposition is done at construction time by Householder transformation.
/// </remarks> /// </remarks>
public class UserQR : QR public sealed class UserQR : QR
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="UserQR"/> class. This object will compute the /// Initializes a new instance of the <see cref="UserQR"/> class. This object will compute the
@ -54,71 +54,70 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
/// <param name="matrix">The matrix to factor.</param> /// <param name="matrix">The matrix to factor.</param>
/// <param name="method">The QR factorization method to use.</param> /// <param name="method">The QR factorization method to use.</param>
/// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception>
public UserQR(Matrix<float> matrix, QRMethod method = QRMethod.Full) public static UserQR Create(Matrix<float> matrix, QRMethod method = QRMethod.Full)
{ {
if (matrix == null)
{
throw new ArgumentNullException("matrix");
}
if (matrix.RowCount < matrix.ColumnCount) if (matrix.RowCount < matrix.ColumnCount)
{ {
throw Matrix.DimensionsDontMatch<ArgumentException>(matrix); throw Matrix.DimensionsDontMatch<ArgumentException>(matrix);
} }
QrMethod = method; Matrix<float> q;
Matrix<float> r;
var minmn = Math.Min(matrix.RowCount, matrix.ColumnCount); var minmn = Math.Min(matrix.RowCount, matrix.ColumnCount);
var u = new float[minmn][]; var u = new float[minmn][];
if (method == QRMethod.Full) if (method == QRMethod.Full)
{ {
MatrixR = matrix.Clone(); r = matrix.Clone();
Q = matrix.CreateMatrix(matrix.RowCount, matrix.RowCount); q = matrix.CreateMatrix(matrix.RowCount, matrix.RowCount);
for (var i = 0; i < matrix.RowCount; i++) for (var i = 0; i < matrix.RowCount; i++)
{ {
Q.At(i, i, 1.0f); q.At(i, i, 1.0f);
} }
for (var i = 0; i < minmn; i++) for (var i = 0; i < minmn; i++)
{ {
u[i] = GenerateColumn(MatrixR, i, i); u[i] = GenerateColumn(r, i, i);
ComputeQR(u[i], MatrixR, i, matrix.RowCount, i + 1, matrix.ColumnCount, ComputeQR(u[i], r, i, matrix.RowCount, i + 1, matrix.ColumnCount, Control.NumberOfParallelWorkerThreads);
Control.NumberOfParallelWorkerThreads);
} }
for (var i = minmn - 1; i >= 0; i--) for (var i = minmn - 1; i >= 0; i--)
{ {
ComputeQR(u[i], Q, i, matrix.RowCount, i, matrix.RowCount, ComputeQR(u[i], q, i, matrix.RowCount, i, matrix.RowCount, Control.NumberOfParallelWorkerThreads);
Control.NumberOfParallelWorkerThreads);
} }
} }
else else
{ {
MatrixR = matrix.CreateMatrix(matrix.ColumnCount, matrix.ColumnCount); q = matrix.Clone();
Q = matrix.Clone();
for (var i = 0; i < minmn; i++) for (var i = 0; i < minmn; i++)
{ {
u[i] = GenerateColumn(Q, i, i); u[i] = GenerateColumn(q, i, i);
ComputeQR(u[i], Q, i, matrix.RowCount, i + 1, matrix.ColumnCount, ComputeQR(u[i], q, i, matrix.RowCount, i + 1, matrix.ColumnCount, Control.NumberOfParallelWorkerThreads);
Control.NumberOfParallelWorkerThreads);
} }
MatrixR = Q.SubMatrix(0, matrix.ColumnCount, 0, matrix.ColumnCount); r = q.SubMatrix(0, matrix.ColumnCount, 0, matrix.ColumnCount);
Q.Clear(); q.Clear();
for (var i = 0; i < matrix.ColumnCount; i++) for (var i = 0; i < matrix.ColumnCount; i++)
{ {
Q.At(i, i, 1.0f); q.At(i, i, 1.0f);
} }
for (var i = minmn - 1; i >= 0; i--) for (var i = minmn - 1; i >= 0; i--)
{ {
ComputeQR(u[i], Q, i, matrix.RowCount, i, matrix.ColumnCount, ComputeQR(u[i], q, i, matrix.RowCount, i, matrix.ColumnCount, Control.NumberOfParallelWorkerThreads);
Control.NumberOfParallelWorkerThreads);
} }
} }
return new UserQR(q, r, method);
}
UserQR(Matrix<float> q, Matrix<float> rFull, QRMethod method)
: base(q, rFull, method)
{
} }
/// <summary> /// <summary>
@ -128,7 +127,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
/// <param name="row">The first row</param> /// <param name="row">The first row</param>
/// <param name="column">Column index</param> /// <param name="column">Column index</param>
/// <returns>Generated vector</returns> /// <returns>Generated vector</returns>
private static float[] GenerateColumn(Matrix<float> a, int row, int column) static float[] GenerateColumn(Matrix<float> a, int row, int column)
{ {
var ru = a.RowCount - row; var ru = a.RowCount - row;
var u = new float[ru]; var u = new float[ru];
@ -139,23 +138,23 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
a.At(i, row, 0.0f); a.At(i, row, 0.0f);
} }
var norm = u.Sum(t => t * t); var norm = u.Sum(t => t*t);
norm = (float)Math.Sqrt(norm); norm = (float) Math.Sqrt(norm);
if (row == a.RowCount - 1 || norm == 0) if (row == a.RowCount - 1 || norm == 0)
{ {
a.At(row, column, -u[0]); a.At(row, column, -u[0]);
u[0] = (float)Constants.Sqrt2; u[0] = (float) Constants.Sqrt2;
return u; return u;
} }
var scale = 1.0f / norm; var scale = 1.0f/norm;
if (u[0] < 0.0) if (u[0] < 0.0)
{ {
scale *= -1.0f; scale *= -1.0f;
} }
a.At(row, column, -1.0f / scale); a.At(row, column, -1.0f/scale);
for (var i = 0; i < ru; i++) for (var i = 0; i < ru; i++)
{ {
@ -163,7 +162,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
} }
u[0] += 1.0f; u[0] += 1.0f;
var s = (float)Math.Sqrt(1.0 / u[0]); var s = (float) Math.Sqrt(1.0/u[0]);
for (var i = 0; i < ru; i++) for (var i = 0; i < ru; i++)
{ {
@ -183,7 +182,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
/// <param name="columnStart">The first column</param> /// <param name="columnStart">The first column</param>
/// <param name="columnDim">The last column</param> /// <param name="columnDim">The last column</param>
/// <param name="availableCores">Number of available CPUs</param> /// <param name="availableCores">Number of available CPUs</param>
private static void ComputeQR(float[] u, Matrix<float> a, int rowStart, int rowDim, int columnStart, int columnDim, int availableCores) static void ComputeQR(float[] u, Matrix<float> a, int rowStart, int rowDim, int columnStart, int columnDim, int availableCores)
{ {
if (rowDim < rowStart || columnDim < columnStart) if (rowDim < rowStart || columnDim < columnStart)
{ {
@ -194,8 +193,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
if ((availableCores > 1) && (tmpColCount > 200)) if ((availableCores > 1) && (tmpColCount > 200))
{ {
var tmpSplit = columnStart + (tmpColCount / 2); var tmpSplit = columnStart + (tmpColCount/2);
var tmpCores = availableCores / 2; var tmpCores = availableCores/2;
CommonParallel.Invoke( CommonParallel.Invoke(
() => ComputeQR(u, a, rowStart, rowDim, columnStart, tmpSplit, tmpCores), () => ComputeQR(u, a, rowStart, rowDim, columnStart, tmpSplit, tmpCores),
@ -208,12 +207,12 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
var scale = 0.0f; var scale = 0.0f;
for (var i = rowStart; i < rowDim; i++) for (var i = rowStart; i < rowDim; i++)
{ {
scale += u[i - rowStart] * a.At(i, j); scale += u[i - rowStart]*a.At(i, j);
} }
for (var i = rowStart; i < rowDim; i++) for (var i = rowStart; i < rowDim; i++)
{ {
a.At(i, j, a.At(i, j) - (u[i - rowStart] * scale)); a.At(i, j, a.At(i, j) - (u[i - rowStart]*scale));
} }
} }
} }
@ -226,17 +225,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
/// <param name="result">The left hand side <see cref="Matrix{T}"/>, <b>X</b>.</param> /// <param name="result">The left hand side <see cref="Matrix{T}"/>, <b>X</b>.</param>
public override void Solve(Matrix<float> input, Matrix<float> result) public override void Solve(Matrix<float> input, Matrix<float> result)
{ {
// Check for proper arguments.
if (input == null)
{
throw new ArgumentNullException("input");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
// The solution X should have the same number of columns as B // The solution X should have the same number of columns as B
if (input.ColumnCount != result.ColumnCount) if (input.ColumnCount != result.ColumnCount)
{ {
@ -244,13 +232,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
} }
// The dimension compatibility conditions for X = A\B require the two matrices A and B to have the same number of rows // The dimension compatibility conditions for X = A\B require the two matrices A and B to have the same number of rows
if (MatrixR.RowCount != input.RowCount) if (FullR.RowCount != input.RowCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension); throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension);
} }
// The solution X row dimension is equal to the column dimension of A // The solution X row dimension is equal to the column dimension of A
if (MatrixR.ColumnCount != result.RowCount) if (FullR.ColumnCount != result.RowCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension); throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension);
} }
@ -258,20 +246,20 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
var inputCopy = input.Clone(); var inputCopy = input.Clone();
// Compute Y = transpose(Q)*B // Compute Y = transpose(Q)*B
var column = new float[MatrixR.RowCount]; var column = new float[FullR.RowCount];
for (var j = 0; j < input.ColumnCount; j++) for (var j = 0; j < input.ColumnCount; j++)
{ {
for (var k = 0; k < MatrixR.RowCount; k++) for (var k = 0; k < FullR.RowCount; k++)
{ {
column[k] = inputCopy.At(k, j); column[k] = inputCopy.At(k, j);
} }
for (var i = 0; i < MatrixR.RowCount; i++) for (var i = 0; i < FullR.RowCount; i++)
{ {
float s = 0; float s = 0;
for (var k = 0; k < MatrixR.RowCount; k++) for (var k = 0; k < FullR.RowCount; k++)
{ {
s += Q.At(k, i) * column[k]; s += Q.At(k, i)*column[k];
} }
inputCopy.At(i, j, s); inputCopy.At(i, j, s);
@ -279,23 +267,23 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
} }
// Solve R*X = Y; // Solve R*X = Y;
for (var k = MatrixR.ColumnCount - 1; k >= 0; k--) for (var k = FullR.ColumnCount - 1; k >= 0; k--)
{ {
for (var j = 0; j < input.ColumnCount; j++) for (var j = 0; j < input.ColumnCount; j++)
{ {
inputCopy.At(k, j, inputCopy.At(k, j) / MatrixR.At(k, k)); inputCopy.At(k, j, inputCopy.At(k, j)/FullR.At(k, k));
} }
for (var i = 0; i < k; i++) for (var i = 0; i < k; i++)
{ {
for (var j = 0; j < input.ColumnCount; j++) for (var j = 0; j < input.ColumnCount; j++)
{ {
inputCopy.At(i, j, inputCopy.At(i, j) - (inputCopy.At(k, j) * MatrixR.At(i, k))); inputCopy.At(i, j, inputCopy.At(i, j) - (inputCopy.At(k, j)*FullR.At(i, k)));
} }
} }
} }
for (var i = 0; i < MatrixR.ColumnCount; i++) for (var i = 0; i < FullR.ColumnCount; i++)
{ {
for (var j = 0; j < inputCopy.ColumnCount; j++) for (var j = 0; j < inputCopy.ColumnCount; j++)
{ {
@ -311,60 +299,50 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
/// <param name="result">The left hand side <see cref="Matrix{T}"/>, <b>x</b>.</param> /// <param name="result">The left hand side <see cref="Matrix{T}"/>, <b>x</b>.</param>
public override void Solve(Vector<float> input, Vector<float> result) public override void Solve(Vector<float> input, Vector<float> result)
{ {
if (input == null)
{
throw new ArgumentNullException("input");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
// Ax=b where A is an m x n matrix // Ax=b where A is an m x n matrix
// Check that b is a column vector with m entries // Check that b is a column vector with m entries
if (MatrixR.RowCount != input.Count) if (FullR.RowCount != input.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength); throw new ArgumentException(Resources.ArgumentVectorsSameLength);
} }
// Check that x is a column vector with n entries // Check that x is a column vector with n entries
if (MatrixR.ColumnCount != result.Count) if (FullR.ColumnCount != result.Count)
{ {
throw Matrix.DimensionsDontMatch<ArgumentException>(MatrixR, result); throw Matrix.DimensionsDontMatch<ArgumentException>(FullR, result);
} }
var inputCopy = input.Clone(); var inputCopy = input.Clone();
// Compute Y = transpose(Q)*B // Compute Y = transpose(Q)*B
var column = new float[MatrixR.RowCount]; var column = new float[FullR.RowCount];
for (var k = 0; k < MatrixR.RowCount; k++) for (var k = 0; k < FullR.RowCount; k++)
{ {
column[k] = inputCopy[k]; column[k] = inputCopy[k];
} }
for (var i = 0; i < MatrixR.RowCount; i++) for (var i = 0; i < FullR.RowCount; i++)
{ {
float s = 0; float s = 0;
for (var k = 0; k < MatrixR.RowCount; k++) for (var k = 0; k < FullR.RowCount; k++)
{ {
s += Q.At(k, i) * column[k]; s += Q.At(k, i)*column[k];
} }
inputCopy[i] = s; inputCopy[i] = s;
} }
// Solve R*X = Y; // Solve R*X = Y;
for (var k = MatrixR.ColumnCount - 1; k >= 0; k--) for (var k = FullR.ColumnCount - 1; k >= 0; k--)
{ {
inputCopy[k] /= MatrixR.At(k, k); inputCopy[k] /= FullR.At(k, k);
for (var i = 0; i < k; i++) for (var i = 0; i < k; i++)
{ {
inputCopy[i] -= inputCopy[k] * MatrixR.At(i, k); inputCopy[i] -= inputCopy[k]*FullR.At(i, k);
} }
} }
for (var i = 0; i < MatrixR.ColumnCount; i++) for (var i = 0; i < FullR.ColumnCount; i++)
{ {
result[i] = inputCopy[i]; result[i] = inputCopy[i];
} }

2
src/Numerics/LinearAlgebra/Single/Matrix.cs

@ -471,7 +471,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
public override QR<float> QR(QRMethod method = QRMethod.Thin) public override QR<float> QR(QRMethod method = QRMethod.Thin)
{ {
return new UserQR(this, method); return UserQR.Create(this, method);
} }
public override GramSchmidt<float> GramSchmidt() public override GramSchmidt<float> GramSchmidt()

13
src/UnitTests/LinearAlgebraTests/Complex/Factorization/QRTests.cs

@ -24,11 +24,11 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Complex; using MathNet.Numerics.LinearAlgebra.Complex;
using MathNet.Numerics.LinearAlgebra.Complex.Factorization; using MathNet.Numerics.LinearAlgebra.Complex.Factorization;
using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Factorization;
using NUnit.Framework; using NUnit.Framework;
using System;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Factorization namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Factorization
{ {
@ -39,22 +39,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Factorization
/// </summary> /// </summary>
public class QRTests public class QRTests
{ {
/// <summary>
/// Constructor with <c>null</c> throws <c>ArgumentNullException</c>.
/// </summary>
[Test]
public void ConstructorNull()
{
Assert.Throws<ArgumentNullException>(() => new DenseQR(null));
}
/// <summary> /// <summary>
/// Constructor with wide matrix throws <c>ArgumentException</c>. /// Constructor with wide matrix throws <c>ArgumentException</c>.
/// </summary> /// </summary>
[Test] [Test]
public void ConstructorWideMatrixThrowsInvalidMatrixOperationException() public void ConstructorWideMatrixThrowsInvalidMatrixOperationException()
{ {
Assert.Throws<ArgumentException>(() => new DenseQR(new DenseMatrix(3, 4))); Assert.Throws<ArgumentException>(() => UserQR.Create(new DenseMatrix(3, 4)));
} }
/// <summary> /// <summary>

13
src/UnitTests/LinearAlgebraTests/Complex/Factorization/UserQRTests.cs

@ -24,10 +24,10 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Complex.Factorization; using MathNet.Numerics.LinearAlgebra.Complex.Factorization;
using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Factorization;
using NUnit.Framework; using NUnit.Framework;
using System;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Factorization namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Factorization
{ {
@ -38,22 +38,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Factorization
/// </summary> /// </summary>
public class UserQRTests public class UserQRTests
{ {
/// <summary>
/// Constructor with <c>null</c> throws <c>ArgumentNullException</c>.
/// </summary>
[Test]
public void ConstructorNull()
{
Assert.Throws<ArgumentNullException>(() => new UserQR(null));
}
/// <summary> /// <summary>
/// Constructor with wide matrix throws <c>ArgumentException</c>. /// Constructor with wide matrix throws <c>ArgumentException</c>.
/// </summary> /// </summary>
[Test] [Test]
public void ConstructorWideMatrixThrowsInvalidMatrixOperationException() public void ConstructorWideMatrixThrowsInvalidMatrixOperationException()
{ {
Assert.Throws<ArgumentException>(() => new UserQR(new UserDefinedMatrix(3, 4))); Assert.Throws<ArgumentException>(() => UserQR.Create(new UserDefinedMatrix(3, 4)));
} }
/// <summary> /// <summary>

13
src/UnitTests/LinearAlgebraTests/Complex32/Factorization/QRTests.cs

@ -24,11 +24,11 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Complex32; using MathNet.Numerics.LinearAlgebra.Complex32;
using MathNet.Numerics.LinearAlgebra.Complex32.Factorization; using MathNet.Numerics.LinearAlgebra.Complex32.Factorization;
using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Factorization;
using NUnit.Framework; using NUnit.Framework;
using System;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Factorization namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Factorization
{ {
@ -39,22 +39,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Factorization
/// </summary> /// </summary>
public class QRTests public class QRTests
{ {
/// <summary>
/// Constructor with <c>null</c> throws <c>ArgumentNullException</c>.
/// </summary>
[Test]
public void ConstructorNull()
{
Assert.Throws<ArgumentNullException>(() => new DenseQR(null));
}
/// <summary> /// <summary>
/// Constructor with wide matrix throws <c>ArgumentException</c>. /// Constructor with wide matrix throws <c>ArgumentException</c>.
/// </summary> /// </summary>
[Test] [Test]
public void ConstructorWideMatrixThrowsInvalidMatrixOperationException() public void ConstructorWideMatrixThrowsInvalidMatrixOperationException()
{ {
Assert.Throws<ArgumentException>(() => new DenseQR(new DenseMatrix(3, 4))); Assert.Throws<ArgumentException>(() => UserQR.Create(new DenseMatrix(3, 4)));
} }
/// <summary> /// <summary>

13
src/UnitTests/LinearAlgebraTests/Complex32/Factorization/UserQRTests.cs

@ -24,10 +24,10 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Complex32.Factorization; using MathNet.Numerics.LinearAlgebra.Complex32.Factorization;
using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Factorization;
using NUnit.Framework; using NUnit.Framework;
using System;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Factorization namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Factorization
{ {
@ -38,22 +38,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Factorization
/// </summary> /// </summary>
public class UserQRTests public class UserQRTests
{ {
/// <summary>
/// Constructor with <c>null</c> throws <c>ArgumentNullException</c>.
/// </summary>
[Test]
public void ConstructorNull()
{
Assert.Throws<ArgumentNullException>(() => new UserQR(null));
}
/// <summary> /// <summary>
/// Constructor with wide matrix throws <c>ArgumentException</c>. /// Constructor with wide matrix throws <c>ArgumentException</c>.
/// </summary> /// </summary>
[Test] [Test]
public void ConstructorWideMatrixThrowsInvalidMatrixOperationException() public void ConstructorWideMatrixThrowsInvalidMatrixOperationException()
{ {
Assert.Throws<ArgumentException>(() => new UserQR(new UserDefinedMatrix(3, 4))); Assert.Throws<ArgumentException>(() => UserQR.Create(new UserDefinedMatrix(3, 4)));
} }
/// <summary> /// <summary>

13
src/UnitTests/LinearAlgebraTests/Double/Factorization/QRTests.cs

@ -24,11 +24,11 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Double; using MathNet.Numerics.LinearAlgebra.Double;
using MathNet.Numerics.LinearAlgebra.Double.Factorization; using MathNet.Numerics.LinearAlgebra.Double.Factorization;
using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Factorization;
using NUnit.Framework; using NUnit.Framework;
using System;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Factorization namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Factorization
{ {
@ -37,22 +37,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Factorization
/// </summary> /// </summary>
public class QRTests public class QRTests
{ {
/// <summary>
/// Constructor with <c>null</c> throws <c>ArgumentNullException</c>.
/// </summary>
[Test]
public void ConstructorNull()
{
Assert.Throws<ArgumentNullException>(() => new DenseQR(null));
}
/// <summary> /// <summary>
/// Constructor with wide matrix throws <c>ArgumentException</c>. /// Constructor with wide matrix throws <c>ArgumentException</c>.
/// </summary> /// </summary>
[Test] [Test]
public void ConstructorWideMatrixThrowsInvalidMatrixOperationException() public void ConstructorWideMatrixThrowsInvalidMatrixOperationException()
{ {
Assert.Throws<ArgumentException>(() => new DenseQR(new DenseMatrix(3, 4))); Assert.Throws<ArgumentException>(() => UserQR.Create(new DenseMatrix(3, 4)));
} }
/// <summary> /// <summary>

13
src/UnitTests/LinearAlgebraTests/Double/Factorization/UserQRTests.cs

@ -24,10 +24,10 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Double.Factorization; using MathNet.Numerics.LinearAlgebra.Double.Factorization;
using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Factorization;
using NUnit.Framework; using NUnit.Framework;
using System;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Factorization namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Factorization
{ {
@ -36,22 +36,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Factorization
/// </summary> /// </summary>
public class UserQRTests public class UserQRTests
{ {
/// <summary>
/// Constructor with <c>null</c> throws <c>ArgumentNullException</c>.
/// </summary>
[Test]
public void ConstructorNull()
{
Assert.Throws<ArgumentNullException>(() => new UserQR(null));
}
/// <summary> /// <summary>
/// Constructor with wide matrix throws <c>ArgumentException</c>. /// Constructor with wide matrix throws <c>ArgumentException</c>.
/// </summary> /// </summary>
[Test] [Test]
public void ConstructorWideMatrixThrowsInvalidMatrixOperationException() public void ConstructorWideMatrixThrowsInvalidMatrixOperationException()
{ {
Assert.Throws<ArgumentException>(() => new UserQR(new UserDefinedMatrix(3, 4))); Assert.Throws<ArgumentException>(() => UserQR.Create(new UserDefinedMatrix(3, 4)));
} }
/// <summary> /// <summary>

13
src/UnitTests/LinearAlgebraTests/Single/Factorization/QRTests.cs

@ -24,11 +24,11 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Factorization;
using MathNet.Numerics.LinearAlgebra.Single; using MathNet.Numerics.LinearAlgebra.Single;
using MathNet.Numerics.LinearAlgebra.Single.Factorization; using MathNet.Numerics.LinearAlgebra.Single.Factorization;
using NUnit.Framework; using NUnit.Framework;
using System;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Factorization namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Factorization
{ {
@ -37,22 +37,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Factorization
/// </summary> /// </summary>
public class QRTests public class QRTests
{ {
/// <summary>
/// Constructor with <c>null</c> throws <c>ArgumentNullException</c>.
/// </summary>
[Test]
public void ConstructorNull()
{
Assert.Throws<ArgumentNullException>(() => new DenseQR(null));
}
/// <summary> /// <summary>
/// Constructor with wide matrix throws <c>ArgumentException</c>. /// Constructor with wide matrix throws <c>ArgumentException</c>.
/// </summary> /// </summary>
[Test] [Test]
public void ConstructorWideMatrixThrowsInvalidMatrixOperationException() public void ConstructorWideMatrixThrowsInvalidMatrixOperationException()
{ {
Assert.Throws<ArgumentException>(() => new DenseQR(new DenseMatrix(3, 4))); Assert.Throws<ArgumentException>(() => UserQR.Create(new DenseMatrix(3, 4)));
} }
/// <summary> /// <summary>

13
src/UnitTests/LinearAlgebraTests/Single/Factorization/UserQRTests.cs

@ -24,10 +24,10 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
// </copyright> // </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Factorization; using MathNet.Numerics.LinearAlgebra.Factorization;
using MathNet.Numerics.LinearAlgebra.Single.Factorization; using MathNet.Numerics.LinearAlgebra.Single.Factorization;
using NUnit.Framework; using NUnit.Framework;
using System;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Factorization namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Factorization
{ {
@ -36,22 +36,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Factorization
/// </summary> /// </summary>
public class UserQRTests public class UserQRTests
{ {
/// <summary>
/// Constructor with <c>null</c> throws <c>ArgumentNullException</c>.
/// </summary>
[Test]
public void ConstructorNull()
{
Assert.Throws<ArgumentNullException>(() => new UserQR(null));
}
/// <summary> /// <summary>
/// Constructor with wide matrix throws <c>ArgumentException</c>. /// Constructor with wide matrix throws <c>ArgumentException</c>.
/// </summary> /// </summary>
[Test] [Test]
public void ConstructorWideMatrixThrowsInvalidMatrixOperationException() public void ConstructorWideMatrixThrowsInvalidMatrixOperationException()
{ {
Assert.Throws<ArgumentException>(() => new UserQR(new UserDefinedMatrix(3, 4))); Assert.Throws<ArgumentException>(() => UserQR.Create(new UserDefinedMatrix(3, 4)));
} }
/// <summary> /// <summary>

Loading…
Cancel
Save