Browse Source

Vector: Added addition methods

Signed-off-by: Marcus Cuda <marcus@cuda.net>
pull/2/head
Marcus Cuda 17 years ago
parent
commit
d51753d6c5
  1. 7
      src/Numerics/Algorithms/LinearAlgebra/ILinearAlgebra.cs
  2. 36
      src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebra.cs
  3. 2
      src/Numerics/Interpolation/Algorithms/AkimaSplineInterpolation.cs
  4. 4
      src/Numerics/Interpolation/Algorithms/BarycentricInterpolation.cs
  5. 2
      src/Numerics/Interpolation/Algorithms/BulirschStoerRationalInterpolation.cs
  6. 2
      src/Numerics/Interpolation/Algorithms/CubicHermiteSplineInterpolation.cs
  7. 2
      src/Numerics/Interpolation/Algorithms/CubicSplineInterpolation.cs
  8. 2
      src/Numerics/Interpolation/Algorithms/FloaterHormannRationalInterpolation.cs
  9. 2
      src/Numerics/Interpolation/Algorithms/LinearSplineInterpolation.cs
  10. 2
      src/Numerics/Interpolation/Algorithms/NevillePolynomialInterpolation.cs
  11. 164
      src/Numerics/LinearAlgebra/Double/DenseVector.cs
  12. 157
      src/Numerics/LinearAlgebra/Double/Vector.cs
  13. 4
      src/Numerics/Properties/Resources.Designer.cs
  14. 2
      src/Numerics/Properties/Resources.resx
  15. 60
      src/UnitTests/LinearAlgebraTests/Double/UserDefinedVectorTests.cs
  16. 300
      src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs
  17. 1
      src/UnitTests/UnitTests.csproj

7
src/Numerics/Algorithms/LinearAlgebra/ILinearAlgebra.cs

@ -21,6 +21,7 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
namespace MathNet.Numerics.Algorithms.LinearAlgebra
{
/// <summary>
@ -28,5 +29,11 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra
/// </summary>
public interface ILinearAlgebra
{
/// <summary>
/// Adds the two arrays together: <c>a += c</c>.
/// </summary>
/// <param name="a">One of the arrays to add.</param>
/// <param name="b">The other array to add.</param>
void AddArrays(double[] a, double[] b);
}
}

36
src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebra.cs

@ -21,6 +21,8 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using System;
using MathNet.Numerics.Threading;
namespace MathNet.Numerics.Algorithms.LinearAlgebra
{
@ -29,5 +31,37 @@ namespace MathNet.Numerics.Algorithms.LinearAlgebra
/// </summary>
internal class ManagedLinearAlgebra : ILinearAlgebra
{
#region ILinearAlgebra Members
/// <summary>
/// Adds the two arrays together: <c>a += c</c>.
/// </summary>
/// <param name="a">
/// One of the arrays to add.
/// </param>
/// <param name="b">
/// The other array to add.
/// </param>
public void AddArrays(double[] a, double[] b)
{
if (a == null)
{
throw new ArgumentNullException("a");
}
if (b == null)
{
throw new ArgumentNullException("b");
}
if (a.Length != b.Length)
{
throw new ArgumentException(Properties.Resources.ArgumentVectorsSameLength);
}
Parallel.For(0, a.Length, i => a[i] += b[i]);
}
#endregion
}
}
}

2
src/Numerics/Interpolation/Algorithms/AkimaSplineInterpolation.cs

@ -129,7 +129,7 @@ namespace MathNet.Numerics.Interpolation.Algorithms
if (samplePoints.Count != sampleValues.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLengths);
throw new ArgumentException(Resources.ArgumentVectorsSameLength);
}
int n = samplePoints.Count;

4
src/Numerics/Interpolation/Algorithms/BarycentricInterpolation.cs

