Browse Source

LA: make UnitPreconditioner generic & shared

pull/163/head
Christoph Ruegg 13 years ago
parent
commit
e5fed32a8b
  1. 3
      src/Numerics/LinearAlgebra/Complex/Solvers/BiCgStab.cs
  2. 3
      src/Numerics/LinearAlgebra/Complex/Solvers/GpBiCg.cs
  3. 3
      src/Numerics/LinearAlgebra/Complex/Solvers/MlkBiCgStab.cs
  4. 144
      src/Numerics/LinearAlgebra/Complex/Solvers/Preconditioners/UnitPreconditioner.cs
  5. 3
      src/Numerics/LinearAlgebra/Complex/Solvers/TFQMR.cs
  6. 3
      src/Numerics/LinearAlgebra/Complex32/Solvers/BiCgStab.cs
  7. 3
      src/Numerics/LinearAlgebra/Complex32/Solvers/GpBiCg.cs
  8. 3
      src/Numerics/LinearAlgebra/Complex32/Solvers/MlkBiCgStab.cs
  9. 139
      src/Numerics/LinearAlgebra/Complex32/Solvers/Preconditioners/UnitPreconditioner.cs
  10. 3
      src/Numerics/LinearAlgebra/Complex32/Solvers/TFQMR.cs
  11. 3
      src/Numerics/LinearAlgebra/Double/Solvers/BiCgStab.cs
  12. 3
      src/Numerics/LinearAlgebra/Double/Solvers/GpBiCg.cs
  13. 3
      src/Numerics/LinearAlgebra/Double/Solvers/MlkBiCgStab.cs
  14. 137
      src/Numerics/LinearAlgebra/Double/Solvers/Preconditioners/UnitPreconditioner.cs
  15. 2
      src/Numerics/LinearAlgebra/Double/Solvers/TFQMR.cs
  16. 3
      src/Numerics/LinearAlgebra/Single/Solvers/BiCgStab.cs
  17. 3
      src/Numerics/LinearAlgebra/Single/Solvers/GpBiCg.cs
  18. 3
      src/Numerics/LinearAlgebra/Single/Solvers/MlkBiCgStab.cs
  19. 3
      src/Numerics/LinearAlgebra/Single/Solvers/TFQMR.cs
  20. 13
      src/Numerics/LinearAlgebra/Solvers/UnitPreconditioner.cs
  21. 5
      src/Numerics/Numerics.csproj
  22. 5
      src/UnitTests/LinearAlgebraTests/Complex/Solvers/Preconditioners/UnitPreconditionerTest.cs
  23. 5
      src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Preconditioners/UnitPreconditionerTest.cs
  24. 4
      src/UnitTests/LinearAlgebraTests/Double/Solvers/Preconditioners/UnitPreconditionerTest.cs
  25. 4
      src/UnitTests/LinearAlgebraTests/Single/Solvers/Preconditioners/UnitPreconditionerTest.cs

3
src/Numerics/LinearAlgebra/Complex/Solvers/BiCgStab.cs

@ -29,7 +29,6 @@
// </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.Properties;
@ -278,7 +277,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers
if (_preconditioner == null)
{
_preconditioner = new UnitPreconditioner();
_preconditioner = new UnitPreconditioner<Complex>();
}
_preconditioner.Initialize(matrix);

3
src/Numerics/LinearAlgebra/Complex/Solvers/GpBiCg.cs

@ -29,7 +29,6 @@
// </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.Properties;
@ -322,7 +321,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers
if (_preconditioner == null)
{
_preconditioner = new UnitPreconditioner();
_preconditioner = new UnitPreconditioner<Complex>();
}
_preconditioner.Initialize(matrix);

3
src/Numerics/LinearAlgebra/Complex/Solvers/MlkBiCgStab.cs

@ -33,7 +33,6 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.Properties;
@ -343,7 +342,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers
if (_preconditioner == null)
{
_preconditioner = new UnitPreconditioner();
_preconditioner = new UnitPreconditioner<Complex>();
}
_preconditioner.Initialize(matrix);

