Browse Source

LA: Vector canonical modulus vs remainder #175

pull/194/head
Christoph Ruegg 13 years ago
parent
commit
58c0f873e6
  1. 16
      src/Numerics/Euclid.cs
  2. 50
      src/Numerics/LinearAlgebra/Complex/Vector.cs
  3. 50
      src/Numerics/LinearAlgebra/Complex32/Vector.cs
  4. 46
      src/Numerics/LinearAlgebra/Double/DenseVector.cs
  5. 37
      src/Numerics/LinearAlgebra/Double/SparseVector.cs
  6. 67
      src/Numerics/LinearAlgebra/Double/Vector.cs
  7. 45
      src/Numerics/LinearAlgebra/Single/DenseVector.cs
  8. 35
      src/Numerics/LinearAlgebra/Single/SparseVector.cs
  9. 57
      src/Numerics/LinearAlgebra/Single/Vector.cs
  10. 150
      src/Numerics/LinearAlgebra/Vector.Arithmetic.cs
  11. 26
      src/Numerics/LinearAlgebra/Vector.Operators.cs
  12. 24
      src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs
  13. 32
      src/UnitTests/LinearAlgebraTests/Single/VectorTests.Arithmetic.cs

16
src/Numerics/Euclid.cs

@ -50,6 +50,14 @@ namespace MathNet.Numerics
return ((dividend%divisor) + divisor)%divisor;
}
/// <summary>
/// Canonical Modulus. The result has the sign of the divisor.
/// </summary>
public static float Modulus(float dividend, float divisor)
{
return ((dividend%divisor) + divisor)%divisor;
}
/// <summary>
/// Canonical Modulus. The result has the sign of the divisor.
/// </summary>
@ -74,6 +82,14 @@ namespace MathNet.Numerics
return dividend%divisor;
}
/// <summary>
/// Remainder (% operator). The result has the sign of the dividend.
/// </summary>
public static float Remainder(float dividend, float divisor)
{
return dividend%divisor;
}
/// <summary>
/// Remainder (% operator). The result has the sign of the dividend.
/// </summary>

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

@ -191,11 +191,23 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
}
/// <summary>
/// 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.
/// </summary>
/// <param name="divisor">The vector to pointwise modulus this one by.</param>
/// <param name="divisor">The pointwise denominator vector to use.</param>
/// <param name="result">The result of the modulus.</param>
protected override void DoPointwiseModulus(Vector<Complex> divisor, Vector<Complex> result)
protected override sealed void DoPointwiseModulus(Vector<Complex> divisor, Vector<Complex> result)
{
throw new NotSupportedException();
}
/// <summary>
/// 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.
/// </summary>
/// <param name="divisor">The pointwise denominator vector to use.</param>
/// <param name="result">The result of the modulus.</param>
protected override sealed void DoPointwiseRemainder(Vector<Complex> divisor, Vector<Complex> result)
{
throw new NotSupportedException();
}
@ -231,21 +243,45 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
}
/// <summary>
/// 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.
/// </summary>
/// <param name="divisor">The scalar denominator to use.</param>
/// <param name="result">A vector to store the results in.</param>
protected override sealed void DoModulus(Complex divisor, Vector<Complex> result)
{
throw new NotSupportedException();
}
/// <summary>
/// Computes the canonical modulus, where the result has the sign of the divisor,
/// for the given dividend for each element of the vector.
/// </summary>
/// <param name="dividend">The scalar numerator to use.</param>
/// <param name="result">A vector to store the results in.</param>
protected override sealed void DoModulusByThis(Complex dividend, Vector<Complex> result)
{
throw new NotSupportedException();
}
/// <summary>
/// Computes the canonical modulus, where the result has the sign of the divisor,
/// for each element of the vector for the given divisor.
/// </summary>
/// <param name="divisor">The scalar denominator to use.</param>
/// <param name="result">A vector to store the results in.</param>
protected override void DoModulus(Complex divisor, Vector<Complex> result)
protected override sealed void DoRemainder(Complex divisor, Vector<Complex> result)
{
throw new NotSupportedException();
}
/// <summary>
/// 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.
/// </summary>
/// <param name="dividend">The scalar numerator to use.</param>
/// <param name="result">A vector to store the results in.</param>
protected override void DoModulusByThis(Complex dividend, Vector<Complex> result)
protected override sealed void DoRemainderByThis(Complex dividend, Vector<Complex> result)
{
throw new NotSupportedException();
}

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