@ -127,12 +127,12 @@ namespace MathNet.Numerics.Interpolation.Algorithms
if (samplePoints.Count != sampleValues.Count)
{
throw new ArgumentException(Properties.Resources.ArgumentVectorsSameLengths);
throw new ArgumentException(Properties.Resources.ArgumentVectorsSameLength);
}
if (samplePoints.Count != barycentricWeights.Count)
{
throw new ArgumentException(Properties.Resources.ArgumentVectorsSameLengths);
throw new ArgumentException(Properties.Resources.ArgumentVectorsSameLength);
}
_points = samplePoints;

2
src/Numerics/Interpolation/Algorithms/BulirschStoerRationalInterpolation.cs

@ -115,7 +115,7 @@ namespace MathNet.Numerics.Interpolation.Algorithms
if (samplePoints.Count != sampleValues.Count)
{
throw new ArgumentException(Properties.Resources.ArgumentVectorsSameLengths);
throw new ArgumentException(Properties.Resources.ArgumentVectorsSameLength);
}
_points = samplePoints;

2
src/Numerics/Interpolation/Algorithms/CubicHermiteSplineInterpolation.cs

@ -141,7 +141,7 @@ namespace MathNet.Numerics.Interpolation.Algorithms
if (samplePoints.Count != sampleValues.Count
|| samplePoints.Count != sampleDerivatives.Count)
{
throw new ArgumentException(Properties.Resources.ArgumentVectorsSameLengths);
throw new ArgumentException(Properties.Resources.ArgumentVectorsSameLength);
}
double[] coefficients = new double[4 * (samplePoints.Count - 1)];

2
src/Numerics/Interpolation/Algorithms/CubicSplineInterpolation.cs

@ -200,7 +200,7 @@ namespace MathNet.Numerics.Interpolation.Algorithms
if (samplePoints.Count != sampleValues.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLengths);
throw new ArgumentException(Resources.ArgumentVectorsSameLength);
}
int n = samplePoints.Count;

2
src/Numerics/Interpolation/Algorithms/FloaterHormannRationalInterpolation.cs

@ -182,7 +182,7 @@ namespace MathNet.Numerics.Interpolation.Algorithms
if (samplePoints.Count != sampleValues.Count)
{
throw new ArgumentException(Properties.Resources.ArgumentVectorsSameLengths);
throw new ArgumentException(Properties.Resources.ArgumentVectorsSameLength);
}
if (0 > order || samplePoints.Count <= order)

2
src/Numerics/Interpolation/Algorithms/LinearSplineInterpolation.cs

@ -128,7 +128,7 @@ namespace MathNet.Numerics.Interpolation.Algorithms
if (samplePoints.Count != sampleValues.Count)
{
throw new ArgumentException(Properties.Resources.ArgumentVectorsSameLengths);
throw new ArgumentException(Properties.Resources.ArgumentVectorsSameLength);
}
double[] coefficients = new double[4 * (samplePoints.Count - 1)];

2
src/Numerics/Interpolation/Algorithms/NevillePolynomialInterpolation.cs

@ -120,7 +120,7 @@ namespace MathNet.Numerics.Interpolation.Algorithms
if (samplePoints.Count != sampleValues.Count)
{
throw new ArgumentException(Properties.Resources.ArgumentVectorsSameLengths);
throw new ArgumentException(Properties.Resources.ArgumentVectorsSameLength);
}
_points = samplePoints;

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

