From 0e3bc094080534cfdabf33240d05f168fa4373cc Mon Sep 17 00:00:00 2001 From: tibel Date: Fri, 24 May 2013 16:32:30 +0200 Subject: [PATCH] add missing vector modulus operators --- src/Numerics/LinearAlgebra/Complex/Vector.cs | 20 +++ .../LinearAlgebra/Complex32/Vector.cs | 20 +++ src/Numerics/LinearAlgebra/Double/Vector.cs | 26 +++ src/Numerics/LinearAlgebra/Generic/Vector.cs | 157 +++++++++++++++++- src/Numerics/LinearAlgebra/Single/Vector.cs | 26 +++ 5 files changed, 240 insertions(+), 9 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Complex/Vector.cs b/src/Numerics/LinearAlgebra/Complex/Vector.cs index 39514064..dfd1826a 100644 --- a/src/Numerics/LinearAlgebra/Complex/Vector.cs +++ b/src/Numerics/LinearAlgebra/Complex/Vector.cs @@ -185,6 +185,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } } + /// + /// Pointwise modulus this vector with another vector and stores the result into the result vector. + /// + /// The vector to pointwise modulus this one by. + /// The result of the modulus. + protected override void DoPointwiseModulus(Vector other, Vector result) + { + throw new NotSupportedException(); + } + /// /// Computes the dot product between this vector and another vector. /// @@ -216,6 +226,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex throw new NotSupportedException(); } + /// + /// Computes the modulus for the given dividend for each element of the vector. + /// + /// The dividend to use. + /// A vector to store the results in. + protected override void DoModulusByThis(Complex scalar, Vector result) + { + throw new NotSupportedException(); + } + /// /// Returns the value of the absolute minimum element. /// diff --git a/src/Numerics/LinearAlgebra/Complex32/Vector.cs b/src/Numerics/LinearAlgebra/Complex32/Vector.cs index 93aece30..19109c2e 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Vector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Vector.cs @@ -185,6 +185,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } } + /// + /// Pointwise modulus this vector with another vector and stores the result into the result vector. + /// + /// The vector to pointwise modulus this one by. + /// The result of the modulus. + protected override void DoPointwiseModulus(Vector other, Vector result) + { + throw new NotSupportedException(); + } + /// /// Computes the dot product between this vector and another vector. /// @@ -216,6 +226,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 throw new NotSupportedException(); } + /// + /// Computes the modulus for the given dividend for each element of the vector. + /// + /// The dividend to use. + /// A vector to store the results in. + protected override void DoModulusByThis(Complex32 scalar, Vector result) + { + throw new NotSupportedException(); + } + /// /// Returns the value of the absolute minimum element. /// diff --git a/src/Numerics/LinearAlgebra/Double/Vector.cs b/src/Numerics/LinearAlgebra/Double/Vector.cs index f6427777..3b8ef23d 100644 --- a/src/Numerics/LinearAlgebra/Double/Vector.cs +++ b/src/Numerics/LinearAlgebra/Double/Vector.cs @@ -184,6 +184,32 @@ namespace MathNet.Numerics.LinearAlgebra.Double } } + /// + /// Pointwise modulus this vector with another vector and stores the result into the result vector. + /// + /// The vector to pointwise modulus this one by. + /// The result of the modulus. + protected override void DoPointwiseModulus(Vector other, Vector result) + { + for (var index = 0; index < Count; index++) + { + result.At(index, At(index) % other.At(index)); + } + } + + /// + /// Computes the modulus for the given dividend for each element of the vector. + /// + /// The dividend to use. + /// A vector to store the results in. + protected override void DoModulusByThis(double scalar, Vector result) + { + for (var index = 0; index < Count; index++) + { + result.At(index, scalar % At(index)); + } + } + /// /// Computes the dot product between this vector and another vector. /// diff --git a/src/Numerics/LinearAlgebra/Generic/Vector.cs b/src/Numerics/LinearAlgebra/Generic/Vector.cs index cb2b69b3..d896c2e7 100644 --- a/src/Numerics/LinearAlgebra/Generic/Vector.cs +++ b/src/Numerics/LinearAlgebra/Generic/Vector.cs @@ -240,9 +240,16 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// /// Computes the modulus for each element of the vector for the given divisor. /// - /// The divisor to use. + /// The divisor to use. /// A vector to store the results in. - protected abstract void DoModulus(T divisor, Vector result); + protected abstract void DoModulus(T scalar, Vector result); + + /// + /// Computes the modulus for the given dividend for each element of the vector. + /// + /// The dividend to use. + /// A vector to store the results in. + protected abstract void DoModulusByThis(T scalar, Vector result); /// /// Pointwise multiplies this vector with another vector and stores the result into the result vector. @@ -258,6 +265,13 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// The result of the division. protected abstract void DoPointwiseDivide(Vector other, Vector result); + /// + /// Pointwise modulus this vector with another vector and stores the result into the result vector. + /// + /// The vector to pointwise modulus this one by. + /// The result of the modulus. + protected abstract void DoPointwiseModulus(Vector other, Vector result); + /// /// Adds a scalar to each element of the vector. /// @@ -712,21 +726,21 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// /// Computes the modulus for each element of the vector for the given divisor. /// - /// The divisor to use. + /// The divisor to use. /// A vector containing the result. - public Vector Modulus(T divisor) + public Vector Modulus(T scalar) { var result = CreateVector(Count); - DoModulus(divisor, result); + DoModulus(scalar, result); return result; } /// /// Computes the modulus for each element of the vector for the given divisor. /// - /// The divisor to use. + /// The divisor to use. /// A vector to store the results in. - public void Modulus(T divisor, Vector result) + public void Modulus(T scalar, Vector result) { if (result == null) { @@ -738,7 +752,39 @@ namespace MathNet.Numerics.LinearAlgebra.Generic throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } - DoModulus(divisor, result); + DoModulus(scalar, result); + } + + /// + /// Computes the modulus for the given dividend for each element of the vector. + /// + /// The dividend to use. + /// A vector containing the result. + public Vector ModulusByThis(T scalar) + { + var result = CreateVector(Count); + DoModulusByThis(scalar, result); + return result; + } + + /// + /// Computes the modulus for the given dividend for each element of the vector. + /// + /// The dividend to use. + /// A vector to store the results in. + public void ModulusByThis(T scalar, Vector result) + { + if (result == null) + { + throw new ArgumentNullException("result"); + } + + if (Count != result.Count) + { + throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); + } + + DoModulusByThis(scalar, result); } /// @@ -857,6 +903,64 @@ namespace MathNet.Numerics.LinearAlgebra.Generic DoPointwiseDivide(other, result); } + /// + /// Pointwise modulus this vector with another vector. + /// + /// The vector to pointwise modulus this one by. + /// A new vector which is the pointwise modulus of the two vectors. + /// If the other vector is . + /// If this vector and are not the same size. + public Vector PointwiseModulus(Vector other) + { + if (other == null) + { + throw new ArgumentNullException("other"); + } + + if (Count != other.Count) + { + throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); + } + + var result = CreateVector(Count); + DoPointwiseModulus(other, result); + return result; + } + + /// + /// Pointwise modulus this vector with another vector and stores the result into the result vector. + /// + /// The vector to pointwise modulus this one by. + /// The vector to store the result of the pointwise modulus. + /// If the other vector is . + /// If the result vector is . + /// If this vector and are not the same size. + /// If this vector and are not the same size. + public void PointwiseModulus(Vector other, Vector result) + { + if (result == null) + { + throw new ArgumentNullException("result"); + } + + if (other == null) + { + throw new ArgumentNullException("other"); + } + + if (Count != other.Count) + { + throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); + } + + if (Count != result.Count) + { + throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); + } + + DoPointwiseModulus(other, result); + } + /// /// Outer product of two vectors /// @@ -1145,7 +1249,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// Computes the modulus of each element of the vector of the given divisor. /// /// The vector whose elements we want to compute the modulus of. - /// The divisor to use, + /// The divisor to use. /// The result of the calculation /// If is . public static Vector operator %(Vector leftSide, T rightSide) @@ -1158,6 +1262,41 @@ namespace MathNet.Numerics.LinearAlgebra.Generic return leftSide.Modulus(rightSide); } + /// + /// Computes the modulus of the given dividend of each element of the vector. + /// + /// The dividend we want to compute the modulus of. + /// The vector whose elements we want to use as divisor. + /// The result of the calculation + /// If is . + public static Vector operator %(T leftSide, Vector rightSide) + { + if (rightSide == null) + { + throw new ArgumentNullException("rightSide"); + } + + return rightSide.ModulusByThis(leftSide); + } + + /// + /// Computes the pointwise modulus of each element of two vectors. + /// + /// The vector whose elements we want to compute the modulus of. + /// The divisor to use. + /// The result of the calculation + /// If and are not the same size. + /// If is . + public static Vector operator %(Vector leftSide, Vector rightSide) + { + if (leftSide == null) + { + throw new ArgumentNullException("leftSide"); + } + + return leftSide.PointwiseModulus(rightSide); + } + /// /// Computes the p-Norm. /// diff --git a/src/Numerics/LinearAlgebra/Single/Vector.cs b/src/Numerics/LinearAlgebra/Single/Vector.cs index 9996a41e..d7ea08fe 100644 --- a/src/Numerics/LinearAlgebra/Single/Vector.cs +++ b/src/Numerics/LinearAlgebra/Single/Vector.cs @@ -184,6 +184,19 @@ namespace MathNet.Numerics.LinearAlgebra.Single } } + /// + /// Pointwise modulus this vector with another vector and stores the result into the result vector. + /// + /// The vector to pointwise modulus this one by. + /// The result of the modulus. + protected override void DoPointwiseModulus(Vector other, Vector result) + { + for (var index = 0; index < Count; index++) + { + result.At(index, At(index) % other.At(index)); + } + } + /// /// Computes the dot product between this vector and another vector. /// @@ -218,6 +231,19 @@ namespace MathNet.Numerics.LinearAlgebra.Single } } + /// + /// Computes the modulus for the given dividend for each element of the vector. + /// + /// The dividend to use. + /// A vector to store the results in. + protected override void DoModulusByThis(float scalar, Vector result) + { + for (var index = 0; index < Count; index++) + { + result.At(index, scalar % At(index)); + } + } + /// /// Returns the value of the absolute minimum element. ///