@ -186,11 +186,23 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
}
/// <summary>
/// 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.
/// </summary>
/// <param name="divisor">The vector to pointwise modulus this one by.</param>
/// <param name="divisor">The pointwise denominator vector to use.</param>
/// <param name="result">The result of the modulus.</param>
protected override void DoPointwiseModulus(Vector<Complex32> divisor, Vector<Complex32> result)
protected override sealed void DoPointwiseModulus(Vector<Complex32> divisor, Vector<Complex32> result)
{
throw new NotSupportedException();
}
/// <summary>
/// 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.
/// </summary>
/// <param name="divisor">The pointwise denominator vector to use.</param>
/// <param name="result">The result of the modulus.</param>
protected override sealed void DoPointwiseRemainder(Vector<Complex32> divisor, Vector<Complex32> result)
{
throw new NotSupportedException();
}
@ -226,21 +238,45 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
}
/// <summary>
/// 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.
/// </summary>
/// <param name="divisor">The scalar denominator to use.</param>
/// <param name="result">A vector to store the results in.</param>
protected override sealed void DoModulus(Complex32 divisor, Vector<Complex32> result)
{
throw new NotSupportedException();
}
/// <summary>
/// Computes the canonical modulus, where the result has the sign of the divisor,
/// for the given dividend for each element of the vector.
/// </summary>
/// <param name="dividend">The scalar numerator to use.</param>
/// <param name="result">A vector to store the results in.</param>
protected override sealed void DoModulusByThis(Complex32 dividend, Vector<Complex32> result)
{
throw new NotSupportedException();
}
/// <summary>
/// Computes the remainder (% operator), where the result has the sign of the dividend,
/// for each element of the vector for the given divisor.
/// </summary>
/// <param name="divisor">The scalar denominator to use.</param>
/// <param name="result">A vector to store the results in.</param>
protected override void DoModulus(Complex32 divisor, Vector<Complex32> result)
protected override sealed void DoRemainder(Complex32 divisor, Vector<Complex32> result)
{
throw new NotSupportedException();
}
/// <summary>
/// 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.
/// </summary>
/// <param name="dividend">The scalar numerator to use.</param>
/// <param name="result">A vector to store the results in.</param>
protected override void DoModulusByThis(Complex32 dividend, Vector<Complex32> result)
protected override sealed void DoRemainderByThis(Complex32 dividend, Vector<Complex32> result)
{
throw new NotSupportedException();
}

46
src/Numerics/LinearAlgebra/Double/DenseVector.cs

@ -469,7 +469,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
/// <summary>
/// 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.
/// </summary>
/// <param name="divisor">The divisor to use.</param>
/// <param name="result">A vector to store the results in.</param>
@ -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);
}
});
}
}
/// <summary>
/// Computes the remainder (% operator), where the result has the sign of the dividend,
/// for each element of the vector for the given divisor.
/// </summary>
/// <param name="divisor">The divisor to use.</param>
/// <param name="result">A vector to store the results in.</param>
protected override void DoRemainder(double divisor, Vector<double> 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;
}
});
}
}
/// <summary>
/// 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.
/// </summary>
/// <param name="leftSide">The vector whose elements we want to compute the modulus of.</param>
/// <param name="leftSide">The vector whose elements we want to compute the remainder of.</param>
/// <param name="rightSide">The divisor to use,</param>
/// <returns>The result of the calculation</returns>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> is <see langword="null" />.</exception>
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);
}
/// <summary>