@ -21,6 +21,10 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using MathNet.Numerics.Algorithms;
using MathNet.Numerics.Algorithms.LinearAlgebra;
using MathNet.Numerics.Threading;
namespace MathNet.Numerics.LinearAlgebra.Double
{
using System;
@ -32,6 +36,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </summary>
public class DenseVector : Vector
{
/// <summary>
/// The linear algebra provider.
/// </summary>
private readonly ILinearAlgebra _linearAlgebra = AlgorithmFactory.LinearAlgebra;
/// <summary>
/// Initializes a new instance of the <see cref="DenseVector"/> class with a given size.
/// </summary>
@ -176,7 +185,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (Count != target.Count)
{
throw new ArgumentException("target", Resources.ArgumentVectorsSameLengths);
throw new ArgumentException("target", Resources.ArgumentVectorsSameLength);
}
var otherVector = target as DenseVector;
@ -192,5 +201,158 @@ namespace MathNet.Numerics.LinearAlgebra.Double
Buffer.BlockCopy(Data, 0, otherVector.Data, 0, Data.Length * Constants.SizeOfDouble);
}
}
/// <summary>
/// Adds a scalar to each element of the vector.
/// </summary>
/// <param name="scalar">The scalar to add.</param>
public override void Add(double scalar)
{
if (scalar.AlmostZero())
{
return;
}
Parallel.For(0, Count, i => Data[i] += scalar);
}
/// <summary>
/// Adds a scalar to each element of the vector and stores the result in the result vector.
/// </summary>
/// <param name="scalar">The scalar to add.</param>
/// <param name="result">The vector to store the result of the addition.</param>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public override void Add(double scalar, Vector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != result.Count)
{
throw new ArgumentException("result", Resources.ArgumentVectorsSameLength);
}
CopyTo(result);
result.Add(scalar);
}
/// <summary>
/// Adds another vector to this vector.
/// </summary>
/// <param name="other">The vector to add to this one.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
public override void Add(Vector other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException("other", Resources.ArgumentVectorsSameLength);
}
var denseVector = other as DenseVector;
if (denseVector == null)
{
base.Add(other);
}
else
{
_linearAlgebra.AddArrays(Data, denseVector.Data);
}
}
/// <summary>
/// Adds another vector to this vector and stores the result into the result vector.
/// </summary>
/// <param name="other">The vector to add to this one.</param>
/// <param name="result">The vector to store the result of the addition.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public override void Add(Vector other, Vector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != other.Count)
{
throw new ArgumentException("other", Resources.ArgumentVectorsSameLength);
}
if (Count != result.Count)
{
throw new ArgumentException("result", Resources.ArgumentVectorsSameLength);
}
if (ReferenceEquals(this, result) || ReferenceEquals(other, result))
{
var tmp = result.CreateVector(result.Count);
Add(other, tmp);
tmp.CopyTo(result);
}
else
{
CopyTo(result);
result.Add(other);
}
}
/// <summary>
/// Returns a <strong>Vector</strong> containing the same values of rightSide.
/// </summary>
/// <remarks>This method is included for completeness.</remarks>
/// <param name="rightSide">The vector to get the values from.</param>
/// <returns>A vector containing a the same values as <paramref name="rightSide"/>.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static Vector operator +(DenseVector rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
return rightSide.Plus();
}
/// <summary>
/// Adds two <strong>Vectors</strong> together and returns the results.
/// </summary>
/// <param name="leftSide">One of the vectors to add.</param>
/// <param name="rightSide">The other vector to add.</param>
/// <returns>The result of the addition.</returns>
/// <exception cref="ArgumentException">If <paramref name="leftSide"/> and <paramref name="rightSide"/> are not the same size.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> or <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static Vector operator +(DenseVector leftSide, DenseVector rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
if (leftSide.Count != rightSide.Count)
{
throw new ArgumentException("rightSide", Resources.ArgumentVectorsSameLength);
}
var ret = leftSide.Clone();
ret.Add(rightSide);
return ret;
}
}
}

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

