diff --git a/src/UnitTests/LinearAlgebraTests/Complex/DenseVectorArithmeticTheory.cs b/src/UnitTests/LinearAlgebraTests/Complex/DenseVectorArithmeticTheory.cs new file mode 100644 index 00000000..3e870889 --- /dev/null +++ b/src/UnitTests/LinearAlgebraTests/Complex/DenseVectorArithmeticTheory.cs @@ -0,0 +1,48 @@ +// +// 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-2011 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.Complex +{ + using System.Numerics; + using LinearAlgebra.Complex; + using LinearAlgebra.Generic; + using NUnit.Framework; + + [TestFixture] + public class DenseVectorArithmeticTheory : VectorArithmeticTheory + { + [Datapoints] + Vector[] denseVectors = new Vector[] + { + new DenseVector(new[] { new Complex(1, 1), new Complex(2, 1), new Complex(3, 1), new Complex(4, 1), new Complex(5, 1) }), + new DenseVector(new[] { new Complex(2, -1), new Complex(0, 0), new Complex(0, 2), new Complex(-5, 1), new Complex(0, 0) }) + }; + } +} diff --git a/src/UnitTests/LinearAlgebraTests/Complex/SparseVectorArithmeticTheory.cs b/src/UnitTests/LinearAlgebraTests/Complex/SparseVectorArithmeticTheory.cs new file mode 100644 index 00000000..b4a13256 --- /dev/null +++ b/src/UnitTests/LinearAlgebraTests/Complex/SparseVectorArithmeticTheory.cs @@ -0,0 +1,50 @@ +// +// 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-2011 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.Complex +{ + using System.Numerics; + using LinearAlgebra.Complex; + using LinearAlgebra.Generic; + using NUnit.Framework; + + [TestFixture] + public class SparseVectorArithmeticTheory : VectorArithmeticTheory + { + [Datapoints] + Vector[] denseVectors = new Vector[] + { + new SparseVector(new[] { new Complex(1, 1), new Complex(2, 1), new Complex(3, 1), new Complex(4, 1), new Complex(5, 1) }), + new SparseVector(new[] { new Complex(2, -1), new Complex(0, 0), new Complex(0, 2), new Complex(-5, 1), new Complex(0, 0) }), + new SparseVector(5), + new SparseVector(int.MaxValue) + }; + } +} diff --git a/src/UnitTests/LinearAlgebraTests/Complex/SparseVectorTest.cs b/src/UnitTests/LinearAlgebraTests/Complex/SparseVectorTest.cs index 30abdb03..724feaff 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex/SparseVectorTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex/SparseVectorTest.cs @@ -180,122 +180,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex CollectionAssert.AreEqual(array, array); } - /// - /// Can call unary plus operator on a vector. - /// - [Test] - public void CanCallUnaryPlusOperatorOnSparseVector() - { - var vector = new SparseVector(Data); - - var other = +vector; - - for (var i = 0; i < Data.Length; i++) - { - Assert.AreEqual(vector[i], other[i]); - } - } - - [Test, Timeout(100)] - public void CanCallUnaryPlusOperatorOnSparseVectorEmptyMaxLength() - { - var vector = new SparseVector(int.MaxValue); - - var result = +vector; - - Assert.AreEqual(0, result.AbsoluteMaximum()); - Assert.AreEqual(0, result.AbsoluteMinimum()); - } - - /// - /// Can add two sparse vectors using "+" operator. - /// - [Test] - public void CanAddTwoSparseVectorsUsingOperator() - { - var vector = new SparseVector(Data); - var other = new SparseVector(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]); - } - } - - [Test, Timeout(100)] - public void CanAddSparseVectorsUsingOperatorEmptyMaxLength() - { - var vector = new SparseVector(int.MaxValue); - var other = new SparseVector(int.MaxValue); - - var result = vector + other; - - Assert.AreEqual(0, result.AbsoluteMaximum()); - Assert.AreEqual(0, result.AbsoluteMinimum()); - } - - /// - /// Can call unary negate operator on a sparse vector. - /// - [Test] - public void CanCallUnaryNegationOperatorOnSparseVector() - { - var vector = new SparseVector(Data); - - var other = -vector; - - for (var i = 0; i < Data.Length; i++) - { - Assert.AreEqual(-Data[i], other[i]); - } - } - - [Test, Timeout(100)] - public void CanCallUnaryNegationOperatorOnSparseVectorEmptyMaxLength() - { - var vector = new SparseVector(int.MaxValue); - - var result = -vector; - - Assert.AreEqual(0, result.AbsoluteMaximum()); - Assert.AreEqual(0, result.AbsoluteMinimum()); - } - - /// - /// Can subtract two sparse vectors using "-" operator. - /// - [Test] - public void CanSubtractTwoSparseVectorsUsingOperator() - { - var vector = new SparseVector(Data); - var other = new SparseVector(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(Complex.Zero, result[i]); - } - } - - [Test, Timeout(100)] - public void CanSubtractTwoSparseVectorsUsingOperatorEmptyMaxLength() - { - var vector = new SparseVector(int.MaxValue); - var other = new SparseVector(int.MaxValue); - - var result = vector - other; - - Assert.AreEqual(0, result.AbsoluteMaximum()); - Assert.AreEqual(0, result.AbsoluteMinimum()); - } - /// /// Can multiply a sparse vector by a scalar using "*" operator. /// diff --git a/src/UnitTests/LinearAlgebraTests/Complex/VectorArithmeticTheory.cs b/src/UnitTests/LinearAlgebraTests/Complex/VectorArithmeticTheory.cs new file mode 100644 index 00000000..8bd1c16f --- /dev/null +++ b/src/UnitTests/LinearAlgebraTests/Complex/VectorArithmeticTheory.cs @@ -0,0 +1,112 @@ +// +// 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-2011 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.Complex +{ + using System; + using System.Numerics; + using LinearAlgebra.Generic; + using NUnit.Framework; + + [TestFixture] + public abstract class VectorArithmeticTheory + { + [Theory, Timeout(100)] + public void CanCallUnaryPlusOperatorOnVector(Vector vector) + { + var hash = vector.GetHashCode(); + + var result = +vector; + + Assert.That(vector.GetHashCode(), Is.EqualTo(hash)); + Assert.That(result, Is.Not.SameAs(vector)); + Assert.That(result, Is.EqualTo(vector)); + } + + [Theory, Timeout(100)] + public void CanCallUnaryMinusOperatorOnVector(Vector vector) + { + var hash = vector.GetHashCode(); + + var result = -vector; + + Assert.That(vector.GetHashCode(), Is.EqualTo(hash)); + Assert.That(result, Is.Not.SameAs(vector)); + Assert.That(-result, Is.EqualTo(vector)); + + for (var i = 0; i < Math.Min(vector.Count, 20); i++) + { + Assert.That(result[i], Is.EqualTo(-vector[i])); + } + } + + [Theory, Pairwise, Timeout(100)] + public void CanAddTwoVectorsUsingOperator(Vector a, Vector b) + { + Assume.That(a.Count, Is.EqualTo(b.Count)); + + var hasha = a.GetHashCode(); + var hashb = b.GetHashCode(); + + var result = a + b; + + Assert.That(a.GetHashCode(), Is.EqualTo(hasha)); + Assert.That(b.GetHashCode(), Is.EqualTo(hashb)); + Assert.That(result, Is.Not.SameAs(a)); + Assert.That(result, Is.Not.SameAs(b)); + + for (var i = 0; i < Math.Min(a.Count, 20); i++) + { + Assert.That(result[i], Is.EqualTo(a[i] + b[i])); + } + } + + [Theory, Pairwise, Timeout(100)] + public void CanSubtractTwoVectorsUsingOperator(Vector a, Vector b) + { + Assume.That(a.Count, Is.EqualTo(b.Count)); + + var hasha = a.GetHashCode(); + var hashb = b.GetHashCode(); + + var result = a - b; + + Assert.That(a.GetHashCode(), Is.EqualTo(hasha)); + Assert.That(b.GetHashCode(), Is.EqualTo(hashb)); + Assert.That(result, Is.Not.SameAs(a)); + Assert.That(result, Is.Not.SameAs(b)); + + for (var i = 0; i < Math.Min(a.Count, 20); i++) + { + Assert.That(result[i], Is.EqualTo(a[i] - b[i])); + } + } + } +} diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/DenseVectorArithmeticTheory.cs b/src/UnitTests/LinearAlgebraTests/Complex32/DenseVectorArithmeticTheory.cs new file mode 100644 index 00000000..3d004af3 --- /dev/null +++ b/src/UnitTests/LinearAlgebraTests/Complex32/DenseVectorArithmeticTheory.cs @@ -0,0 +1,48 @@ +// +// 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-2011 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.Complex32 +{ + using LinearAlgebra.Complex32; + using LinearAlgebra.Generic; + using Numerics; + using NUnit.Framework; + + [TestFixture] + public class DenseVectorArithmeticTheory : VectorArithmeticTheory + { + [Datapoints] + Vector[] denseVectors = new Vector[] + { + new DenseVector(new[] { new Complex32(1, 1), new Complex32(2, 1), new Complex32(3, 1), new Complex32(4, 1), new Complex32(5, 1) }), + new DenseVector(new[] { new Complex32(2, -1), new Complex32(0, 0), new Complex32(0, 2), new Complex32(-5, 1), new Complex32(0, 0) }) + }; + } +} diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/SparseVectorArithmeticTheory.cs b/src/UnitTests/LinearAlgebraTests/Complex32/SparseVectorArithmeticTheory.cs new file mode 100644 index 00000000..8c59ff70 --- /dev/null +++ b/src/UnitTests/LinearAlgebraTests/Complex32/SparseVectorArithmeticTheory.cs @@ -0,0 +1,50 @@ +// +// 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-2011 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.Complex32 +{ + using LinearAlgebra.Complex32; + using LinearAlgebra.Generic; + using Numerics; + using NUnit.Framework; + + [TestFixture] + public class SparseVectorArithmeticTheory : VectorArithmeticTheory + { + [Datapoints] + Vector[] denseVectors = new Vector[] + { + new SparseVector(new[] { new Complex32(1, 1), new Complex32(2, 1), new Complex32(3, 1), new Complex32(4, 1), new Complex32(5, 1) }), + new SparseVector(new[] { new Complex32(2, -1), new Complex32(0, 0), new Complex32(0, 2), new Complex32(-5, 1), new Complex32(0, 0) }), + new SparseVector(5), + new SparseVector(int.MaxValue) + }; + } +} diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/SparseVectorTest.cs b/src/UnitTests/LinearAlgebraTests/Complex32/SparseVectorTest.cs index f45276db..765770cd 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex32/SparseVectorTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex32/SparseVectorTest.cs @@ -180,122 +180,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 CollectionAssert.AreEqual(array, array); } - /// - /// Can call unary plus operator on a vector. - /// - [Test] - public void CanCallUnaryPlusOperatorOnSparseVector() - { - var vector = new SparseVector(Data); - - var other = +vector; - - for (var i = 0; i < Data.Length; i++) - { - Assert.AreEqual(vector[i], other[i]); - } - } - - [Test, Timeout(100)] - public void CanCallUnaryPlusOperatorOnSparseVectorEmptyMaxLength() - { - var vector = new SparseVector(int.MaxValue); - - var result = +vector; - - Assert.AreEqual(0, result.AbsoluteMaximum()); - Assert.AreEqual(0, result.AbsoluteMinimum()); - } - - /// - /// Can add two sparse vectors using "+" operator. - /// - [Test] - public void CanAddTwoSparseVectorsUsingOperator() - { - var vector = new SparseVector(Data); - var other = new SparseVector(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.0f, result[i]); - } - } - - [Test, Timeout(100)] - public void CanAddSparseVectorsUsingOperatorEmptyMaxLength() - { - var vector = new SparseVector(int.MaxValue); - var other = new SparseVector(int.MaxValue); - - var result = vector + other; - - Assert.AreEqual(0, result.AbsoluteMaximum()); - Assert.AreEqual(0, result.AbsoluteMinimum()); - } - - /// - /// Can call unary negate operator on a sparse vector. - /// - [Test] - public void CanCallUnaryNegationOperatorOnSparseVector() - { - var vector = new SparseVector(Data); - - var other = -vector; - - for (var i = 0; i < Data.Length; i++) - { - Assert.AreEqual(-Data[i], other[i]); - } - } - - [Test, Timeout(100)] - public void CanCallUnaryNegationOperatorOnSparseVectorEmptyMaxLength() - { - var vector = new SparseVector(int.MaxValue); - - var result = -vector; - - Assert.AreEqual(0, result.AbsoluteMaximum()); - Assert.AreEqual(0, result.AbsoluteMinimum()); - } - - /// - /// Can subtract two sparse vectors using "-" operator. - /// - [Test] - public void CanSubtractTwoSparseVectorsUsingOperator() - { - var vector = new SparseVector(Data); - var other = new SparseVector(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(Complex32.Zero, result[i]); - } - } - - [Test, Timeout(100)] - public void CanSubtractTwoSparseVectorsUsingOperatorEmptyMaxLength() - { - var vector = new SparseVector(int.MaxValue); - var other = new SparseVector(int.MaxValue); - - var result = vector - other; - - Assert.AreEqual(0, result.AbsoluteMaximum()); - Assert.AreEqual(0, result.AbsoluteMinimum()); - } - /// /// Can multiply a sparse vector by a Complex using "*" operator. /// diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/VectorArithmeticTheory.cs b/src/UnitTests/LinearAlgebraTests/Complex32/VectorArithmeticTheory.cs new file mode 100644 index 00000000..38dbb5a6 --- /dev/null +++ b/src/UnitTests/LinearAlgebraTests/Complex32/VectorArithmeticTheory.cs @@ -0,0 +1,112 @@ +// +// 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-2011 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.Complex32 +{ + using System; + using LinearAlgebra.Generic; + using Numerics; + using NUnit.Framework; + + [TestFixture] + public abstract class VectorArithmeticTheory + { + [Theory, Timeout(100)] + public void CanCallUnaryPlusOperatorOnVector(Vector vector) + { + var hash = vector.GetHashCode(); + + var result = +vector; + + Assert.That(vector.GetHashCode(), Is.EqualTo(hash)); + Assert.That(result, Is.Not.SameAs(vector)); + Assert.That(result, Is.EqualTo(vector)); + } + + [Theory, Timeout(100)] + public void CanCallUnaryMinusOperatorOnVector(Vector vector) + { + var hash = vector.GetHashCode(); + + var result = -vector; + + Assert.That(vector.GetHashCode(), Is.EqualTo(hash)); + Assert.That(result, Is.Not.SameAs(vector)); + Assert.That(-result, Is.EqualTo(vector)); + + for (var i = 0; i < Math.Min(vector.Count, 20); i++) + { + Assert.That(result[i], Is.EqualTo(-vector[i])); + } + } + + [Theory, Pairwise, Timeout(100)] + public void CanAddTwoVectorsUsingOperator(Vector a, Vector b) + { + Assume.That(a.Count, Is.EqualTo(b.Count)); + + var hasha = a.GetHashCode(); + var hashb = b.GetHashCode(); + + var result = a + b; + + Assert.That(a.GetHashCode(), Is.EqualTo(hasha)); + Assert.That(b.GetHashCode(), Is.EqualTo(hashb)); + Assert.That(result, Is.Not.SameAs(a)); + Assert.That(result, Is.Not.SameAs(b)); + + for (var i = 0; i < Math.Min(a.Count, 20); i++) + { + Assert.That(result[i], Is.EqualTo(a[i] + b[i])); + } + } + + [Theory, Pairwise, Timeout(100)] + public void CanSubtractTwoVectorsUsingOperator(Vector a, Vector b) + { + Assume.That(a.Count, Is.EqualTo(b.Count)); + + var hasha = a.GetHashCode(); + var hashb = b.GetHashCode(); + + var result = a - b; + + Assert.That(a.GetHashCode(), Is.EqualTo(hasha)); + Assert.That(b.GetHashCode(), Is.EqualTo(hashb)); + Assert.That(result, Is.Not.SameAs(a)); + Assert.That(result, Is.Not.SameAs(b)); + + for (var i = 0; i < Math.Min(a.Count, 20); i++) + { + Assert.That(result[i], Is.EqualTo(a[i] - b[i])); + } + } + } +} diff --git a/src/UnitTests/LinearAlgebraTests/Double/DenseVectorArithmeticTheory.cs b/src/UnitTests/LinearAlgebraTests/Double/DenseVectorArithmeticTheory.cs new file mode 100644 index 00000000..7acf81dd --- /dev/null +++ b/src/UnitTests/LinearAlgebraTests/Double/DenseVectorArithmeticTheory.cs @@ -0,0 +1,47 @@ +// +// 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-2011 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 LinearAlgebra.Double; + using LinearAlgebra.Generic; + using NUnit.Framework; + + [TestFixture] + public class DenseVectorArithmeticTheory : VectorArithmeticTheory + { + [Datapoints] + Vector[] denseVectors = new Vector[] + { + new DenseVector(new double[] { 1, 2, 3, 4, 5 }), + new DenseVector(new double[] { 2, 0, 0, -5, 0 }), + }; + } +} diff --git a/src/UnitTests/LinearAlgebraTests/Double/SparseVectorArithmeticTheory.cs b/src/UnitTests/LinearAlgebraTests/Double/SparseVectorArithmeticTheory.cs new file mode 100644 index 00000000..5db104eb --- /dev/null +++ b/src/UnitTests/LinearAlgebraTests/Double/SparseVectorArithmeticTheory.cs @@ -0,0 +1,49 @@ +// +// 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-2011 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 LinearAlgebra.Double; + using LinearAlgebra.Generic; + using NUnit.Framework; + + [TestFixture] + public class SparseVectorArithmeticTheory : VectorArithmeticTheory + { + [Datapoints] + Vector[] denseVectors = new Vector[] + { + new SparseVector(new double[] { 1, 2, 3, 4, 5 }), + new SparseVector(new double[] { 2, 0, 0, -5, 0 }), + new SparseVector(5), + new SparseVector(int.MaxValue) + }; + } +} diff --git a/src/UnitTests/LinearAlgebraTests/Double/SparseVectorTest.cs b/src/UnitTests/LinearAlgebraTests/Double/SparseVectorTest.cs index 6d42cf2f..df81ebf7 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/SparseVectorTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/SparseVectorTest.cs @@ -179,120 +179,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double CollectionAssert.AreEqual(array, array); } - /// - /// Can call unary plus operator on a vector. - /// - [Test] - public void CanCallUnaryPlusOperatorOnSparseVector() - { - var vector = new SparseVector(Data); - - var result = +vector; - - for (var i = 0; i < Data.Length; i++) - { - Assert.AreEqual(vector[i], result[i]); - } - } - - [Test, Timeout(100)] - public void CanCallUnaryPlusOperatorOnSparseVectorEmptyMaxLength() - { - var vector = new SparseVector(int.MaxValue); - - var result = +vector; - - Assert.AreEqual(0, result.AbsoluteMaximum()); - Assert.AreEqual(0, result.AbsoluteMinimum()); - } - - /// - /// Can add two sparse vectors using "+" operator. - /// - [Test] - public void CanAddTwoSparseVectorsUsingOperator() - { - var vector = new SparseVector(Data); - var other = new SparseVector(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]); - } - } - - [Test, Timeout(100)] - public void CanAddSparseVectorsUsingOperatorEmptyMaxLength() - { - var vector = new SparseVector(int.MaxValue); - var other = new SparseVector(int.MaxValue); - - var result = vector + other; - - Assert.AreEqual(0, result.AbsoluteMaximum()); - Assert.AreEqual(0, result.AbsoluteMinimum()); - } - - /// - /// Can call unary negate operator on a sparse vector. - /// - [Test] - public void CanCallUnaryNegationOperatorOnSparseVector() - { - var vector = new SparseVector(Data); - var other = -vector; - for (var i = 0; i < Data.Length; i++) - { - Assert.AreEqual(-Data[i], other[i]); - } - } - - [Test, Timeout(100)] - public void CanCallUnaryNegationOperatorOnSparseVectorEmptyMaxLength() - { - var vector = new SparseVector(int.MaxValue); - - var result = -vector; - - Assert.AreEqual(0, result.AbsoluteMaximum()); - Assert.AreEqual(0, result.AbsoluteMinimum()); - } - - /// - /// Can subtract two sparse vectors using "-" operator. - /// - [Test] - public void CanSubtractTwoSparseVectorsUsingOperator() - { - var vector = new SparseVector(Data); - var other = new SparseVector(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]); - } - } - - [Test, Timeout(100)] - public void CanSubtractTwoSparseVectorsUsingOperatorEmptyMaxLength() - { - var vector = new SparseVector(int.MaxValue); - var other = new SparseVector(int.MaxValue); - - var result = vector - other; - - Assert.AreEqual(0, result.AbsoluteMaximum()); - Assert.AreEqual(0, result.AbsoluteMinimum()); - } - /// /// Can multiply a sparse vector by a scalar using "*" operator. /// diff --git a/src/UnitTests/LinearAlgebraTests/Double/VectorArithmeticTheory.cs b/src/UnitTests/LinearAlgebraTests/Double/VectorArithmeticTheory.cs new file mode 100644 index 00000000..ef9db2e2 --- /dev/null +++ b/src/UnitTests/LinearAlgebraTests/Double/VectorArithmeticTheory.cs @@ -0,0 +1,111 @@ +// +// 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-2011 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; + + [TestFixture] + public abstract class VectorArithmeticTheory + { + [Theory, Timeout(100)] + public void CanCallUnaryPlusOperatorOnVector(Vector vector) + { + var hash = vector.GetHashCode(); + + var result = +vector; + + Assert.That(vector.GetHashCode(), Is.EqualTo(hash)); + Assert.That(result, Is.Not.SameAs(vector)); + Assert.That(result, Is.EqualTo(vector)); + } + + [Theory, Timeout(100)] + public void CanCallUnaryMinusOperatorOnVector(Vector vector) + { + var hash = vector.GetHashCode(); + + var result = -vector; + + Assert.That(vector.GetHashCode(), Is.EqualTo(hash)); + Assert.That(result, Is.Not.SameAs(vector)); + Assert.That(-result, Is.EqualTo(vector)); + + for (var i = 0; i < Math.Min(vector.Count, 20); i++) + { + Assert.That(result[i], Is.EqualTo(-vector[i])); + } + } + + [Theory, Pairwise, Timeout(100)] + public void CanAddTwoVectorsUsingOperator(Vector a, Vector b) + { + Assume.That(a.Count, Is.EqualTo(b.Count)); + + var hasha = a.GetHashCode(); + var hashb = b.GetHashCode(); + + var result = a + b; + + Assert.That(a.GetHashCode(), Is.EqualTo(hasha)); + Assert.That(b.GetHashCode(), Is.EqualTo(hashb)); + Assert.That(result, Is.Not.SameAs(a)); + Assert.That(result, Is.Not.SameAs(b)); + + for (var i = 0; i < Math.Min(a.Count, 20); i++) + { + Assert.That(result[i], Is.EqualTo(a[i] + b[i])); + } + } + + [Theory, Pairwise, Timeout(100)] + public void CanSubtractTwoVectorsUsingOperator(Vector a, Vector b) + { + Assume.That(a.Count, Is.EqualTo(b.Count)); + + var hasha = a.GetHashCode(); + var hashb = b.GetHashCode(); + + var result = a - b; + + Assert.That(a.GetHashCode(), Is.EqualTo(hasha)); + Assert.That(b.GetHashCode(), Is.EqualTo(hashb)); + Assert.That(result, Is.Not.SameAs(a)); + Assert.That(result, Is.Not.SameAs(b)); + + for (var i = 0; i < Math.Min(a.Count, 20); i++) + { + Assert.That(result[i], Is.EqualTo(a[i] - b[i])); + } + } + } +} diff --git a/src/UnitTests/LinearAlgebraTests/Single/DenseVectorArithmeticTheory.cs b/src/UnitTests/LinearAlgebraTests/Single/DenseVectorArithmeticTheory.cs new file mode 100644 index 00000000..555d67f9 --- /dev/null +++ b/src/UnitTests/LinearAlgebraTests/Single/DenseVectorArithmeticTheory.cs @@ -0,0 +1,47 @@ +// +// 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-2011 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.Single +{ + using LinearAlgebra.Generic; + using LinearAlgebra.Single; + using NUnit.Framework; + + [TestFixture] + public class DenseVectorArithmeticTheory : VectorArithmeticTheory + { + [Datapoints] + Vector[] denseVectors = new Vector[] + { + new DenseVector(new float[] { 1, 2, 3, 4, 5 }), + new DenseVector(new float[] { 2, 0, 0, -5, 0 }), + }; + } +} diff --git a/src/UnitTests/LinearAlgebraTests/Single/SparseVectorArithmeticTheory.cs b/src/UnitTests/LinearAlgebraTests/Single/SparseVectorArithmeticTheory.cs new file mode 100644 index 00000000..bfcff525 --- /dev/null +++ b/src/UnitTests/LinearAlgebraTests/Single/SparseVectorArithmeticTheory.cs @@ -0,0 +1,49 @@ +// +// 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-2011 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.Single +{ + using LinearAlgebra.Generic; + using LinearAlgebra.Single; + using NUnit.Framework; + + [TestFixture] + public class SparseVectorArithmeticTheory : VectorArithmeticTheory + { + [Datapoints] + Vector[] denseVectors = new Vector[] + { + new SparseVector(new float[] { 1, 2, 3, 4, 5 }), + new SparseVector(new float[] { 2, 0, 0, -5, 0 }), + new SparseVector(5), + new SparseVector(int.MaxValue) + }; + } +} diff --git a/src/UnitTests/LinearAlgebraTests/Single/SparseVectorTest.cs b/src/UnitTests/LinearAlgebraTests/Single/SparseVectorTest.cs index 77b0d322..6bef3733 100644 --- a/src/UnitTests/LinearAlgebraTests/Single/SparseVectorTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Single/SparseVectorTest.cs @@ -179,122 +179,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single CollectionAssert.AreEqual(array, array); } - /// - /// Can call unary plus operator on a vector. - /// - [Test] - public void CanCallUnaryPlusOperatorOnSparseVector() - { - var vector = new SparseVector(Data); - - var other = +vector; - - for (var i = 0; i < Data.Length; i++) - { - Assert.AreEqual(vector[i], other[i]); - } - } - - [Test, Timeout(100)] - public void CanCallUnaryPlusOperatorOnSparseVectorEmptyMaxLength() - { - var vector = new SparseVector(int.MaxValue); - - var result = +vector; - - Assert.AreEqual(0, result.AbsoluteMaximum()); - Assert.AreEqual(0, result.AbsoluteMinimum()); - } - - /// - /// Can add two sparse vectors using "+" operator. - /// - [Test] - public void CanAddTwoSparseVectorsUsingOperator() - { - var vector = new SparseVector(Data); - var other = new SparseVector(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.0f, result[i]); - } - } - - [Test, Timeout(100)] - public void CanAddSparseVectorsUsingOperatorEmptyMaxLength() - { - var vector = new SparseVector(int.MaxValue); - var other = new SparseVector(int.MaxValue); - - var result = vector + other; - - Assert.AreEqual(0, result.AbsoluteMaximum()); - Assert.AreEqual(0, result.AbsoluteMinimum()); - } - - /// - /// Can call unary negate operator on a sparse vector. - /// - [Test] - public void CanCallUnaryNegationOperatorOnSparseVector() - { - var vector = new SparseVector(Data); - - var other = -vector; - - for (var i = 0; i < Data.Length; i++) - { - Assert.AreEqual(-Data[i], other[i]); - } - } - - [Test, Timeout(100)] - public void CanCallUnaryNegationOperatorOnSparseVectorEmptyMaxLength() - { - var vector = new SparseVector(int.MaxValue); - - var result = -vector; - - Assert.AreEqual(0, result.AbsoluteMaximum()); - Assert.AreEqual(0, result.AbsoluteMinimum()); - } - - /// - /// Can subtract two sparse vectors using "-" operator. - /// - [Test] - public void CanSubtractTwoSparseVectorsUsingOperator() - { - var vector = new SparseVector(Data); - var other = new SparseVector(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.0f, result[i]); - } - } - - [Test, Timeout(100)] - public void CanSubtractTwoSparseVectorsUsingOperatorEmptyMaxLength() - { - var vector = new SparseVector(int.MaxValue); - var other = new SparseVector(int.MaxValue); - - var result = vector - other; - - Assert.AreEqual(0, result.AbsoluteMaximum()); - Assert.AreEqual(0, result.AbsoluteMinimum()); - } - /// /// Can multiply a sparse vector by a scalar using "*" operator. /// diff --git a/src/UnitTests/LinearAlgebraTests/Single/VectorArithmeticTheory.cs b/src/UnitTests/LinearAlgebraTests/Single/VectorArithmeticTheory.cs new file mode 100644 index 00000000..627c7ff1 --- /dev/null +++ b/src/UnitTests/LinearAlgebraTests/Single/VectorArithmeticTheory.cs @@ -0,0 +1,111 @@ +// +// 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-2011 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.Single +{ + using System; + using LinearAlgebra.Generic; + using NUnit.Framework; + + [TestFixture] + public abstract class VectorArithmeticTheory + { + [Theory, Timeout(100)] + public void CanCallUnaryPlusOperatorOnVector(Vector vector) + { + var hash = vector.GetHashCode(); + + var result = +vector; + + Assert.That(vector.GetHashCode(), Is.EqualTo(hash)); + Assert.That(result, Is.Not.SameAs(vector)); + Assert.That(result, Is.EqualTo(vector)); + } + + [Theory, Timeout(100)] + public void CanCallUnaryMinusOperatorOnVector(Vector vector) + { + var hash = vector.GetHashCode(); + + var result = -vector; + + Assert.That(vector.GetHashCode(), Is.EqualTo(hash)); + Assert.That(result, Is.Not.SameAs(vector)); + Assert.That(-result, Is.EqualTo(vector)); + + for (var i = 0; i < Math.Min(vector.Count, 20); i++) + { + Assert.That(result[i], Is.EqualTo(-vector[i])); + } + } + + [Theory, Pairwise, Timeout(100)] + public void CanAddTwoVectorsUsingOperator(Vector a, Vector b) + { + Assume.That(a.Count, Is.EqualTo(b.Count)); + + var hasha = a.GetHashCode(); + var hashb = b.GetHashCode(); + + var result = a + b; + + Assert.That(a.GetHashCode(), Is.EqualTo(hasha)); + Assert.That(b.GetHashCode(), Is.EqualTo(hashb)); + Assert.That(result, Is.Not.SameAs(a)); + Assert.That(result, Is.Not.SameAs(b)); + + for (var i = 0; i < Math.Min(a.Count, 20); i++) + { + Assert.That(result[i], Is.EqualTo(a[i] + b[i])); + } + } + + [Theory, Pairwise, Timeout(100)] + public void CanSubtractTwoVectorsUsingOperator(Vector a, Vector b) + { + Assume.That(a.Count, Is.EqualTo(b.Count)); + + var hasha = a.GetHashCode(); + var hashb = b.GetHashCode(); + + var result = a - b; + + Assert.That(a.GetHashCode(), Is.EqualTo(hasha)); + Assert.That(b.GetHashCode(), Is.EqualTo(hashb)); + Assert.That(result, Is.Not.SameAs(a)); + Assert.That(result, Is.Not.SameAs(b)); + + for (var i = 0; i < Math.Min(a.Count, 20); i++) + { + Assert.That(result[i], Is.EqualTo(a[i] - b[i])); + } + } + } +} diff --git a/src/UnitTests/UnitTests.csproj b/src/UnitTests/UnitTests.csproj index 12ca55fe..251a057a 100644 --- a/src/UnitTests/UnitTests.csproj +++ b/src/UnitTests/UnitTests.csproj @@ -132,6 +132,7 @@ Code + Code @@ -246,6 +247,7 @@ Code + Code @@ -264,6 +266,7 @@ Code + Code @@ -276,6 +279,7 @@ Code + Code @@ -390,6 +394,7 @@ Code + Code @@ -408,6 +413,7 @@ Code + Code @@ -417,6 +423,7 @@ Code + Code @@ -426,6 +433,7 @@ Code + Code @@ -564,6 +572,7 @@ Code + Code @@ -678,6 +687,7 @@ Code + Code @@ -696,6 +706,7 @@ Code + Code @@ -705,6 +716,7 @@ Code +