37
src/Numerics/LinearAlgebra/Double/SparseVector.cs

@ -460,11 +460,37 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
/// <summary>
/// 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.
/// </summary>
/// <param name="divisor">The divisor to use.</param>
/// <param name="divisor">The scalar denominator to use.</param>
/// <param name="result">A vector to store the results in.</param>
protected override void DoModulus(double divisor, Vector<double> 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));
}
}
}
/// <summary>
/// Computes the remainder (% operator), where the result has the sign of the dividend,
/// for each element of the vector for the given divisor.
/// </summary>
/// <param name="divisor">The scalar denominator to use.</param>
/// <param name="result">A vector to store the results in.</param>
protected override void DoRemainder(double divisor, Vector<double> 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
}
/// <summary>
/// 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.
/// </summary>
/// <param name="leftSide">The vector whose elements we want to compute the modulus of.</param>
/// <param name="rightSide">The divisor to use,</param>
@ -618,7 +645,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("leftSide");
}
return (SparseVector)leftSide.Modulus(rightSide);
return (SparseVector)leftSide.Remainder(rightSide);
}
/// <summary>

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

@ -41,7 +41,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public abstract class Vector : Vector<double>
{
/// <summary>
/// Initializes a new instance of the Vector class.
/// Initializes a new instance of the Vector class.
/// </summary>
protected Vector(VectorStorage<double> storage)
: base(storage)
@ -184,15 +184,30 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
/// <summary>
/// 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.
/// </summary>
/// <param name="divisor">The vector to pointwise modulus this one by.</param>
/// <param name="divisor">The pointwise denominator vector to use.</param>
/// <param name="result">The result of the modulus.</param>
protected override void DoPointwiseModulus(Vector<double> divisor, Vector<double> 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)));
}
}
/// <summary>
/// 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.
/// </summary>
/// <param name="divisor">The pointwise denominator vector to use.</param>
/// <param name="result">The result of the modulus.</param>
protected override void DoPointwiseRemainder(Vector<double> divisor, Vector<double> 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
}
/// <summary>
/// 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.
/// </summary>
/// <param name="divisor">The scalar denominator to use.</param>
/// <param name="result">A vector to store the results in.</param>
@ -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));
}
}
/// <summary>
/// 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.
/// </summary>
/// <param name="dividend">The scalar numerator to use.</param>
/// <param name="result">A vector to store the results in.</param>
protected override void DoModulusByThis(double dividend, Vector<double> result)
{
for (var index = 0; index < Count; index++)
{
result.At(index, Euclid.Modulus(dividend, At(index)));
}
}
/// <summary>
/// Computes the remainder (% operator), where the result has the sign of the dividend,
/// for each element of the vector for the given divisor.
/// </summary>
/// <param name="divisor">The scalar denominator to use.</param>
/// <param name="result">A vector to store the results in.</param>
protected override void DoRemainder(double divisor, Vector<double> result)
{
for (int i = 0; i < Count; i++)
{
result.At(i, At(i)%divisor);
}
}
/// <summary>
/// Computes the remainder (% operator), where the result has the sign of the dividend,
/// for the given dividend for each element of the vector.
/// </summary>
/// <param name="dividend">The scalar numerator to use.</param>
/// <param name="result">A vector to store the results in.</param>
protected override void DoRemainderByThis(double dividend, Vector<double> result)
{
for (var index = 0; index < Count; index++)
{
@ -259,7 +304,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <summary>
/// Returns the index of the absolute minimum element.
/// </summary>
/// <returns>The index of absolute minimum element.</returns>
/// <returns>The index of absolute minimum element.</returns>
public override int AbsoluteMinimumIndex()
{
var index = 0;
@ -289,7 +334,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <summary>
/// Returns the index of the absolute maximum element.
/// </summary>
/// <returns>The index of absolute maximum element.</returns>
/// <returns>The index of absolute maximum element.</returns>
public override int AbsoluteMaximumIndex()
{
var index = 0;
@ -407,7 +452,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <summary>
/// Returns the index of the absolute maximum element.
/// </summary>
/// <returns>The index of absolute maximum element.</returns>
/// <returns>The index of absolute maximum element.</returns>
public override int MaximumIndex()
{
var index = 0;
@ -428,7 +473,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <summary>
/// Returns the index of the minimum element.
/// </summary>
/// <returns>The index of minimum element.</returns>
/// <returns>The index of minimum element.</returns>
public override int MinimumIndex()
{
var index = 0;

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

@ -458,9 +458,10 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
/// <summary>
/// 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.
/// </summary>
/// <param name="divisor">The divisor to use.</param>
/// <param name="divisor">The scalar denominator to use.</param>
/// <param name="result">A vector to store the results in.</param>
protected override void DoModulus(float divisor, Vector<float> 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);
}
});
}
}
/// <summary>
/// Computes the remainder (% operator), where the result has the sign of the dividend,
/// for each element of the vector for the given divisor.
/// </summary>
/// <param name="divisor">The scalar denominator to use.</param>
/// <param name="result">A vector to store the results in.</param>
protected override void DoRemainder(float divisor, Vector<float> 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;
}
});
}
}
/// <summary>
/// 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.
/// </summary>
/// <param name="leftSide">The vector whose elements we want to compute the modulus of.</param>
/// <param name="rightSide">The divisor to use,</param>
@ -495,7 +522,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
throw new ArgumentNullException("leftSide");
}
return (DenseVector)leftSide.Modulus(rightSide);
return (DenseVector)leftSide.Remainder(rightSide);
}
/// <summary>

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

