Browse Source

added mod method and operator for vectors (float and double only

pull/36/head
Marcus Cuda 16 years ago
parent
commit
37daa5779f
  1. 10
      src/Numerics/LinearAlgebra/Complex/Vector.cs
  2. 10
      src/Numerics/LinearAlgebra/Complex32/Vector.cs
  3. 42
      src/Numerics/LinearAlgebra/Double/DenseVector.cs
  4. 41
      src/Numerics/LinearAlgebra/Double/SparseVector.cs
  5. 13
      src/Numerics/LinearAlgebra/Double/Vector.cs
  6. 56
      src/Numerics/LinearAlgebra/Generic/Vector.cs
  7. 41
      src/Numerics/LinearAlgebra/Single/DenseVector.cs
  8. 41
      src/Numerics/LinearAlgebra/Single/SparseVector.cs
  9. 13
      src/Numerics/LinearAlgebra/Single/Vector.cs
  10. 57
      src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs
  11. 57
      src/UnitTests/LinearAlgebraTests/Single/VectorTests.Arithmetic.cs

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

@ -195,6 +195,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return dot;
}
/// <summary>
/// Computes the modulus 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 DoModulus(Complex divisor, Vector<Complex> result)
{
throw new NotImplementedException();
}
/// <summary>
/// Returns the value of the absolute minimum element.
/// </summary>

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

@ -195,6 +195,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return dot;
}
/// <summary>
/// Computes the modulus 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 DoModulus(Complex32 divisor, Vector<Complex32> result)
{
throw new NotImplementedException();
}
/// <summary>
/// Returns the value of the absolute minimum element.
/// </summary>

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

@ -591,6 +591,48 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return (DenseVector)leftSide.Multiply(1.0 / rightSide);
}
/// <summary>
/// Computes the modulus 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 DoModulus(double divisor, Vector<double> result)
{
var denseResult = result as DenseVector;
if (denseResult == null)
{
for (var index = 0; index < Count; index++)
{
result.At(index, Data[index] % divisor);
}
}
else
{
for (var index = 0; index < Count; index++)
{
denseResult.Data[index] = Data[index] % divisor;
}
}
}
/// <summary>
/// Computes the modulus 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>
/// <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, float rightSide)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return (DenseVector)leftSide.Modulus(rightSide);
}
/// <summary>
/// Returns the index of the absolute minimum element.
/// </summary>

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

@ -697,6 +697,47 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return (SparseVector)leftSide.Multiply(1.0 / rightSide);
}
/// <summary>
/// Computes the modulus 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 DoModulus(double divisor, Vector<double> result)
{
if (ReferenceEquals(this, result))
{
for (var index = 0; index < NonZerosCount; index++)
{
_nonZeroValues[index] %= divisor;
}
}
else
{
result.Clear();
for (var index = 0; index < NonZerosCount; index++)
{
result.At(_nonZeroIndices[index], _nonZeroValues[index] % divisor);
}
}
}
/// <summary>
/// Computes the modulus 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>
/// <returns>The result of the calculation</returns>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> is <see langword="null" />.</exception>
public static SparseVector operator %(SparseVector leftSide, float rightSide)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return (SparseVector)leftSide.Modulus(rightSide);
}
/// <summary>
/// Returns the index of the absolute minimum element.
/// </summary>

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

@ -194,6 +194,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return dot;
}
/// <summary>
/// Computes the modulus 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 DoModulus(double divisor, Vector<double> result)
{
for (var index = 0; index < Count; index++)
{
result[index] = At(index) % divisor;
}
}
/// <summary>
/// Returns the value of the absolute minimum element.
/// </summary>

56
src/Numerics/LinearAlgebra/Generic/Vector.cs

@ -872,6 +872,45 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// <returns>The sum of the absolute value of the vector's elements.</returns>
public abstract T SumMagnitudes();
/// <summary>
/// Computes the modulus for each element of the vector for the given divisor.
/// </summary>
/// <param name="divisor">The divisor to use.</param>
/// <returns>A vector containing the result.</returns>
public virtual Vector<T> Modulus(T divisor)
{
var result = CreateVector(Count);
Modulus(divisor, result);
return result;
}
/// <summary>
/// Computes the modulus 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>
public virtual void Modulus(T divisor, Vector<T> result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
DoModulus(divisor, result);
}
/// <summary>
/// Computes the modulus 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 abstract void DoModulus(T divisor, Vector<T> result);
#endregion
#region Arithmetic Operator Overloading
@ -1044,6 +1083,23 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
return leftSide.Divide(rightSide);
}
/// <summary>
/// Computes the modulus 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>
/// <returns>The result of the calculation</returns>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> is <see langword="null" />.</exception>
public static Vector<T> operator %(Vector<T> leftSide, T rightSide)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return leftSide.Modulus(rightSide);
}
#endregion
#region Vector Norms

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

