From 848874e1b793284e6e95c55ef99c9839a60e849f Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Fri, 20 Jun 2014 00:37:31 +0200 Subject: [PATCH] LA: CoerceZero for matrices and vectors (via MapInplace) --- src/Numerics/LinearAlgebra/Complex/Matrix.cs | 9 ++++++++- src/Numerics/LinearAlgebra/Complex/Vector.cs | 8 ++++++++ src/Numerics/LinearAlgebra/Complex32/Matrix.cs | 8 ++++++++ src/Numerics/LinearAlgebra/Complex32/Vector.cs | 8 ++++++++ src/Numerics/LinearAlgebra/Double/Matrix.cs | 8 ++++++++ src/Numerics/LinearAlgebra/Double/Vector.cs | 8 ++++++++ src/Numerics/LinearAlgebra/Matrix.cs | 13 +++++++++++++ src/Numerics/LinearAlgebra/Single/Matrix.cs | 8 ++++++++ src/Numerics/LinearAlgebra/Single/Vector.cs | 8 ++++++++ src/Numerics/LinearAlgebra/Vector.cs | 13 +++++++++++++ 10 files changed, 90 insertions(+), 1 deletion(-) diff --git a/src/Numerics/LinearAlgebra/Complex/Matrix.cs b/src/Numerics/LinearAlgebra/Complex/Matrix.cs index fe7de0c2..4c28604e 100644 --- a/src/Numerics/LinearAlgebra/Complex/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/Matrix.cs @@ -41,7 +41,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex using Complex = Numerics.Complex; #else using Complex = System.Numerics.Complex; - #endif /// @@ -58,6 +57,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex { } + /// + /// Set all values whose absolute value is smaller than the threshold to zero. + /// + public override void CoerceZero(double threshold) + { + MapInplace(x => x.Magnitude < threshold ? Complex.Zero : x, Zeros.AllowSkip); + } + /// Calculates the induced L1 norm of this matrix. /// The maximum absolute column sum of the matrix. public override double L1Norm() diff --git a/src/Numerics/LinearAlgebra/Complex/Vector.cs b/src/Numerics/LinearAlgebra/Complex/Vector.cs index 22f82db2..6cda7da6 100644 --- a/src/Numerics/LinearAlgebra/Complex/Vector.cs +++ b/src/Numerics/LinearAlgebra/Complex/Vector.cs @@ -55,6 +55,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex { } + /// + /// Set all values whose absolute value is smaller than the threshold to zero. + /// + public override void CoerceZero(double threshold) + { + MapInplace(x => x.Magnitude < threshold ? Complex.Zero : x, Zeros.AllowSkip); + } + /// /// Adds a scalar to each element of the vector and stores the result in the result vector. /// diff --git a/src/Numerics/LinearAlgebra/Complex32/Matrix.cs b/src/Numerics/LinearAlgebra/Complex32/Matrix.cs index cf988d1b..19d418ed 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Matrix.cs @@ -52,6 +52,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 { } + /// + /// Set all values whose absolute value is smaller than the threshold to zero. + /// + public override void CoerceZero(double threshold) + { + MapInplace(x => x.Magnitude < threshold ? Complex32.Zero : x, Zeros.AllowSkip); + } + /// Calculates the induced L1 norm of this matrix. /// The maximum absolute column sum of the matrix. public override double L1Norm() diff --git a/src/Numerics/LinearAlgebra/Complex32/Vector.cs b/src/Numerics/LinearAlgebra/Complex32/Vector.cs index a2fa809e..865296d5 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Vector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Vector.cs @@ -50,6 +50,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 { } + /// + /// Set all values whose absolute value is smaller than the threshold to zero. + /// + public override void CoerceZero(double threshold) + { + MapInplace(x => x.Magnitude < threshold ? Complex32.Zero : x, Zeros.AllowSkip); + } + /// /// Adds a scalar to each element of the vector and stores the result in the result vector. /// diff --git a/src/Numerics/LinearAlgebra/Double/Matrix.cs b/src/Numerics/LinearAlgebra/Double/Matrix.cs index 32766df8..57224988 100644 --- a/src/Numerics/LinearAlgebra/Double/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Double/Matrix.cs @@ -50,6 +50,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double { } + /// + /// Set all values whose absolute value is smaller than the threshold to zero. + /// + public override void CoerceZero(double threshold) + { + MapInplace(x => Math.Abs(x) < threshold ? 0d : x, Zeros.AllowSkip); + } + /// Calculates the induced L1 norm of this matrix. /// The maximum absolute column sum of the matrix. public override double L1Norm() diff --git a/src/Numerics/LinearAlgebra/Double/Vector.cs b/src/Numerics/LinearAlgebra/Double/Vector.cs index 9f4599d4..01e80270 100644 --- a/src/Numerics/LinearAlgebra/Double/Vector.cs +++ b/src/Numerics/LinearAlgebra/Double/Vector.cs @@ -48,6 +48,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double { } + /// + /// Set all values whose absolute value is smaller than the threshold to zero. + /// + public override void CoerceZero(double threshold) + { + MapInplace(x => Math.Abs(x) < threshold ? 0d : x, Zeros.AllowSkip); + } + /// /// Adds a scalar to each element of the vector and stores the result in the result vector. /// diff --git a/src/Numerics/LinearAlgebra/Matrix.cs b/src/Numerics/LinearAlgebra/Matrix.cs index ba11a741..13d2add5 100644 --- a/src/Numerics/LinearAlgebra/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Matrix.cs @@ -240,6 +240,19 @@ namespace MathNet.Numerics.LinearAlgebra Storage.Clear(rowIndex, rowCount, columnIndex, columnCount); } + /// + /// Set all values whose absolute value is smaller than the threshold to zero, in-place. + /// + public abstract void CoerceZero(double threshold); + + /// + /// Set all values that meet the predicate to zero, in-place. + /// + public void CoerceZero(Func zeroPredicate) + { + MapInplace(x => zeroPredicate(x) ? Zero : x, Zeros.AllowSkip); + } + /// /// Creates a clone of this instance. /// diff --git a/src/Numerics/LinearAlgebra/Single/Matrix.cs b/src/Numerics/LinearAlgebra/Single/Matrix.cs index c5898764..f941a595 100644 --- a/src/Numerics/LinearAlgebra/Single/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Single/Matrix.cs @@ -50,6 +50,14 @@ namespace MathNet.Numerics.LinearAlgebra.Single { } + /// + /// Set all values whose absolute value is smaller than the threshold to zero. + /// + public override void CoerceZero(double threshold) + { + MapInplace(x => Math.Abs(x) < threshold ? 0f : x, Zeros.AllowSkip); + } + /// Calculates the induced L1 norm of this matrix. /// The maximum absolute column sum of the matrix. public override double L1Norm() diff --git a/src/Numerics/LinearAlgebra/Single/Vector.cs b/src/Numerics/LinearAlgebra/Single/Vector.cs index bea3e5a1..0ff911fa 100644 --- a/src/Numerics/LinearAlgebra/Single/Vector.cs +++ b/src/Numerics/LinearAlgebra/Single/Vector.cs @@ -48,6 +48,14 @@ namespace MathNet.Numerics.LinearAlgebra.Single { } + /// + /// Set all values whose absolute value is smaller than the threshold to zero. + /// + public override void CoerceZero(double threshold) + { + MapInplace(x => Math.Abs(x) < threshold ? 0f : x, Zeros.AllowSkip); + } + /// /// Adds a scalar to each element of the vector and stores the result in the result vector. /// diff --git a/src/Numerics/LinearAlgebra/Vector.cs b/src/Numerics/LinearAlgebra/Vector.cs index a32839d5..074335d7 100644 --- a/src/Numerics/LinearAlgebra/Vector.cs +++ b/src/Numerics/LinearAlgebra/Vector.cs @@ -132,6 +132,19 @@ namespace MathNet.Numerics.LinearAlgebra Storage.Clear(index, count); } + /// + /// Set all values whose absolute value is smaller than the threshold to zero, in-place. + /// + public abstract void CoerceZero(double threshold); + + /// + /// Set all values that meet the predicate to zero, in-place. + /// + public void CoerceZero(Func zeroPredicate) + { + MapInplace(x => zeroPredicate(x) ? Zero : x, Zeros.AllowSkip); + } + /// /// Returns a deep-copy clone of the vector. ///