Browse Source

LA: fix return type of Norms to double (instead of T)

optimization-1
Christoph Ruegg 13 years ago
parent
commit
6360e77a22
  1. 22
      src/Numerics/Distance.cs
  2. 12
      src/Numerics/LinearAlgebra/Complex/DenseVector.cs
  3. 2
      src/Numerics/LinearAlgebra/Complex/Solvers/DivergenceStopCriterium.cs
  4. 6
      src/Numerics/LinearAlgebra/Complex/Solvers/FailureStopCriterium.cs
  5. 2
      src/Numerics/LinearAlgebra/Complex/Solvers/ILUTPPreconditioner.cs
  6. 6
      src/Numerics/LinearAlgebra/Complex/Solvers/ResidualStopCriterium.cs
  7. 4
      src/Numerics/LinearAlgebra/Complex/Solvers/TFQMR.cs
  8. 12
      src/Numerics/LinearAlgebra/Complex/SparseVector.cs
  9. 24
      src/Numerics/LinearAlgebra/Complex/Vector.cs
  10. 14
      src/Numerics/LinearAlgebra/Complex32/DenseVector.cs
  11. 6
      src/Numerics/LinearAlgebra/Complex32/Factorization/UserGramSchmidt.cs
  12. 2
      src/Numerics/LinearAlgebra/Complex32/Solvers/DivergenceStopCriterium.cs
  13. 6
      src/Numerics/LinearAlgebra/Complex32/Solvers/FailureStopCriterium.cs
  14. 2
      src/Numerics/LinearAlgebra/Complex32/Solvers/ILUTPPreconditioner.cs
  15. 2
      src/Numerics/LinearAlgebra/Complex32/Solvers/MlkBiCgStab.cs
  16. 8
      src/Numerics/LinearAlgebra/Complex32/Solvers/ResidualStopCriterium.cs
  17. 4
      src/Numerics/LinearAlgebra/Complex32/Solvers/TFQMR.cs
  18. 14
      src/Numerics/LinearAlgebra/Complex32/SparseVector.cs
  19. 24
      src/Numerics/LinearAlgebra/Complex32/Vector.cs
  20. 4
      src/Numerics/LinearAlgebra/Double/Solvers/FailureStopCriterium.cs
  21. 8
      src/Numerics/LinearAlgebra/Double/Vector.cs
  22. 14
      src/Numerics/LinearAlgebra/Single/DenseVector.cs
  23. 4
      src/Numerics/LinearAlgebra/Single/Factorization/UserGramSchmidt.cs
  24. 6
      src/Numerics/LinearAlgebra/Single/Solvers/FailureStopCriterium.cs
  25. 2
      src/Numerics/LinearAlgebra/Single/Solvers/MlkBiCgStab.cs
  26. 4
      src/Numerics/LinearAlgebra/Single/Solvers/ResidualStopCriterium.cs
  27. 4
      src/Numerics/LinearAlgebra/Single/Solvers/TFQMR.cs
  28. 14
      src/Numerics/LinearAlgebra/Single/SparseVector.cs
  29. 24
      src/Numerics/LinearAlgebra/Single/Vector.cs
  30. 10
      src/Numerics/LinearAlgebra/Vector.Arithmetic.cs
  31. 2
      src/UnitTests/LinearAlgebraTests/Complex/VectorTests.cs
  32. 4
      src/UnitTests/LinearAlgebraTests/Complex32/MatrixTests.Arithmetic.cs
  33. 6
      src/UnitTests/LinearAlgebraTests/Complex32/VectorTests.Norm.cs
  34. 2
      src/UnitTests/LinearAlgebraTests/Complex32/VectorTests.cs

22
src/Numerics/Distance.cs