@ -591,6 +591,47 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return (DenseVector)leftSide.Multiply(1f / rightSide);
}
/// <summary>
/// Computes the modulus 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 DoModulus(float divisor, Vector<float> result)
{
var denseResult = result as DenseVector;
if (denseResult == null)
{
for (var index = 0; index < Count; index++)
{
result.At(index, Data[index] % divisor);
}
}
else
{
for (var index = 0; index < Count; index++)
{
denseResult.Data[index] = Data[index] % divisor;
}
}
}
/// <summary>
/// Computes the modulus 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>
/// <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, float rightSide)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return (DenseVector)leftSide.Modulus(rightSide);
}
/// <summary>
/// Returns the index of the absolute minimum element.
/// </summary>

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

@ -704,6 +704,47 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return (SparseVector)leftSide.Multiply(1.0f / rightSide);
}
/// <summary>
/// Computes the modulus 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 DoModulus(float divisor, Vector<float> result)
{
if (ReferenceEquals(this, result))
{
for (var index = 0; index < NonZerosCount; index++)
{
_nonZeroValues[index] %= divisor;
}
}
else
{
result.Clear();
for (var index = 0; index < NonZerosCount; index++)
{
result.At(_nonZeroIndices[index], _nonZeroValues[index] % divisor);
}
}
}
/// <summary>
/// Computes the modulus 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>
/// <returns>The result of the calculation</returns>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> is <see langword="null" />.</exception>
public static SparseVector operator %(SparseVector leftSide, float rightSide)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return (SparseVector)leftSide.Modulus(rightSide);
}
/// <summary>
/// Returns the index of the absolute minimum element.
/// </summary>

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

@ -195,6 +195,19 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return dot;
}
/// <summary>
/// Computes the modulus 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 DoModulus(float divisor, Vector<float> result)
{
for (var index = 0; index < Count; index++)
{
result.At(index, At(index) % divisor);
}
}
/// <summary>
/// Returns the value of the absolute minimum element.
/// </summary>

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

@ -1073,5 +1073,62 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
Vector<double> vector2 = null;
Assert.Throws<ArgumentNullException>(() => vector1.OuterProduct(vector2));
}
/// <summary>
/// Can compute the modules of each element of vector.
/// </summary>
[Test]
public void CanComputeModulus()
{
var vector = CreateVector(Data);
var mod = vector.Modulus(3.2f);
for (var index = 0; index < Data.Length; index++)
{
AssertHelpers.AlmostEqual(Data[index] % 3.2, mod[index], 7);
}
}
/// <summary>
/// Can compute the modules of each element of vector using a result vector.
/// </summary>
public void CanComputeModulusUsingResultVector()
{
var vector = CreateVector(Data);
var mod = CreateVector(vector.Count);
vector.Modulus(3.2f, mod);
for (var index = 0; index < Data.Length; index++)
{
AssertHelpers.AlmostEqual(Data[index] % 3.2, mod[index], 7);
}
}
/// <summary>
/// Can compute the modules of each element of vector using a result vector.
/// </summary>
public void CanComputeModulusUsingSameResultVector()
{
var vector = CreateVector(Data);
vector.Modulus(3.2f, vector);
for (var index = 0; index < Data.Length; index++)
{
AssertHelpers.AlmostEqual(Data[index] % 3.2, vector[index], 7);
}
}
/// <summary>
/// Can compute the modules of each element of vector using the operator %.
/// </summary>
[Test]
public void CanComputeModulusUsingOperator()
{
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], 7);
}
}
}
}

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

@ -1073,5 +1073,62 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single
Vector<float> vector2 = null;
Assert.Throws<ArgumentNullException>(() => vector1.OuterProduct(vector2));
}
/// <summary>
/// Can compute the modules of each element of vector.
/// </summary>
[Test]
public void CanComputeModulus()
{
var vector = CreateVector(Data);
var mod = vector.Modulus(3.2f);
for (var index = 0; index < Data.Length; index++)
{
AssertHelpers.AlmostEqual(Data[index] % 3.2, mod[index], 7);
}
}
/// <summary>
/// Can compute the modules of each element of vector using a result vector.
/// </summary>
public void CanComputeModulusUsingResultVector()
{
var vector = CreateVector(Data);
var mod = CreateVector(vector.Count);
vector.Modulus(3.2f, mod);
for (var index = 0; index < Data.Length; index++)
{
AssertHelpers.AlmostEqual(Data[index] % 3.2, mod[index], 7);
}
}
/// <summary>
/// Can compute the modules of each element of vector using a result vector.
/// </summary>
public void CanComputeModulusUsingSameResultVector()
{
var vector = CreateVector(Data);
vector.Modulus(3.2f, vector);
for (var index = 0; index < Data.Length; index++)
{
AssertHelpers.AlmostEqual(Data[index] % 3.2, vector[index], 7);
}
}
/// <summary>
/// Can compute the modules of each element of vector using the operator %.
/// </summary>
[Test]
public void CanComputeModulusUsingOperator()
{
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], 7);
}
}
}
}

Loading…
Cancel
Save