144
src/Numerics/LinearAlgebra/Complex/Solvers/Preconditioners/UnitPreconditioner.cs

@ -1,144 +0,0 @@
// <copyright file="UnitPreconditioner.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2009-2010 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners
{
#if NOSYSNUMERICS
using Complex = Numerics.Complex;
#else
using Complex = System.Numerics.Complex;
#endif
/// <summary>
/// A unit preconditioner. This preconditioner does not actually do anything
/// it is only used when running an <see cref="IIterativeSolver{T}"/> without
/// a preconditioner.
/// </summary>
internal sealed class UnitPreconditioner : IPreConditioner<Complex>
{
/// <summary>
/// The coefficient matrix on which this preconditioner operates.
/// Is used to check dimensions on the different vectors that are processed.
/// </summary>
private int _size;
/// <summary>
/// Initializes the preconditioner and loads the internal data structures.
/// </summary>
/// <param name="matrix">
/// The matrix upon which the preconditioner is based.
/// </param>
/// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <see langword="null"/>. </exception>
/// <exception cref="ArgumentException">If <paramref name="matrix"/> is not a square matrix.</exception>
public void Initialize(Matrix<Complex> matrix)
{
if (matrix == null)
{
throw new ArgumentNullException("matrix");
}
if (matrix.RowCount != matrix.ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSquare, "matrix");
}
_size = matrix.RowCount;
}
/// <summary>
/// Approximates the solution to the matrix equation <b>Ax = b</b>.
/// </summary>
/// <param name="rhs">The right hand side vector.</param>
/// <param name="lhs">The left hand side vector. Also known as the result vector.</param>
/// <exception cref="ArgumentNullException">If <paramref name="rhs"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="lhs"/> is <see langword="null"/>. </exception>
/// <exception cref="ArgumentException">
/// <para>
/// If <paramref name="rhs"/> and <paramref name="lhs"/> do not have the same size.
/// </para>
/// <para>
/// - or -
/// </para>
/// <para>
/// If the size of <paramref name="rhs"/> is different the number of rows of the coefficient matrix.
/// </para>
/// </exception>
public void Approximate(Vector<Complex> rhs, Vector<Complex> lhs)
{
if (rhs == null)
{
throw new ArgumentNullException("rhs");
}
if (lhs == null)
{
throw new ArgumentNullException("lhs");
}
if ((lhs.Count != rhs.Count) || (lhs.Count != _size))
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength);
}
rhs.CopyTo(lhs);
}
/// <summary>
/// Approximates the solution to the matrix equation <b>Ax = b</b>.
/// </summary>
/// <param name="rhs">The right hand side vector.</param>
/// <returns>The left hand side vector.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="rhs"/> is <see langword="null"/>. </exception>
/// <exception cref="ArgumentException">
/// If the size of <paramref name="rhs"/> is different the number of rows of the coefficient matrix.
/// </exception>
public Vector<Complex> Approximate(Vector<Complex> rhs)
{
if (rhs == null)
{
throw new ArgumentNullException("rhs");
}
if (rhs.Count != _size)
{
throw new ArgumentException(Resources.ArgumentMatrixDimensions);
}
var result = new DenseVector(rhs.Count);
Approximate(rhs, result);
return result;
}
}
}

3
src/Numerics/LinearAlgebra/Complex/Solvers/TFQMR.cs

@ -29,7 +29,6 @@
// </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.Properties;
@ -262,7 +261,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers
if (_preconditioner == null)
{
_preconditioner = new UnitPreconditioner();
_preconditioner = new UnitPreconditioner<Complex>();
}
_preconditioner.Initialize(matrix);

3
src/Numerics/LinearAlgebra/Complex32/Solvers/BiCgStab.cs

@ -29,7 +29,6 @@
// </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.Properties;
@ -271,7 +270,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers
if (_preconditioner == null)
{
_preconditioner = new UnitPreconditioner();
_preconditioner = new UnitPreconditioner<Numerics.Complex32>();
}
_preconditioner.Initialize(matrix);