@ -47,7 +47,7 @@ namespace MathNet.Numerics
/// <summary>
/// Sum of Absolute Difference (SAD), i.e. the L1-norm (Manhattan) of the difference.
/// </summary>
public static float SAD(Vector<float> a, Vector<float> b)
public static double SAD(Vector<float> a, Vector<float> b)
{
return (a - b).L1Norm();
}
@ -59,7 +59,7 @@ namespace MathNet.Numerics
{
if (a.Length != b.Length) throw new ArgumentException(Resources.ArgumentVectorsSameLength);
var sum = 0d;
double sum = 0d;
for (var i = 0; i < a.Length; i++)
{
sum += Math.Abs(a[i] - b[i]);
@ -74,7 +74,7 @@ namespace MathNet.Numerics
{
if (a.Length != b.Length) throw new ArgumentException(Resources.ArgumentVectorsSameLength);
var sum = 0f;
float sum = 0f;
for (var i = 0; i < a.Length; i++)
{
sum += Math.Abs(a[i] - b[i]);
@ -93,7 +93,7 @@ namespace MathNet.Numerics
/// <summary>
/// Mean-Absolute Error (MAE), i.e. the normalized L1-norm (Manhattan) of the difference.
/// </summary>
public static float MAE(Vector<float> a, Vector<float> b)
public static double MAE(Vector<float> a, Vector<float> b)
{
return (a - b).L1Norm()/a.Count;
}
@ -126,7 +126,7 @@ namespace MathNet.Numerics
/// <summary>
/// Sum of Squared Difference (SSD), i.e. the squared L2-norm (Euclidean) of the difference.
/// </summary>
public static float SSD(Vector<float> a, Vector<float> b)
public static double SSD(Vector<float> a, Vector<float> b)
{
var norm = (a - b).L2Norm();
return norm*norm;
@ -164,7 +164,7 @@ namespace MathNet.Numerics
/// <summary>
/// Mean-Squared Error (MSE), i.e. the normalized squared L2-norm (Euclidean) of the difference.
/// </summary>
public static float MSE(Vector<float> a, Vector<float> b)
public static double MSE(Vector<float> a, Vector<float> b)
{
var norm = (a - b).L2Norm();
return norm*norm/a.Count;
@ -197,7 +197,7 @@ namespace MathNet.Numerics
/// <summary>
/// Euclidean Distance, i.e. the L2-norm of the difference.
/// </summary>
public static float Euclidean(Vector<float> a, Vector<float> b)
public static double Euclidean(Vector<float> a, Vector<float> b)
{
return (a - b).L2Norm();
}
@ -229,7 +229,7 @@ namespace MathNet.Numerics
/// <summary>
/// Manhattan Distance, i.e. the L1-norm of the difference.
/// </summary>
public static float Manhattan(Vector<float> a, Vector<float> b)
public static double Manhattan(Vector<float> a, Vector<float> b)
{
return (a - b).L1Norm();
}
@ -261,7 +261,7 @@ namespace MathNet.Numerics
/// <summary>
/// Chebyshev Distance, i.e. the Infinity-norm of the difference.
/// </summary>
public static float Chebyshev(Vector<float> a, Vector<float> b)
public static double Chebyshev(Vector<float> a, Vector<float> b)
{
return (a - b).InfinityNorm();
}
@ -272,7 +272,7 @@ namespace MathNet.Numerics
public static double Chebyshev(double[] a, double[] b)
{
if (a.Length != b.Length) throw new ArgumentOutOfRangeException("b");
var max = Math.Abs(a[0] - b[0]);
double max = Math.Abs(a[0] - b[0]);
for (int i = 1; i < a.Length; i++)
{
var next = Math.Abs(a[i] - b[i]);
@ -290,7 +290,7 @@ namespace MathNet.Numerics
public static float Chebyshev(float[] a, float[] b)
{
if (a.Length != b.Length) throw new ArgumentOutOfRangeException("b");
var max = Math.Abs(a[0] - b[0]);
float max = Math.Abs(a[0] - b[0]);
for (int i = 1; i < a.Length; i++)
{
var next = Math.Abs(a[i] - b[i]);

12
src/Numerics/LinearAlgebra/Complex/DenseVector.cs

@ -552,9 +552,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// Calculates the L1 norm of the vector, also known as Manhattan norm.
/// </summary>
/// <returns>The sum of the absolute values.</returns>
public override Complex L1Norm()
public override double L1Norm()
{
var sum = Complex.Zero;
double sum = 0d;
for (var i = 0; i < _length; i++)
{
sum += _values[i].Magnitude;
@ -566,7 +566,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// Calculates the L2 norm of the vector, also known as Euclidean norm.
/// </summary>
/// <returns>The square root of the sum of the squared values.</returns>
public override Complex L2Norm()
public override double L2Norm()
{
// TODO: native provider
return _values.Aggregate(Complex.Zero, SpecialFunctions.Hypotenuse).Magnitude;
@ -576,7 +576,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// Calculates the infinity norm of the vector.
/// </summary>
/// <returns>The square root of the sum of the squared values.</returns>
public override Complex InfinityNorm()
public override double InfinityNorm()
{
return CommonParallel.Aggregate(_values, (i, v) => v.Magnitude, Math.Max, 0d);
}
@ -586,7 +586,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// </summary>
/// <param name="p">The p value.</param>
/// <returns>Scalar <c>ret = ( ∑|this[i]|^p )^(1/p)</c></returns>
public override Complex Norm(double p)
public override double Norm(double p)
{
if (p < 0d) throw new ArgumentOutOfRangeException("p");
@ -594,7 +594,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
if (p == 2d) return L2Norm();
if (double.IsPositiveInfinity(p)) return InfinityNorm();
var sum = 0d;
double sum = 0d;
for (var i = 0; i < _length; i++)
{
sum += Math.Pow(_values[i].Magnitude, p);

2
src/Numerics/LinearAlgebra/Complex/Solvers/DivergenceStopCriterium.cs

@ -220,7 +220,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers
// Store the infinity norms of both the solution and residual vectors
// These values will be used to calculate the relative drop in residuals later on.
_residualHistory[_residualHistory.Length - 1] = residualVector.InfinityNorm().Real;
_residualHistory[_residualHistory.Length - 1] = residualVector.InfinityNorm();
// Check if we have NaN's. If so we've gone way beyond normal divergence.
// Stop the iteration.

6
src/Numerics/LinearAlgebra/Complex/Solvers/FailureStopCriterium.cs

@ -96,10 +96,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers
}
// Store the infinity norms of both the solution and residual vectors
var residualNorm = residualVector.InfinityNorm();
var solutionNorm = solutionVector.InfinityNorm();
double residualNorm = residualVector.InfinityNorm();
double solutionNorm = solutionVector.InfinityNorm();
_status = double.IsNaN(solutionNorm.Real) || double.IsNaN(residualNorm.Real) ? IterationStatus.Failure : IterationStatus.Continue;
_status = double.IsNaN(solutionNorm) || double.IsNaN(residualNorm) ? IterationStatus.Failure : IterationStatus.Continue;
_lastIteration = iterationNumber;
return _status;

2
src/Numerics/LinearAlgebra/Complex/Solvers/ILUTPPreconditioner.cs

@ -435,7 +435,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers
// {
// w(j) = 0
// }
if (workVector[j].Magnitude <= _dropTolerance*vectorNorm.Real)
if (workVector[j].Magnitude <= _dropTolerance*vectorNorm)
{
workVector[j] = 0.0;
}

6
src/Numerics/LinearAlgebra/Complex/Solvers/ResidualStopCriterium.cs

@ -218,12 +218,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers
// Check the residuals by calculating:
// ||r_i|| <= stop_tol * ||b||
var stopCriterium = ComputeStopCriterium(sourceVector.InfinityNorm().Real);
var stopCriterium = ComputeStopCriterium(sourceVector.InfinityNorm());
// First check that we have real numbers not NaN's.
// NaN's can occur when the iterative process diverges so we
// stop if that is the case.
if (double.IsNaN(stopCriterium) || double.IsNaN(residualNorm.Real))
if (double.IsNaN(stopCriterium) || double.IsNaN(residualNorm))
{
_iterationCount = 0;
_status = IterationStatus.Diverged;
@ -233,7 +233,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers
// ||r_i|| <= stop_tol * ||b||
// Stop the calculation if it's clearly smaller than the tolerance
var decimalMagnitude = Math.Abs(stopCriterium.Magnitude()) + 1;
if (residualNorm.Real.IsSmallerWithDecimalPlaces(stopCriterium, decimalMagnitude))
if (residualNorm.IsSmallerWithDecimalPlaces(stopCriterium, decimalMagnitude))
{
if (_lastIteration <= iterationNumber)
{

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

@ -145,7 +145,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers
double theta = 0;
// Initialize
var tau = input.L2Norm().Real;
var tau = input.L2Norm();
Complex rho = tau*tau;
// Calculate the initial values for v
@ -204,7 +204,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers
yinternal.Add(temp, d);
// theta = ||pseudoResiduals||_2 / tau
theta = pseudoResiduals.L2Norm().Real/tau;
theta = pseudoResiduals.L2Norm()/tau;
var c = 1/Math.Sqrt(1 + (theta*theta));
// tau = tau * theta * c

12
src/Numerics/LinearAlgebra/Complex/SparseVector.cs

@ -711,9 +711,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// Calculates the L1 norm of the vector, also known as Manhattan norm.
/// </summary>
/// <returns>The sum of the absolute values.</returns>
public override Complex L1Norm()
public override double L1Norm()
{
double result = 0;
double result = 0d;
for (var i = 0; i < _storage.ValueCount; i++)
{
result += _storage.Values[i].Magnitude;
@ -725,7 +725,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// Calculates the infinity norm of the vector.
/// </summary>
/// <returns>The square root of the sum of the squared values.</returns>
public override Complex InfinityNorm()
public override double InfinityNorm()
{
return CommonParallel.Aggregate(0, _storage.ValueCount, i => _storage.Values[i].Magnitude, Math.Max, 0d);
}
@ -735,20 +735,20 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// </summary>
/// <param name="p">The p value.</param>
/// <returns>Scalar <c>ret = ( ∑|this[i]|^p )^(1/p)</c></returns>
public override Complex Norm(double p)
public override double Norm(double p)
{
if (p < 0d) throw new ArgumentOutOfRangeException("p");
if (_storage.ValueCount == 0)
{
return Complex.Zero;
return 0d;
}
if (p == 1d) return L1Norm();
if (p == 2d) return L2Norm();
if (double.IsPositiveInfinity(p)) return InfinityNorm();
var sum = 0d;
double sum = 0d;
for (var index = 0; index < _storage.ValueCount; index++)
{
sum += Math.Pow(_storage.Values[index].Magnitude, p);

24
src/Numerics/LinearAlgebra/Complex/Vector.cs

@ -328,9 +328,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// Calculates the L1 norm of the vector, also known as Manhattan norm.
/// </summary>
/// <returns>The sum of the absolute values.</returns>
public override Complex L1Norm()
public override double L1Norm()
{
var sum = Complex.Zero;
double sum = 0d;
for (var i = 0; i < Count; i++)
{
sum += At(i).Magnitude;
@ -342,16 +342,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// Calculates the L2 norm of the vector, also known as Euclidean norm.
/// </summary>
/// <returns>The square root of the sum of the squared values.</returns>
public override Complex L2Norm()
public override double L2Norm()
{
return DoConjugateDotProduct(this).SquareRoot();
return DoConjugateDotProduct(this).SquareRoot().Real;
}
/// <summary>
/// Calculates the infinity norm of the vector.
/// </summary>
/// <returns>The square root of the sum of the squared values.</returns>
public override Complex InfinityNorm()
public override double InfinityNorm()
{
return CommonParallel.Aggregate(0, Count, i => At(i).Magnitude, Math.Max, 0d);
}
@ -365,7 +365,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <returns>
/// <c>Scalar ret = ( ∑|At(i)|^p )^(1/p)</c>
/// </returns>
public override Complex Norm(double p)
public override double Norm(double p)
{
if (p < 0d) throw new ArgumentOutOfRangeException("p");
@ -373,12 +373,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
if (p == 2d) return L2Norm();
if (double.IsPositiveInfinity(p)) return InfinityNorm();
var sum = 0d;
double sum = 0d;
for (var index = 0; index < Count; index++)
{
sum += Math.Pow(At(index).Magnitude, p);
}
return Math.Pow(sum, 1.0 / p);
return Math.Pow(sum, 1.0/p);
}
/// <summary>
@ -434,19 +434,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// </returns>
public override Vector<Complex> Normalize(double p)
{
if (p < 0.0)
if (p < 0d)
{
throw new ArgumentOutOfRangeException("p");
}
var norm = Norm(p);
double norm = Norm(p);
var clone = Clone();
if (norm.Real == 0.0)
if (norm == 0d)
{
return clone;
}
clone.Multiply(1.0 / norm, clone);
clone.Multiply(1d / norm, clone);
return clone;
}

14
src/Numerics/LinearAlgebra/Complex32/DenseVector.cs

@ -547,9 +547,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// Calculates the L1 norm of the vector, also known as Manhattan norm.
/// </summary>
/// <returns>The sum of the absolute values.</returns>
public override Complex32 L1Norm()
public override double L1Norm()
{
var sum = Complex32.Zero;
double sum = 0d;
for (var i = 0; i < _length; i++)
{
sum += _values[i].Magnitude;
@ -561,7 +561,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// Calculates the L2 norm of the vector, also known as Euclidean norm.
/// </summary>
/// <returns>The square root of the sum of the squared values.</returns>
public override Complex32 L2Norm()
public override double L2Norm()
{
// TODO: native provider
return _values.Aggregate(Complex32.Zero, SpecialFunctions.Hypotenuse).Magnitude;
@ -571,7 +571,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// Calculates the infinity norm of the vector.
/// </summary>
/// <returns>The square root of the sum of the squared values.</returns>
public override Complex32 InfinityNorm()
public override double InfinityNorm()
{
return CommonParallel.Aggregate(_values, (i, v) => v.Magnitude, Math.Max, 0f);
}
@ -581,7 +581,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// </summary>
/// <param name="p">The p value.</param>
/// <returns>Scalar <c>ret = ( ∑|this[i]|^p )^(1/p)</c></returns>
public override Complex32 Norm(double p)
public override double Norm(double p)
{
if (p < 0d) throw new ArgumentOutOfRangeException("p");
@ -589,12 +589,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
if (p == 2d) return L2Norm();
if (double.IsPositiveInfinity(p)) return InfinityNorm();
var sum = 0d;
double sum = 0d;
for (var i = 0; i < _length; i++)
{
sum += Math.Pow(_values[i].Magnitude, p);
}
return (float)Math.Pow(sum, 1.0 / p);
return Math.Pow(sum, 1.0 / p);
}
/// <summary>

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

@ -64,8 +64,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
for (var k = 0; k < q.ColumnCount; k++)
{
var norm = q.Column(k).L2Norm().Real;
if (norm == 0.0f)
var norm = (float) q.Column(k).L2Norm();
if (norm == 0f)
{
throw new ArgumentException(Resources.ArgumentMatrixNotRankDeficient);
}
@ -73,7 +73,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
r.At(k, k, norm);
for (var i = 0; i < q.RowCount; i++)
{
q.At(i, k, q.At(i, k) / norm);
q.At(i, k, (q.At(i, k) / norm));
}
for (var j = k + 1; j < q.ColumnCount; j++)

2
src/Numerics/LinearAlgebra/Complex32/Solvers/DivergenceStopCriterium.cs

@ -215,7 +215,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers
// Store the infinity norms of both the solution and residual vectors
// These values will be used to calculate the relative drop in residuals later on.
_residualHistory[_residualHistory.Length - 1] = residualVector.InfinityNorm().Real;
_residualHistory[_residualHistory.Length - 1] = residualVector.InfinityNorm();
// Check if we have NaN's. If so we've gone way beyond normal divergence.
// Stop the iteration.

6
src/Numerics/LinearAlgebra/Complex32/Solvers/FailureStopCriterium.cs

@ -91,10 +91,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers
}
// Store the infinity norms of both the solution and residual vectors
var residualNorm = residualVector.InfinityNorm();
var solutionNorm = solutionVector.InfinityNorm();
double residualNorm = residualVector.InfinityNorm();
double solutionNorm = solutionVector.InfinityNorm();
_status = float.IsNaN(solutionNorm.Real) || float.IsNaN(residualNorm.Real) ? IterationStatus.Failure : IterationStatus.Continue;
_status = double.IsNaN(solutionNorm) || double.IsNaN(residualNorm) ? IterationStatus.Failure : IterationStatus.Continue;
_lastIteration = iterationNumber;
return _status;

2
src/Numerics/LinearAlgebra/Complex32/Solvers/ILUTPPreconditioner.cs

@ -430,7 +430,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers
// {
// w(j) = 0
// }
if (workVector[j].Magnitude <= _dropTolerance*vectorNorm.Real)
if (workVector[j].Magnitude <= _dropTolerance*vectorNorm)
{
workVector[j] = 0.0f;
}

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

@ -193,7 +193,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers
result.Add(orthogonalMatrix.Column(i));
// Normalize the result vector
result[i].Multiply(1 / result[i].L2Norm().Real, result[i]);
result[i].Multiply(1/(float) result[i].L2Norm(), result[i]);
}
return result;

8
src/Numerics/LinearAlgebra/Complex32/Solvers/ResidualStopCriterium.cs

@ -209,16 +209,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers
// Store the infinity norms of both the solution and residual vectors
// These values will be used to calculate the relative drop in residuals
// later on.
var residualNorm = residualVector.InfinityNorm();
var residualNorm = (float) residualVector.InfinityNorm();
// Check the residuals by calculating:
// ||r_i|| <= stop_tol * ||b||
var stopCriterium = ComputeStopCriterium(sourceVector.InfinityNorm().Real);
var stopCriterium = ComputeStopCriterium((float) sourceVector.InfinityNorm());
// First check that we have real numbers not NaN's.
// NaN's can occur when the iterative process diverges so we
// stop if that is the case.
if (float.IsNaN(stopCriterium) || float.IsNaN(residualNorm.Real))
if (float.IsNaN(stopCriterium) || float.IsNaN(residualNorm))
{
_iterationCount = 0;
_status = IterationStatus.Diverged;
@ -228,7 +228,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers
// ||r_i|| <= stop_tol * ||b||
// Stop the calculation if it's clearly smaller than the tolerance
var decimalMagnitude = Math.Abs(stopCriterium.Magnitude()) + 1;
if (residualNorm.Real.IsSmallerWithDecimalPlaces(stopCriterium, decimalMagnitude))
if (residualNorm.IsSmallerWithDecimalPlaces(stopCriterium, decimalMagnitude))
{
if (_lastIteration <= iterationNumber)
{

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

@ -142,7 +142,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers
float theta = 0;
// Initialize
var tau = input.L2Norm().Real;
var tau = (float) input.L2Norm();
Numerics.Complex32 rho = tau*tau;
// Calculate the initial values for v
@ -201,7 +201,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers
yinternal.Add(temp, d);
// theta = ||pseudoResiduals||_2 / tau
theta = pseudoResiduals.L2Norm().Real/tau;
theta = (float) pseudoResiduals.L2Norm()/tau;
var c = 1/(float) Math.Sqrt(1 + (theta*theta));
// tau = tau * theta * c

14
src/Numerics/LinearAlgebra/Complex32/SparseVector.cs

@ -706,9 +706,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// Calculates the L1 norm of the vector, also known as Manhattan norm.
/// </summary>
/// <returns>The sum of the absolute values.</returns>
public override Complex32 L1Norm()
public override double L1Norm()
{
var result = 0f;
double result = 0d;
for (var i = 0; i < _storage.ValueCount; i++)
{
result += _storage.Values[i].Magnitude;
@ -720,7 +720,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// Calculates the infinity norm of the vector.
/// </summary>
/// <returns>The square root of the sum of the squared values.</returns>
public override Complex32 InfinityNorm()
public override double InfinityNorm()
{
return CommonParallel.Aggregate(0, _storage.ValueCount, i => _storage.Values[i].Magnitude, Math.Max, 0f);
}
@ -730,25 +730,25 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// </summary>
/// <param name="p">The p value.</param>
/// <returns>Scalar <c>ret = ( ∑|this[i]|^p )^(1/p)</c></returns>
public override Complex32 Norm(double p)
public override double Norm(double p)
{
if (p < 0d) throw new ArgumentOutOfRangeException("p");
if (_storage.ValueCount == 0)
{
return Complex32.Zero;
return 0d;
}
if (p == 1d) return L1Norm();
if (p == 2d) return L2Norm();
if (double.IsPositiveInfinity(p)) return InfinityNorm();
var sum = 0d;
double sum = 0d;
for (var index = 0; index < _storage.ValueCount; index++)
{
sum += Math.Pow(_storage.Values[index].Magnitude, p);
}
return (float)Math.Pow(sum, 1.0 / p);
return Math.Pow(sum, 1.0 / p);
}
/// <summary>

24
src/Numerics/LinearAlgebra/Complex32/Vector.cs

@ -323,9 +323,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// Calculates the L1 norm of the vector, also known as Manhattan norm.
/// </summary>
/// <returns>The sum of the absolute values.</returns>
public override Complex32 L1Norm()
public override double L1Norm()
{
var sum = Complex32.Zero;
double sum = 0d;
for (var i = 0; i < Count; i++)
{
sum += At(i).Magnitude;
@ -337,16 +337,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// Calculates the L2 norm of the vector, also known as Euclidean norm.
/// </summary>
/// <returns>The square root of the sum of the squared values.</returns>
public override Complex32 L2Norm()
public override double L2Norm()
{
return DoConjugateDotProduct(this).SquareRoot();
return DoConjugateDotProduct(this).SquareRoot().Real;
}
/// <summary>
/// Calculates the infinity norm of the vector.
/// </summary>
/// <returns>The square root of the sum of the squared values.</returns>
public override Complex32 InfinityNorm()
public override double InfinityNorm()
{
return CommonParallel.Aggregate(0, Count, i => At(i).Magnitude, Math.Max, 0f);
}
@ -360,7 +360,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <returns>
/// <c>Scalar ret = ( ∑|At(i)|^p )^(1/p)</c>
/// </returns>
public override Complex32 Norm(double p)
public override double Norm(double p)
{
if (p < 0d) throw new ArgumentOutOfRangeException("p");
@ -368,12 +368,12 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
if (p == 2d) return L2Norm();
if (double.IsPositiveInfinity(p)) return InfinityNorm();
var sum = 0d;
double sum = 0d;
for (var index = 0; index < Count; index++)
{
sum += Math.Pow(At(index).Magnitude, p);
}
return (float) Math.Pow(sum, 1.0/p);
return Math.Pow(sum, 1.0/p);
}
/// <summary>
@ -429,19 +429,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// </returns>
public override Vector<Complex32> Normalize(double p)
{
if (p < 0.0)
if (p < 0d)
{
throw new ArgumentOutOfRangeException("p");
}
var norm = Norm(p);
double norm = Norm(p);
var clone = Clone();
if (norm.Real == 0.0f)
if (norm == 0d)
{
return clone;
}
clone.Multiply(1.0f / norm, clone);
clone.Multiply((float)(1d / norm), clone);
return clone;
}

4
src/Numerics/LinearAlgebra/Double/Solvers/FailureStopCriterium.cs

@ -89,8 +89,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers
}
// Store the infinity norms of both the solution and residual vectors
var residualNorm = residualVector.InfinityNorm();
var solutionNorm = solutionVector.InfinityNorm();
double residualNorm = residualVector.InfinityNorm();
double solutionNorm = solutionVector.InfinityNorm();
_status = double.IsNaN(solutionNorm) || double.IsNaN(residualNorm) ? IterationStatus.Failure : IterationStatus.Continue;

8
src/Numerics/LinearAlgebra/Double/Vector.cs

@ -457,19 +457,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </returns>
public override Vector<double> Normalize(double p)
{
if (p < 0.0)
if (p < 0d)
{
throw new ArgumentOutOfRangeException("p");
}
var norm = Norm(p);
double norm = Norm(p);
var clone = Clone();
if (norm == 0.0)
if (norm == 0d)
{
return clone;
}
clone.Multiply(1.0 / norm, clone);
clone.Multiply(1d / norm, clone);
return clone;
}

14
src/Numerics/LinearAlgebra/Single/DenseVector.cs

@ -608,9 +608,9 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// Calculates the L1 norm of the vector, also known as Manhattan norm.
/// </summary>
/// <returns>The sum of the absolute values.</returns>
public override float L1Norm()
public override double L1Norm()
{
var sum = 0f;
double sum = 0d;
for (var i = 0; i < _length; i++)
{
sum += Math.Abs(_values[i]);
@ -622,7 +622,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// Calculates the L2 norm of the vector, also known as Euclidean norm.
/// </summary>
/// <returns>The square root of the sum of the squared values.</returns>
public override float L2Norm()
public override double L2Norm()
{
// TODO: native provider
return _values.Aggregate(0f, SpecialFunctions.Hypotenuse);
@ -632,7 +632,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// Calculates the infinity norm of the vector.
/// </summary>
/// <returns>The square root of the sum of the squared values.</returns>
public override float InfinityNorm()
public override double InfinityNorm()
{
return CommonParallel.Aggregate(_values, (i, v) => Math.Abs(v), Math.Max, 0f);
}
@ -642,7 +642,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// </summary>
/// <param name="p">The p value.</param>
/// <returns>Scalar <c>ret = ( ∑|this[i]|^p )^(1/p)</c></returns>
public override float Norm(double p)
public override double Norm(double p)
{
if (p < 0d) throw new ArgumentOutOfRangeException("p");
@ -650,12 +650,12 @@ namespace MathNet.Numerics.LinearAlgebra.Single
if (p == 2d) return L2Norm();
if (double.IsPositiveInfinity(p)) return InfinityNorm();
var sum = 0d;
double sum = 0d;
for (var index = 0; index < _length; index++)
{
sum += Math.Pow(Math.Abs(_values[index]), p);
}
return (float)Math.Pow(sum, 1.0 / p);
return Math.Pow(sum, 1.0 / p);
}
/// <summary>

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

@ -62,8 +62,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
for (var k = 0; k < q.ColumnCount; k++)
{
var norm = q.Column(k).L2Norm();
if (norm == 0.0)
var norm = (float) q.Column(k).L2Norm();
if (norm == 0f)
{
throw new ArgumentException(Resources.ArgumentMatrixNotRankDeficient);
}

6
src/Numerics/LinearAlgebra/Single/Solvers/FailureStopCriterium.cs

@ -89,10 +89,10 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers
}
// Store the infinity norms of both the solution and residual vectors
var residualNorm = residualVector.InfinityNorm();
var solutionNorm = solutionVector.InfinityNorm();
double residualNorm = residualVector.InfinityNorm();
double solutionNorm = solutionVector.InfinityNorm();
_status = float.IsNaN(solutionNorm) || float.IsNaN(residualNorm) ? IterationStatus.Failure : IterationStatus.Continue;
_status = double.IsNaN(solutionNorm) || double.IsNaN(residualNorm) ? IterationStatus.Failure : IterationStatus.Continue;
_lastIteration = iterationNumber;
return _status;

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

@ -196,7 +196,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers
result.Add(orthogonalMatrix.Column(i));
// Normalize the result vector
result[i].Multiply(1 / result[i].L2Norm(), result[i]);
result[i].Multiply(1/(float) result[i].L2Norm(), result[i]);
}
return result;

4
src/Numerics/LinearAlgebra/Single/Solvers/ResidualStopCriterium.cs

@ -207,11 +207,11 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers
// Store the infinity norms of both the solution and residual vectors
// These values will be used to calculate the relative drop in residuals
// later on.
var residualNorm = residualVector.InfinityNorm();
var residualNorm = (float) residualVector.InfinityNorm();
// Check the residuals by calculating:
// ||r_i|| <= stop_tol * ||b||
var stopCriterium = ComputeStopCriterium(sourceVector.InfinityNorm());
var stopCriterium = ComputeStopCriterium((float) sourceVector.InfinityNorm());
// First check that we have real numbers not NaN's.
// NaN's can occur when the iterative process diverges so we

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

@ -142,7 +142,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers
float theta = 0;
// Initialize
var tau = input.L2Norm();
var tau = (float) input.L2Norm();
var rho = tau*tau;
// Calculate the initial values for v
@ -201,7 +201,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers
yinternal.Add(temp, d);
// theta = ||pseudoResiduals||_2 / tau
theta = pseudoResiduals.L2Norm()/tau;
theta = (float) pseudoResiduals.L2Norm()/tau;
var c = 1/(float) Math.Sqrt(1 + (theta*theta));
// tau = tau * theta * c

14
src/Numerics/LinearAlgebra/Single/SparseVector.cs

@ -718,9 +718,9 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// Calculates the L1 norm of the vector, also known as Manhattan norm.
/// </summary>
/// <returns>The sum of the absolute values.</returns>
public override float L1Norm()
public override double L1Norm()
{
var result = 0f;
double result = 0d;
for (var i = 0; i < _storage.ValueCount; i++)
{
result += Math.Abs(_storage.Values[i]);
@ -732,7 +732,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// Calculates the infinity norm of the vector.
/// </summary>
/// <returns>The square root of the sum of the squared values.</returns>
public override float InfinityNorm()
public override double InfinityNorm()
{
return CommonParallel.Aggregate(0, _storage.ValueCount, i => Math.Abs(_storage.Values[i]), Math.Max, 0f);
}
@ -742,25 +742,25 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// </summary>
/// <param name="p">The p value.</param>
/// <returns>Scalar <c>ret = ( ∑|this[i]|^p )^(1/p)</c></returns>
public override float Norm(double p)
public override double Norm(double p)
{
if (p < 0d) throw new ArgumentOutOfRangeException("p");
if (_storage.ValueCount == 0)
{
return 0f;
return 0d;
}
if (p == 1d) return L1Norm();
if (p == 2d) return L2Norm();
if (double.IsPositiveInfinity(p)) return InfinityNorm();
var sum = 0d;
double sum = 0d;
for (var index = 0; index < _storage.ValueCount; index++)
{
sum += Math.Pow(Math.Abs(_storage.Values[index]), p);
}
return (float)Math.Pow(sum, 1.0 / p);
return Math.Pow(sum, 1.0 / p);
}
/// <summary>

24
src/Numerics/LinearAlgebra/Single/Vector.cs

@ -325,9 +325,9 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// Calculates the L1 norm of the vector, also known as Manhattan norm.
/// </summary>
/// <returns>The sum of the absolute values.</returns>
public override float L1Norm()
public override double L1Norm()
{
var sum = 0.0f;
double sum = 0d;
for (var i = 0; i < Count; i++)
{
sum += Math.Abs(At(i));
@ -339,16 +339,16 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// Calculates the L2 norm of the vector, also known as Euclidean norm.
/// </summary>
/// <returns>The square root of the sum of the squared values.</returns>
public override float L2Norm()
public override double L2Norm()
{
return (float)Math.Sqrt(DoDotProduct(this));
return Math.Sqrt(DoDotProduct(this));
}
/// <summary>
/// Calculates the infinity norm of the vector.
/// </summary>
/// <returns>The square root of the sum of the squared values.</returns>
public override float InfinityNorm()
public override double InfinityNorm()
{
return CommonParallel.Aggregate(0, Count, i => Math.Abs(At(i)), Math.Max, 0f);
}
@ -362,7 +362,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <returns>
/// <c>Scalar ret = ( ∑|At(i)|^p )^(1/p)</c>
/// </returns>
public override float Norm(double p)
public override double Norm(double p)
{
if (p < 0d) throw new ArgumentOutOfRangeException("p");
@ -370,12 +370,12 @@ namespace MathNet.Numerics.LinearAlgebra.Single
if (p == 2d) return L2Norm();
if (double.IsPositiveInfinity(p)) return InfinityNorm();
var sum = 0d;
double sum = 0d;
for (var index = 0; index < Count; index++)
{
sum += Math.Pow(Math.Abs(At(index)), p);
}
return (float) Math.Pow(sum, 1.0/p);
return Math.Pow(sum, 1.0/p);
}
/// <summary>
@ -457,19 +457,19 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// </returns>
public override Vector<float> Normalize(double p)
{
if (p < 0.0)
if (p < 0d)
{
throw new ArgumentOutOfRangeException("p");
}
var norm = Norm(p);
double norm = Norm(p);
var clone = Clone();
if (norm == 0.0)
if (norm == 0d)
{
return clone;
}
clone.Multiply(1.0f / norm, clone);
clone.Multiply((float)(1d / norm), clone);
return clone;
}

10
src/Numerics/LinearAlgebra/Vector.Arithmetic.cs

@ -748,26 +748,26 @@ namespace MathNet.Numerics.LinearAlgebra
/// Calculates the L1 norm of the vector, also known as Manhattan norm.
/// </summary>
/// <returns>The sum of the absolute values.</returns>
public abstract T L1Norm();
public abstract double L1Norm();
/// <summary>
/// Calculates the L2 norm of the vector, also known as Euclidean norm.
/// </summary>
/// <returns>The square root of the sum of the squared values.</returns>
public abstract T L2Norm();
public abstract double L2Norm();
/// <summary>
/// Calculates the infinity norm of the vector.
/// </summary>
/// <returns>The square root of the sum of the squared values.</returns>
public abstract T InfinityNorm();
public abstract double InfinityNorm();
/// <summary>
/// Computes the p-Norm.
/// </summary>
/// <param name="p">The p value.</param>
/// <returns><c>Scalar ret = (sum(abs(this[i])^p))^(1/p)</c></returns>
public abstract T Norm(double p);
public abstract double Norm(double p);
/// <summary>
/// Normalizes this vector to a unit vector with respect to the p-norm.
@ -840,7 +840,7 @@ namespace MathNet.Numerics.LinearAlgebra
/// Computes the sum of the absolute value of the vector's elements.
/// </summary>
/// <returns>The sum of the absolute value of the vector's elements.</returns>
public T SumMagnitudes()
public double SumMagnitudes()
{
return L1Norm();
}

2
src/UnitTests/LinearAlgebraTests/Complex/VectorTests.cs

@ -466,7 +466,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex
var vector = CreateVector(testData);
var actual = vector.SumMagnitudes();
var expected = testData.Sum(complex => complex.Magnitude);
Assert.AreEqual(expected, actual.Real);
Assert.AreEqual(expected, actual);
}
/// <summary>

4
src/UnitTests/LinearAlgebraTests/Complex32/MatrixTests.Arithmetic.cs

@ -839,7 +839,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
for (var j = 0; j < result.ColumnCount; j++)
{
var col = result.Column(j);
AssertHelpers.AlmostEqual(Complex32.One, col.Norm(p), 6);
AssertHelpers.AlmostEqual(1d, col.Norm(p), 6);
}
}
@ -864,7 +864,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
for (var i = 0; i < matrix.RowCount; i++)
{
var row = matrix.Row(i);
AssertHelpers.AlmostEqual(Complex32.One, row.Norm(p), 6);
AssertHelpers.AlmostEqual(1d, row.Norm(p), 6);
}
}

6
src/UnitTests/LinearAlgebraTests/Complex32/VectorTests.Norm.cs

@ -80,7 +80,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
public void CanComputeNormP(int p, float expected)
{
var vector = CreateVector(Data);
AssertHelpers.AlmostEqual(expected, vector.Norm(p).Real, 5);
AssertHelpers.AlmostEqual(expected, (float) vector.Norm(p), 5);
}
/// <summary>
@ -90,8 +90,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
public void CanComputeNormInfinity()
{
var vector = CreateVector(Data);
AssertHelpers.AlmostEqual(5.0990195, vector.InfinityNorm().Real, 7);
AssertHelpers.AlmostEqual(5.0990195, vector.Norm(Single.PositiveInfinity).Real, 7);
AssertHelpers.AlmostEqual(5.0990195, vector.InfinityNorm(), 7);
AssertHelpers.AlmostEqual(5.0990195, vector.Norm(Single.PositiveInfinity), 7);
}
/// <summary>

2
src/UnitTests/LinearAlgebraTests/Complex32/VectorTests.cs

@ -466,7 +466,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
var vector = CreateVector(testData);
var actual = vector.SumMagnitudes();
var expected = testData.Sum(complex => complex.Magnitude);
Assert.AreEqual(expected, actual.Real);
Assert.AreEqual(expected, (float) actual);
}
/// <summary>

Loading…
Cancel
Save