// // Math.NET Numerics, part of the Math.NET Project // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com // Copyright (c) 2009-2010 Math.NET // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without // restriction, including without limitation the rights to use, // copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following // conditions: // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. // namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double { using System; using LinearAlgebra.Generic; using NUnit.Framework; /// /// Abstract class with the common arithmetic set of vector tests. /// public abstract partial class VectorTests { /// /// Can call unary "+" operator. /// [Test] public void CanCallUnaryPlusOperator() { var vector = CreateVector(Data); var other = +vector; CollectionAssert.AreEqual(vector, other); } /// /// Can add a scalar to a vector. /// [Test] public void CanAddScalarToVector() { var copy = CreateVector(Data); var vector = copy.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]); } } /// /// Can add a scalar to a vector using result vector. /// [Test] public void CanAddScalarToVectorIntoResultVector() { var vector = CreateVector(Data); var result = CreateVector(Data.Length); vector.Add(2.0, result); CollectionAssert.AreEqual(Data, vector, "Making sure the original vector wasn't modified."); for (var i = 0; i < Data.Length; i++) { Assert.AreEqual(Data[i] + 2.0, result[i]); } vector.Add(0.0, result); CollectionAssert.AreEqual(Data, result); } /// /// Adding scalar to a vector using result vector with wrong size throws an exception. /// [Test] public void AddingScalarWithWrongSizeResultVectorThrowsArgumentException() { var vector = CreateVector(Data.Length); var result = CreateVector(Data.Length + 1); Assert.Throws(() => vector.Add(0.0, result)); } /// /// Adding two vectors of different size throws an exception. /// [Test] public void AddingTwoVectorsOfDifferentSizeThrowsArgumentException() { var vector = CreateVector(Data.Length); var other = CreateVector(Data.Length + 1); Assert.Throws(() => vector.Add(other)); } /// /// Adding two vectors when a result vector is different size throws an exception. /// [Test] public void AddingTwoVectorsAndResultIsDifferentSizeThrowsArgumentException() { var vector = CreateVector(Data.Length); var other = CreateVector(Data.Length); var result = CreateVector(Data.Length + 1); Assert.Throws(() => vector.Add(other, result)); } /// /// Addition operator throws ArgumentException if vectors are different size. /// [Test] public void AdditionOperatorIfVectorsAreDifferentSizeThrowsArgumentException() { var a = CreateVector(Data.Length); var b = CreateVector(Data.Length + 1); Assert.Throws(() => a += b); } /// /// Can add two vectors. /// [Test] public void CanAddTwoVectors() { var copy = CreateVector(Data); var other = CreateVector(Data); var vector = copy.Add(other); for (var i = 0; i < Data.Length; i++) { Assert.AreEqual(Data[i] * 2.0, vector[i]); } } /// /// Can add two vectors using a result vector. /// [Test] public void CanAddTwoVectorsIntoResultVector() { var vector = CreateVector(Data); var other = CreateVector(Data); var result = CreateVector(Data.Length); vector.Add(other, result); CollectionAssert.AreEqual(Data, vector, "Making sure the original vector wasn't modified."); CollectionAssert.AreEqual(Data, other, "Making sure the original vector wasn't modified."); for (var i = 0; i < Data.Length; i++) { Assert.AreEqual(Data[i] * 2.0, result[i]); } } /// /// Can add two vectors using "+" operator. /// [Test] public void CanAddTwoVectorsUsingOperator() { var vector = CreateVector(Data); var other = CreateVector(Data); var result = vector + other; CollectionAssert.AreEqual(Data, vector, "Making sure the original vector wasn't modified."); CollectionAssert.AreEqual(Data, other, "Making sure the original vector wasn't modified."); for (var i = 0; i < Data.Length; i++) { Assert.AreEqual(Data[i] * 2.0, result[i]); } } /// /// Can add a vector to itself. /// [Test] public void CanAddVectorToItself() { var copy = CreateVector(Data); var vector = copy.Add(copy); for (var i = 0; i < Data.Length; i++) { Assert.AreEqual(Data[i] * 2.0, vector[i]); } } /// /// Can add a vector to itself using a result vector. /// [Test] public void CanAddVectorToItselfIntoResultVector() { var vector = CreateVector(Data); var result = CreateVector(Data.Length); vector.Add(vector, result); CollectionAssert.AreEqual(Data, vector, "Making sure the original vector wasn't modified."); for (var i = 0; i < Data.Length; i++) { Assert.AreEqual(Data[i] * 2.0, result[i]); } } /// /// Can add a vector to itself as result vector. /// [Test] public void CanAddTwoVectorsUsingItselfAsResultVector() { var vector = CreateVector(Data); var other = CreateVector(Data); vector.Add(other, vector); CollectionAssert.AreEqual(Data, other, "Making sure the original vector wasn't modified."); for (var i = 0; i < Data.Length; i++) { Assert.AreEqual(Data[i] * 2.0, vector[i]); } } /// /// Can negate a vector. /// [Test] public void CanCallNegate() { var vector = CreateVector(Data); var other = vector.Negate(); for (var i = 0; i < Data.Length; i++) { Assert.AreEqual(-Data[i], other[i]); } } /// /// Can call unary negation operator. /// [Test] public void CanCallUnaryNegationOperator() { var vector = CreateVector(Data); var other = -vector; for (var i = 0; i < Data.Length; i++) { Assert.AreEqual(-Data[i], other[i]); } } /// /// Can subtract a scalar from a vector. /// [Test] public void CanSubtractScalarFromVector() { var copy = CreateVector(Data); var vector = copy.Subtract(2.0); for (var i = 0; i < Data.Length; i++) { Assert.AreEqual(Data[i] - 2.0, vector[i]); } vector.Subtract(0.0); for (var i = 0; i < Data.Length; i++) { Assert.AreEqual(Data[i] - 2.0, vector[i]); } } /// /// Can subtract a scalar from a vector using a result vector. /// [Test] public void CanSubtractScalarFromVectorIntoResultVector() { var vector = CreateVector(Data); var result = CreateVector(Data.Length); vector.Subtract(2.0, result); CollectionAssert.AreEqual(Data, vector, "Making sure the original vector wasn't modified."); 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.Subtract(0.0, result); for (var i = 0; i < Data.Length; i++) { Assert.AreEqual(Data[i], result[i]); } } /// /// Subtracting a scalar with wrong size result vector throws ArgumentException. /// [Test] public void SubtractingScalarWithWrongSizeResultVectorThrowsArgumentException() { var vector = CreateVector(Data.Length); var result = CreateVector(Data.Length + 1); Assert.Throws(() => vector.Subtract(0.0, result)); } /// /// Subtracting two vectors of differing size throws ArgumentException. /// [Test] public void SubtractingTwoVectorsOfDifferingSizeThrowsArgumentException() { var vector = CreateVector(Data.Length); var other = CreateVector(Data.Length + 1); Assert.Throws(() => vector.Subtract(other)); } /// /// Subtracting two vectors when a result vector is different size throws ArgumentException. /// [Test] public void SubtractingTwoVectorsAndResultIsDifferentSizeThrowsArgumentException() { var vector = CreateVector(Data.Length); var other = CreateVector(Data.Length); var result = CreateVector(Data.Length + 1); Assert.Throws(() => vector.Subtract(other, result)); } /// /// Subtraction operator throws ArgumentException if vectors are different size. /// [Test] public void SubtractionOperatorIfVectorsAreDifferentSizeThrowsArgumentException() { var a = CreateVector(Data.Length); var b = CreateVector(Data.Length + 1); Assert.Throws(() => a -= b); } /// /// Can subtract two vectors. /// [Test] public void CanSubtractTwoVectors() { var copy = CreateVector(Data); var other = CreateVector(Data); var vector = copy.Subtract(other); for (var i = 0; i < Data.Length; i++) { Assert.AreEqual(0.0, vector[i]); } } /// /// Can subtract two vectors using a result vector. /// [Test] public void CanSubtractTwoVectorsIntoResultVector() { var vector = CreateVector(Data); var other = CreateVector(Data); var result = CreateVector(Data.Length); vector.Subtract(other, result); CollectionAssert.AreEqual(Data, vector, "Making sure the original vector wasn't modified."); CollectionAssert.AreEqual(Data, other, "Making sure the original vector wasn't modified."); for (var i = 0; i < Data.Length; i++) { Assert.AreEqual(0.0, result[i]); } } /// /// Can subtract two vectors using "-" operator. /// [Test] public void CanSubtractTwoVectorsUsingOperator() { var vector = CreateVector(Data); var other = CreateVector(Data); var result = vector - other; CollectionAssert.AreEqual(Data, vector, "Making sure the original vector wasn't modified."); CollectionAssert.AreEqual(Data, other, "Making sure the original vector wasn't modified."); for (var i = 0; i < Data.Length; i++) { Assert.AreEqual(0.0, result[i]); } } /// /// Can subtract a vector from itself. /// [Test] public void CanSubtractVectorFromItself() { var copy = CreateVector(Data); var vector = copy.Subtract(copy); for (var i = 0; i < Data.Length; i++) { Assert.AreEqual(0.0, vector[i]); } } /// /// Can subtract a vector from itself using a result vector. /// [Test] public void CanSubtractVectorFromItselfIntoResultVector() { var vector = CreateVector(Data); var result = CreateVector(Data.Length); vector.Subtract(vector, result); CollectionAssert.AreEqual(Data, vector, "Making sure the original vector wasn't modified."); for (var i = 0; i < Data.Length; i++) { Assert.AreEqual(0.0, result[i]); } } /// /// Can subtract two vectors using itself as result vector. /// [Test] public void CanSubtractTwoVectorsUsingItselfAsResultVector() { var vector = CreateVector(Data); var other = CreateVector(Data); vector.Subtract(other, vector); CollectionAssert.AreEqual(Data, other, "Making sure the original vector wasn't modified."); for (var i = 0; i < Data.Length; i++) { Assert.AreEqual(0.0, vector[i]); } } /// /// Can divide a vector by a scalar. /// [Test] public void CanDivideVectorByScalar() { var copy = CreateVector(Data); var vector = copy.Divide(2.0); for (var i = 0; i < Data.Length; i++) { Assert.AreEqual(Data[i] / 2.0, vector[i]); } vector.Divide(1.0); for (var i = 0; i < Data.Length; i++) { Assert.AreEqual(Data[i] / 2.0, vector[i]); } } /// /// Can divide a vector by a scalar using a result vector. /// [Test] public void CanDivideVectorByScalarIntoResultVector() { var vector = CreateVector(Data); var result = CreateVector(Data.Length); vector.Divide(2.0, result); CollectionAssert.AreEqual(Data, vector, "Making sure the original vector wasn't modified."); for (var i = 0; i < Data.Length; i++) { Assert.AreEqual(Data[i] / 2.0, result[i]); } vector.Divide(1.0, result); for (var i = 0; i < Data.Length; i++) { Assert.AreEqual(Data[i], result[i]); } } /// /// Can multiply a vector by a scalar. /// [Test] public void CanMultiplyVectorByScalar() { var copy = CreateVector(Data); var vector = copy.Multiply(2.0); for (var i = 0; i < Data.Length; i++) { Assert.AreEqual(Data[i] * 2.0, vector[i]); } vector.Multiply(1.0); for (var i = 0; i < Data.Length; i++) { Assert.AreEqual(Data[i] * 2.0, vector[i]); } } /// /// Can multiply a vector by a scalar using a result vector. /// [Test] public void CanMultiplyVectorByScalarIntoResultVector() { var vector = CreateVector(Data); var result = CreateVector(Data.Length); vector.Multiply(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.Multiply(1.0, result); for (var i = 0; i < Data.Length; i++) { Assert.AreEqual(Data[i], result[i]); } } /// /// Multiplying by scalar with wrong result vector size throws ArgumentException. /// [Test] public void MultiplyingScalarWithWrongSizeResultVectorThrowsArgumentException() { var vector = CreateVector(Data.Length); var result = CreateVector(Data.Length + 1); Assert.Throws(() => vector.Multiply(0.0, result)); } /// /// Dividing by scalar with wrong result vector size throws ArgumentException. /// [Test] public void DividingScalarWithWrongSizeResultVectorThrowsArgumentException() { var vector = CreateVector(Data.Length); var result = CreateVector(Data.Length + 1); Assert.Throws(() => vector.Divide(0.0, result)); } /// /// Can multiply a vector by scalar using operators. /// [Test] public void CanMultiplyVectorByScalarUsingOperators() { var vector = CreateVector(Data); vector = vector * 2.0; for (var i = 0; i < Data.Length; i++) { Assert.AreEqual(Data[i] * 2.0, vector[i]); } vector = vector * 1.0; for (var i = 0; i < Data.Length; i++) { Assert.AreEqual(Data[i] * 2.0, vector[i]); } vector = CreateVector(Data); vector = 2.0 * vector; for (var i = 0; i < Data.Length; i++) { Assert.AreEqual(Data[i] * 2.0, vector[i]); } vector = 1.0 * vector; for (var i = 0; i < Data.Length; i++) { Assert.AreEqual(Data[i] * 2.0, vector[i]); } } /// /// Can divide a vector by scalar using operators. /// [Test] public void CanDivideVectorByScalarUsingOperators() { var vector = CreateVector(Data); vector = vector / 2.0; for (var i = 0; i < Data.Length; i++) { Assert.AreEqual(Data[i] / 2.0, vector[i]); } vector = vector / 1.0; for (var i = 0; i < Data.Length; i++) { Assert.AreEqual(Data[i] / 2.0, vector[i]); } } /// /// Can calculate the dot product /// [Test] public void CanDotProduct() { var dataA = CreateVector(Data); var dataB = CreateVector(Data); Assert.AreEqual(55.0, dataA.DotProduct(dataB)); } /// /// Dot product throws ArgumentException when an argument has different size /// [Test] public void DotProductWhenDifferentSizeThrowsArgumentException() { var dataA = CreateVector(Data); var dataB = CreateVector(new double[] { 1, 2, 3, 4, 5, 6 }); Assert.Throws(() => dataA.DotProduct(dataB)); } /// /// Can calculate the dot product using "*" operator. /// [Test] public void CanDotProductUsingOperator() { var dataA = CreateVector(Data); var dataB = CreateVector(Data); Assert.AreEqual(55.0, dataA * dataB); } /// /// Operator "*" throws ArgumentException when the argument has different size. /// [Test] public void OperatorDotProductWhenDifferentSizeThrowsArgumentException() { var dataA = CreateVector(Data); var dataB = CreateVector(new double[] { 1, 2, 3, 4, 5, 6 }); Assert.Throws(() => { var d = dataA * dataB; }); } /// /// Can pointwise multiply two vectors. /// [Test] public void CanPointwiseMultiply() { var vector1 = CreateVector(Data); var vector2 = vector1.Clone(); var result = vector1.PointwiseMultiply(vector2); for (var i = 0; i < vector1.Count; i++) { Assert.AreEqual(Data[i] * Data[i], result[i]); } } /// /// Can pointwise multiply two vectors using a result vector. /// [Test] public void CanPointwiseMultiplyIntoResultVector() { var vector1 = CreateVector(Data); var vector2 = vector1.Clone(); var result = CreateVector(vector1.Count); vector1.PointwiseMultiply(vector2, result); for (var i = 0; i < vector1.Count; i++) { Assert.AreEqual(Data[i] * Data[i], result[i]); } } /// /// Pointwise multiply with a result vector wrong size throws ArgumentException. /// [Test] public void PointwiseMultiplyWithInvalidResultLengthThrowsArgumentException() { var vector1 = CreateVector(Data); var vector2 = vector1.Clone(); var result = CreateVector(vector1.Count + 1); Assert.Throws(() => vector1.PointwiseMultiply(vector2, result)); } /// /// Can pointwise divide two vectors using a result vector. /// [Test] public void CanPointWiseDivide() { var vector1 = CreateVector(Data); var vector2 = vector1.Clone(); var result = vector1.PointwiseDivide(vector2); for (var i = 0; i < vector1.Count; i++) { Assert.AreEqual(Data[i] / Data[i], result[i]); } } /// /// Can pointwise divide two vectors using a result vector. /// [Test] public void CanPointWiseDivideIntoResultVector() { var vector1 = CreateVector(Data); var vector2 = vector1.Clone(); var result = CreateVector(vector1.Count); vector1.PointwiseDivide(vector2, result); for (var i = 0; i < vector1.Count; i++) { Assert.AreEqual(Data[i] / Data[i], result[i]); } } /// /// Pointwise divide with a result vector wrong size throws ArgumentException. /// [Test] public void PointwiseDivideWithInvalidResultLengthThrowsArgumentException() { var vector1 = CreateVector(Data); var vector2 = vector1.Clone(); var result = CreateVector(vector1.Count + 1); Assert.Throws(() => vector1.PointwiseDivide(vector2, result)); } /// /// Can calculate the outer product of two vectors. /// [Test] public void CanCalculateOuterProduct() { var vector1 = CreateVector(Data); var vector2 = CreateVector(Data); var m = Vector.OuterProduct(vector1, vector2); for (var i = 0; i < vector1.Count; i++) { for (var j = 0; j < vector2.Count; j++) { Assert.AreEqual(m[i, j], vector1[i] * vector2[j]); } } } /// /// Can calculate the tensor multiply. /// [Test] public void CanCalculateTensorMultiply() { var vector1 = CreateVector(Data); var vector2 = CreateVector(Data); var m = vector1.OuterProduct(vector2); for (var i = 0; i < vector1.Count; i++) { for (var j = 0; j < vector2.Count; j++) { Assert.AreEqual(m[i, j], vector1[i] * vector2[j]); } } } /// /// Can compute the modules of each element of vector. /// [Test] public void CanComputeModulus() { var vector = CreateVector(Data); var mod = vector.Modulus(3.2); for (var index = 0; index < Data.Length; index++) { AssertHelpers.AlmostEqual(Data[index] % 3.2, mod[index], 14); } } /// /// Can compute the modules of each element of vector using a result vector. /// [Test] public void CanComputeModulusUsingResultVector() { var vector = CreateVector(Data); var mod = CreateVector(vector.Count); vector.Modulus(3.2, mod); for (var index = 0; index < Data.Length; index++) { AssertHelpers.AlmostEqual(Data[index] % 3.2, mod[index], 14); } } /// /// Can compute the modules of each element of vector using a result vector. /// [Test] public void CanComputeModulusUsingSameResultVector() { var vector = CreateVector(Data); vector.Modulus(3.2, vector); for (var index = 0; index < Data.Length; index++) { AssertHelpers.AlmostEqual(Data[index] % 3.2, vector[index], 14); } } /// /// Can compute the modules of each element of vector using the operator %. /// [Test] public void CanComputeModulusUsingOperator() { var vector = CreateVector(Data); var mod = vector % 4.5; for (var index = 0; index < Data.Length; index++) { AssertHelpers.AlmostEqual(Data[index] % 4.5, mod[index], 14); } } } }