@ -21,6 +21,8 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using MathNet.Numerics.Threading;
namespace MathNet.Numerics.LinearAlgebra.Double
{
using System;
@ -112,7 +114,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (Count != target.Count)
{
throw new ArgumentException("target", Resources.ArgumentVectorsSameLengths);
throw new ArgumentException("target", Resources.ArgumentVectorsSameLength);
}
if (ReferenceEquals(this, target))
@ -286,7 +288,156 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </returns>
public override string ToString()
{
return this.ToString(null, null);
return ToString(null, null);
}
/// <summary>
/// Adds a scalar to each element of the vector.
/// </summary>
/// <param name="scalar">The scalar to add.</param>
public virtual void Add(double scalar)
{
if (scalar.AlmostZero())
{
return;
}
Parallel.For(0, Count, i => this[i] += scalar);
}
/// <summary>
/// Adds a scalar to each element of the vector and stores the result in the result vector.
/// </summary>
/// <param name="scalar">The scalar to add.</param>
/// <param name="result">The vector to store the result of the addition.</param>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public virtual void Add(double scalar, Vector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
CopyTo(result);
result.Add(scalar);
}
/// <summary>
/// Returns a clone of this vector.
/// </summary>
/// <returns>A clone of this vector.</returns>
/// <remarks>Added as an alternative to the unary addition operator.</remarks>
public virtual Vector Plus()
{
return this;
}
/// <summary>
/// Adds another vector to this vector.
/// </summary>
/// <param name="other">The vector to add to this one.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
public virtual void Add(Vector other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
Parallel.For(0, Count, i => this[i] += other[i]);
}
/// <summary>
/// Adds another vector to this vector and stores the result into the result vector.
/// </summary>
/// <param name="other">The vector to add to this one.</param>
/// <param name="result">The vector to store the result of the addition.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public virtual void Add(Vector other, Vector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
if (ReferenceEquals(this, result) || ReferenceEquals(other, result))
{
var tmp = result.CreateVector(result.Count);
Add(other, tmp);
tmp.CopyTo(result);
}
else
{
CopyTo(result);
result.Add(other);
}
}
/// <summary>
/// Returns a <strong>Vector</strong> containing the same values of rightSide.
/// </summary>
/// <remarks>This method is included for completeness.</remarks>
/// <param name="rightSide">The vector to get the values from.</param>
/// <returns>A vector containing a the same values as <paramref name="rightSide"/>.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static Vector operator +(Vector rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
return rightSide.Plus();
}
/// <summary>
/// Adds two <strong>Vectors</strong> together and returns the results.
/// </summary>
/// <param name="leftSide">One of the vectors to add.</param>
/// <param name="rightSide">The other vector to add.</param>
/// <returns>The result of the addition.</returns>
/// <exception cref="ArgumentException">If <paramref name="leftSide"/> and <paramref name="rightSide"/> are not the same size.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> or <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static Vector operator +(Vector leftSide, Vector rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
if (leftSide.Count != rightSide.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "rightSide");
}
var ret = leftSide.Clone();
ret.Add(rightSide);
return ret;
}
#region Implemented Interfaces
@ -301,7 +452,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </returns>
object ICloneable.Clone()
{
return this.Clone();
return Clone();
}
#endregion

4
src/Numerics/Properties/Resources.Designer.cs