3
src/Numerics/LinearAlgebra/Complex32/Solvers/GpBiCg.cs

@ -29,7 +29,6 @@
// </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.Properties;
@ -320,7 +319,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers
if (_preconditioner == null)
{
_preconditioner = new UnitPreconditioner();
_preconditioner = new UnitPreconditioner<Numerics.Complex32>();
}
_preconditioner.Initialize(matrix);

3
src/Numerics/LinearAlgebra/Complex32/Solvers/MlkBiCgStab.cs

@ -33,7 +33,6 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.Properties;
@ -340,7 +339,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers
if (_preconditioner == null)
{
_preconditioner = new UnitPreconditioner();
_preconditioner = new UnitPreconditioner<Numerics.Complex32>();
}
_preconditioner.Initialize(matrix);

139
src/Numerics/LinearAlgebra/Complex32/Solvers/Preconditioners/UnitPreconditioner.cs

@ -1,139 +0,0 @@
// <copyright file="UnitPreconditioner.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2009-2010 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners
{
using Numerics;
/// <summary>
/// A unit preconditioner. This preconditioner does not actually do anything
/// it is only used when running an <see cref="IIterativeSolver{T}"/> without
/// a preconditioner.
/// </summary>
internal sealed class UnitPreconditioner : IPreConditioner<Complex32>
{
/// <summary>
/// The coefficient matrix on which this preconditioner operates.
/// Is used to check dimensions on the different vectors that are processed.
/// </summary>
private int _size;
/// <summary>
/// Initializes the preconditioner and loads the internal data structures.
/// </summary>
/// <param name="matrix">
/// The matrix upon which the preconditioner is based.
/// </param>
/// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <see langword="null"/>. </exception>
/// <exception cref="ArgumentException">If <paramref name="matrix"/> is not a square matrix.</exception>
public void Initialize(Matrix<Complex32> matrix)
{
if (matrix == null)
{
throw new ArgumentNullException("matrix");
}
if (matrix.RowCount != matrix.ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSquare, "matrix");
}
_size = matrix.RowCount;
}
/// <summary>
/// Approximates the solution to the matrix equation <b>Ax = b</b>.
/// </summary>
/// <param name="rhs">The right hand side vector.</param>
/// <param name="lhs">The left hand side vector. Also known as the result vector.</param>
/// <exception cref="ArgumentNullException">If <paramref name="rhs"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="lhs"/> is <see langword="null"/>. </exception>
/// <exception cref="ArgumentException">
/// <para>
/// If <paramref name="rhs"/> and <paramref name="lhs"/> do not have the same size.
/// </para>
/// <para>
/// - or -
/// </para>
/// <para>
/// If the size of <paramref name="rhs"/> is different the number of rows of the coefficient matrix.
/// </para>
/// </exception>
public void Approximate(Vector<Complex32> rhs, Vector<Complex32> lhs)
{
if (rhs == null)
{
throw new ArgumentNullException("rhs");
}
if (lhs == null)
{
throw new ArgumentNullException("lhs");
}
if ((lhs.Count != rhs.Count) || (lhs.Count != _size))
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength);
}
rhs.CopyTo(lhs);
}
/// <summary>
/// Approximates the solution to the matrix equation <b>Ax = b</b>.
/// </summary>
/// <param name="rhs">The right hand side vector.</param>
/// <returns>The left hand side vector.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="rhs"/> is <see langword="null"/>. </exception>
/// <exception cref="ArgumentException">
/// If the size of <paramref name="rhs"/> is different the number of rows of the coefficient matrix.
/// </exception>
public Vector<Complex32> Approximate(Vector<Complex32> rhs)
{
if (rhs == null)
{
throw new ArgumentNullException("rhs");
}
if (rhs.Count != _size)
{
throw new ArgumentException(Resources.ArgumentMatrixDimensions);
}
var result = new DenseVector(rhs.Count);
Approximate(rhs, result);
return result;
}
}
}

3
src/Numerics/LinearAlgebra/Complex32/Solvers/TFQMR.cs

