diff --git a/src/Numerics/Euclid.cs b/src/Numerics/Euclid.cs index d7522132..05bc2fce 100644 --- a/src/Numerics/Euclid.cs +++ b/src/Numerics/Euclid.cs @@ -50,6 +50,14 @@ namespace MathNet.Numerics return ((dividend%divisor) + divisor)%divisor; } + /// + /// Canonical Modulus. The result has the sign of the divisor. + /// + public static float Modulus(float dividend, float divisor) + { + return ((dividend%divisor) + divisor)%divisor; + } + /// /// Canonical Modulus. The result has the sign of the divisor. /// @@ -74,6 +82,14 @@ namespace MathNet.Numerics return dividend%divisor; } + /// + /// Remainder (% operator). The result has the sign of the dividend. + /// + public static float Remainder(float dividend, float divisor) + { + return dividend%divisor; + } + /// /// Remainder (% operator). The result has the sign of the dividend. /// diff --git a/src/Numerics/LinearAlgebra/Complex/Vector.cs b/src/Numerics/LinearAlgebra/Complex/Vector.cs index 7e3f7bbe..f98f7447 100644 --- a/src/Numerics/LinearAlgebra/Complex/Vector.cs +++ b/src/Numerics/LinearAlgebra/Complex/Vector.cs @@ -191,11 +191,23 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } /// - /// Pointwise modulus this vector with another vector and stores the result into the result vector. + /// Pointwise canonical modulus, where the result has the sign of the divisor, + /// of this vector with another vector and stores the result into the result vector. /// - /// The vector to pointwise modulus this one by. + /// The pointwise denominator vector to use. /// The result of the modulus. - protected override void DoPointwiseModulus(Vector divisor, Vector result) + protected override sealed void DoPointwiseModulus(Vector divisor, Vector result) + { + throw new NotSupportedException(); + } + + /// + /// Pointwise remainder (% operator), where the result has the sign of the dividend, + /// of this vector with another vector and stores the result into the result vector. + /// + /// The pointwise denominator vector to use. + /// The result of the modulus. + protected override sealed void DoPointwiseRemainder(Vector divisor, Vector result) { throw new NotSupportedException(); } @@ -231,21 +243,45 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } /// - /// Computes the modulus for each element of the vector for the given divisor. + /// Computes the canonical modulus, where the result has the sign of the divisor, + /// for each element of the vector for the given divisor. + /// + /// The scalar denominator to use. + /// A vector to store the results in. + protected override sealed void DoModulus(Complex divisor, Vector result) + { + throw new NotSupportedException(); + } + + /// + /// Computes the canonical modulus, where the result has the sign of the divisor, + /// for the given dividend for each element of the vector. + /// + /// The scalar numerator to use. + /// A vector to store the results in. + protected override sealed void DoModulusByThis(Complex dividend, Vector result) + { + throw new NotSupportedException(); + } + + /// + /// Computes the canonical modulus, where the result has the sign of the divisor, + /// for each element of the vector for the given divisor. /// /// The scalar denominator to use. /// A vector to store the results in. - protected override void DoModulus(Complex divisor, Vector result) + protected override sealed void DoRemainder(Complex divisor, Vector result) { throw new NotSupportedException(); } /// - /// Computes the modulus for the given dividend for each element of the vector. + /// Computes the canonical modulus, where the result has the sign of the divisor, + /// for the given dividend for each element of the vector. /// /// The scalar numerator to use. /// A vector to store the results in. - protected override void DoModulusByThis(Complex dividend, Vector result) + protected override sealed void DoRemainderByThis(Complex dividend, Vector result) { throw new NotSupportedException(); } diff --git a/src/Numerics/LinearAlgebra/Complex32/Vector.cs b/src/Numerics/LinearAlgebra/Complex32/Vector.cs index 310c1d68..5879840f 100644 --- a/src/Numerics/LinearAlgebra/Complex32/Vector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/Vector.cs @@ -186,11 +186,23 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } /// - /// Pointwise modulus this vector with another vector and stores the result into the result vector. + /// Pointwise canonical modulus, where the result has the sign of the divisor, + /// of this vector with another vector and stores the result into the result vector. /// - /// The vector to pointwise modulus this one by. + /// The pointwise denominator vector to use. /// The result of the modulus. - protected override void DoPointwiseModulus(Vector divisor, Vector result) + protected override sealed void DoPointwiseModulus(Vector divisor, Vector result) + { + throw new NotSupportedException(); + } + + /// + /// Pointwise remainder (% operator), where the result has the sign of the dividend, + /// of this vector with another vector and stores the result into the result vector. + /// + /// The pointwise denominator vector to use. + /// The result of the modulus. + protected override sealed void DoPointwiseRemainder(Vector divisor, Vector result) { throw new NotSupportedException(); } @@ -226,21 +238,45 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } /// - /// Computes the modulus for each element of the vector for the given divisor. + /// Computes the canonical modulus, where the result has the sign of the divisor, + /// for each element of the vector for the given divisor. + /// + /// The scalar denominator to use. + /// A vector to store the results in. + protected override sealed void DoModulus(Complex32 divisor, Vector result) + { + throw new NotSupportedException(); + } + + /// + /// Computes the canonical modulus, where the result has the sign of the divisor, + /// for the given dividend for each element of the vector. + /// + /// The scalar numerator to use. + /// A vector to store the results in. + protected override sealed void DoModulusByThis(Complex32 dividend, Vector result) + { + throw new NotSupportedException(); + } + + /// + /// Computes the remainder (% operator), where the result has the sign of the dividend, + /// for each element of the vector for the given divisor. /// /// The scalar denominator to use. /// A vector to store the results in. - protected override void DoModulus(Complex32 divisor, Vector result) + protected override sealed void DoRemainder(Complex32 divisor, Vector result) { throw new NotSupportedException(); } /// - /// Computes the modulus for the given dividend for each element of the vector. + /// Computes the remainder (% operator), where the result has the sign of the dividend, + /// for the given dividend for each element of the vector. /// /// The scalar numerator to use. /// A vector to store the results in. - protected override void DoModulusByThis(Complex32 dividend, Vector result) + protected override sealed void DoRemainderByThis(Complex32 dividend, Vector result) { throw new NotSupportedException(); } diff --git a/src/Numerics/LinearAlgebra/Double/DenseVector.cs b/src/Numerics/LinearAlgebra/Double/DenseVector.cs index 53d1465a..991004d7 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseVector.cs @@ -469,7 +469,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double } /// - /// Computes the modulus for each element of the vector for the given divisor. + /// Computes the canonical modulus, where the result has the sign of the divisor, + /// for each element of the vector for the given divisor. /// /// The divisor to use. /// A vector to store the results in. @@ -483,21 +484,46 @@ namespace MathNet.Numerics.LinearAlgebra.Double else { CommonParallel.For(0, _length, 4096, (a, b) => + { + for (int i = a; i < b; i++) { - for (int i = a; i < b; i++) - { - dense._values[i] = _values[i]%divisor; - } - }); + dense._values[i] = Euclid.Modulus(_values[i], divisor); + } + }); + } + } + + /// + /// Computes the remainder (% operator), where the result has the sign of the dividend, + /// for each element of the vector for the given divisor. + /// + /// The divisor to use. + /// A vector to store the results in. + protected override void DoRemainder(double divisor, Vector result) + { + var dense = result as DenseVector; + if (dense == null) + { + base.DoModulus(divisor, result); + } + else + { + CommonParallel.For(0, _length, 4096, (a, b) => + { + for (int i = a; i < b; i++) + { + dense._values[i] = _values[i]%divisor; + } + }); } } /// - /// Computes the modulus of each element of the vector of the given divisor. + /// Computes the remainder (% operator), where the result has the sign of the dividend, + /// of each element of the vector of the given divisor. /// - /// The vector whose elements we want to compute the modulus of. + /// The vector whose elements we want to compute the remainder of. /// The divisor to use, - /// The result of the calculation /// If is . public static DenseVector operator %(DenseVector leftSide, double rightSide) { @@ -506,7 +532,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("leftSide"); } - return (DenseVector)leftSide.Modulus(rightSide); + return (DenseVector)leftSide.Remainder(rightSide); } /// diff --git a/src/Numerics/LinearAlgebra/Double/SparseVector.cs b/src/Numerics/LinearAlgebra/Double/SparseVector.cs index c02aea3f..6555f23d 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseVector.cs @@ -460,11 +460,37 @@ namespace MathNet.Numerics.LinearAlgebra.Double } /// - /// Computes the modulus for each element of the vector for the given divisor. + /// Computes the canonical modulus, where the result has the sign of the divisor, + /// for each element of the vector for the given divisor. /// - /// The divisor to use. + /// The scalar denominator to use. /// A vector to store the results in. protected override void DoModulus(double divisor, Vector result) + { + if (ReferenceEquals(this, result)) + { + for (var index = 0; index < _storage.ValueCount; index++) + { + _storage.Values[index] = Euclid.Modulus(_storage.Values[index], divisor); + } + } + else + { + result.Clear(); + for (var index = 0; index < _storage.ValueCount; index++) + { + result.At(_storage.Indices[index], Euclid.Modulus(_storage.Values[index], divisor)); + } + } + } + + /// + /// Computes the remainder (% operator), where the result has the sign of the dividend, + /// for each element of the vector for the given divisor. + /// + /// The scalar denominator to use. + /// A vector to store the results in. + protected override void DoRemainder(double divisor, Vector result) { if (ReferenceEquals(this, result)) { @@ -478,7 +504,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double result.Clear(); for (var index = 0; index < _storage.ValueCount; index++) { - result.At(_storage.Indices[index], _storage.Values[index] % divisor); + result.At(_storage.Indices[index], _storage.Values[index]%divisor); } } } @@ -605,7 +631,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double } /// - /// Computes the modulus of each element of the vector of the given divisor. + /// Computes the remainder (% operator), where the result has the sign of the dividend, + /// 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, @@ -618,7 +645,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("leftSide"); } - return (SparseVector)leftSide.Modulus(rightSide); + return (SparseVector)leftSide.Remainder(rightSide); } /// diff --git a/src/Numerics/LinearAlgebra/Double/Vector.cs b/src/Numerics/LinearAlgebra/Double/Vector.cs index 13370e19..b92421f2 100644 --- a/src/Numerics/LinearAlgebra/Double/Vector.cs +++ b/src/Numerics/LinearAlgebra/Double/Vector.cs @@ -41,7 +41,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double public abstract class Vector : Vector { /// - /// Initializes a new instance of the Vector class. + /// Initializes a new instance of the Vector class. /// protected Vector(VectorStorage storage) : base(storage) @@ -184,15 +184,30 @@ namespace MathNet.Numerics.LinearAlgebra.Double } /// - /// Pointwise modulus this vector with another vector and stores the result into the result vector. + /// Pointwise canonical modulus, where the result has the sign of the divisor, + /// of this vector with another vector and stores the result into the result vector. /// - /// The vector to pointwise modulus this one by. + /// The pointwise denominator vector to use. /// The result of the modulus. protected override void DoPointwiseModulus(Vector divisor, Vector result) { for (var index = 0; index < Count; index++) { - result.At(index, At(index) % divisor.At(index)); + result.At(index, Euclid.Modulus(At(index), divisor.At(index))); + } + } + + /// + /// Pointwise remainder (% operator), where the result has the sign of the dividend, + /// of this vector with another vector and stores the result into the result vector. + /// + /// The pointwise denominator vector to use. + /// The result of the modulus. + protected override void DoPointwiseRemainder(Vector divisor, Vector result) + { + for (var index = 0; index < Count; index++) + { + result.At(index, At(index)%divisor.At(index)); } } @@ -222,7 +237,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double } /// - /// Computes the modulus for each element of the vector for the given divisor. + /// Computes the canonical modulus, where the result has the sign of the divisor, + /// for each element of the vector for the given divisor. /// /// The scalar denominator to use. /// A vector to store the results in. @@ -230,16 +246,45 @@ namespace MathNet.Numerics.LinearAlgebra.Double { for (int i = 0; i < Count; i++) { - result.At(i, At(i)%divisor); + result.At(i, Euclid.Modulus(At(i), divisor)); } } /// - /// Computes the modulus for the given dividend for each element of the vector. + /// Computes the canonical modulus, where the result has the sign of the divisor, + /// for the given dividend for each element of the vector. /// /// The scalar numerator to use. /// A vector to store the results in. protected override void DoModulusByThis(double dividend, Vector result) + { + for (var index = 0; index < Count; index++) + { + result.At(index, Euclid.Modulus(dividend, At(index))); + } + } + + /// + /// Computes the remainder (% operator), where the result has the sign of the dividend, + /// for each element of the vector for the given divisor. + /// + /// The scalar denominator to use. + /// A vector to store the results in. + protected override void DoRemainder(double divisor, Vector result) + { + for (int i = 0; i < Count; i++) + { + result.At(i, At(i)%divisor); + } + } + + /// + /// Computes the remainder (% operator), where the result has the sign of the dividend, + /// for the given dividend for each element of the vector. + /// + /// The scalar numerator to use. + /// A vector to store the results in. + protected override void DoRemainderByThis(double dividend, Vector result) { for (var index = 0; index < Count; index++) { @@ -259,7 +304,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// /// Returns the index of the absolute minimum element. /// - /// The index of absolute minimum element. + /// The index of absolute minimum element. public override int AbsoluteMinimumIndex() { var index = 0; @@ -289,7 +334,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// /// Returns the index of the absolute maximum element. /// - /// The index of absolute maximum element. + /// The index of absolute maximum element. public override int AbsoluteMaximumIndex() { var index = 0; @@ -407,7 +452,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// /// Returns the index of the absolute maximum element. /// - /// The index of absolute maximum element. + /// The index of absolute maximum element. public override int MaximumIndex() { var index = 0; @@ -428,7 +473,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// /// Returns the index of the minimum element. /// - /// The index of minimum element. + /// The index of minimum element. public override int MinimumIndex() { var index = 0; diff --git a/src/Numerics/LinearAlgebra/Single/DenseVector.cs b/src/Numerics/LinearAlgebra/Single/DenseVector.cs index 19762f2d..6527b00f 100644 --- a/src/Numerics/LinearAlgebra/Single/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/DenseVector.cs @@ -458,9 +458,10 @@ namespace MathNet.Numerics.LinearAlgebra.Single } /// - /// Computes the modulus for each element of the vector for the given divisor. + /// Computes the canonical modulus, where the result has the sign of the divisor, + /// for each element of the vector for the given divisor. /// - /// The divisor to use. + /// The scalar denominator to use. /// A vector to store the results in. protected override void DoModulus(float divisor, Vector result) { @@ -472,17 +473,43 @@ namespace MathNet.Numerics.LinearAlgebra.Single else { CommonParallel.For(0, _length, 4096, (a, b) => + { + for (int i = a; i < b; i++) { - for (int i = a; i < b; i++) - { - dense._values[i] = _values[i]%divisor; - } - }); + dense._values[i] = Euclid.Modulus(_values[i], divisor); + } + }); + } + } + + /// + /// Computes the remainder (% operator), where the result has the sign of the dividend, + /// for each element of the vector for the given divisor. + /// + /// The scalar denominator to use. + /// A vector to store the results in. + protected override void DoRemainder(float divisor, Vector result) + { + var dense = result as DenseVector; + if (dense == null) + { + base.DoModulus(divisor, result); + } + else + { + CommonParallel.For(0, _length, 4096, (a, b) => + { + for (int i = a; i < b; i++) + { + dense._values[i] = _values[i]%divisor; + } + }); } } /// - /// Computes the modulus of each element of the vector of the given divisor. + /// Computes the remainder (% operator), where the result has the sign of the dividend, + /// 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, @@ -495,7 +522,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single throw new ArgumentNullException("leftSide"); } - return (DenseVector)leftSide.Modulus(rightSide); + return (DenseVector)leftSide.Remainder(rightSide); } /// diff --git a/src/Numerics/LinearAlgebra/Single/SparseVector.cs b/src/Numerics/LinearAlgebra/Single/SparseVector.cs index 5fcc98cc..ad5a9bfe 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseVector.cs @@ -461,11 +461,37 @@ namespace MathNet.Numerics.LinearAlgebra.Single } /// - /// Computes the modulus for each element of the vector for the given divisor. + /// Computes the canonical modulus, where the result has the sign of the divisor, + /// for each element of the vector for the given divisor. /// - /// The divisor to use. + /// The scalar denominator to use. /// A vector to store the results in. protected override void DoModulus(float divisor, Vector result) + { + if (ReferenceEquals(this, result)) + { + for (var index = 0; index < _storage.ValueCount; index++) + { + _storage.Values[index] = Euclid.Modulus(_storage.Values[index], divisor); + } + } + else + { + result.Clear(); + for (var index = 0; index < _storage.ValueCount; index++) + { + result.At(_storage.Indices[index], Euclid.Modulus(_storage.Values[index], divisor)); + } + } + } + + /// + /// Computes the remainder (% operator), where the result has the sign of the dividend, + /// for each element of the vector for the given divisor. + /// + /// The scalar denominator to use. + /// A vector to store the results in. + protected override void DoRemainder(float divisor, Vector result) { if (ReferenceEquals(this, result)) { @@ -606,7 +632,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single } /// - /// Computes the modulus of each element of the vector of the given divisor. + /// Computes the remainder (% operator), where the result has the sign of the dividend, + /// 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, @@ -619,7 +646,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single throw new ArgumentNullException("leftSide"); } - return (SparseVector)leftSide.Modulus(rightSide); + return (SparseVector)leftSide.Remainder(rightSide); } /// diff --git a/src/Numerics/LinearAlgebra/Single/Vector.cs b/src/Numerics/LinearAlgebra/Single/Vector.cs index 00bf0351..edff9cfe 100644 --- a/src/Numerics/LinearAlgebra/Single/Vector.cs +++ b/src/Numerics/LinearAlgebra/Single/Vector.cs @@ -184,15 +184,30 @@ namespace MathNet.Numerics.LinearAlgebra.Single } /// - /// Pointwise modulus this vector with another vector and stores the result into the result vector. + /// Pointwise canonical modulus, where the result has the sign of the divisor, + /// of this vector with another vector and stores the result into the result vector. /// - /// The vector to pointwise modulus this one by. + /// The pointwise denominator vector to use. /// The result of the modulus. protected override void DoPointwiseModulus(Vector divisor, Vector result) { for (var index = 0; index < Count; index++) { - result.At(index, At(index) % divisor.At(index)); + result.At(index, Euclid.Modulus(At(index), divisor.At(index))); + } + } + + /// + /// Pointwise remainder (% operator), where the result has the sign of the dividend, + /// of this vector with another vector and stores the result into the result vector. + /// + /// The pointwise denominator vector to use. + /// The result of the modulus. + protected override void DoPointwiseRemainder(Vector divisor, Vector result) + { + for (var index = 0; index < Count; index++) + { + result.At(index, At(index)%divisor.At(index)); } } @@ -222,7 +237,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single } /// - /// Computes the modulus for each element of the vector for the given divisor. + /// Computes the canonical modulus, where the result has the sign of the divisor, + /// for each element of the vector for the given divisor. /// /// The scalar denominator to use. /// A vector to store the results in. @@ -230,16 +246,45 @@ namespace MathNet.Numerics.LinearAlgebra.Single { for (int i = 0; i < Count; i++) { - result.At(i, At(i)%divisor); + result.At(i, Euclid.Modulus(At(i), divisor)); } } /// - /// Computes the modulus for the given dividend for each element of the vector. + /// Computes the canonical modulus, where the result has the sign of the divisor, + /// for the given dividend for each element of the vector. /// /// The scalar numerator to use. /// A vector to store the results in. protected override void DoModulusByThis(float dividend, Vector result) + { + for (var index = 0; index < Count; index++) + { + result.At(index, Euclid.Modulus(dividend, At(index))); + } + } + + /// + /// Computes the remainder (% operator), where the result has the sign of the dividend, + /// for each element of the vector for the given divisor. + /// + /// The scalar denominator to use. + /// A vector to store the results in. + protected override void DoRemainder(float divisor, Vector result) + { + for (int i = 0; i < Count; i++) + { + result.At(i, At(i)%divisor); + } + } + + /// + /// Computes the remainder (% operator), where the result has the sign of the dividend, + /// for the given dividend for each element of the vector. + /// + /// The scalar numerator to use. + /// A vector to store the results in. + protected override void DoRemainderByThis(float dividend, Vector result) { for (var index = 0; index < Count; index++) { diff --git a/src/Numerics/LinearAlgebra/Vector.Arithmetic.cs b/src/Numerics/LinearAlgebra/Vector.Arithmetic.cs index 5f3f5531..840d8e23 100644 --- a/src/Numerics/LinearAlgebra/Vector.Arithmetic.cs +++ b/src/Numerics/LinearAlgebra/Vector.Arithmetic.cs @@ -132,19 +132,37 @@ namespace MathNet.Numerics.LinearAlgebra protected abstract void DoDivideByThis(T dividend, Vector result); /// - /// Computes the modulus for each element of the vector for the given divisor. + /// Computes the canonical modulus, where the result has the sign of the divisor, + /// for each element of the vector for the given divisor. /// /// The scalar denominator to use. /// A vector to store the results in. protected abstract void DoModulus(T divisor, Vector result); /// - /// Computes the modulus for the given dividend for each element of the vector. + /// Computes the canonical modulus, where the result has the sign of the divisor, + /// for the given dividend for each element of the vector. /// /// The scalar numerator to use. /// A vector to store the results in. protected abstract void DoModulusByThis(T dividend, Vector result); + /// + /// Computes the remainder (% operator), where the result has the sign of the dividend, + /// for each element of the vector for the given divisor. + /// + /// The scalar denominator to use. + /// A vector to store the results in. + protected abstract void DoRemainder(T divisor, Vector result); + + /// + /// Computes the remainder (% operator), where the result has the sign of the dividend, + /// for the given dividend for each element of the vector. + /// + /// The scalar numerator to use. + /// A vector to store the results in. + protected abstract void DoRemainderByThis(T dividend, Vector result); + /// /// Pointwise multiplies this vector with another vector and stores the result into the result vector. /// @@ -160,12 +178,21 @@ namespace MathNet.Numerics.LinearAlgebra protected abstract void DoPointwiseDivide(Vector divisor, Vector result); /// - /// Pointwise modulus this vector with another vector and stores the result into the result vector. + /// Pointwise canonical modulus, where the result has the sign of the divisor, + /// of this vector with another vector and stores the result into the result vector. /// /// The pointwise denominator vector to use. /// The result of the modulus. protected abstract void DoPointwiseModulus(Vector divisor, Vector result); + /// + /// Pointwise remainder (% operator), where the result has the sign of the dividend, + /// of this vector with another vector and stores the result into the result vector. + /// + /// The pointwise denominator vector to use. + /// The result of the modulus. + protected abstract void DoPointwiseRemainder(Vector divisor, Vector result); + /// /// Adds a scalar to each element of the vector. /// @@ -540,7 +567,8 @@ namespace MathNet.Numerics.LinearAlgebra } /// - /// Computes the modulus (vector % divisor) for each element of the vector for the given divisor. + /// Computes the canonical modulus, where the result has the sign of the divisor, + /// for each element of the vector for the given divisor. /// /// The scalar denominator to use. /// A vector containing the result. @@ -552,7 +580,8 @@ namespace MathNet.Numerics.LinearAlgebra } /// - /// Computes the modulus (vector % divisor) for each element of the vector for the given divisor. + /// Computes the canonical modulus, where the result has the sign of the divisor, + /// for each element of the vector for the given divisor. /// /// The scalar denominator to use. /// A vector to store the results in. @@ -567,7 +596,8 @@ namespace MathNet.Numerics.LinearAlgebra } /// - /// Computes the modulus (dividend % vector) for the given dividend for each element of the vector. + /// Computes the canonical modulus, where the result has the sign of the divisor, + /// for the given dividend for each element of the vector. /// /// The scalar numerator to use. /// A vector containing the result. @@ -579,7 +609,8 @@ namespace MathNet.Numerics.LinearAlgebra } /// - /// Computes the modulus (dividend % vector) for the given dividend for each element of the vector. + /// Computes the canonical modulus, where the result has the sign of the divisor, + /// for the given dividend for each element of the vector. /// /// The scalar numerator to use. /// A vector to store the results in. @@ -593,6 +624,64 @@ namespace MathNet.Numerics.LinearAlgebra DoModulusByThis(dividend, result); } + /// + /// Computes the remainder (vector % divisor), where the result has the sign of the dividend, + /// for each element of the vector for the given divisor. + /// + /// The scalar denominator to use. + /// A vector containing the result. + public Vector Remainder(T divisor) + { + var result = Build.SameAs(this); + DoRemainder(divisor, result); + return result; + } + + /// + /// Computes the remainder (vector % divisor), where the result has the sign of the dividend, + /// for each element of the vector for the given divisor. + /// + /// The scalar denominator to use. + /// A vector to store the results in. + public void Remainder(T divisor, Vector result) + { + if (Count != result.Count) + { + throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); + } + + DoRemainder(divisor, result); + } + + /// + /// Computes the remainder (dividend % vector), where the result has the sign of the dividend, + /// for the given dividend for each element of the vector. + /// + /// The scalar numerator to use. + /// A vector containing the result. + public Vector RemainderByThis(T dividend) + { + var result = Build.SameAs(this); + DoRemainderByThis(dividend, result); + return result; + } + + /// + /// Computes the remainder (dividend % vector), where the result has the sign of the dividend, + /// for the given dividend for each element of the vector. + /// + /// The scalar numerator to use. + /// A vector to store the results in. + public void RemainderByThis(T dividend, Vector result) + { + if (Count != result.Count) + { + throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); + } + + DoRemainderByThis(dividend, result); + } + /// /// Pointwise multiplies this vector with another vector. /// @@ -674,7 +763,8 @@ namespace MathNet.Numerics.LinearAlgebra } /// - /// Pointwise modulus this vector with another vector. + /// Pointwise canonical modulus, where the result has the sign of the divisor, + /// of this vector with another vector. /// /// The pointwise denominator vector to use. /// A new vector which is the pointwise modulus of the two vectors. @@ -692,7 +782,8 @@ namespace MathNet.Numerics.LinearAlgebra } /// - /// Pointwise modulus this vector with another vector and stores the result into the result vector. + /// Pointwise canonical modulus, where the result has the sign of the divisor, + /// of this vector with another vector and stores the result into the result vector. /// /// The pointwise denominator vector to use. /// The vector to store the result of the pointwise modulus. @@ -712,6 +803,47 @@ namespace MathNet.Numerics.LinearAlgebra DoPointwiseModulus(divisor, result); } + /// + /// Pointwise remainder (% operator), where the result has the sign of the dividend, + /// of this vector with another vector. + /// + /// The pointwise denominator vector to use. + /// A new vector which is the pointwise remainder of the two vectors. + /// If this vector and are not the same size. + public Vector PointwiseRemainder(Vector divisor) + { + if (Count != divisor.Count) + { + throw new ArgumentException(Resources.ArgumentVectorsSameLength, "divisor"); + } + + var result = Build.SameAs(this, divisor); + DoPointwiseRemainder(divisor, result); + return result; + } + + /// + /// Pointwise remainder (% operator), where the result has the sign of the dividend, + /// this vector with another vector and stores the result into the result vector. + /// + /// The pointwise denominator vector to use. + /// The vector to store the result of the pointwise remainder. + /// If this vector and are not the same size. + /// If this vector and are not the same size. + public void PointwiseRemainder(Vector divisor, Vector result) + { + if (Count != divisor.Count) + { + throw new ArgumentException(Resources.ArgumentVectorsSameLength, "divisor"); + } + + if (Count != result.Count) + { + throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); + } + + DoPointwiseRemainder(divisor, result); + } /// /// Outer product of two vectors diff --git a/src/Numerics/LinearAlgebra/Vector.Operators.cs b/src/Numerics/LinearAlgebra/Vector.Operators.cs index 7ef041d1..d275f113 100644 --- a/src/Numerics/LinearAlgebra/Vector.Operators.cs +++ b/src/Numerics/LinearAlgebra/Vector.Operators.cs @@ -207,40 +207,40 @@ namespace MathNet.Numerics.LinearAlgebra } /// - /// Computes the modulus of each element of the vector of the given divisor. + /// Computes the remainder (% operator), where the result has the sign of the dividend, + /// of each element of the vector of the given divisor. /// - /// The vector whose elements we want to compute the modulus of. + /// The vector whose elements we want to compute the remainder of. /// The divisor to use. - /// The result of the calculation /// If is . public static Vector operator %(Vector dividend, T divisor) { - return dividend.Modulus(divisor); + return dividend.Remainder(divisor); } /// - /// Computes the modulus of the given dividend of each element of the vector. + /// Computes the remainder (% operator), where the result has the sign of the dividend, + /// of the given dividend of each element of the vector. /// - /// The dividend we want to compute the modulus of. + /// The dividend we want to compute the remainder of. /// The vector whose elements we want to use as divisor. - /// The result of the calculation /// If is . public static Vector operator %(T dividend, Vector divisor) { - return divisor.ModulusByThis(dividend); + return divisor.RemainderByThis(dividend); } /// - /// Computes the pointwise modulus of each element of two vectors. + /// Computes the pointwise remainder (% operator), where the result has the sign of the dividend, + /// of each element of two vectors. /// - /// The vector whose elements we want to compute the modulus of. + /// The vector whose elements we want to compute the remainder of. /// The divisor to use. - /// The result of the calculation /// If and are not the same size. /// If is . public static Vector operator %(Vector dividend, Vector divisor) { - return dividend.PointwiseModulus(divisor); + return dividend.PointwiseRemainder(divisor); } [SpecialName] @@ -258,7 +258,7 @@ namespace MathNet.Numerics.LinearAlgebra [SpecialName] public static Vector op_DotPercent(Vector dividend, Vector divisor) { - return dividend.PointwiseModulus(divisor); + return dividend.PointwiseRemainder(divisor); } } } diff --git a/src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs b/src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs index 04751103..7dd60986 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs @@ -791,13 +791,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double } /// - /// Can compute the modules of each element of vector. + /// Can compute the remainder of each element of vector. /// [Test] - public void CanComputeModulus() + public void CanComputeRemainder() { var vector = CreateVector(Data); - var mod = vector.Modulus(3.2); + var mod = vector.Remainder(3.2); for (var index = 0; index < Data.Length; index++) { AssertHelpers.AlmostEqualRelative(Data[index] % 3.2, mod[index], 14); @@ -805,15 +805,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double } /// - /// Can compute the modules of each element of vector using a result vector. + /// Can compute the remainder of each element of vector using a result vector. /// [Test] - public void CanComputeModulusUsingResultVector() + public void CanComputeRemainderUsingResultVector() { var vector = CreateVector(Data); var mod = CreateVector(vector.Count); - vector.Modulus(3.2, mod); - + vector.Remainder(3.2, mod); for (var index = 0; index < Data.Length; index++) { AssertHelpers.AlmostEqualRelative(Data[index] % 3.2, mod[index], 14); @@ -821,14 +820,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double } /// - /// Can compute the modules of each element of vector using a result vector. + /// Can compute the remainder of each element of vector using a result vector. /// [Test] - public void CanComputeModulusUsingSameResultVector() + public void CanComputeRemainderUsingSameResultVector() { var vector = CreateVector(Data); - vector.Modulus(3.2, vector); - + vector.Remainder(3.2, vector); for (var index = 0; index < Data.Length; index++) { AssertHelpers.AlmostEqualRelative(Data[index] % 3.2, vector[index], 14); @@ -836,10 +834,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double } /// - /// Can compute the modules of each element of vector using the operator %. + /// Can compute the remainder of each element of vector using the operator %. /// [Test] - public void CanComputeModulusUsingOperator() + public void CanComputeRemainderUsingOperator() { var vector = CreateVector(Data); var mod = vector % 4.5; diff --git a/src/UnitTests/LinearAlgebraTests/Single/VectorTests.Arithmetic.cs b/src/UnitTests/LinearAlgebraTests/Single/VectorTests.Arithmetic.cs index 315c077d..418899fb 100644 --- a/src/UnitTests/LinearAlgebraTests/Single/VectorTests.Arithmetic.cs +++ b/src/UnitTests/LinearAlgebraTests/Single/VectorTests.Arithmetic.cs @@ -791,61 +791,59 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single } /// - /// Can compute the modules of each element of vector. + /// Can compute the remainder of each element of vector. /// [Test] - public void CanComputeModulus() + public void CanComputeRemainder() { var vector = CreateVector(Data); - var mod = vector.Modulus(3.2f); + var mod = vector.Remainder(3.2f); for (var index = 0; index < Data.Length; index++) { - AssertHelpers.AlmostEqual(Data[index] % 3.2, mod[index], 6); + AssertHelpers.AlmostEqualRelative(Data[index] % 3.2f, mod[index], 14); } } /// - /// Can compute the modules of each element of vector using a result vector. + /// Can compute the remainder of each element of vector using a result vector. /// [Test] - public void CanComputeModulusUsingResultVector() + public void CanComputeRemainderUsingResultVector() { var vector = CreateVector(Data); var mod = CreateVector(vector.Count); - vector.Modulus(3.2f, mod); - + vector.Remainder(3.2f, mod); for (var index = 0; index < Data.Length; index++) { - AssertHelpers.AlmostEqual(Data[index] % 3.2, mod[index], 6); + AssertHelpers.AlmostEqualRelative(Data[index] % 3.2f, mod[index], 14); } } /// - /// Can compute the modules of each element of vector using a result vector. + /// Can compute the remainder of each element of vector using a result vector. /// [Test] - public void CanComputeModulusUsingSameResultVector() + public void CanComputeRemainderUsingSameResultVector() { var vector = CreateVector(Data); - vector.Modulus(3.2f, vector); - + vector.Remainder(3.2f, vector); for (var index = 0; index < Data.Length; index++) { - AssertHelpers.AlmostEqual(Data[index] % 3.2, vector[index], 6); + AssertHelpers.AlmostEqualRelative(Data[index] % 3.2f, vector[index], 14); } } /// - /// Can compute the modules of each element of vector using the operator %. + /// Can compute the remainder of each element of vector using the operator %. /// [Test] - public void CanComputeModulusUsingOperator() + public void CanComputeRemainderUsingOperator() { var vector = CreateVector(Data); var mod = vector % 4.5f; for (var index = 0; index < Data.Length; index++) { - AssertHelpers.AlmostEqual(Data[index] % 4.5, mod[index], 6); + AssertHelpers.AlmostEqualRelative(Data[index] % 4.5f, mod[index], 14); } } }