@ -387,9 +387,9 @@ namespace MathNet.Numerics.Properties {
/// <summary>
/// Looks up a localized string similar to All vectors must have the same dimensionality..
/// </summary>
internal static string ArgumentVectorsSameLengths {
internal static string ArgumentVectorsSameLength {
get {
return ResourceManager.GetString("ArgumentVectorsSameLengths", resourceCulture);
return ResourceManager.GetString("ArgumentVectorsSameLength", resourceCulture);
}
}

2
src/Numerics/Properties/Resources.resx

@ -207,7 +207,7 @@
<data name="ArgumentVectorLengthsMultipleOf" xml:space="preserve">
<value>Array length must be a multiple of {0}.</value>
</data>
<data name="ArgumentVectorsSameLengths" xml:space="preserve">
<data name="ArgumentVectorsSameLength" xml:space="preserve">
<value>All vectors must have the same dimensionality.</value>
</data>
<data name="ArgumentVectorThreeDimensional" xml:space="preserve">

60
src/UnitTests/LinearAlgebraTests/Double/UserDefinedVectorTests.cs

@ -0,0 +1,60 @@
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
{
using System;
using System.Collections.Generic;
using MathNet.Numerics.LinearAlgebra.Double;
internal class UserDefinedVector : Vector
{
private readonly double[] _data;
public UserDefinedVector(int size)
: base(size)
{
_data = new double[size];
}
public override double this[int index]
{
get
{
return _data[index];
}
set
{
_data[index] = value;
}
}
public override Matrix CreateMatrix(int rows, int columns)
{
throw new NotImplementedException();
}
public override Vector CreateVector(int size)
{
return new UserDefinedVector(size);
}
}
public class UserDefinedVectorTests : VectorTests
{
protected override Vector CreateVector(int size)
{
return new UserDefinedVector(size);
}
protected override Vector CreateVector(IList<double> data)
{
var vector = new UserDefinedVector(data.Count);
for (var index = 0; index < data.Count; index++)
{
vector[index] = data[index];
}
return vector;
}
}
}

300
src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs

@ -1,40 +1,38 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="VectorTests.cs" company="">
// </copyright>
// <summary>
// vector tests.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Globalization;
using MathNet.Numerics.LinearAlgebra.Double;
using MbUnit.Framework;
namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
{
using System;
using System.Collections.Generic;
using System.Globalization;
using MathNet.Numerics.LinearAlgebra.Double;
using MbUnit.Framework;
/// <summary>
/// The vector tests.
/// </summary>
public abstract class VectorTests
{
/// <summary>
/// The test data.
/// </summary>
private readonly double[] _data = { 1, 2, 3, 4, 5 };
private readonly double[] _data = {1, 2, 3, 4, 5};
/// <summary>
/// can clone vector.
/// </summary>
[Test]
[MultipleAsserts]
public void CanCloneVector()
{
var vector = CreateVector(_data);
var clone = vector.Clone();
Assert.AreNotSame(vector, clone);
Assert.AreEqual(vector.Count, clone.Count);
for (var index = 0; index < _data.Length; index++)
{
Assert.AreEqual(vector[index], clone[index]);
}
}
[Test]
[MultipleAsserts]
public void CanCloneVectorUsingICloneable()
{
var vector = CreateVector(_data);
var clone = (Vector)((ICloneable)vector).Clone();
Assert.AreNotSame(vector, clone);
Assert.AreEqual(vector.Count, clone.Count);
for (var index = 0; index < _data.Length; index++)
{
@ -42,9 +40,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
}
}
/// <summary>
/// can convert vector to string.
/// </summary>
[Test]
public void CanConvertVectorToString()
{
@ -54,9 +49,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
Assert.AreEqual(string.Format("1{0}2{0}3{0}4{0}5", sep), str);
}
/// <summary>
/// can copy partial vector to another.
/// </summary>
[Test]
[MultipleAsserts]
public void CanCopyPartialVectorToAnother()
@ -73,9 +65,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
Assert.AreEqual(0.0, other[4]);
}
/// <summary>
/// can copy vector to another.
/// </summary>
[Test]
[MultipleAsserts]
public void CanCopyVectorToAnother()
@ -91,18 +80,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
}
}
/// <summary>
/// can create matrix.
/// </summary>
[Test]
[Ignore]
public void CanCreateMatrix()
{
}
/// <summary>
/// can create vector.
/// </summary>
[Test]
public void CanCreateVector()
{
@ -111,9 +94,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
Assert.AreEqual(expected.GetType(), actual.GetType(), "vectors are same type.");
}
/// <summary>
/// can enumerate over vector.
/// </summary>
[Test]
[MultipleAsserts]
public void CanEnumerateOverVector()
@ -128,9 +108,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
}
}
/// <summary>
/// can equate vectors.
/// </summary>
[Test]
[MultipleAsserts]
public void CanEquateVectors()
@ -144,9 +121,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
Assert.IsFalse(vector1.Equals(null));
}
/// <summary>
/// can get indexed enumerator.
/// </summary>
[Test]
[MultipleAsserts]
public void CanGetIndexedEnumerator()
@ -161,9 +135,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
}
}
/// <summary>
/// can get indexed enumerator over range.
/// </summary>
[Test]
[MultipleAsserts]
public void CanGetIndexedEnumeratorOverRange()
@ -178,16 +149,235 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
}
}
/// <summary>
/// throws argument exception if size is not positive.
/// </summary>
[Test]
[MultipleAsserts]
public void ThrowsArgumentExceptionIfSizeIsNotPositive()
{
Assert.Throws<ArgumentException>(() => CreateVector(-1));
Assert.Throws<ArgumentException>(() => CreateVector(0));
}
[Test]
public void CanCallPlus()
{
var vector = CreateVector(_data);
var other = vector.Plus();
Assert.AreSame(vector, other, "Should be the same vector");
}
[Test]
public void OperatorPlusThrowsArgumentNullExceptionWhenCallOnNullVector()
{
Vector vector = null;
Vector other = null;
Assert.Throws<ArgumentNullException>(() => other = +vector);
}
[Test]
public void CanCallUnaryPlusOperator()
{
var vector = CreateVector(_data);
var other = +vector;
Assert.AreSame(vector, other, "Should be the same vector");
}
[Test]
[MultipleAsserts]
public void CanAddScalarToVector()
{
var vector = CreateVector(_data);
vector.Add(2.0);
for( var i = 0; i < _data.Length; i++)
{
Assert.AreEqual(_data[i]+2.0, vector[i]);
}
vector.Add(0.0);
for (var i = 0; i < _data.Length; i++)
{
Assert.AreEqual(_data[i] + 2.0, vector[i]);
}
}
[Test]
[MultipleAsserts]
public void CanAddScalarToVectorUsingResultVector()
{
var vector = CreateVector(_data);
var result = CreateVector(_data.Length);
vector.Add(2.0, result);
for (var i = 0; i < _data.Length; i++)
{
Assert.AreEqual(_data[i], vector[i], "Making sure the original vector wasn't modified.");
Assert.AreEqual(_data[i] + 2.0, result[i]);
}
vector.Add(0.0, result);
for (var i = 0; i < _data.Length; i++)
{
Assert.AreEqual(_data[i], result[i]);
}
}
[Test]
public void ThrowsArgumentNullExceptionWhenAddingScalarWithNullResultVector()
{
var vector = CreateVector(_data.Length);
Assert.Throws<ArgumentNullException>(() => vector.Add(0.0, null));
}
[Test]
public void ThrowsArgumentExceptionWhenAddingScalarWithWrongSizeResultVector()
{
var vector = CreateVector(_data.Length);
var result = CreateVector(_data.Length + 1);
Assert.Throws<ArgumentException>(() => vector.Add(0.0, result));
}
[Test]
public void ThrowsArgumentNullExceptionWhenAddingTwoVectorsAndOneIsNull()
{
var vector = CreateVector(_data);
Assert.Throws<ArgumentNullException>(() => vector.Add(null));
}
[Test]
public void ThrowsArgumentExceptionWhenAddingTwoVectorsOfDifferingSize()
{
var vector = CreateVector(_data.Length);
var other = CreateVector(_data.Length +1);
Assert.Throws<ArgumentException>(() => vector.Add(other));
}
[Test]
public void ThrowsArgumentNullExceptionWhenAddingTwoVectorsAndResultIsNull()
{
var vector = CreateVector(_data.Length);
var other = CreateVector(_data.Length+1);
Assert.Throws<ArgumentNullException>(() => vector.Add(other,null));
}
[Test]
public void ThrowsArgumentExceptionWhenAddingTwoVectorsAndResultIsDifferentSize()
{
var vector = CreateVector(_data.Length);
var other = CreateVector(_data.Length);
var result = CreateVector(_data.Length + 1);
Assert.Throws<ArgumentException>(() => vector.Add(other, result));
}
[Test]
public void AdditionOperatorThrowsArgumentNullExpectionIfAVectorIsNull()
{
Vector a = null;
var b = CreateVector(_data.Length);
Assert.Throws<ArgumentNullException>(()=> a += b);
a = b;
b = null;
Assert.Throws<ArgumentNullException>(() => a += b);
}
[Test]
public void AdditionOperatorThrowsArgumentExpectionIfVectorsAreDifferentSize()
{
var a = CreateVector(_data.Length);
var b = CreateVector(_data.Length + 1);
Assert.Throws<ArgumentException>(() => a += b);
}
[Test]
public void CanAddTwoVectors()
{
var vector = CreateVector(_data);
var other = CreateVector(_data);
vector.Add(other);
for (var i = 0; i < _data.Length; i++)
{
Assert.AreEqual(_data[i] * 2.0, vector[i]);
}
}
[Test]
[MultipleAsserts]
public void CanAddTwoVectorsUsingResultVector()
{
var vector = CreateVector(_data);
var other = CreateVector(_data);
var result = CreateVector(_data.Length);
vector.Add(other, result);
for (var i = 0; i < _data.Length; i++)
{
Assert.AreEqual(_data[i], vector[i], "Making sure the original vector wasn't modified.");
Assert.AreEqual(_data[i], other[i], "Making sure the original vector wasn't modified.");
Assert.AreEqual(_data[i] * 2.0, result[i]);
}
}
[Test]
[MultipleAsserts]
public void CanAddTwoVectorsUsingOperator()
{
var vector = CreateVector(_data);
var other = CreateVector(_data);
var result = vector + other;
for (var i = 0; i < _data.Length; i++)
{
Assert.AreEqual(_data[i], vector[i], "Making sure the original vector wasn't modified.");
Assert.AreEqual(_data[i], other[i], "Making sure the original vector wasn't modified.");
Assert.AreEqual(_data[i] * 2.0, result[i]);
}
}
[Test]
public void CanAddVectorToItself()
{
var vector = CreateVector(_data);
vector.Add(vector);
for (var i = 0; i < _data.Length; i++)
{
Assert.AreEqual(_data[i] * 2.0, vector[i]);
}
}
[Test]
[MultipleAsserts]
public void CanAddVectorToItselfUsingResultVector()
{
var vector = CreateVector(_data);
var result = CreateVector(_data.Length);
vector.Add(vector, result);
for (var i = 0; i < _data.Length; i++)
{
Assert.AreEqual(_data[i], vector[i], "Making sure the original vector wasn't modified.");
Assert.AreEqual(_data[i] * 2.0, result[i]);
}
}
[Test]
[MultipleAsserts]
public void CanAddTwoVectorsUsingItselfAsResultVector()
{
var vector = CreateVector(_data);
var other = CreateVector(_data);
vector.Add(other, vector);
for (var i = 0; i < _data.Length; i++)
{
Assert.AreEqual(_data[i], other[i], "Making sure the original vector wasn't modified.");
Assert.AreEqual(_data[i] * 2.0, vector[i]);
}
}
protected abstract Vector CreateVector(int size);
protected abstract Vector CreateVector(IList<double> data);

1
src/UnitTests/UnitTests.csproj

@ -81,6 +81,7 @@
<Compile Include="InterpolationTests\InterpolationInfrastructureContract.cs" />
<Compile Include="InterpolationTests\InterpolationFunctionalTest.cs" />
<Compile Include="LinearAlgebraTests\Double\DenseVectorTests.cs" />
<Compile Include="LinearAlgebraTests\Double\UserDefinedVectorTests.cs" />
<Compile Include="LinearAlgebraTests\Double\VectorTests.cs" />
<Compile Include="NumberTheoryTests\GcdRelatedTest.cs" />
<Compile Include="NumberTheoryTests\IntegerTheoryTest.cs" />

Loading…
Cancel
Save