@ -29,7 +29,6 @@
// </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.Properties;
@ -259,7 +258,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers
if (_preconditioner == null)
{
_preconditioner = new UnitPreconditioner();
_preconditioner = new UnitPreconditioner<Numerics.Complex32>();
}
_preconditioner.Initialize(matrix);

3
src/Numerics/LinearAlgebra/Double/Solvers/BiCgStab.cs

@ -29,7 +29,6 @@
// </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.Properties;
@ -273,7 +272,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers
if (_preconditioner == null)
{
_preconditioner = new UnitPreconditioner();
_preconditioner = new UnitPreconditioner<double>();
}
_preconditioner.Initialize(matrix);

3
src/Numerics/LinearAlgebra/Double/Solvers/GpBiCg.cs

@ -29,7 +29,6 @@
// </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.Properties;
@ -328,7 +327,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers
if (_preconditioner == null)
{
_preconditioner = new UnitPreconditioner();
_preconditioner = new UnitPreconditioner<double>();
}
_preconditioner.Initialize(matrix);

3
src/Numerics/LinearAlgebra/Double/Solvers/MlkBiCgStab.cs

@ -33,7 +33,6 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.Properties;
@ -348,7 +347,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers
if (_preconditioner == null)
{
_preconditioner = new UnitPreconditioner();
_preconditioner = new UnitPreconditioner<double>();
}
_preconditioner.Initialize(matrix);

137
src/Numerics/LinearAlgebra/Double/Solvers/Preconditioners/UnitPreconditioner.cs

@ -1,137 +0,0 @@
// <copyright file="UnitPreconditioner.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.LinearAlgebra.Double.Solvers.Preconditioners
{
/// <summary>
/// A unit preconditioner. This preconditioner does not actually do anything
/// it is only used when running an <see cref="IIterativeSolver{T}"/> without
/// a preconditioner.
/// </summary>
internal sealed class UnitPreconditioner : IPreConditioner<double>
{
/// <summary>
/// The coefficient matrix on which this preconditioner operates.
/// Is used to check dimensions on the different vectors that are processed.
/// </summary>
private int _size;
/// <summary>
/// Initializes the preconditioner and loads the internal data structures.
/// </summary>
/// <param name="matrix">
/// The matrix upon which the preconditioner is based.
/// </param>
/// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <see langword="null"/>. </exception>
/// <exception cref="ArgumentException">If <paramref name="matrix"/> is not a square matrix.</exception>
public void Initialize(Matrix<double> matrix)
{
if (matrix == null)
{
throw new ArgumentNullException("matrix");
}
if (matrix.RowCount != matrix.ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSquare, "matrix");
}
_size = matrix.RowCount;
}
/// <summary>
/// Approximates the solution to the matrix equation <b>Ax = b</b>.
/// </summary>
/// <param name="rhs">The right hand side vector.</param>
/// <param name="lhs">The left hand side vector. Also known as the result vector.</param>
/// <exception cref="ArgumentNullException">If <paramref name="rhs"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="lhs"/> is <see langword="null"/>. </exception>
/// <exception cref="ArgumentException">
/// <para>
/// If <paramref name="rhs"/> and <paramref name="lhs"/> do not have the same size.
/// </para>
/// <para>
/// - or -
/// </para>
/// <para>
/// If the size of <paramref name="rhs"/> is different the number of rows of the coefficient matrix.
/// </para>
/// </exception>
public void Approximate(Vector<double> rhs, Vector<double> lhs)
{
if (rhs == null)
{
throw new ArgumentNullException("rhs");
}
if (lhs == null)
{
throw new ArgumentNullException("lhs");
}
if ((lhs.Count != rhs.Count) || (lhs.Count != _size))
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength);
}
rhs.CopyTo(lhs);
}
/// <summary>
/// Approximates the solution to the matrix equation <b>Ax = b</b>.
/// </summary>
/// <param name="rhs">The right hand side vector.</param>
/// <returns>The left hand side vector.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="rhs"/> is <see langword="null"/>. </exception>
/// <exception cref="ArgumentException">
/// If the size of <paramref name="rhs"/> is different the number of rows of the coefficient matrix.
/// </exception>
public Vector<double> Approximate(Vector<double> rhs)
{
if (rhs == null)
{
throw new ArgumentNullException("rhs");
}
if (rhs.Count != _size)
{
throw new ArgumentException(Resources.ArgumentMatrixDimensions);
}
var result = new DenseVector(rhs.Count);
Approximate(rhs, result);
return result;
}
}
}

