Browse Source

merged andriy's changes

la-knuth
Marcus Cuda 16 years ago
parent
commit
9ba1fcc556
  1. 2046
      src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.cs
  2. 18
      src/Numerics/LinearAlgebra/Double/SparseVector.cs
  3. 89
      src/UnitTests/LinearAlgebraTests/Double/DenseVectorTests.cs
  4. 97
      src/UnitTests/LinearAlgebraTests/Double/SparseVectorTest.cs
  5. 390
      src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs
  6. 12
      src/UnitTests/LinearAlgebraTests/Double/VectorTests.Norm.cs
  7. 140
      src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs

2046
src/Numerics/Algorithms/LinearAlgebra/ManagedLinearAlgebraProvider.cs

File diff suppressed because it is too large

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

@ -768,12 +768,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double
NonZerosCount = NonZerosCount NonZerosCount = NonZerosCount
}; };
Buffer.BlockCopy(_nonZeroIndices, 0, result._nonZeroIndices, 0, NonZerosCount * Constants.SizeOfInt); if (NonZerosCount != 0)
{
CommonParallel.For( CommonParallel.For(
0, 0,
NonZerosCount, NonZerosCount,
index => result._nonZeroValues[index] = -_nonZeroValues[index]); index => result._nonZeroValues[index] = -_nonZeroValues[index]);
Buffer.BlockCopy(_nonZeroIndices, 0, result._nonZeroIndices, 0, NonZerosCount * Constants.SizeOfInt);
}
return result; return result;
} }
@ -921,7 +923,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{ {
if (NonZerosCount == 0) if (NonZerosCount == 0)
{ {
// No non-zero elements. Return 0 // No non-zero elements. Return 0
return 0; return 0;
} }
@ -1101,7 +1103,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
} }
var copy = new SparseVector(Count); var copy = new SparseVector(Count);
for (int i = 0; i < _nonZeroIndices.Length; i++) for (var i = 0; i < _nonZeroIndices.Length; i++)
{ {
var d = _nonZeroValues[i] * other[_nonZeroIndices[i]]; var d = _nonZeroValues[i] * other[_nonZeroIndices[i]];
if (d != 0.0) if (d != 0.0)

89
src/UnitTests/LinearAlgebraTests/Double/DenseVectorTests.cs

@ -53,8 +53,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanCreateDenseVectorFromArray() public void CanCreateDenseVectorFromArray()
{ {
var data = new double[this._data.Length]; var data = new double[Data.Length];
Array.Copy(this._data, data, this._data.Length); Array.Copy(Data, data, Data.Length);
var vector = new DenseVector(data); var vector = new DenseVector(data);
Assert.AreSame(data, vector.Data); Assert.AreSame(data, vector.Data);
@ -71,12 +71,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanCreateDenseVectorFromAnotherDenseVector() public void CanCreateDenseVectorFromAnotherDenseVector()
{ {
var vector = new DenseVector(this._data); var vector = new DenseVector(Data);
var other = new DenseVector(vector); var other = new DenseVector(vector);
Assert.AreNotSame(vector, other); Assert.AreNotSame(vector, other);
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(vector[i], other[i]); Assert.AreEqual(vector[i], other[i]);
} }
@ -86,11 +85,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanCreateDenseVectorFromAnotherVector() public void CanCreateDenseVectorFromAnotherVector()
{ {
var vector = (Vector)new DenseVector(this._data); var vector = (Vector)new DenseVector(Data);
var other = new DenseVector(vector); var other = new DenseVector(vector);
Assert.AreNotSame(vector, other); Assert.AreNotSame(vector, other);
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(vector[i], other[i]); Assert.AreEqual(vector[i], other[i]);
} }
@ -100,10 +99,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanCreateDenseVectorFromUserDefinedVector() public void CanCreateDenseVectorFromUserDefinedVector()
{ {
var vector = new UserDefinedVector(this._data); var vector = new UserDefinedVector(Data);
var other = new DenseVector(vector); var other = new DenseVector(vector);
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(vector[i], other[i]); Assert.AreEqual(vector[i], other[i]);
} }
@ -131,7 +130,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanConvertDenseVectorToArray() public void CanConvertDenseVectorToArray()
{ {
var vector = new DenseVector(this._data); var vector = new DenseVector(Data);
var array = (double[])vector; var array = (double[])vector;
Assert.IsInstanceOfType(typeof(double[]), array); Assert.IsInstanceOfType(typeof(double[]), array);
Assert.AreSame(vector.Data, array); Assert.AreSame(vector.Data, array);
@ -151,9 +150,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test] [Test]
public void CanCallUnaryPlusOperatorOnDenseVector() public void CanCallUnaryPlusOperatorOnDenseVector()
{ {
var vector = new DenseVector(this._data); var vector = new DenseVector(Data);
var other = +vector; var other = +vector;
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(vector[i], other[i]); Assert.AreEqual(vector[i], other[i]);
} }
@ -163,26 +162,26 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanAddTwoDenseVectorsUsingOperator() public void CanAddTwoDenseVectorsUsingOperator()
{ {
var vector = new DenseVector(this._data); var vector = new DenseVector(Data);
var other = new DenseVector(this._data); var other = new DenseVector(Data);
var result = vector + other; var result = vector + other;
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i], vector[i], "Making sure the original vector wasn't modified."); Assert.AreEqual(Data[i], vector[i], "Making sure the original vector wasn't modified.");
Assert.AreEqual(this._data[i], other[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(this._data[i] * 2.0, result[i]); Assert.AreEqual(Data[i] * 2.0, result[i]);
} }
} }
[Test] [Test]
public void CanCallUnaryNegationOperatorOnDenseVector() public void CanCallUnaryNegationOperatorOnDenseVector()
{ {
var vector = new DenseVector(this._data); var vector = new DenseVector(Data);
var other = -vector; var other = -vector;
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(-this._data[i], other[i]); Assert.AreEqual(-Data[i], other[i]);
} }
} }
@ -190,14 +189,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanSubtractTwoDenseVectorsUsingOperator() public void CanSubtractTwoDenseVectorsUsingOperator()
{ {
var vector = new DenseVector(this._data); var vector = new DenseVector(Data);
var other = new DenseVector(this._data); var other = new DenseVector(Data);
var result = vector - other; var result = vector - other;
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i], vector[i], "Making sure the original vector wasn't modified."); Assert.AreEqual(Data[i], vector[i], "Making sure the original vector wasn't modified.");
Assert.AreEqual(this._data[i], other[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(0.0, result[i]); Assert.AreEqual(0.0, result[i]);
} }
} }
@ -206,32 +205,32 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanMultiplyDenseVectorByScalarUsingOperators() public void CanMultiplyDenseVectorByScalarUsingOperators()
{ {
var vector = new DenseVector(this._data); var vector = new DenseVector(Data);
vector = vector * 2.0; vector = vector * 2.0;
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i] * 2.0, vector[i]); Assert.AreEqual(Data[i] * 2.0, vector[i]);
} }
vector = vector * 1.0; vector = vector * 1.0;
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i] * 2.0, vector[i]); Assert.AreEqual(Data[i] * 2.0, vector[i]);
} }
vector = new DenseVector(this._data); vector = new DenseVector(Data);
vector = 2.0 * vector; vector = 2.0 * vector;
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i] * 2.0, vector[i]); Assert.AreEqual(Data[i] * 2.0, vector[i]);
} }
vector = 1.0 * vector; vector = 1.0 * vector;
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i] * 2.0, vector[i]); Assert.AreEqual(Data[i] * 2.0, vector[i]);
} }
} }
@ -239,26 +238,26 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanDivideDenseVectorByScalarUsingOperators() public void CanDivideDenseVectorByScalarUsingOperators()
{ {
var vector = new DenseVector(this._data); var vector = new DenseVector(Data);
vector = vector / 2.0; vector = vector / 2.0;
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i] / 2.0, vector[i]); Assert.AreEqual(Data[i] / 2.0, vector[i]);
} }
vector = vector / 1.0; vector = vector / 1.0;
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i] / 2.0, vector[i]); Assert.AreEqual(Data[i] / 2.0, vector[i]);
} }
} }
[Test] [Test]
public void CanCalculateOuterProductForDenseVector() public void CanCalculateOuterProductForDenseVector()
{ {
var vector1 = this.CreateVector(this._data); var vector1 = CreateVector(Data);
var vector2 = this.CreateVector(this._data); var vector2 = CreateVector(Data);
Matrix m = Vector.OuterProduct(vector1, vector2); Matrix m = Vector.OuterProduct(vector1, vector2);
for (var i = 0; i < vector1.Count; i++) for (var i = 0; i < vector1.Count; i++)
{ {
@ -274,7 +273,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
public void OuterProducForDenseVectortWithFirstParameterNullShouldThrowException() public void OuterProducForDenseVectortWithFirstParameterNullShouldThrowException()
{ {
DenseVector vector1 = null; DenseVector vector1 = null;
var vector2 = this.CreateVector(this._data); var vector2 = CreateVector(Data);
Vector.OuterProduct(vector1, vector2); Vector.OuterProduct(vector1, vector2);
} }
@ -282,7 +281,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[ExpectedArgumentNullException] [ExpectedArgumentNullException]
public void OuterProductForDenseVectorWithSecondParameterNullShouldThrowException() public void OuterProductForDenseVectorWithSecondParameterNullShouldThrowException()
{ {
var vector1 = this.CreateVector(this._data); var vector1 = CreateVector(Data);
DenseVector vector2 = null; DenseVector vector2 = null;
Vector.OuterProduct(vector1, vector2); Vector.OuterProduct(vector1, vector2);
} }

97
src/UnitTests/LinearAlgebraTests/Double/SparseVectorTest.cs

@ -57,8 +57,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanCreateSparseVectorFromArray() public void CanCreateSparseVectorFromArray()
{ {
var data = new double[_data.Length]; var data = new double[Data.Length];
System.Array.Copy(_data, data, _data.Length); Array.Copy(Data, data, Data.Length);
var vector = new SparseVector(data); var vector = new SparseVector(data);
for (var i = 0; i < data.Length; i++) for (var i = 0; i < data.Length; i++)
@ -71,12 +71,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanCreateSparseVectorFromAnotherSparseVector() public void CanCreateSparseVectorFromAnotherSparseVector()
{ {
var vector = new SparseVector(_data); var vector = new SparseVector(Data);
var other = new SparseVector(vector); var other = new SparseVector(vector);
Assert.AreNotSame(vector, other); Assert.AreNotSame(vector, other);
for (var i = 0; i < _data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(vector[i], other[i]); Assert.AreEqual(vector[i], other[i]);
} }
@ -86,11 +85,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanCreateSparseVectorFromAnotherVector() public void CanCreateSparseVectorFromAnotherVector()
{ {
var vector = (Vector)new SparseVector(_data); var vector = (Vector)new SparseVector(Data);
var other = new SparseVector(vector); var other = new SparseVector(vector);
Assert.AreNotSame(vector, other); Assert.AreNotSame(vector, other);
for (var i = 0; i < _data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(vector[i], other[i]); Assert.AreEqual(vector[i], other[i]);
} }
@ -100,10 +99,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanCreateSparseVectorFromUserDefinedVector() public void CanCreateSparseVectorFromUserDefinedVector()
{ {
var vector = new UserDefinedVector(_data); var vector = new UserDefinedVector(Data);
var other = new SparseVector(vector); var other = new SparseVector(vector);
for (var i = 0; i < _data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(vector[i], other[i]); Assert.AreEqual(vector[i], other[i]);
} }
@ -131,7 +130,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanConvertSparseVectorToArray() public void CanConvertSparseVectorToArray()
{ {
var vector = new SparseVector(_data); var vector = new SparseVector(Data);
var array = vector.ToArray(); var array = vector.ToArray();
Assert.IsInstanceOfType(typeof(double[]), array); Assert.IsInstanceOfType(typeof(double[]), array);
Assert.AreElementsEqual(vector, array); Assert.AreElementsEqual(vector, array);
@ -150,9 +149,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test] [Test]
public void CanCallUnaryPlusOperatorOnSparseVector() public void CanCallUnaryPlusOperatorOnSparseVector()
{ {
var vector = new SparseVector(_data); var vector = new SparseVector(Data);
var other = +vector; var other = +vector;
for (var i = 0; i < _data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(vector[i], other[i]); Assert.AreEqual(vector[i], other[i]);
} }
@ -162,26 +161,26 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanAddTwoSparseVectorsUsingOperator() public void CanAddTwoSparseVectorsUsingOperator()
{ {
var vector = new SparseVector(_data); var vector = new SparseVector(Data);
var other = new SparseVector(_data); var other = new SparseVector(Data);
var result = vector + other; var result = vector + other;
for (var i = 0; i < _data.Length; i++) 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], 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], other[i], "Making sure the original vector wasn't modified.");
Assert.AreEqual(_data[i] * 2.0, result[i]); Assert.AreEqual(Data[i] * 2.0, result[i]);
} }
} }
[Test] [Test]
public void CanCallUnaryNegationOperatorOnSparseVector() public void CanCallUnaryNegationOperatorOnSparseVector()
{ {
var vector = new SparseVector(_data); var vector = new SparseVector(Data);
var other = -vector; var other = -vector;
for (var i = 0; i < _data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(-_data[i], other[i]); Assert.AreEqual(-Data[i], other[i]);
} }
} }
@ -189,14 +188,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanSubtractTwoSparseVectorsUsingOperator() public void CanSubtractTwoSparseVectorsUsingOperator()
{ {
var vector = new SparseVector(_data); var vector = new SparseVector(Data);
var other = new SparseVector(_data); var other = new SparseVector(Data);
var result = vector - other; var result = vector - other;
for (var i = 0; i < _data.Length; i++) 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], 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], other[i], "Making sure the original vector wasn't modified.");
Assert.AreEqual(0.0, result[i]); Assert.AreEqual(0.0, result[i]);
} }
} }
@ -205,32 +204,32 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanMultiplySparseVectorByScalarUsingOperators() public void CanMultiplySparseVectorByScalarUsingOperators()
{ {
var vector = new SparseVector(_data); var vector = new SparseVector(Data);
vector = vector * 2.0; vector = vector * 2.0;
for (var i = 0; i < _data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(_data[i] * 2.0, vector[i]); Assert.AreEqual(Data[i] * 2.0, vector[i]);
} }
vector = vector * 1.0; vector = vector * 1.0;
for (var i = 0; i < _data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(_data[i] * 2.0, vector[i]); Assert.AreEqual(Data[i] * 2.0, vector[i]);
} }
vector = new SparseVector(_data); vector = new SparseVector(Data);
vector = 2.0 * vector; vector = 2.0 * vector;
for (var i = 0; i < _data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(_data[i] * 2.0, vector[i]); Assert.AreEqual(Data[i] * 2.0, vector[i]);
} }
vector = 1.0 * vector; vector = 1.0 * vector;
for (var i = 0; i < _data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(_data[i] * 2.0, vector[i]); Assert.AreEqual(Data[i] * 2.0, vector[i]);
} }
} }
@ -238,26 +237,26 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanDivideSparseVectorByScalarUsingOperators() public void CanDivideSparseVectorByScalarUsingOperators()
{ {
var vector = new SparseVector(_data); var vector = new SparseVector(Data);
vector = vector / 2.0; vector = vector / 2.0;
for (var i = 0; i < _data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(_data[i] / 2.0, vector[i]); Assert.AreEqual(Data[i] / 2.0, vector[i]);
} }
vector = vector / 1.0; vector = vector / 1.0;
for (var i = 0; i < _data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(_data[i] / 2.0, vector[i]); Assert.AreEqual(Data[i] / 2.0, vector[i]);
} }
} }
[Test] [Test]
public void CanCalculateOuterProductForSparseVector() public void CanCalculateOuterProductForSparseVector()
{ {
var vector1 = this.CreateVector(this._data); var vector1 = CreateVector(Data);
var vector2 = this.CreateVector(this._data); var vector2 = CreateVector(Data);
Matrix m = Vector.OuterProduct(vector1, vector2); Matrix m = Vector.OuterProduct(vector1, vector2);
for (var i = 0; i < vector1.Count; i++) for (var i = 0; i < vector1.Count; i++)
{ {
@ -273,7 +272,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
public void OuterProducForSparseVectortWithFirstParameterNullShouldThrowException() public void OuterProducForSparseVectortWithFirstParameterNullShouldThrowException()
{ {
SparseVector vector1 = null; SparseVector vector1 = null;
var vector2 = this.CreateVector(this._data); var vector2 = CreateVector(Data);
Vector.OuterProduct(vector1, vector2); Vector.OuterProduct(vector1, vector2);
} }
@ -281,7 +280,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[ExpectedArgumentNullException] [ExpectedArgumentNullException]
public void OuterProductForSparseVectorWithSecondParameterNullShouldThrowException() public void OuterProductForSparseVectorWithSecondParameterNullShouldThrowException()
{ {
var vector1 = this.CreateVector(this._data); var vector1 = CreateVector(Data);
SparseVector vector2 = null; SparseVector vector2 = null;
Vector.OuterProduct(vector1, vector2); Vector.OuterProduct(vector1, vector2);
} }
@ -291,11 +290,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanCreateSparseVectorFromDenseVector() public void CanCreateSparseVectorFromDenseVector()
{ {
var vector = (Vector)new DenseVector(_data); var vector = (Vector)new DenseVector(Data);
var other = new SparseVector(vector); var other = new SparseVector(vector);
Assert.AreNotSame(vector, other); Assert.AreNotSame(vector, other);
for (var i = 0; i < _data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(vector[i], other[i]); Assert.AreEqual(vector[i], other[i]);
} }
@ -398,7 +397,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
public void PointwiseMultiplySparseVector() public void PointwiseMultiplySparseVector()
{ {
var zeroArray = new[] { 0.0, 1.0, 0.0, 1.0, 0.0 }; var zeroArray = new[] { 0.0, 1.0, 0.0, 1.0, 0.0 };
var vector1 = new SparseVector(this._data); var vector1 = new SparseVector(Data);
var vector2 = new SparseVector(zeroArray); var vector2 = new SparseVector(zeroArray);
var result = new SparseVector(vector1.Count); var result = new SparseVector(vector1.Count);
@ -406,7 +405,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
for (var i = 0; i < vector1.Count; i++) for (var i = 0; i < vector1.Count; i++)
{ {
Assert.AreEqual(this._data[i] * zeroArray[i], result[i]); Assert.AreEqual(Data[i] * zeroArray[i], result[i]);
} }
Assert.AreEqual(2, result.NonZerosCount); Assert.AreEqual(2, result.NonZerosCount);
} }

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

@ -35,10 +35,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test] [Test]
public void CanCallPlus() public void CanCallPlus()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
var other = vector.Plus(); var other = vector.Plus();
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(vector[i], other[i]); Assert.AreEqual(vector[i], other[i]);
} }
@ -55,10 +55,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test] [Test]
public void CanCallUnaryPlusOperator() public void CanCallUnaryPlusOperator()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
var other = +vector; var other = +vector;
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(vector[i], other[i]); Assert.AreEqual(vector[i], other[i]);
} }
@ -68,18 +68,18 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanAddScalarToVector() public void CanAddScalarToVector()
{ {
var copy = this.CreateVector(this._data); var copy = CreateVector(Data);
var vector = copy.Add(2.0); var vector = copy.Add(2.0);
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i] + 2.0, vector[i]); Assert.AreEqual(Data[i] + 2.0, vector[i]);
} }
vector.Add(0.0); vector.Add(0.0);
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i] + 2.0, vector[i]); Assert.AreEqual(Data[i] + 2.0, vector[i]);
} }
} }
@ -87,67 +87,67 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanAddScalarToVectorUsingResultVector() public void CanAddScalarToVectorUsingResultVector()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
var result = this.CreateVector(this._data.Length); var result = CreateVector(Data.Length);
vector.Add(2.0, result); vector.Add(2.0, result);
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i], vector[i], "Making sure the original vector wasn't modified."); Assert.AreEqual(Data[i], vector[i], "Making sure the original vector wasn't modified.");
Assert.AreEqual(this._data[i] + 2.0, result[i]); Assert.AreEqual(Data[i] + 2.0, result[i]);
} }
vector.Add(0.0, result); vector.Add(0.0, result);
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i], result[i]); Assert.AreEqual(Data[i], result[i]);
} }
} }
[Test] [Test]
public void ThrowsArgumentNullExceptionWhenAddingScalarWithNullResultVector() public void ThrowsArgumentNullExceptionWhenAddingScalarWithNullResultVector()
{ {
var vector = this.CreateVector(this._data.Length); var vector = CreateVector(Data.Length);
Assert.Throws<ArgumentNullException>(() => vector.Add(0.0, null)); Assert.Throws<ArgumentNullException>(() => vector.Add(0.0, null));
} }
[Test] [Test]
public void ThrowsArgumentExceptionWhenAddingScalarWithWrongSizeResultVector() public void ThrowsArgumentExceptionWhenAddingScalarWithWrongSizeResultVector()
{ {
var vector = this.CreateVector(this._data.Length); var vector = CreateVector(Data.Length);
var result = this.CreateVector(this._data.Length + 1); var result = CreateVector(Data.Length + 1);
Assert.Throws<ArgumentException>(() => vector.Add(0.0, result)); Assert.Throws<ArgumentException>(() => vector.Add(0.0, result));
} }
[Test] [Test]
public void ThrowsArgumentNullExceptionWhenAddingTwoVectorsAndOneIsNull() public void ThrowsArgumentNullExceptionWhenAddingTwoVectorsAndOneIsNull()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
Assert.Throws<ArgumentNullException>(() => vector.Add(null)); Assert.Throws<ArgumentNullException>(() => vector.Add(null));
} }
[Test] [Test]
public void ThrowsArgumentExceptionWhenAddingTwoVectorsOfDifferingSize() public void ThrowsArgumentExceptionWhenAddingTwoVectorsOfDifferingSize()
{ {
var vector = this.CreateVector(this._data.Length); var vector = CreateVector(Data.Length);
var other = this.CreateVector(this._data.Length + 1); var other = CreateVector(Data.Length + 1);
Assert.Throws<ArgumentException>(() => vector.Add(other)); Assert.Throws<ArgumentException>(() => vector.Add(other));
} }
[Test] [Test]
public void ThrowsArgumentNullExceptionWhenAddingTwoVectorsAndResultIsNull() public void ThrowsArgumentNullExceptionWhenAddingTwoVectorsAndResultIsNull()
{ {
var vector = this.CreateVector(this._data.Length); var vector = CreateVector(Data.Length);
var other = this.CreateVector(this._data.Length + 1); var other = CreateVector(Data.Length + 1);
Assert.Throws<ArgumentNullException>(() => vector.Add(other, null)); Assert.Throws<ArgumentNullException>(() => vector.Add(other, null));
} }
[Test] [Test]
public void ThrowsArgumentExceptionWhenAddingTwoVectorsAndResultIsDifferentSize() public void ThrowsArgumentExceptionWhenAddingTwoVectorsAndResultIsDifferentSize()
{ {
var vector = this.CreateVector(this._data.Length); var vector = CreateVector(Data.Length);
var other = this.CreateVector(this._data.Length); var other = CreateVector(Data.Length);
var result = this.CreateVector(this._data.Length + 1); var result = CreateVector(Data.Length + 1);
Assert.Throws<ArgumentException>(() => vector.Add(other, result)); Assert.Throws<ArgumentException>(() => vector.Add(other, result));
} }
@ -155,7 +155,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
public void AdditionOperatorThrowsArgumentNullExpectionIfAVectorIsNull() public void AdditionOperatorThrowsArgumentNullExpectionIfAVectorIsNull()
{ {
Vector a = null; Vector a = null;
var b = this.CreateVector(this._data.Length); var b = CreateVector(Data.Length);
Assert.Throws<ArgumentNullException>(() => a += b); Assert.Throws<ArgumentNullException>(() => a += b);
a = b; a = b;
@ -166,21 +166,21 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test] [Test]
public void AdditionOperatorThrowsArgumentExpectionIfVectorsAreDifferentSize() public void AdditionOperatorThrowsArgumentExpectionIfVectorsAreDifferentSize()
{ {
var a = this.CreateVector(this._data.Length); var a = CreateVector(Data.Length);
var b = this.CreateVector(this._data.Length + 1); var b = CreateVector(Data.Length + 1);
Assert.Throws<ArgumentException>(() => a += b); Assert.Throws<ArgumentException>(() => a += b);
} }
[Test] [Test]
public void CanAddTwoVectors() public void CanAddTwoVectors()
{ {
var copy = this.CreateVector(this._data); var copy = CreateVector(Data);
var other = this.CreateVector(this._data); var other = CreateVector(Data);
var vector = copy.Add(other); var vector = copy.Add(other);
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i] * 2.0, vector[i]); Assert.AreEqual(Data[i] * 2.0, vector[i]);
} }
} }
@ -188,16 +188,16 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanAddTwoVectorsUsingResultVector() public void CanAddTwoVectorsUsingResultVector()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
var other = this.CreateVector(this._data); var other = CreateVector(Data);
var result = this.CreateVector(this._data.Length); var result = CreateVector(Data.Length);
vector.Add(other, result); vector.Add(other, result);
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i], vector[i], "Making sure the original vector wasn't modified."); Assert.AreEqual(Data[i], vector[i], "Making sure the original vector wasn't modified.");
Assert.AreEqual(this._data[i], other[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(this._data[i] * 2.0, result[i]); Assert.AreEqual(Data[i] * 2.0, result[i]);
} }
} }
@ -205,27 +205,27 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanAddTwoVectorsUsingOperator() public void CanAddTwoVectorsUsingOperator()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
var other = this.CreateVector(this._data); var other = CreateVector(Data);
var result = vector + other; var result = vector + other;
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i], vector[i], "Making sure the original vector wasn't modified."); Assert.AreEqual(Data[i], vector[i], "Making sure the original vector wasn't modified.");
Assert.AreEqual(this._data[i], other[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(this._data[i] * 2.0, result[i]); Assert.AreEqual(Data[i] * 2.0, result[i]);
} }
} }
[Test] [Test]
public void CanAddVectorToItself() public void CanAddVectorToItself()
{ {
var copy = this.CreateVector(this._data); var copy = CreateVector(Data);
var vector = copy.Add(copy); var vector = copy.Add(copy);
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i] * 2.0, vector[i]); Assert.AreEqual(Data[i] * 2.0, vector[i]);
} }
} }
@ -233,14 +233,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanAddVectorToItselfUsingResultVector() public void CanAddVectorToItselfUsingResultVector()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
var result = this.CreateVector(this._data.Length); var result = CreateVector(Data.Length);
vector.Add(vector, result); vector.Add(vector, result);
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i], vector[i], "Making sure the original vector wasn't modified."); Assert.AreEqual(Data[i], vector[i], "Making sure the original vector wasn't modified.");
Assert.AreEqual(this._data[i] * 2.0, result[i]); Assert.AreEqual(Data[i] * 2.0, result[i]);
} }
} }
@ -248,25 +248,25 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanAddTwoVectorsUsingItselfAsResultVector() public void CanAddTwoVectorsUsingItselfAsResultVector()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
var other = this.CreateVector(this._data); var other = CreateVector(Data);
vector.Add(other, vector); vector.Add(other, vector);
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i], other[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(this._data[i] * 2.0, vector[i]); Assert.AreEqual(Data[i] * 2.0, vector[i]);
} }
} }
[Test] [Test]
public void CanCallNegate() public void CanCallNegate()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
var other = vector.Negate(); var other = vector.Negate();
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(-this._data[i], other[i]); Assert.AreEqual(-Data[i], other[i]);
} }
} }
@ -281,11 +281,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test] [Test]
public void CanCallUnaryNegationOperator() public void CanCallUnaryNegationOperator()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
var other = -vector; var other = -vector;
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(-this._data[i], other[i]); Assert.AreEqual(-Data[i], other[i]);
} }
} }
@ -293,18 +293,18 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanSubtractScalarFromVector() public void CanSubtractScalarFromVector()
{ {
var copy = this.CreateVector(this._data); var copy = CreateVector(Data);
var vector = copy.Subtract(2.0); var vector = copy.Subtract(2.0);
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i] - 2.0, vector[i]); Assert.AreEqual(Data[i] - 2.0, vector[i]);
} }
vector.Subtract(0.0); vector.Subtract(0.0);
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i] - 2.0, vector[i]); Assert.AreEqual(Data[i] - 2.0, vector[i]);
} }
} }
@ -312,67 +312,67 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanSubtractScalarFromVectorUsingResultVector() public void CanSubtractScalarFromVectorUsingResultVector()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
var result = this.CreateVector(this._data.Length); var result = CreateVector(Data.Length);
vector.Subtract(2.0, result); vector.Subtract(2.0, result);
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i], vector[i], "Making sure the original vector wasn't modified."); Assert.AreEqual(Data[i], vector[i], "Making sure the original vector wasn't modified.");
Assert.AreEqual(this._data[i] - 2.0, result[i]); Assert.AreEqual(Data[i] - 2.0, result[i]);
} }
vector.Subtract(0.0, result); vector.Subtract(0.0, result);
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i], result[i]); Assert.AreEqual(Data[i], result[i]);
} }
} }
[Test] [Test]
public void ThrowsArgumentNullExceptionWhenSubtractingScalarWithNullResultVector() public void ThrowsArgumentNullExceptionWhenSubtractingScalarWithNullResultVector()
{ {
var vector = this.CreateVector(this._data.Length); var vector = CreateVector(Data.Length);
Assert.Throws<ArgumentNullException>(() => vector.Subtract(0.0, null)); Assert.Throws<ArgumentNullException>(() => vector.Subtract(0.0, null));
} }
[Test] [Test]
public void ThrowsArgumentExceptionWhenSubtractingScalarWithWrongSizeResultVector() public void ThrowsArgumentExceptionWhenSubtractingScalarWithWrongSizeResultVector()
{ {
var vector = this.CreateVector(this._data.Length); var vector = CreateVector(Data.Length);
var result = this.CreateVector(this._data.Length + 1); var result = CreateVector(Data.Length + 1);
Assert.Throws<ArgumentException>(() => vector.Subtract(0.0, result)); Assert.Throws<ArgumentException>(() => vector.Subtract(0.0, result));
} }
[Test] [Test]
public void ThrowsArgumentNullExceptionWhenSubtractingTwoVectorsAndOneIsNull() public void ThrowsArgumentNullExceptionWhenSubtractingTwoVectorsAndOneIsNull()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
Assert.Throws<ArgumentNullException>(() => vector.Subtract(null)); Assert.Throws<ArgumentNullException>(() => vector.Subtract(null));
} }
[Test] [Test]
public void ThrowsArgumentExceptionWhenSubtractingTwoVectorsOfDifferingSize() public void ThrowsArgumentExceptionWhenSubtractingTwoVectorsOfDifferingSize()
{ {
var vector = this.CreateVector(this._data.Length); var vector = CreateVector(Data.Length);
var other = this.CreateVector(this._data.Length + 1); var other = CreateVector(Data.Length + 1);
Assert.Throws<ArgumentException>(() => vector.Subtract(other)); Assert.Throws<ArgumentException>(() => vector.Subtract(other));
} }
[Test] [Test]
public void ThrowsArgumentNullExceptionWhenSubtractingTwoVectorsAndResultIsNull() public void ThrowsArgumentNullExceptionWhenSubtractingTwoVectorsAndResultIsNull()
{ {
var vector = this.CreateVector(this._data.Length); var vector = CreateVector(Data.Length);
var other = this.CreateVector(this._data.Length + 1); var other = CreateVector(Data.Length + 1);
Assert.Throws<ArgumentNullException>(() => vector.Subtract(other, null)); Assert.Throws<ArgumentNullException>(() => vector.Subtract(other, null));
} }
[Test] [Test]
public void ThrowsArgumentExceptionWhenSubtractingTwoVectorsAndResultIsDifferentSize() public void ThrowsArgumentExceptionWhenSubtractingTwoVectorsAndResultIsDifferentSize()
{ {
var vector = this.CreateVector(this._data.Length); var vector = CreateVector(Data.Length);
var other = this.CreateVector(this._data.Length); var other = CreateVector(Data.Length);
var result = this.CreateVector(this._data.Length + 1); var result = CreateVector(Data.Length + 1);
Assert.Throws<ArgumentException>(() => vector.Subtract(other, result)); Assert.Throws<ArgumentException>(() => vector.Subtract(other, result));
} }
@ -380,7 +380,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
public void SubtractionOperatorThrowsArgumentNullExpectionIfAVectorIsNull() public void SubtractionOperatorThrowsArgumentNullExpectionIfAVectorIsNull()
{ {
Vector a = null; Vector a = null;
var b = this.CreateVector(this._data.Length); var b = CreateVector(Data.Length);
Assert.Throws<ArgumentNullException>(() => a -= b); Assert.Throws<ArgumentNullException>(() => a -= b);
a = b; a = b;
@ -391,19 +391,19 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test] [Test]
public void SubtractionOperatorThrowsArgumentExpectionIfVectorsAreDifferentSize() public void SubtractionOperatorThrowsArgumentExpectionIfVectorsAreDifferentSize()
{ {
var a = this.CreateVector(this._data.Length); var a = CreateVector(Data.Length);
var b = this.CreateVector(this._data.Length + 1); var b = CreateVector(Data.Length + 1);
Assert.Throws<ArgumentException>(() => a -= b); Assert.Throws<ArgumentException>(() => a -= b);
} }
[Test] [Test]
public void CanSubtractTwoVectors() public void CanSubtractTwoVectors()
{ {
var copy = this.CreateVector(this._data); var copy = CreateVector(Data);
var other = this.CreateVector(this._data); var other = CreateVector(Data);
var vector = copy.Subtract(other); var vector = copy.Subtract(other);
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(0.0, vector[i]); Assert.AreEqual(0.0, vector[i]);
} }
@ -413,15 +413,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanSubtractTwoVectorsUsingResultVector() public void CanSubtractTwoVectorsUsingResultVector()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
var other = this.CreateVector(this._data); var other = CreateVector(Data);
var result = this.CreateVector(this._data.Length); var result = CreateVector(Data.Length);
vector.Subtract(other, result); vector.Subtract(other, result);
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i], vector[i], "Making sure the original vector wasn't modified."); Assert.AreEqual(Data[i], vector[i], "Making sure the original vector wasn't modified.");
Assert.AreEqual(this._data[i], other[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(0.0, result[i]); Assert.AreEqual(0.0, result[i]);
} }
} }
@ -430,14 +430,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanSubtractTwoVectorsUsingOperator() public void CanSubtractTwoVectorsUsingOperator()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
var other = this.CreateVector(this._data); var other = CreateVector(Data);
var result = vector - other; var result = vector - other;
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i], vector[i], "Making sure the original vector wasn't modified."); Assert.AreEqual(Data[i], vector[i], "Making sure the original vector wasn't modified.");
Assert.AreEqual(this._data[i], other[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(0.0, result[i]); Assert.AreEqual(0.0, result[i]);
} }
} }
@ -445,10 +445,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test] [Test]
public void CanSubtractVectorFromItself() public void CanSubtractVectorFromItself()
{ {
var copy = this.CreateVector(this._data); var copy = CreateVector(Data);
var vector = copy.Subtract(copy); var vector = copy.Subtract(copy);
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(0.0, vector[i]); Assert.AreEqual(0.0, vector[i]);
} }
@ -458,13 +458,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanSubtractVectorFromItselfUsingResultVector() public void CanSubtractVectorFromItselfUsingResultVector()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
var result = this.CreateVector(this._data.Length); var result = CreateVector(Data.Length);
vector.Subtract(vector, result); vector.Subtract(vector, result);
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i], vector[i], "Making sure the original vector wasn't modified."); Assert.AreEqual(Data[i], vector[i], "Making sure the original vector wasn't modified.");
Assert.AreEqual(0.0, result[i]); Assert.AreEqual(0.0, result[i]);
} }
} }
@ -473,13 +473,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanSubtractTwoVectorsUsingItselfAsResultVector() public void CanSubtractTwoVectorsUsingItselfAsResultVector()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
var other = this.CreateVector(this._data); var other = CreateVector(Data);
vector.Subtract(other, vector); vector.Subtract(other, vector);
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i], other[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(0.0, vector[i]); Assert.AreEqual(0.0, vector[i]);
} }
} }
@ -488,18 +488,18 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanDivideVectorByScalar() public void CanDivideVectorByScalar()
{ {
var copy = this.CreateVector(this._data); var copy = CreateVector(Data);
var vector = copy.Divide(2.0); var vector = copy.Divide(2.0);
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i] / 2.0, vector[i]); Assert.AreEqual(Data[i] / 2.0, vector[i]);
} }
vector.Divide(1.0); vector.Divide(1.0);
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i] / 2.0, vector[i]); Assert.AreEqual(Data[i] / 2.0, vector[i]);
} }
} }
@ -507,20 +507,20 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanDivideVectorByScalarUsingResultVector() public void CanDivideVectorByScalarUsingResultVector()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
var result = this.CreateVector(this._data.Length); var result = CreateVector(Data.Length);
vector.Divide(2.0, result); vector.Divide(2.0, result);
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i], vector[i], "Making sure the original vector wasn't modified."); Assert.AreEqual(Data[i], vector[i], "Making sure the original vector wasn't modified.");
Assert.AreEqual(this._data[i] / 2.0, result[i]); Assert.AreEqual(Data[i] / 2.0, result[i]);
} }
vector.Divide(1.0, result); vector.Divide(1.0, result);
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i], result[i]); Assert.AreEqual(Data[i], result[i]);
} }
} }
@ -528,18 +528,18 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanMultiplyVectorByScalar() public void CanMultiplyVectorByScalar()
{ {
var copy = this.CreateVector(this._data); var copy = CreateVector(Data);
var vector = copy.Multiply(2.0); var vector = copy.Multiply(2.0);
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i] * 2.0, vector[i]); Assert.AreEqual(Data[i] * 2.0, vector[i]);
} }
vector.Multiply(1.0); vector.Multiply(1.0);
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i] * 2.0, vector[i]); Assert.AreEqual(Data[i] * 2.0, vector[i]);
} }
} }
@ -547,50 +547,50 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanMultiplyVectorByScalarUsingResultVector() public void CanMultiplyVectorByScalarUsingResultVector()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
var result = this.CreateVector(this._data.Length); var result = CreateVector(Data.Length);
vector.Multiply(2.0, result); vector.Multiply(2.0, result);
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i], vector[i], "Making sure the original vector wasn't modified."); Assert.AreEqual(Data[i], vector[i], "Making sure the original vector wasn't modified.");
Assert.AreEqual(this._data[i] * 2.0, result[i]); Assert.AreEqual(Data[i] * 2.0, result[i]);
} }
vector.Multiply(1.0, result); vector.Multiply(1.0, result);
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i], result[i]); Assert.AreEqual(Data[i], result[i]);
} }
} }
[Test] [Test]
public void ThrowsArgumentNullExceptionWhenMultiplyingScalarWithNullResultVector() public void ThrowsArgumentNullExceptionWhenMultiplyingScalarWithNullResultVector()
{ {
var vector = this.CreateVector(this._data.Length); var vector = CreateVector(Data.Length);
Assert.Throws<ArgumentNullException>(() => vector.Multiply(1.0, null)); Assert.Throws<ArgumentNullException>(() => vector.Multiply(1.0, null));
} }
[Test] [Test]
public void ThrowsArgumentNullExceptionWhenDividingScalarWithNullResultVector() public void ThrowsArgumentNullExceptionWhenDividingScalarWithNullResultVector()
{ {
var vector = this.CreateVector(this._data.Length); var vector = CreateVector(Data.Length);
Assert.Throws<ArgumentNullException>(() => vector.Divide(1.0, null)); Assert.Throws<ArgumentNullException>(() => vector.Divide(1.0, null));
} }
[Test] [Test]
public void ThrowsArgumentExceptionWhenMultiplyingScalarWithWrongSizeResultVector() public void ThrowsArgumentExceptionWhenMultiplyingScalarWithWrongSizeResultVector()
{ {
var vector = this.CreateVector(this._data.Length); var vector = CreateVector(Data.Length);
var result = this.CreateVector(this._data.Length + 1); var result = CreateVector(Data.Length + 1);
Assert.Throws<ArgumentException>(() => vector.Multiply(0.0, result)); Assert.Throws<ArgumentException>(() => vector.Multiply(0.0, result));
} }
[Test] [Test]
public void ThrowsArgumentExceptionWhenDividingScalarWithWrongSizeResultVector() public void ThrowsArgumentExceptionWhenDividingScalarWithWrongSizeResultVector()
{ {
var vector = this.CreateVector(this._data.Length); var vector = CreateVector(Data.Length);
var result = this.CreateVector(this._data.Length + 1); var result = CreateVector(Data.Length + 1);
Assert.Throws<ArgumentException>(() => vector.Divide(0.0, result)); Assert.Throws<ArgumentException>(() => vector.Divide(0.0, result));
} }
@ -598,32 +598,32 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanMultiplyVectorByScalarUsingOperators() public void CanMultiplyVectorByScalarUsingOperators()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
vector = vector * 2.0; vector = vector * 2.0;
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i] * 2.0, vector[i]); Assert.AreEqual(Data[i] * 2.0, vector[i]);
} }
vector = vector * 1.0; vector = vector * 1.0;
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i] * 2.0, vector[i]); Assert.AreEqual(Data[i] * 2.0, vector[i]);
} }
vector = this.CreateVector(this._data); vector = CreateVector(Data);
vector = 2.0 * vector; vector = 2.0 * vector;
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i] * 2.0, vector[i]); Assert.AreEqual(Data[i] * 2.0, vector[i]);
} }
vector = 1.0 * vector; vector = 1.0 * vector;
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i] * 2.0, vector[i]); Assert.AreEqual(Data[i] * 2.0, vector[i]);
} }
} }
@ -631,18 +631,18 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanDivideVectorByScalarUsingOperators() public void CanDivideVectorByScalarUsingOperators()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
vector = vector / 2.0; vector = vector / 2.0;
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i] / 2.0, vector[i]); Assert.AreEqual(Data[i] / 2.0, vector[i]);
} }
vector = vector / 1.0; vector = vector / 1.0;
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(this._data[i] / 2.0, vector[i]); Assert.AreEqual(Data[i] / 2.0, vector[i]);
} }
} }
@ -666,8 +666,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test] [Test]
public void CanDotProduct() public void CanDotProduct()
{ {
var dataA = this.CreateVector(this._data); var dataA = CreateVector(Data);
var dataB = this.CreateVector(this._data); var dataB = CreateVector(Data);
Assert.AreEqual(55.0, dataA.DotProduct(dataB)); Assert.AreEqual(55.0, dataA.DotProduct(dataB));
} }
@ -676,7 +676,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[ExpectedArgumentNullException] [ExpectedArgumentNullException]
public void DotProductThrowsExceptionWhenArgumentIsNull() public void DotProductThrowsExceptionWhenArgumentIsNull()
{ {
var dataA = this.CreateVector(this._data); var dataA = CreateVector(Data);
Vector dataB = null; Vector dataB = null;
dataA.DotProduct(dataB); dataA.DotProduct(dataB);
@ -686,8 +686,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[ExpectedArgumentException] [ExpectedArgumentException]
public void DotProductThrowsExceptionWhenArgumentHasDifferentSize() public void DotProductThrowsExceptionWhenArgumentHasDifferentSize()
{ {
var dataA = this.CreateVector(this._data); var dataA = CreateVector(Data);
var dataB = this.CreateVector(new double[] { 1, 2, 3, 4, 5, 6 }); var dataB = CreateVector(new double[] { 1, 2, 3, 4, 5, 6 });
dataA.DotProduct(dataB); dataA.DotProduct(dataB);
} }
@ -695,8 +695,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test] [Test]
public void CanDotProductUsingOperator() public void CanDotProductUsingOperator()
{ {
var dataA = this.CreateVector(this._data); var dataA = CreateVector(Data);
var dataB = this.CreateVector(this._data); var dataB = CreateVector(Data);
Assert.AreEqual(55.0, dataA * dataB); Assert.AreEqual(55.0, dataA * dataB);
} }
@ -705,7 +705,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[ExpectedArgumentNullException] [ExpectedArgumentNullException]
public void OperatorDotProductThrowsExceptionWhenLeftArgumentIsNull() public void OperatorDotProductThrowsExceptionWhenLeftArgumentIsNull()
{ {
var dataA = this.CreateVector(this._data); var dataA = CreateVector(Data);
Vector dataB = null; Vector dataB = null;
var d = dataA * dataB; var d = dataA * dataB;
@ -716,7 +716,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
public void OperatorDotProductThrowsExceptionWhenRightArgumentIsNull() public void OperatorDotProductThrowsExceptionWhenRightArgumentIsNull()
{ {
Vector dataA = null; Vector dataA = null;
var dataB = this.CreateVector(this._data); var dataB = CreateVector(Data);
var d = dataA * dataB; var d = dataA * dataB;
} }
@ -725,8 +725,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[ExpectedArgumentException] [ExpectedArgumentException]
public void OperatorDotProductThrowsExceptionWhenArgumentHasDifferentSize() public void OperatorDotProductThrowsExceptionWhenArgumentHasDifferentSize()
{ {
var dataA = this.CreateVector(this._data); var dataA = CreateVector(Data);
var dataB = this.CreateVector(new double[] { 1, 2, 3, 4, 5, 6 }); var dataB = CreateVector(new double[] { 1, 2, 3, 4, 5, 6 });
var d = dataA * dataB; var d = dataA * dataB;
} }
@ -734,25 +734,25 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test] [Test]
public void PointwiseMultiply() public void PointwiseMultiply()
{ {
var vector1 = this.CreateVector(this._data); var vector1 = CreateVector(Data);
var vector2 = vector1.Clone(); var vector2 = vector1.Clone();
var result = vector1.PointwiseMultiply(vector2); var result = vector1.PointwiseMultiply(vector2);
for (var i = 0; i < vector1.Count; i++) for (var i = 0; i < vector1.Count; i++)
{ {
Assert.AreEqual(this._data[i] * this._data[i], result[i]); Assert.AreEqual(Data[i] * Data[i], result[i]);
} }
} }
[Test] [Test]
public void PointwiseMultiplyUsingResultVector() public void PointwiseMultiplyUsingResultVector()
{ {
var vector1 = this.CreateVector(this._data); var vector1 = CreateVector(Data);
var vector2 = vector1.Clone(); var vector2 = vector1.Clone();
var result = CreateVector(vector1.Count); var result = CreateVector(vector1.Count);
vector1.PointwiseMultiply(vector2, result); vector1.PointwiseMultiply(vector2, result);
for (var i = 0; i < vector1.Count; i++) for (var i = 0; i < vector1.Count; i++)
{ {
Assert.AreEqual(this._data[i] * this._data[i], result[i]); Assert.AreEqual(Data[i] * Data[i], result[i]);
} }
} }
@ -760,7 +760,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[ExpectedArgumentNullException] [ExpectedArgumentNullException]
public void PointwiseMultiplyWithOtherNullShouldThrowException() public void PointwiseMultiplyWithOtherNullShouldThrowException()
{ {
var vector1 = this.CreateVector(this._data); var vector1 = CreateVector(Data);
Vector vector2 = null; Vector vector2 = null;
var result = CreateVector(vector1.Count); var result = CreateVector(vector1.Count);
vector1.PointwiseMultiply(vector2, result); vector1.PointwiseMultiply(vector2, result);
@ -770,7 +770,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[ExpectedArgumentNullException] [ExpectedArgumentNullException]
public void PointwiseMultiplyWithResultNullShouldThrowException() public void PointwiseMultiplyWithResultNullShouldThrowException()
{ {
var vector1 = this.CreateVector(this._data); var vector1 = CreateVector(Data);
var vector2 = vector1.Clone(); var vector2 = vector1.Clone();
Vector result = null; Vector result = null;
vector1.PointwiseMultiply(vector2, result); vector1.PointwiseMultiply(vector2, result);
@ -780,34 +780,34 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[ExpectedArgumentException] [ExpectedArgumentException]
public void PointwiseMultiplyWithInvalidResultLengthShouldThrowException() public void PointwiseMultiplyWithInvalidResultLengthShouldThrowException()
{ {
var vector1 = this.CreateVector(this._data); var vector1 = CreateVector(Data);
var vector2 = vector1.Clone(); var vector2 = vector1.Clone();
var result = this.CreateVector(vector1.Count + 1); var result = CreateVector(vector1.Count + 1);
vector1.PointwiseMultiply(vector2, result); vector1.PointwiseMultiply(vector2, result);
} }
[Test] [Test]
public void PointWiseDivide() public void PointWiseDivide()
{ {
var vector1 = this.CreateVector(this._data); var vector1 = CreateVector(Data);
var vector2 = vector1.Clone(); var vector2 = vector1.Clone();
var result = vector1.PointwiseDivide(vector2); var result = vector1.PointwiseDivide(vector2);
for (var i = 0; i < vector1.Count; i++) for (var i = 0; i < vector1.Count; i++)
{ {
Assert.AreEqual(this._data[i] / this._data[i], result[i]); Assert.AreEqual(Data[i] / Data[i], result[i]);
} }
} }
[Test] [Test]
public void PointWiseDivideUsingResultVector() public void PointWiseDivideUsingResultVector()
{ {
var vector1 = this.CreateVector(this._data); var vector1 = CreateVector(Data);
var vector2 = vector1.Clone(); var vector2 = vector1.Clone();
var result = CreateVector(vector1.Count); var result = CreateVector(vector1.Count);
vector1.PointwiseDivide(vector2, result); vector1.PointwiseDivide(vector2, result);
for (var i = 0; i < vector1.Count; i++) for (var i = 0; i < vector1.Count; i++)
{ {
Assert.AreEqual(this._data[i] / this._data[i], result[i]); Assert.AreEqual(Data[i] / Data[i], result[i]);
} }
} }
@ -815,7 +815,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[ExpectedArgumentNullException] [ExpectedArgumentNullException]
public void PointwiseDivideWithOtherNullShouldThrowException() public void PointwiseDivideWithOtherNullShouldThrowException()
{ {
var vector1 = this.CreateVector(this._data); var vector1 = CreateVector(Data);
Vector vector2 = null; Vector vector2 = null;
var result = CreateVector(vector1.Count); var result = CreateVector(vector1.Count);
vector1.PointwiseDivide(vector2, result); vector1.PointwiseDivide(vector2, result);
@ -825,7 +825,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[ExpectedArgumentNullException] [ExpectedArgumentNullException]
public void PointwiseDivideWithResultNullShouldThrowException() public void PointwiseDivideWithResultNullShouldThrowException()
{ {
var vector1 = this.CreateVector(this._data); var vector1 = CreateVector(Data);
var vector2 = vector1.Clone(); var vector2 = vector1.Clone();
Vector result = null; Vector result = null;
vector1.PointwiseDivide(vector2, result); vector1.PointwiseDivide(vector2, result);
@ -835,17 +835,17 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[ExpectedArgumentException] [ExpectedArgumentException]
public void PointwiseDivideWithInvalidResultLengthShouldThrowException() public void PointwiseDivideWithInvalidResultLengthShouldThrowException()
{ {
var vector1 = this.CreateVector(this._data); var vector1 = CreateVector(Data);
var vector2 = vector1.Clone(); var vector2 = vector1.Clone();
var result = this.CreateVector(vector1.Count + 1); var result = CreateVector(vector1.Count + 1);
vector1.PointwiseDivide(vector2, result); vector1.PointwiseDivide(vector2, result);
} }
[Test] [Test]
public void CanCalculateOuterProduct() public void CanCalculateOuterProduct()
{ {
var vector1 = this.CreateVector(this._data); var vector1 = CreateVector(Data);
var vector2 = this.CreateVector(this._data); var vector2 = CreateVector(Data);
Matrix m = Vector.OuterProduct(vector1, vector2); Matrix m = Vector.OuterProduct(vector1, vector2);
for (var i = 0; i < vector1.Count; i++) for (var i = 0; i < vector1.Count; i++)
{ {
@ -861,7 +861,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
public void OuterProductWithFirstParameterNullShouldThrowException() public void OuterProductWithFirstParameterNullShouldThrowException()
{ {
Vector vector1 = null; Vector vector1 = null;
var vector2 = this.CreateVector(this._data); var vector2 = CreateVector(Data);
Vector.OuterProduct(vector1, vector2); Vector.OuterProduct(vector1, vector2);
} }
@ -869,7 +869,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[ExpectedArgumentNullException] [ExpectedArgumentNullException]
public void OutercProductWithSecondParameterNullShouldThrowException() public void OutercProductWithSecondParameterNullShouldThrowException()
{ {
var vector1 = this.CreateVector(this._data); var vector1 = CreateVector(Data);
Vector vector2 = null; Vector vector2 = null;
Vector.OuterProduct(vector1, vector2); Vector.OuterProduct(vector1, vector2);
} }
@ -877,8 +877,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test] [Test]
public void CanCalculateTensorMultiply() public void CanCalculateTensorMultiply()
{ {
var vector1 = this.CreateVector(this._data); var vector1 = CreateVector(Data);
var vector2 = this.CreateVector(this._data); var vector2 = CreateVector(Data);
var m = vector1.TensorMultiply(vector2); var m = vector1.TensorMultiply(vector2);
for (var i = 0; i < vector1.Count; i++) for (var i = 0; i < vector1.Count; i++)
{ {
@ -893,7 +893,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[ExpectedArgumentNullException] [ExpectedArgumentNullException]
public void TensorMultiplyWithNullParameterNullShouldThrowException() public void TensorMultiplyWithNullParameterNullShouldThrowException()
{ {
var vector1 = this.CreateVector(this._data); var vector1 = CreateVector(Data);
Vector vector2 = null; Vector vector2 = null;
vector1.TensorMultiply(vector2); vector1.TensorMultiply(vector2);
} }

12
src/UnitTests/LinearAlgebraTests/Double/VectorTests.Norm.cs

@ -38,21 +38,21 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test] [Test]
public void CanComputeNorm() public void CanComputeNorm()
{ {
var vector = CreateVector(_data); var vector = CreateVector(Data);
AssertHelpers.AlmostEqual(7.416198487095663, vector.Norm(2), 15); AssertHelpers.AlmostEqual(7.416198487095663, vector.Norm(2), 15);
} }
[Test] [Test]
public void CanComputeNorm1() public void CanComputeNorm1()
{ {
var vector = CreateVector(_data); var vector = CreateVector(Data);
AssertHelpers.AlmostEqual(15.0, vector.Norm(1), 15); AssertHelpers.AlmostEqual(15.0, vector.Norm(1), 15);
} }
[Test] [Test]
public void CanComputeSquareNorm() public void CanComputeSquareNorm()
{ {
var vector = CreateVector(_data); var vector = CreateVector(Data);
AssertHelpers.AlmostEqual(55.0, vector.Norm(2) * vector.Norm(2), 15); AssertHelpers.AlmostEqual(55.0, vector.Norm(2) * vector.Norm(2), 15);
} }
@ -63,14 +63,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Row(10, 5.0540557845353753)] [Row(10, 5.0540557845353753)]
public void CanComputeNormP(int p, double expected) public void CanComputeNormP(int p, double expected)
{ {
var vector = CreateVector(_data); var vector = CreateVector(Data);
AssertHelpers.AlmostEqual(expected, vector.Norm(p), 15); AssertHelpers.AlmostEqual(expected, vector.Norm(p), 15);
} }
[Test] [Test]
public void CanComputeNormInfinity() public void CanComputeNormInfinity()
{ {
var vector = CreateVector(_data); var vector = CreateVector(Data);
AssertHelpers.AlmostEqual(5.0, vector.Norm(Double.PositiveInfinity), 15); AssertHelpers.AlmostEqual(5.0, vector.Norm(Double.PositiveInfinity), 15);
} }
@ -78,7 +78,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanNormalizeVector() public void CanNormalizeVector()
{ {
var vector = CreateVector(_data); var vector = CreateVector(Data);
var result = vector.Normalize(2); var result = vector.Normalize(2);
AssertHelpers.AlmostEqual(0.134839972492648, result[0], 14); AssertHelpers.AlmostEqual(0.134839972492648, result[0], 14);
AssertHelpers.AlmostEqual(0.269679944985297, result[1], 14); AssertHelpers.AlmostEqual(0.269679944985297, result[1], 14);

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

@ -37,18 +37,18 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[TestFixture] [TestFixture]
public abstract partial class VectorTests public abstract partial class VectorTests
{ {
protected readonly double[] _data = { 1, 2, 3, 4, 5 }; protected readonly double[] Data = { 1, 2, 3, 4, 5 };
[Test] [Test]
[MultipleAsserts] [MultipleAsserts]
public void CanCloneVector() public void CanCloneVector()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
var clone = vector.Clone(); var clone = vector.Clone();
Assert.AreNotSame(vector, clone); Assert.AreNotSame(vector, clone);
Assert.AreEqual(vector.Count, clone.Count); Assert.AreEqual(vector.Count, clone.Count);
for (var index = 0; index < this._data.Length; index++) for (var index = 0; index < Data.Length; index++)
{ {
Assert.AreEqual(vector[index], clone[index]); Assert.AreEqual(vector[index], clone[index]);
} }
@ -58,12 +58,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanCloneVectorUsingICloneable() public void CanCloneVectorUsingICloneable()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
var clone = (Vector)((ICloneable)vector).Clone(); var clone = (Vector)((ICloneable)vector).Clone();
Assert.AreNotSame(vector, clone); Assert.AreNotSame(vector, clone);
Assert.AreEqual(vector.Count, clone.Count); Assert.AreEqual(vector.Count, clone.Count);
for (var index = 0; index < this._data.Length; index++) for (var index = 0; index < Data.Length; index++)
{ {
Assert.AreEqual(vector[index], clone[index]); Assert.AreEqual(vector[index], clone[index]);
} }
@ -72,7 +72,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test] [Test]
public void CanConvertVectorToString() public void CanConvertVectorToString()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
var str = vector.ToString(); var str = vector.ToString();
var sep = CultureInfo.CurrentCulture.TextInfo.ListSeparator; var sep = CultureInfo.CurrentCulture.TextInfo.ListSeparator;
Assert.AreEqual(string.Format("1{0}2{0}3{0}4{0}5", sep), str); Assert.AreEqual(string.Format("1{0}2{0}3{0}4{0}5", sep), str);
@ -82,8 +82,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanCopyPartialVectorToAnother() public void CanCopyPartialVectorToAnother()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
var other = this.CreateVector(this._data.Length); var other = CreateVector(Data.Length);
vector.CopyTo(other, 2, 2, 2); vector.CopyTo(other, 2, 2, 2);
@ -98,7 +98,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanCopyPartialVectorToSelf() public void CanCopyPartialVectorToSelf()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
vector.CopyTo(vector, 0, 2, 2); vector.CopyTo(vector, 0, 2, 2);
Assert.AreEqual(1.0, vector[0]); Assert.AreEqual(1.0, vector[0]);
@ -112,12 +112,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanCopyVectorToAnother() public void CanCopyVectorToAnother()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
var other = this.CreateVector(this._data.Length); var other = CreateVector(Data.Length);
vector.CopyTo(other); vector.CopyTo(other);
for (var index = 0; index < this._data.Length; index++) for (var index = 0; index < Data.Length; index++)
{ {
Assert.AreEqual(vector[index], other[index]); Assert.AreEqual(vector[index], other[index]);
} }
@ -132,7 +132,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test] [Test]
public void CanCreateVector() public void CanCreateVector()
{ {
var expected = this.CreateVector(5); var expected = CreateVector(5);
var actual = expected.CreateVector(5); var actual = expected.CreateVector(5);
Assert.AreEqual(expected.GetType(), actual.GetType(), "vectors are same type."); Assert.AreEqual(expected.GetType(), actual.GetType(), "vectors are same type.");
} }
@ -140,19 +140,19 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test] [Test]
public void CanEnumerateOverVector() public void CanEnumerateOverVector()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
Assert.AreElementsEqual(this._data, vector); Assert.AreElementsEqual(Data, vector);
} }
[Test] [Test]
[MultipleAsserts] [MultipleAsserts]
public void CanEnumerateOverVectorUsingIEnumerable() public void CanEnumerateOverVectorUsingIEnumerable()
{ {
var enumerable = (IEnumerable)this.CreateVector(this._data); var enumerable = (IEnumerable)CreateVector(Data);
var index = 0; var index = 0;
foreach (var element in enumerable) foreach (var element in enumerable)
{ {
Assert.AreEqual(this._data[index++], (double)element); Assert.AreEqual(Data[index++], (double)element);
} }
} }
@ -160,9 +160,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanEquateVectors() public void CanEquateVectors()
{ {
var vector1 = this.CreateVector(this._data); var vector1 = CreateVector(Data);
var vector2 = this.CreateVector(this._data); var vector2 = CreateVector(Data);
var vector3 = this.CreateVector(4); var vector3 = CreateVector(4);
Assert.IsTrue(vector1.Equals(vector1)); Assert.IsTrue(vector1.Equals(vector1));
Assert.IsTrue(vector1.Equals(vector2)); Assert.IsTrue(vector1.Equals(vector2));
Assert.IsFalse(vector1.Equals(vector3)); Assert.IsFalse(vector1.Equals(vector3));
@ -173,46 +173,46 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void ThrowsArgumentExceptionIfSizeIsNotPositive() public void ThrowsArgumentExceptionIfSizeIsNotPositive()
{ {
Assert.Throws<ArgumentException>(() => this.CreateVector(-1)); Assert.Throws<ArgumentException>(() => CreateVector(-1));
Assert.Throws<ArgumentException>(() => this.CreateVector(0)); Assert.Throws<ArgumentException>(() => CreateVector(0));
} }
[Test] [Test]
public void TestingForEqualityWithNonVectorReturnsFalse() public void TestingForEqualityWithNonVectorReturnsFalse()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
Assert.IsFalse(vector.Equals(2)); Assert.IsFalse(vector.Equals(2));
} }
[Test] [Test]
public void CanTestForEqualityUsingObjectEquals() public void CanTestForEqualityUsingObjectEquals()
{ {
var vector1 = this.CreateVector(this._data); var vector1 = CreateVector(Data);
var vector2 = this.CreateVector(this._data); var vector2 = CreateVector(Data);
Assert.IsTrue(vector1.Equals((object)vector2)); Assert.IsTrue(vector1.Equals((object)vector2));
} }
[Test] [Test]
public void VectorGetHashCode() public void VectorGetHashCode()
{ {
var vector = this.CreateVector(new double[] { 1, 2, 3, 4 }); var vector = CreateVector(new double[] { 1, 2, 3, 4 });
Assert.AreEqual(2145910784, vector.GetHashCode()); Assert.AreEqual(2145910784, vector.GetHashCode());
} }
[Test] [Test]
public void GetIndexedEnumerator() public void GetIndexedEnumerator()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
foreach (var pair in vector.GetIndexedEnumerator()) foreach (var pair in vector.GetIndexedEnumerator())
{ {
Assert.AreEqual(this._data[pair.Key], pair.Value); Assert.AreEqual(Data[pair.Key], pair.Value);
} }
} }
[Test] [Test]
public void CanConvertVectorToArray() public void CanConvertVectorToArray()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
var array = vector.ToArray(); var array = vector.ToArray();
Assert.AreElementsEqual(vector, array); Assert.AreElementsEqual(vector, array);
} }
@ -221,7 +221,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanConvertVectorToColumnMatrix() public void CanConvertVectorToColumnMatrix()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
var matrix = vector.ToColumnMatrix(); var matrix = vector.ToColumnMatrix();
Assert.AreEqual(vector.Count, matrix.RowCount); Assert.AreEqual(vector.Count, matrix.RowCount);
@ -237,7 +237,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[MultipleAsserts] [MultipleAsserts]
public void CanConvertVectorToRowMatrix() public void CanConvertVectorToRowMatrix()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
var matrix = vector.ToRowMatrix(); var matrix = vector.ToRowMatrix();
Assert.AreEqual(vector.Count, matrix.ColumnCount); Assert.AreEqual(vector.Count, matrix.ColumnCount);
@ -252,11 +252,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test] [Test]
public void CanSetValues() public void CanSetValues()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
vector.SetValues(this._data); vector.SetValues(Data);
for (var i = 0; i < this._data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
Assert.AreEqual(vector[i], this._data[i]); Assert.AreEqual(vector[i], Data[i]);
} }
} }
@ -269,7 +269,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Row(1, -10, ExpectedException = typeof(ArgumentOutOfRangeException))] [Row(1, -10, ExpectedException = typeof(ArgumentOutOfRangeException))]
public void CanCalculateSubVector(int index, int length) public void CanCalculateSubVector(int index, int length)
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
var sub = vector.SubVector(index, length); var sub = vector.SubVector(index, length);
Assert.AreEqual(length, sub.Count); Assert.AreEqual(length, sub.Count);
for (var i = 0; i < length; i++) for (var i = 0; i < length; i++)
@ -281,81 +281,81 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Test] [Test]
public void CanFindAbsoluteMinimumIndex() public void CanFindAbsoluteMinimumIndex()
{ {
var source = this.CreateVector(this._data); var source = CreateVector(Data);
var expected = 0; const int Expected = 0;
var actual = source.AbsoluteMinimumIndex(); var actual = source.AbsoluteMinimumIndex();
Assert.AreEqual(expected, actual); Assert.AreEqual(Expected, actual);
} }
[Test] [Test]
public void CanFindAbsoluteMinimum() public void CanFindAbsoluteMinimum()
{ {
var source = this.CreateVector(this._data); var source = CreateVector(Data);
double expected = 1; const double Expected = 1;
var actual = source.AbsoluteMinimum(); var actual = source.AbsoluteMinimum();
Assert.AreEqual(expected, actual); Assert.AreEqual(Expected, actual);
} }
[Test] [Test]
public void CanFindAbsoluteMaximumIndex() public void CanFindAbsoluteMaximumIndex()
{ {
var source = this.CreateVector(this._data); var source = CreateVector(Data);
var expected = 4; const int Expected = 4;
var actual = source.AbsoluteMaximumIndex(); var actual = source.AbsoluteMaximumIndex();
Assert.AreEqual(expected, actual); Assert.AreEqual(Expected, actual);
} }
[Test] [Test]
public void CanFindAbsoluteMaximum() public void CanFindAbsoluteMaximum()
{ {
var source = this.CreateVector(this._data); var source = CreateVector(Data);
double expected = 5; const double Expected = 5;
var actual = source.AbsoluteMaximum(); var actual = source.AbsoluteMaximum();
Assert.AreEqual(expected, actual); Assert.AreEqual(Expected, actual);
} }
[Test] [Test]
public void CanFindMaximumIndex() public void CanFindMaximumIndex()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
var expected = 4; const int Expected = 4;
var actual = vector.MaximumIndex(); var actual = vector.MaximumIndex();
Assert.AreEqual(expected, actual); Assert.AreEqual(Expected, actual);
} }
[Test] [Test]
public void CanFindMaximum() public void CanFindMaximum()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
double expected = 5; const double Expected = 5;
var actual = vector.Maximum(); var actual = vector.Maximum();
Assert.AreEqual(expected, actual); Assert.AreEqual(Expected, actual);
} }
[Test] [Test]
public void CanFindMinimumIndex() public void CanFindMinimumIndex()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
var expected = 0; const int Expected = 0;
var actual = vector.MinimumIndex(); var actual = vector.MinimumIndex();
Assert.AreEqual(expected, actual); Assert.AreEqual(Expected, actual);
} }
[Test] [Test]
public void CanFindMinimum() public void CanFindMinimum()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
double expected = 1; const double Expected = 1;
var actual = vector.Minimum(); var actual = vector.Minimum();
Assert.AreEqual(expected, actual); Assert.AreEqual(Expected, actual);
} }
[Test] [Test]
@ -367,7 +367,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[Row(1, -10, ExpectedException = typeof(ArgumentOutOfRangeException))] [Row(1, -10, ExpectedException = typeof(ArgumentOutOfRangeException))]
public void CanGetSubVector(int index, int length) public void CanGetSubVector(int index, int length)
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
var sub = vector.SubVector(index, length); var sub = vector.SubVector(index, length);
Assert.AreEqual(length, sub.Count); Assert.AreEqual(length, sub.Count);
for (var i = 0; i < length; i++) for (var i = 0; i < length; i++)
@ -382,8 +382,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
double[] testData = { -20, -10, 10, 20, 30, }; double[] testData = { -20, -10, 10, 20, 30, };
var vector = CreateVector(testData); var vector = CreateVector(testData);
var actual = vector.Sum(); var actual = vector.Sum();
double expected = 30; const double Expected = 30;
Assert.AreEqual(expected, actual); Assert.AreEqual(Expected, actual);
} }
[Test] [Test]
@ -392,15 +392,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
double[] testData = { -20, -10, 10, 20, 30, }; double[] testData = { -20, -10, 10, 20, 30, };
var vector = CreateVector(testData); var vector = CreateVector(testData);
var actual = vector.SumMagnitudes(); var actual = vector.SumMagnitudes();
double expected = 90; const double Expected = 90;
Assert.AreEqual(expected, actual); Assert.AreEqual(Expected, actual);
} }
[Test] [Test]
[ExpectedArgumentNullException] [ExpectedArgumentNullException]
public void CanSetValuesWithNullParameterShouldThrowException() public void CanSetValuesWithNullParameterShouldThrowException()
{ {
var vector = this.CreateVector(this._data); var vector = CreateVector(Data);
vector.SetValues(null); vector.SetValues(null);
} }
@ -408,8 +408,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[ExpectedArgumentException] [ExpectedArgumentException]
public void CanSetValuesWithNonEqualDataLengthShouldThrowException() public void CanSetValuesWithNonEqualDataLengthShouldThrowException()
{ {
var vector = this.CreateVector(this._data.Length + 2); var vector = CreateVector(Data.Length + 2);
vector.SetValues(this._data); vector.SetValues(Data);
} }
@ -417,8 +417,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
[ExpectedArgumentException] [ExpectedArgumentException]
public void RandomWithNumberOfElementsLessThanZeroShouldThrowException() public void RandomWithNumberOfElementsLessThanZeroShouldThrowException()
{ {
var vector = this.CreateVector(4); var vector = CreateVector(4);
vector = vector.Random(-2, new ContinuousUniform()); vector.Random(-2, new ContinuousUniform());
} }
[Test] [Test]

Loading…
Cancel
Save