@ -461,11 +461,37 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
/// <summary>
/// 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.
/// </summary>
/// <param name="divisor">The divisor to use.</param>
/// <param name="divisor">The scalar denominator to use.</param>
/// <param name="result">A vector to store the results in.</param>
protected override void DoModulus(float divisor, Vector<float> 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));
}
}
}
/// <summary>
/// Computes the remainder (% operator), where the result has the sign of the dividend,
/// for each element of the vector for the given divisor.
/// </summary>
/// <param name="divisor">The scalar denominator to use.</param>
/// <param name="result">A vector to store the results in.</param>
protected override void DoRemainder(float divisor, Vector<float> result)
{
if (ReferenceEquals(this, result))
{
@ -606,7 +632,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
/// <summary>
/// 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.
/// </summary>
/// <param name="leftSide">The vector whose elements we want to compute the modulus of.</param>
/// <param name="rightSide">The divisor to use,</param>
@ -619,7 +646,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
throw new ArgumentNullException("leftSide");
}
return (SparseVector)leftSide.Modulus(rightSide);
return (SparseVector)leftSide.Remainder(rightSide);
}
/// <summary>

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

@ -184,15 +184,30 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
/// <summary>
/// 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.
/// </summary>
/// <param name="divisor">The vector to pointwise modulus this one by.</param>
/// <param name="divisor">The pointwise denominator vector to use.</param>
/// <param name="result">The result of the modulus.</param>
protected override void DoPointwiseModulus(Vector<float> divisor, Vector<float> 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)));
}
}
/// <summary>
/// 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.
/// </summary>
/// <param name="divisor">The pointwise denominator vector to use.</param>
/// <param name="result">The result of the modulus.</param>
protected override void DoPointwiseRemainder(Vector<float> divisor, Vector<float> 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
}
/// <summary>
/// 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.
/// </summary>
/// <param name="divisor">The scalar denominator to use.</param>
/// <param name="result">A vector to store the results in.</param>
@ -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));
}
}
/// <summary>
/// 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.
/// </summary>
/// <param name="dividend">The scalar numerator to use.</param>
/// <param name="result">A vector to store the results in.</param>
protected override void DoModulusByThis(float dividend, Vector<float> result)
{
for (var index = 0; index < Count; index++)
{
result.At(index, Euclid.Modulus(dividend, At(index)));
}
}
/// <summary>
/// Computes the remainder (% operator), where the result has the sign of the dividend,
/// for each element of the vector for the given divisor.
/// </summary>
/// <param name="divisor">The scalar denominator to use.</param>
/// <param name="result">A vector to store the results in.</param>
protected override void DoRemainder(float divisor, Vector<float> result)
{
for (int i = 0; i < Count; i++)
{
result.At(i, At(i)%divisor);
}
}
/// <summary>
/// Computes the remainder (% operator), where the result has the sign of the dividend,
/// for the given dividend for each element of the vector.
/// </summary>
/// <param name="dividend">The scalar numerator to use.</param>
/// <param name="result">A vector to store the results in.</param>
protected override void DoRemainderByThis(float dividend, Vector<float> result)
{
for (var index = 0; index < Count; index++)
{

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

@ -132,19 +132,37 @@ namespace MathNet.Numerics.LinearAlgebra
protected abstract void DoDivideByThis(T dividend, Vector<T> result);
/// <summary>
/// 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.
/// </summary>
/// <param name="divisor">The scalar denominator to use.</param>
/// <param name="result">A vector to store the results in.</param>
protected abstract void DoModulus(T divisor, Vector<T> result);
/// <summary>
/// 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.
/// </summary>
/// <param name="dividend">The scalar numerator to use.</param>
/// <param name="result">A vector to store the results in.</param>
protected abstract void DoModulusByThis(T dividend, Vector<T> result);
/// <summary>
/// Computes the remainder (% operator), where the result has the sign of the dividend,
/// for each element of the vector for the given divisor.
/// </summary>
/// <param name="divisor">The scalar denominator to use.</param>
/// <param name="result">A vector to store the results in.</param>
protected abstract void DoRemainder(T divisor, Vector<T> result);
/// <summary>
/// Computes the remainder (% operator), where the result has the sign of the dividend,
/// for the given dividend for each element of the vector.
/// </summary>
/// <param name="dividend">The scalar numerator to use.</param>
/// <param name="result">A vector to store the results in.</param>
protected abstract void DoRemainderByThis(T dividend, Vector<T> result);
/// <summary>
/// Pointwise multiplies this vector with another vector and stores the result into the result vector.
/// </summary>
@ -160,12 +178,21 @@ namespace MathNet.Numerics.LinearAlgebra
protected abstract void DoPointwiseDivide(Vector<T> divisor, Vector<T> result);
/// <summary>
/// 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.
/// </summary>
/// <param name="divisor">The pointwise denominator vector to use.</param>
/// <param name="result">The result of the modulus.</param>
protected abstract void DoPointwiseModulus(Vector<T> divisor, Vector<T> result);
/// <summary>
/// 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.
/// </summary>
/// <param name="divisor">The pointwise denominator vector to use.</param>
/// <param name="result">The result of the modulus.</param>
protected abstract void DoPointwiseRemainder(Vector<T> divisor, Vector<T> result);
/// <summary>
/// Adds a scalar to each element of the vector.
/// </summary>
@ -540,7 +567,8 @@ namespace MathNet.Numerics.LinearAlgebra
}
/// <summary>
/// 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.
/// </summary>
/// <param name="divisor">The scalar denominator to use.</param>
/// <returns>A vector containing the result.</returns>
@ -552,7 +580,8 @@ namespace MathNet.Numerics.LinearAlgebra
}
/// <summary>
/// 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.
/// </summary>
/// <param name="divisor">The scalar denominator to use.</param>
/// <param name="result">A vector to store the results in.</param>
@ -567,7 +596,8 @@ namespace MathNet.Numerics.LinearAlgebra
}
/// <summary>
/// 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.
/// </summary>
/// <param name="dividend">The scalar numerator to use.</param>
/// <returns>A vector containing the result.</returns>
@ -579,7 +609,8 @@ namespace MathNet.Numerics.LinearAlgebra
}
/// <summary>
/// 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.
/// </summary>
/// <param name="dividend">The scalar numerator to use.</param>
/// <param name="result">A vector to store the results in.</param>
@ -593,6 +624,64 @@ namespace MathNet.Numerics.LinearAlgebra
DoModulusByThis(dividend, result);
}
/// <summary>
/// Computes the remainder (vector % divisor), where the result has the sign of the dividend,
/// for each element of the vector for the given divisor.
/// </summary>
/// <param name="divisor">The scalar denominator to use.</param>
/// <returns>A vector containing the result.</returns>
public Vector<T> Remainder(T divisor)
{
var result = Build.SameAs(this);
DoRemainder(divisor, result);
return result;
}
/// <summary>
/// Computes the remainder (vector % divisor), where the result has the sign of the dividend,
/// for each element of the vector for the given divisor.
/// </summary>
/// <param name="divisor">The scalar denominator to use.</param>
/// <param name="result">A vector to store the results in.</param>
public void Remainder(T divisor, Vector<T> result)
{
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
DoRemainder(divisor, result);
}
/// <summary>
/// Computes the remainder (dividend % vector), where the result has the sign of the dividend,
/// for the given dividend for each element of the vector.
/// </summary>
/// <param name="dividend">The scalar numerator to use.</param>
/// <returns>A vector containing the result.</returns>
public Vector<T> RemainderByThis(T dividend)
{
var result = Build.SameAs(this);
DoRemainderByThis(dividend, result);
return result;
}
/// <summary>
/// Computes the remainder (dividend % vector), where the result has the sign of the dividend,
/// for the given dividend for each element of the vector.
/// </summary>
/// <param name="dividend">The scalar numerator to use.</param>
/// <param name="result">A vector to store the results in.</param>
public void RemainderByThis(T dividend, Vector<T> result)
{
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
DoRemainderByThis(dividend, result);
}
/// <summary>
/// Pointwise multiplies this vector with another vector.
/// </summary>
@ -674,7 +763,8 @@ namespace MathNet.Numerics.LinearAlgebra
}
/// <summary>
/// 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.
/// </summary>
/// <param name="divisor">The pointwise denominator vector to use.</param>
/// <returns>A new vector which is the pointwise modulus of the two vectors.</returns>
@ -692,7 +782,8 @@ namespace MathNet.Numerics.LinearAlgebra
}
/// <summary>
/// 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.
/// </summary>
/// <param name="divisor">The pointwise denominator vector to use.</param>
/// <param name="result">The vector to store the result of the pointwise modulus.</param>
@ -712,6 +803,47 @@ namespace MathNet.Numerics.LinearAlgebra
DoPointwiseModulus(divisor, result);
}
/// <summary>
/// Pointwise remainder (% operator), where the result has the sign of the dividend,
/// of this vector with another vector.
/// </summary>
/// <param name="divisor">The pointwise denominator vector to use.</param>
/// <returns>A new vector which is the pointwise remainder of the two vectors.</returns>
/// <exception cref="ArgumentException">If this vector and <paramref name="divisor"/> are not the same size.</exception>
public Vector<T> PointwiseRemainder(Vector<T> divisor)
{
if (Count != divisor.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "divisor");
}
var result = Build.SameAs(this, divisor);
DoPointwiseRemainder(divisor, result);
return result;
}
/// <summary>
/// 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.
/// </summary>
/// <param name="divisor">The pointwise denominator vector to use.</param>
/// <param name="result">The vector to store the result of the pointwise remainder.</param>
/// <exception cref="ArgumentException">If this vector and <paramref name="divisor"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public void PointwiseRemainder(Vector<T> divisor, Vector<T> result)
{
if (Count != divisor.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "divisor");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
DoPointwiseRemainder(divisor, result);
}
/// <summary>
/// Outer product of two vectors