2
src/Numerics/LinearAlgebra/Double/Solvers/TFQMR.cs

@ -261,7 +261,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers
if (_preconditioner == null)
{
_preconditioner = new UnitPreconditioner();
_preconditioner = new UnitPreconditioner<double>();
}
_preconditioner.Initialize(matrix);

3
src/Numerics/LinearAlgebra/Single/Solvers/BiCgStab.cs

@ -29,7 +29,6 @@
// </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.Properties;
@ -273,7 +272,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers
if (_preconditioner == null)
{
_preconditioner = new UnitPreconditioner();
_preconditioner = new UnitPreconditioner<float>();
}
_preconditioner.Initialize(matrix);

3
src/Numerics/LinearAlgebra/Single/Solvers/GpBiCg.cs

@ -29,7 +29,6 @@
// </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.Properties;
@ -320,7 +319,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers
if (_preconditioner == null)
{
_preconditioner = new UnitPreconditioner();
_preconditioner = new UnitPreconditioner<float>();
}
_preconditioner.Initialize(matrix);

3
src/Numerics/LinearAlgebra/Single/Solvers/MlkBiCgStab.cs

@ -32,7 +32,6 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using MathNet.Numerics.Distributions;
using MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.Properties;
@ -347,7 +346,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers
if (_preconditioner == null)
{
_preconditioner = new UnitPreconditioner();
_preconditioner = new UnitPreconditioner<float>();
}
_preconditioner.Initialize(matrix);

3
src/Numerics/LinearAlgebra/Single/Solvers/TFQMR.cs

@ -29,7 +29,6 @@
// </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.LinearAlgebra.Solvers.Status;
using MathNet.Numerics.Properties;
@ -259,7 +258,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers
if (_preconditioner == null)
{
_preconditioner = new UnitPreconditioner();
_preconditioner = new UnitPreconditioner<float>();
}
_preconditioner.Initialize(matrix);

13
src/Numerics/LinearAlgebra/Single/Solvers/Preconditioners/UnitPreconditioner.cs → src/Numerics/LinearAlgebra/Solvers/UnitPreconditioner.cs

