Browse Source

Added:

AbsoluteMinimum()
AbsoluteMinimumIndex()
SubVector()
la-knuth
Hani Medhat 17 years ago
committed by Marcus Cuda
parent
commit
85158f00d1
  1. 60
      src/Numerics/LinearAlgebra/Double/Vector.cs
  2. 36
      src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs

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

@ -608,6 +608,37 @@ namespace MathNet.Numerics.LinearAlgebra.Double
this.Count,
index => result[index] = this[index] * other[index]);
}
/// <summary>
/// Returns the value of the absolute minimum element.
/// </summary>
/// <returns>The value of the absolute minimum element.</returns>
public virtual double AbsoluteMinimum()
{
return Math.Abs(this[AbsoluteMinimumIndex()]);
}
/// <summary>
/// Returns the index of the absolute minimum element.
/// </summary>
/// <returns>The index of absolute minimum element.</returns>
public virtual int AbsoluteMinimumIndex()
{
int index = 0;
double min = System.Math.Abs(this[index]);
for (int i = 1; i < Count; i++)
{
double test = System.Math.Abs(this[i]);
if (test < min)
{
index = i;
min = test;
}
}
return index;
}
#endregion
#region Arithmetic Operator Overloading
@ -1076,6 +1107,35 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return matrix;
}
/// <summary>
/// Creates a vector containing specified elements.
/// </summary>
/// <param name="start">The first element to begin copying from.</param>
/// <param name="length">The number of elements to copy.</param>
/// <returns>A vector containing a copy of the specified elements.</returns>
/// <exception cref="ArgumentOutOfRangeException"><list><item>If <paramref name="start"/> is not positive or
/// greater than or equal to the size of the vector.</item>
/// <item>If <paramref name="start"/> + <paramref name="length"/> is greater than or equal to the size of the vector.</item>
/// </list></exception>
/// <exception cref="ArgumentException">If <paramref name="length"/> is not positive.</exception>
public virtual Vector SubVector(int start, int length)
{
if (start < 0 || start >= Count)
{
throw new ArgumentOutOfRangeException("start");
}
if (start + length > Count)
{
throw new ArgumentOutOfRangeException("start");
}
Vector result = CreateVector(length);
CommonParallel.For(start,
start + length, index => result[index-start] = this[index]);
return result;
}
#endregion
#region Implemented Interfaces

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

@ -782,5 +782,41 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
}
}
[Test]
public void CanFindAbsoluteMinimumIndex()
{
Vector source = CreateVector(_data);
int expected = 0;
int actual = source.AbsoluteMinimumIndex();
Assert.AreEqual(expected, actual);
}
[Test]
public void CanFindAbsoluteMinimum()
{
Vector source = CreateVector(_data);
double expected = 1;
double actual = source.AbsoluteMinimum();
Assert.AreEqual(expected, actual);
}
[Test]
[Row(0, 5)]
[Row(2, 2)]
[Row(1, 4)]
[Row(6, 10, ExpectedException = typeof(ArgumentOutOfRangeException))]
[Row(1, 10, ExpectedException = typeof(ArgumentOutOfRangeException))]
public void SubVector(int start, int length)
{
Vector vector = CreateVector(_data);
Vector sub = vector.SubVector(start, length);
Assert.AreEqual(length, sub.Count);
for (int i = 0; i < length; i++)
{
Assert.AreEqual(vector[i + start], sub[i]);
}
}
}
}
Loading…
Cancel
Save