26
src/Numerics/LinearAlgebra/Vector.Operators.cs

@ -207,40 +207,40 @@ namespace MathNet.Numerics.LinearAlgebra
}
/// <summary>
/// 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.
/// </summary>
/// <param name="dividend">The vector whose elements we want to compute the modulus of.</param>
/// <param name="dividend">The vector whose elements we want to compute the remainder of.</param>
/// <param name="divisor">The divisor to use.</param>
/// <returns>The result of the calculation</returns>
/// <exception cref="ArgumentNullException">If <paramref name="dividend"/> is <see langword="null" />.</exception>
public static Vector<T> operator %(Vector<T> dividend, T divisor)
{
return dividend.Modulus(divisor);
return dividend.Remainder(divisor);
}
/// <summary>
/// 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.
/// </summary>
/// <param name="dividend">The dividend we want to compute the modulus of.</param>
/// <param name="dividend">The dividend we want to compute the remainder of.</param>
/// <param name="divisor">The vector whose elements we want to use as divisor.</param>
/// <returns>The result of the calculation</returns>
/// <exception cref="ArgumentNullException">If <paramref name="dividend"/> is <see langword="null" />.</exception>
public static Vector<T> operator %(T dividend, Vector<T> divisor)
{
return divisor.ModulusByThis(dividend);
return divisor.RemainderByThis(dividend);
}
/// <summary>
/// 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.
/// </summary>
/// <param name="dividend">The vector whose elements we want to compute the modulus of.</param>
/// <param name="dividend">The vector whose elements we want to compute the remainder of.</param>
/// <param name="divisor">The divisor to use.</param>
/// <returns>The result of the calculation</returns>
/// <exception cref="ArgumentException">If <paramref name="dividend"/> and <paramref name="divisor"/> are not the same size.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="dividend"/> is <see langword="null" />.</exception>
public static Vector<T> operator %(Vector<T> dividend, Vector<T> divisor)
{
return dividend.PointwiseModulus(divisor);
return dividend.PointwiseRemainder(divisor);
}
[SpecialName]
@ -258,7 +258,7 @@ namespace MathNet.Numerics.LinearAlgebra
[SpecialName]
public static Vector<T> op_DotPercent(Vector<T> dividend, Vector<T> divisor)
{
return dividend.PointwiseModulus(divisor);
return dividend.PointwiseRemainder(divisor);
}
}
}

24
src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs

@ -791,13 +791,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
}
/// <summary>
/// Can compute the modules of each element of vector.
/// Can compute the remainder of each element of vector.
/// </summary>
[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
}
/// <summary>
/// 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.
/// </summary>
[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
}
/// <summary>
/// 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.
/// </summary>
[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
}
/// <summary>
/// Can compute the modules of each element of vector using the operator %.
/// Can compute the remainder of each element of vector using the operator %.
/// </summary>
[Test]
public void CanComputeModulusUsingOperator()
public void CanComputeRemainderUsingOperator()
{
var vector = CreateVector(Data);
var mod = vector % 4.5;

32
src/UnitTests/LinearAlgebraTests/Single/VectorTests.Arithmetic.cs

@ -791,61 +791,59 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
}
/// <summary>
/// Can compute the modules of each element of vector.
/// Can compute the remainder of each element of vector.
/// </summary>
[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);
}
}
/// <summary>
/// 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.
/// </summary>
[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);
}
}
/// <summary>
/// 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.
/// </summary>
[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);
}
}
/// <summary>
/// Can compute the modules of each element of vector using the operator %.
/// Can compute the remainder of each element of vector using the operator %.
/// </summary>
[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);
}
}
}

Loading…
Cancel
Save