@ -29,17 +29,16 @@
// </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Solvers;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
namespace MathNet.Numerics.LinearAlgebra.Solvers
{
/// <summary>
/// A unit preconditioner. This preconditioner does not actually do anything
/// it is only used when running an <see cref="IIterativeSolver{T}"/> without
/// a preconditioner.
/// </summary>
internal sealed class UnitPreconditioner : IPreConditioner<float>
internal sealed class UnitPreconditioner<T> : IPreConditioner<T> where T : struct, IEquatable<T>, IFormattable
{
/// <summary>
/// The coefficient matrix on which this preconditioner operates.
@ -55,7 +54,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
/// </param>
/// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <see langword="null"/>. </exception>
/// <exception cref="ArgumentException">If <paramref name="matrix"/> is not a square matrix.</exception>
public void Initialize(Matrix<float> matrix)
public void Initialize(Matrix<T> matrix)
{
if (matrix == null)
{
@ -88,7 +87,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
/// If the size of <paramref name="rhs"/> is different the number of rows of the coefficient matrix.
/// </para>
/// </exception>
public void Approximate(Vector<float> rhs, Vector<float> lhs)
public void Approximate(Vector<T> rhs, Vector<T> lhs)
{
if (rhs == null)
{
@ -117,7 +116,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
/// <exception cref="ArgumentException">
/// If the size of <paramref name="rhs"/> is different the number of rows of the coefficient matrix.
/// </exception>
public Vector<float> Approximate(Vector<float> rhs)
public Vector<T> Approximate(Vector<T> rhs)
{
if (rhs == null)
{
@ -129,7 +128,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers.Preconditioners
throw new ArgumentException(Resources.ArgumentMatrixDimensions);
}
var result = new DenseVector(rhs.Count);
var result = Vector<T>.Builder.DenseVector(rhs.Count);
Approximate(rhs, result);
return result;
}

5
src/Numerics/Numerics.csproj

@ -246,7 +246,6 @@
<Compile Include="LinearAlgebra\Complex32\Solvers\Preconditioners\Ilutp.cs" />
<Compile Include="LinearAlgebra\Complex32\Solvers\Preconditioners\IlutpElementSorter.cs" />
<Compile Include="LinearAlgebra\Complex32\Solvers\Preconditioners\IncompleteLU.cs" />
<Compile Include="LinearAlgebra\Complex32\Solvers\Preconditioners\UnitPreconditioner.cs" />
<Compile Include="LinearAlgebra\Complex32\Solvers\StopCriterium\DivergenceStopCriterium.cs" />
<Compile Include="LinearAlgebra\Complex32\Solvers\StopCriterium\FailureStopCriterium.cs" />
<Compile Include="LinearAlgebra\Complex32\Solvers\StopCriterium\ResidualStopCriterium.cs" />
@ -273,7 +272,6 @@
<Compile Include="LinearAlgebra\Complex\Solvers\Preconditioners\Ilutp.cs" />
<Compile Include="LinearAlgebra\Complex\Solvers\Preconditioners\IlutpElementSorter.cs" />
<Compile Include="LinearAlgebra\Complex\Solvers\Preconditioners\IncompleteLU.cs" />
<Compile Include="LinearAlgebra\Complex\Solvers\Preconditioners\UnitPreconditioner.cs" />
<Compile Include="LinearAlgebra\Complex\Solvers\StopCriterium\DivergenceStopCriterium.cs" />
<Compile Include="LinearAlgebra\Complex\Solvers\StopCriterium\FailureStopCriterium.cs" />
<Compile Include="LinearAlgebra\Complex\Solvers\StopCriterium\ResidualStopCriterium.cs" />
@ -316,7 +314,6 @@
<Compile Include="LinearAlgebra\Single\Solvers\Preconditioners\Ilutp.cs" />
<Compile Include="LinearAlgebra\Single\Solvers\Preconditioners\IlutpElementSorter.cs" />
<Compile Include="LinearAlgebra\Single\Solvers\Preconditioners\IncompleteLU.cs" />
<Compile Include="LinearAlgebra\Single\Solvers\Preconditioners\UnitPreconditioner.cs" />
<Compile Include="LinearAlgebra\Single\Solvers\StopCriterium\DivergenceStopCriterium.cs" />
<Compile Include="LinearAlgebra\Single\Solvers\StopCriterium\FailureStopCriterium.cs" />
<Compile Include="LinearAlgebra\Single\Solvers\StopCriterium\ResidualStopCriterium.cs" />
@ -348,7 +345,7 @@
<Compile Include="LinearAlgebra\Double\Solvers\Preconditioners\IncompleteLU.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="LinearAlgebra\Double\Solvers\Preconditioners\UnitPreconditioner.cs" />
<Compile Include="LinearAlgebra\Solvers\UnitPreconditioner.cs" />
<Compile Include="LinearAlgebra\Solvers\Status\CalculationCancelled.cs" />
<Compile Include="LinearAlgebra\Solvers\Status\CalculationConverged.cs" />
<Compile Include="LinearAlgebra\Solvers\Status\CalculationDiverged.cs" />

5
src/UnitTests/LinearAlgebraTests/Complex/Solvers/Preconditioners/UnitPreconditionerTest.cs

@ -26,7 +26,6 @@
using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Complex;
using MathNet.Numerics.LinearAlgebra.Complex.Solvers.Preconditioners;
using MathNet.Numerics.LinearAlgebra.Solvers;
using NUnit.Framework;
@ -51,7 +50,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Precondi
/// <returns>New preconditioner instance.</returns>
internal override IPreConditioner<Complex> CreatePreconditioner()
{
return new UnitPreconditioner();
return new UnitPreconditioner<Complex>();
}
/// <summary>
@ -63,7 +62,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex.Solvers.Precondi
/// <param name="result">Result vector.</param>
protected override void CheckResult(IPreConditioner<Complex> preconditioner, SparseMatrix matrix, Vector<Complex> vector, Vector<Complex> result)
{
Assert.AreEqual(typeof(UnitPreconditioner), preconditioner.GetType(), "#01");
Assert.AreEqual(typeof(UnitPreconditioner<Complex>), preconditioner.GetType(), "#01");
// Unit preconditioner is doing nothing. Vector and result should be equal
for (var i = 0; i < vector.Count; i++)

5
src/UnitTests/LinearAlgebraTests/Complex32/Solvers/Preconditioners/UnitPreconditionerTest.cs

@ -26,7 +26,6 @@
using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Complex32;
using MathNet.Numerics.LinearAlgebra.Complex32.Solvers.Preconditioners;
using MathNet.Numerics.LinearAlgebra.Solvers;
using NUnit.Framework;
@ -46,7 +45,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Precon
/// <returns>New preconditioner instance.</returns>
internal override IPreConditioner<Complex32> CreatePreconditioner()
{
return new UnitPreconditioner();
return new UnitPreconditioner<Complex32>();
}
/// <summary>
@ -58,7 +57,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32.Solvers.Precon
/// <param name="result">Result vector.</param>
protected override void CheckResult(IPreConditioner<Complex32> preconditioner, SparseMatrix matrix, Vector<Complex32> vector, Vector<Complex32> result)
{
Assert.AreEqual(typeof(UnitPreconditioner), preconditioner.GetType(), "#01");
Assert.AreEqual(typeof(UnitPreconditioner<Complex32>), preconditioner.GetType(), "#01");
// Unit preconditioner is doing nothing. Vector and result should be equal
for (var i = 0; i < vector.Count; i++)

4
src/UnitTests/LinearAlgebraTests/Double/Solvers/Preconditioners/UnitPreconditionerTest.cs

@ -44,7 +44,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Precondit
/// <returns>New preconditioner instance.</returns>
internal override IPreConditioner<double> CreatePreconditioner()
{
return new UnitPreconditioner();
return new UnitPreconditioner<double>();
}
/// <summary>
@ -56,7 +56,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double.Solvers.Precondit
/// <param name="result">Result vector.</param>
protected override void CheckResult(IPreConditioner<double> preconditioner, SparseMatrix matrix, Vector<double> vector, Vector<double> result)
{
Assert.AreEqual(typeof(UnitPreconditioner), preconditioner.GetType(), "#01");
Assert.AreEqual(typeof(UnitPreconditioner<double>), preconditioner.GetType(), "#01");
// Unit preconditioner is doing nothing. Vector and result should be equal
for (var i = 0; i < vector.Count; i++)

4
src/UnitTests/LinearAlgebraTests/Single/Solvers/Preconditioners/UnitPreconditionerTest.cs

@ -44,7 +44,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Precondit
/// <returns>New preconditioner instance.</returns>
internal override IPreConditioner<float> CreatePreconditioner()
{
return new UnitPreconditioner();
return new UnitPreconditioner<float>();
}
/// <summary>
@ -56,7 +56,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single.Solvers.Precondit
/// <param name="result">Result vector.</param>
protected override void CheckResult(IPreConditioner<float> preconditioner, SparseMatrix matrix, Vector<float> vector, Vector<float> result)
{
Assert.AreEqual(typeof(UnitPreconditioner), preconditioner.GetType(), "#01");
Assert.AreEqual(typeof(UnitPreconditioner<float>), preconditioner.GetType(), "#01");
// Unit preconditioner is doing nothing. Vector and result should be equal
for (var i = 0; i < vector.Count; i++)

Loading…
Cancel
Save