diff --git a/src/FSharpUnitTests100602(1).vsp b/src/FSharpUnitTests100602(1).vsp deleted file mode 100644 index 099e8fee..00000000 Binary files a/src/FSharpUnitTests100602(1).vsp and /dev/null differ diff --git a/src/FSharpUnitTests100602(2).vsp b/src/FSharpUnitTests100602(2).vsp deleted file mode 100644 index c9cfadc9..00000000 Binary files a/src/FSharpUnitTests100602(2).vsp and /dev/null differ diff --git a/src/FSharpUnitTests100602.vsp b/src/FSharpUnitTests100602.vsp deleted file mode 100644 index 1f3920d0..00000000 Binary files a/src/FSharpUnitTests100602.vsp and /dev/null differ diff --git a/src/Numerics/LinearAlgebra/Double/DenseVector.cs b/src/Numerics/LinearAlgebra/Double/DenseVector.cs index ffa7a56d..c1249437 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseVector.cs @@ -3,9 +3,7 @@ // 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 @@ -14,10 +12,8 @@ // 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 @@ -33,8 +29,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double using System; using System.Collections.Generic; using System.Globalization; + using Distributions; using NumberTheory; using Properties; + using Random; using Threading; /// @@ -93,8 +91,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double if (vector == null) { CommonParallel.For( - 0, - this.Data.Length, + 0, + this.Data.Length, index => this[index] = other[index]); } else @@ -287,8 +285,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double if (otherVector == null) { CommonParallel.For( - 0, - this.Data.Length, + 0, + this.Data.Length, index => target[index] = this.Data[index]); } else @@ -309,8 +307,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double } CommonParallel.For( - 0, - this.Data.Length, + 0, + this.Data.Length, index => this.Data[index] += scalar); } @@ -465,8 +463,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double } CommonParallel.For( - 0, - this.Data.Length, + 0, + this.Data.Length, index => this.Data[index] -= scalar); } @@ -617,9 +615,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double { var result = new DenseVector(this.Count); CommonParallel.For( - 0, - this.Data.Length, - index => result[index] = -this.Data[index]); + 0, + this.Data.Length, + index => result[index] = -this.Data[index]); return result; } @@ -663,7 +661,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double { return base.DotProduct(other); } - + return Control.LinearAlgebraProvider.DotProduct(this.Data, denseVector.Data); } @@ -758,7 +756,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The value of the absolute minimum element. public override double AbsoluteMinimum() { - return Math.Abs(this.Data[AbsoluteMinimumIndex()]); + return Math.Abs(this.Data[this.AbsoluteMinimumIndex()]); } /// @@ -767,11 +765,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The index of absolute minimum element. public override int AbsoluteMinimumIndex() { - int index = 0; - double min = System.Math.Abs(this.Data[index]); - for (int i = 1; i < Count; i++) + var index = 0; + var min = Math.Abs(this.Data[index]); + for (var i = 1; i < this.Count; i++) { - double test = System.Math.Abs(this.Data[i]); + var test = Math.Abs(this.Data[i]); if (test < min) { index = i; @@ -795,9 +793,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// If is not positive. public override Vector SubVector(int index, int length) { - if (index < 0 || index >= Count) + if (index < 0 || index >= this.Count) { - throw new ArgumentOutOfRangeException("start"); + throw new ArgumentOutOfRangeException("index"); } if (length <= 0) @@ -805,16 +803,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentOutOfRangeException("length"); } - if (index + length > Count) + if (index + length > this.Count) { - throw new ArgumentOutOfRangeException("start"); + throw new ArgumentOutOfRangeException("length"); } - DenseVector result = new DenseVector(length); + var result = new DenseVector(length); CommonParallel.For( - index, - index + length, + index, + index + length, i => result.Data[i - index] = this.Data[i]); return result; } @@ -832,14 +830,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("values"); } - if (values.Length != Count) + if (values.Length != this.Count) { - throw new ArgumentException("values", Resources.ArgumentVectorsSameLength); + throw new ArgumentException(Resources.ArgumentVectorsSameLength, "values"); } CommonParallel.For( - 0, - values.Length, + 0, + values.Length, i => this.Data[i] = values[i]); } @@ -849,7 +847,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The value of maximum element. public override double Maximum() { - return this.Data[MaximumIndex()]; + return this.Data[this.MaximumIndex()]; } /// @@ -858,9 +856,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The index of absolute maximum element. public override int MaximumIndex() { - int index = 0; - double max = this.Data[0]; - for (int i = 1; i < Count; i++) + var index = 0; + var max = this.Data[0]; + for (var i = 1; i < this.Count; i++) { if (max < this.Data[i]) { @@ -878,7 +876,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The value of the minimum element. public override double Minimum() { - return this.Data[MinimumIndex()]; + return this.Data[this.MinimumIndex()]; } /// @@ -887,9 +885,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The index of minimum element. public override int MinimumIndex() { - int index = 0; - double min = this.Data[0]; - for (int i = 1; i < Count; i++) + var index = 0; + var min = this.Data[0]; + for (var i = 1; i < this.Count; i++) { if (min > this.Data[i]) { @@ -908,7 +906,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double public override double Sum() { double result = 0; - for (int i = 0; i < Count; i++) + for (var i = 0; i < this.Count; i++) { result += this.Data[i]; } @@ -923,7 +921,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double public override double SumMagnitudes() { double result = 0; - for (int i = 0; i < Count; i++) + for (var i = 0; i < this.Count; i++) { result += Math.Abs(this.Data[i]); } @@ -945,13 +943,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (Count != other.Count) + if (this.Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } - DenseVector result = new DenseVector(Count); - PointWiseMultiply(other, result); + var result = new DenseVector(this.Count); + this.PointWiseMultiply(other, result); return result; } @@ -976,20 +974,20 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (Count != other.Count) + if (this.Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } - if (Count != result.Count) + if (this.Count != result.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } CommonParallel.For( - 0, - this.Count, - index => result.Data[index] = this.Data[index] * other.Data[index]); + 0, + this.Count, + index => result.Data[index] = this.Data[index] * other.Data[index]); } /// @@ -1006,13 +1004,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (Count != other.Count) + if (this.Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } - DenseVector result = new DenseVector(Count); - PointWiseAdd(other, result); + var result = new DenseVector(this.Count); + this.PointWiseAdd(other, result); return result; } @@ -1037,20 +1035,20 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (Count != other.Count) + if (this.Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } - if (Count != result.Count) + if (this.Count != result.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } CommonParallel.For( - 0, - this.Count, - index => result.Data[index] = this.Data[index] + other.Data[index]); + 0, + this.Count, + index => result.Data[index] = this.Data[index] + other.Data[index]); } /// @@ -1067,13 +1065,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (Count != other.Count) + if (this.Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } - DenseVector result = new DenseVector(Count); - PointWiseSubtract(other, result); + var result = new DenseVector(this.Count); + this.PointWiseSubtract(other, result); return result; } @@ -1098,20 +1096,20 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (Count != other.Count) + if (this.Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } - if (Count != result.Count) + if (this.Count != result.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } CommonParallel.For( - 0, - this.Count, - index => result.Data[index] = this.Data[index] - other.Data[index]); + 0, + this.Count, + index => result.Data[index] = this.Data[index] - other.Data[index]); } /// @@ -1128,13 +1126,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (Count != other.Count) + if (this.Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } - DenseVector result = new DenseVector(Count); - PointWiseDivide(other, result); + var result = new DenseVector(this.Count); + this.PointWiseDivide(other, result); return result; } @@ -1159,20 +1157,20 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (Count != other.Count) + if (this.Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } - if (Count != result.Count) + if (this.Count != result.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } CommonParallel.For( - 0, - this.Count, - index => result.Data[index] = this.Data[index] / other.Data[index]); + 0, + this.Count, + index => result.Data[index] = this.Data[index] / other.Data[index]); } /// @@ -1195,51 +1193,63 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("v"); } - DenseMatrix matrix = new DenseMatrix(u.Count, v.Count); + var matrix = new DenseMatrix(u.Count, v.Count); CommonParallel.For( - 0, - u.Count, + 0, + u.Count, i => CommonParallel.For(0, v.Count, j => matrix.At(i, j, u.Data[i] * v.Data[j]))); return matrix; } /// - /// Generates vector with random elements + /// Generates a vector with random elements /// - /// Dimensionality of vector. + /// Number of elements in the vector. + /// Continuous Random Distribution or Source /// - /// An n-dimensional vector with uniformly distributed - /// random elements in [0, 1) interval. + /// A vector with n-random elements distributed according + /// to the specified random distribution. /// /// If the n vector is non poisitive. - public new virtual DenseVector Random(int n) + public override Vector Random(int length, IContinuousDistribution randomDistribution) { - // use any class which inherits the AbstractRandomNumberGenerator - return Random(n, new MathNet.Numerics.Random.SystemCryptoRandomNumberGenerator()); + if (length < 0) + { + throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); + } + + var v = (DenseVector)this.CreateVector(length); + for (var index = 0; index < v.Data.Length; index++) + { + v.Data[index] = randomDistribution.Sample(); + } + + return v; } /// /// Generates a vector with random elements /// - /// Number of elements in the vector. + /// Number of elements in the vector. /// Continuous Random Distribution or Source /// /// A vector with n-random elements distributed according /// to the specified random distribution. /// /// If the n vector is non poisitive. - public new virtual DenseVector Random(int n, MathNet.Numerics.Random.AbstractRandomNumberGenerator randomDistribution) + public override Vector Random(int length, IDiscreteDistribution randomDistribution) { - if (n < 0) + if (length < 0) { - throw new ArgumentException(Resources.ArgumentMustBePositive, "n"); + throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); + } + + var v = (DenseVector)this.CreateVector(length); + for (var index = 0; index < v.Data.Length; index++) + { + v.Data[index] = randomDistribution.Sample(); } - DenseVector v = new DenseVector(n); - CommonParallel.For( - 0, - v.Count, - index => v.Data[index] = randomDistribution.NextDouble()); return v; } @@ -1265,7 +1275,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double public override double Norm() { var sum = 0.0; - + for (var i = 0; i < this.Data.Length; i++) { sum = SpecialFunctions.Hypotenuse(sum, this.Data[i]); @@ -1281,8 +1291,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double public override double Norm1() { return CommonParallel.Aggregate( - 0, - this.Count, + 0, + this.Count, index => Math.Abs(this.Data[index])); } @@ -1309,8 +1319,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double } var sum = CommonParallel.Aggregate( - 0, - this.Count, + 0, + this.Count, index => Math.Pow(Math.Abs(this.Data[index]), p)); return Math.Pow(sum, 1.0 / p); @@ -1323,35 +1333,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double public override double NormInfinity() { return CommonParallel.Select( - 0, - this.Count, - (index, localData) => localData = Math.Max(localData, Math.Abs(this.Data[index])), + 0, + this.Count, + (index, localData) => localData = Math.Max(localData, Math.Abs(this.Data[index])), Math.Max); - /*double max = 0; - - var syncLock = new object(); - - Parallel.ForEach( - Partitioner.Create(0, this.Count), - () => 0.0, - (range, loop, localData) => - { - for (var i = range.Item1; i < range.Item2; i++) - { - localData = Math.Max(localData, Math.Abs(this.Data[i])); - } - - return localData; - }, - localResult => - { - lock (syncLock) - { - max = Math.Max(max, localResult); - } - }); - - return max;*/ } #endregion diff --git a/src/Numerics/LinearAlgebra/Double/Vector.cs b/src/Numerics/LinearAlgebra/Double/Vector.cs index 360f1e63..42ff098b 100644 --- a/src/Numerics/LinearAlgebra/Double/Vector.cs +++ b/src/Numerics/LinearAlgebra/Double/Vector.cs @@ -3,9 +3,7 @@ // 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 @@ -14,10 +12,8 @@ // 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 @@ -34,6 +30,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double using System.Collections; using System.Collections.Generic; using System.Text; + using Distributions; using Properties; using Threading; @@ -133,8 +130,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double } CommonParallel.For( - 0, - this.Count, + 0, + this.Count, index => this[index] += scalar); } @@ -212,8 +209,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double } CommonParallel.For( - 0, - this.Count, + 0, + this.Count, index => this[index] += other[index]); } @@ -277,8 +274,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double } CommonParallel.For( - 0, - this.Count, + 0, + this.Count, index => this[index] -= scalar); } @@ -356,8 +353,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double } CommonParallel.For( - 0, - this.Count, + 0, + this.Count, index => this[index] -= other[index]); } @@ -421,8 +418,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double } CommonParallel.For( - 0, - this.Count, + 0, + this.Count, index => this[index] *= scalar); } @@ -562,13 +559,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (Count != other.Count) + if (this.Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } - Vector result = CreateVector(Count); - PointWiseMultiply(other, result); + var result = this.CreateVector(this.Count); + this.PointWiseMultiply(other, result); return result; } @@ -593,20 +590,20 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (Count != other.Count) + if (this.Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } - if (Count != result.Count) + if (this.Count != result.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } CommonParallel.For( - 0, - this.Count, - index => result[index] = this[index] * other[index]); + 0, + this.Count, + index => result[index] = this[index] * other[index]); } /// @@ -623,13 +620,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (Count != other.Count) + if (this.Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } - Vector result = CreateVector(Count); - PointWiseAdd(other, result); + var result = this.CreateVector(this.Count); + this.PointWiseAdd(other, result); return result; } @@ -654,22 +651,22 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (Count != other.Count) + if (this.Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } - if (Count != result.Count) + if (this.Count != result.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } CommonParallel.For( - 0, - this.Count, - index => result[index] = this[index] + other[index]); + 0, + this.Count, + index => result[index] = this[index] + other[index]); } - + /// /// Pointwise subtarct this vector with another vector. /// @@ -684,13 +681,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (Count != other.Count) + if (this.Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } - Vector result = CreateVector(Count); - PointWiseSubtract(other, result); + var result = this.CreateVector(this.Count); + this.PointWiseSubtract(other, result); return result; } @@ -715,20 +712,20 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (Count != other.Count) + if (this.Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } - if (Count != result.Count) + if (this.Count != result.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } CommonParallel.For( - 0, - this.Count, - index => result[index] = this[index] - other[index]); + 0, + this.Count, + index => result[index] = this[index] - other[index]); } /// @@ -745,13 +742,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (Count != other.Count) + if (this.Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } - Vector result = CreateVector(Count); - PointWiseDivide(other, result); + var result = this.CreateVector(this.Count); + this.PointWiseDivide(other, result); return result; } @@ -776,20 +773,20 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (Count != other.Count) + if (this.Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } - if (Count != result.Count) + if (this.Count != result.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } CommonParallel.For( - 0, - this.Count, - index => result[index] = this[index] / other[index]); + 0, + this.Count, + index => result[index] = this[index] / other[index]); } /// @@ -812,27 +809,38 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("v"); } - DenseMatrix matrix = new DenseMatrix(u.Count, v.Count); + var matrix = new DenseMatrix(u.Count, v.Count); CommonParallel.For( - 0, - u.Count, + 0, + u.Count, i => CommonParallel.For(0, v.Count, j => matrix.At(i, j, u[i] * v[j]))); return matrix; } /// - /// Generates vector with random elements + /// Generates a vector with random elements /// - /// Dimensionality of vector. + /// Number of elements in the vector. + /// Continuous Random Distribution or Source /// - /// An n-dimensional vector with uniformly distributed - /// random elements in [0, 1) interval. + /// A vector with n-random elements distributed according + /// to the specified random distribution. /// /// If the n vector is non poisitive. - public virtual Vector Random(int length) + public virtual Vector Random(int length, IContinuousDistribution randomDistribution) { - // Just use any class which inherits the AbstractRandomNumberGenerator - return Random(length, new MathNet.Numerics.Random.SystemCryptoRandomNumberGenerator()); + if (length < 0) + { + throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); + } + + var v = this.CreateVector(length); + for (var index = 0; index < v.Count; index++) + { + v[index] = randomDistribution.Sample(); + } + + return v; } /// @@ -845,18 +853,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// to the specified random distribution. /// /// If the n vector is non poisitive. - public virtual Vector Random(int length, MathNet.Numerics.Random.AbstractRandomNumberGenerator randomDistribution) + public virtual Vector Random(int length, IDiscreteDistribution randomDistribution) { if (length < 0) { throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); } - Vector v = CreateVector(length); - CommonParallel.For( - 0, - v.Count, - index => v[index] = randomDistribution.NextDouble()); + var v = this.CreateVector(length); + for (var index = 0; index < v.Count; index++) + { + v[index] = randomDistribution.Sample(); + } + return v; } @@ -879,7 +888,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The value of the absolute minimum element. public virtual double AbsoluteMinimum() { - return Math.Abs(this[AbsoluteMinimumIndex()]); + return Math.Abs(this[this.AbsoluteMinimumIndex()]); } /// @@ -888,11 +897,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The index of absolute minimum element. public virtual int AbsoluteMinimumIndex() { - int index = 0; - double min = System.Math.Abs(this[index]); - for (int i = 1; i < Count; i++) + var index = 0; + var min = Math.Abs(this[index]); + for (var i = 1; i < this.Count; i++) { - double test = System.Math.Abs(this[i]); + var test = Math.Abs(this[i]); if (test < min) { index = i; @@ -901,7 +910,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double } return index; - } + } /// /// Returns the value of maximum element. @@ -909,7 +918,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The value of maximum element. public virtual double Maximum() { - return this[MaximumIndex()]; + return this[this.MaximumIndex()]; } /// @@ -918,9 +927,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The index of absolute maximum element. public virtual int MaximumIndex() { - int index = 0; - double max = this[0]; - for (int i = 1; i < Count; i++) + var index = 0; + var max = this[0]; + for (var i = 1; i < this.Count; i++) { if (max < this[i]) { @@ -938,7 +947,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The value of the minimum element. public virtual double Minimum() { - return this[MinimumIndex()]; + return this[this.MinimumIndex()]; } /// @@ -947,9 +956,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The index of minimum element. public virtual int MinimumIndex() { - int index = 0; - double min = this[0]; - for (int i = 1; i < Count; i++) + var index = 0; + var min = this[0]; + for (var i = 1; i < this.Count; i++) { if (min > this[i]) { @@ -968,7 +977,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double public virtual double Sum() { double result = 0; - for (int i = 0; i < Count; i++) + for (var i = 0; i < this.Count; i++) { result += this[i]; } @@ -983,13 +992,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double public virtual double SumMagnitudes() { double result = 0; - for (int i = 0; i < Count; i++) + for (var i = 0; i < this.Count; i++) { result += Math.Abs(this[i]); } return result; } + #endregion #region Arithmetic Operator Overloading @@ -1227,10 +1237,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double } var sum = CommonParallel.Aggregate( - 0, - this.Count, + 0, + this.Count, index => Math.Pow(Math.Abs(this[index]), p)); - + return Math.Pow(sum, 1.0 / p); } @@ -1243,36 +1253,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double public virtual double NormInfinity() { return CommonParallel.Select( - 0, - this.Count, - (index, localData) => localData = Math.Max(localData, Math.Abs(this[index])), + 0, + this.Count, + (index, localData) => localData = Math.Max(localData, Math.Abs(this[index])), Math.Max); - - /* - var max = 0.0; - var syncLock = new object(); - - Parallel.ForEach( - Partitioner.Create(0, this.Count), - () => 0.0, - (range, loop, localData) => - { - for (var i = range.Item1; i < range.Item2; i++) - { - localData = Math.Max(localData, Math.Abs(this[i])); - } - - return localData; - }, - localResult => - { - lock (syncLock) - { - max = Math.Max(localResult, max); - } - }); - - return max;*/ } /// @@ -1341,8 +1325,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double } CommonParallel.For( - 0, - this.Count, + 0, + this.Count, index => target[index] = this[index]); } @@ -1394,15 +1378,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double this.CopyTo(tmpVector); CommonParallel.For( - 0, - count, + 0, + count, index => destination[destinationOffset + index] = tmpVector[offset + index]); } else { CommonParallel.For( - 0, - count, + 0, + count, index => destination[destinationOffset + index] = this[offset + index]); } } @@ -1471,9 +1455,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// If is not positive. public virtual Vector SubVector(int index, int length) { - if (index < 0 || index >= Count) + if (index < 0 || index >= this.Count) { - throw new ArgumentOutOfRangeException("start"); + throw new ArgumentOutOfRangeException("index"); } if (length <= 0) @@ -1481,16 +1465,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentOutOfRangeException("length"); } - if (index + length > Count) + if (index + length > this.Count) { - throw new ArgumentOutOfRangeException("start"); + throw new ArgumentOutOfRangeException("index"); } - Vector result = CreateVector(length); + var result = this.CreateVector(length); CommonParallel.For( - index, - index + length, + index, + index + length, i => result[i - index] = this[i]); return result; } @@ -1503,21 +1487,22 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// If is not the same size as this vector. public virtual void SetValues(double[] values) { - if (values == null) + if (values == null) { throw new ArgumentNullException("values"); } - if (values.Length != Count) + if (values.Length != this.Count) { - throw new ArgumentException("values", Resources.ArgumentVectorsSameLength); + throw new ArgumentException(Resources.ArgumentVectorsSameLength, "values"); } CommonParallel.For( 0, - values.Length, + values.Length, i => this[i] = values[i]); } + #endregion #region Implemented Interfaces diff --git a/src/Numerics/Random/AbstractRandomNumberGenerator.cs b/src/Numerics/Random/AbstractRandomNumberGenerator.cs index 3ad2ce35..8fdf3fb4 100644 --- a/src/Numerics/Random/AbstractRandomNumberGenerator.cs +++ b/src/Numerics/Random/AbstractRandomNumberGenerator.cs @@ -3,9 +3,7 @@ // 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 @@ -14,10 +12,8 @@ // 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 @@ -37,7 +33,7 @@ namespace MathNet.Numerics.Random /// Abstract class for random number generators. This class introduces a layer between /// and the Math.Net Numerics random number generators to provide thread safety. /// - public abstract class AbstractRandomNumberGenerator : System.Random + public abstract class AbstractRandomNumberGenerator : Random { /// /// A delegate type that represents a method that generates random numbers. @@ -55,15 +51,13 @@ namespace MathNet.Numerics.Random /// private readonly object _lock = new object(); - /// /// Initializes a new instance of the class using /// the value of to set whether /// the instance is thread safe or not. /// - protected AbstractRandomNumberGenerator(): this(Control.ThreadSafeRandomNumberGenerators) + protected AbstractRandomNumberGenerator() : this(Control.ThreadSafeRandomNumberGenerators) { - } /// @@ -74,7 +68,7 @@ namespace MathNet.Numerics.Random /// safe classes. protected AbstractRandomNumberGenerator(bool threadSafe) { - _sampleMethod = threadSafe ? (SampleMethod) ThreadSafeSample : DoSample; + this._sampleMethod = threadSafe ? (SampleMethod)this.ThreadSafeSample : this.DoSample; } /// @@ -91,11 +85,13 @@ namespace MathNet.Numerics.Random { throw new ArgumentException(Resources.ArgumentMustBePositive); } + var ret = new double[n]; - for (int i = 0; i < ret.Length; i++) + for (var i = 0; i < ret.Length; i++) { - ret[i] = Sample(); + ret[i] = this.Sample(); } + return ret; } @@ -107,7 +103,7 @@ namespace MathNet.Numerics.Random /// public override int Next() { - return (int)(Sample() * int.MaxValue); + return (int)(this.Sample() * int.MaxValue); } /// @@ -123,7 +119,7 @@ namespace MathNet.Numerics.Random throw new ArgumentOutOfRangeException(Resources.ArgumentMustBePositive); } - return (int) (Sample() % maxValue); + return (int)(this.Sample() % maxValue); } /// @@ -142,7 +138,7 @@ namespace MathNet.Numerics.Random throw new ArgumentOutOfRangeException(Resources.ArgumentMinValueGreaterThanMaxValue); } - return (int) (Sample()*(maxValue - minValue)) + minValue; + return (int)(this.Sample() * (maxValue - minValue)) + minValue; } /// @@ -157,9 +153,9 @@ namespace MathNet.Numerics.Random throw new ArgumentNullException("buffer"); } - for (int i = 0; i < buffer.Length; i++) + for (var i = 0; i < buffer.Length; i++) { - buffer[i] = (byte) (Next()%256); + buffer[i] = (byte)(this.Next() % 256); } } @@ -169,18 +165,18 @@ namespace MathNet.Numerics.Random /// A double-precision floating point number greater than or equal to 0.0, and less than 1.0. protected override double Sample() { - return _sampleMethod(); + return this._sampleMethod(); } /// /// Thread safe version of which returns a random number between 0.0 and 1.0. /// - /// + /// A double-precision floating point number greater than or equal to 0.0, and less than 1.0 private double ThreadSafeSample() { - lock(_lock) + lock (this._lock) { - return DoSample(); + return this.DoSample(); } } diff --git a/src/UnitTests/LinearAlgebraTests/Double/DenseVectorTests.cs b/src/UnitTests/LinearAlgebraTests/Double/DenseVectorTests.cs index 7b5cc195..981bbfa9 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/DenseVectorTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/DenseVectorTests.cs @@ -3,9 +3,7 @@ // 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 @@ -14,10 +12,8 @@ // 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 @@ -30,10 +26,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double { - using System.Collections.Generic; - using MbUnit.Framework; - using LinearAlgebra.Double; using System; + using System.Collections.Generic; + using LinearAlgebra.Double; + using MbUnit.Framework; public class DenseVectorTests : VectorTests { @@ -45,7 +41,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double protected override Vector CreateVector(IList data) { var vector = new DenseVector(data.Count); - for(var index = 0; index < data.Count; index++) + for (var index = 0; index < data.Count; index++) { vector[index] = data[index]; } @@ -57,8 +53,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [MultipleAsserts] public void CanCreateDenseVectorFromArray() { - var data = new double[_data.Length]; - System.Array.Copy(_data, data, _data.Length); + var data = new double[this._data.Length]; + Array.Copy(this._data, data, this._data.Length); var vector = new DenseVector(data); Assert.AreSame(data, vector.Data); @@ -66,6 +62,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double { Assert.AreEqual(data[i], vector[i]); } + vector[0] = 100.0; Assert.AreEqual(100.0, data[0]); } @@ -74,12 +71,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [MultipleAsserts] public void CanCreateDenseVectorFromAnotherDenseVector() { - var vector = new DenseVector(_data); + var vector = new DenseVector(this._data); var other = new DenseVector(vector); - - + + Assert.AreNotSame(vector, other); - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { Assert.AreEqual(vector[i], other[i]); } @@ -89,11 +86,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [MultipleAsserts] public void CanCreateDenseVectorFromAnotherVector() { - var vector = (Vector)new DenseVector(_data); + var vector = (Vector)new DenseVector(this._data); var other = new DenseVector(vector); Assert.AreNotSame(vector, other); - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { Assert.AreEqual(vector[i], other[i]); } @@ -103,10 +100,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [MultipleAsserts] public void CanCreateDenseVectorFromUserDefinedVector() { - var vector = new UserDefinedVector(_data); + var vector = new UserDefinedVector(this._data); var other = new DenseVector(vector); - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { Assert.AreEqual(vector[i], other[i]); } @@ -118,7 +115,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double var vector = new DenseVector(5, 5); Assert.ForAll(vector, value => value == 5); } - + [Test] [MultipleAsserts] public void CanCreateDenseMatrix() @@ -134,7 +131,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [MultipleAsserts] public void CanConvertDenseVectorToArray() { - var vector = new DenseVector(_data); + var vector = new DenseVector(this._data); var array = (double[])vector; Assert.IsInstanceOfType(typeof(double[]), array); Assert.AreSame(vector.Data, array); @@ -154,7 +151,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [Test] public void CanCallUnaryPlusOperatorOnDenseVector() { - var vector = new DenseVector(_data); + var vector = new DenseVector(this._data); var other = +vector; Assert.AreSame(vector, other, "Should be the same vector"); } @@ -163,26 +160,26 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [MultipleAsserts] public void CanAddTwoDenseVectorsUsingOperator() { - var vector = new DenseVector(_data); - var other = new DenseVector(_data); + var vector = new DenseVector(this._data); + var other = new DenseVector(this._data); var result = vector + other; - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i], vector[i], "Making sure the original vector wasn't modified."); - Assert.AreEqual(_data[i], other[i], "Making sure the original vector wasn't modified."); - Assert.AreEqual(_data[i] * 2.0, result[i]); + Assert.AreEqual(this._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(this._data[i] * 2.0, result[i]); } } [Test] public void CanCallUnaryNegationOperatorOnDenseVector() { - var vector = new DenseVector(_data); + var vector = new DenseVector(this._data); var other = -vector; - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(-_data[i], other[i]); + Assert.AreEqual(-this._data[i], other[i]); } } @@ -190,14 +187,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [MultipleAsserts] public void CanSubtractTwoDenseVectorsUsingOperator() { - var vector = new DenseVector(_data); - var other = new DenseVector(_data); + var vector = new DenseVector(this._data); + var other = new DenseVector(this._data); var result = vector - other; - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i], vector[i], "Making sure the original vector wasn't modified."); - Assert.AreEqual(_data[i], other[i], "Making sure the original vector wasn't modified."); + Assert.AreEqual(this._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(0.0, result[i]); } } @@ -206,32 +203,32 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [MultipleAsserts] public void CanMultiplyDenseVectorByScalarUsingOperators() { - var vector = new DenseVector(_data); + var vector = new DenseVector(this._data); vector = vector * 2.0; - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i] * 2.0, vector[i]); + Assert.AreEqual(this._data[i] * 2.0, vector[i]); } vector = vector * 1.0; - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i] * 2.0, vector[i]); + Assert.AreEqual(this._data[i] * 2.0, vector[i]); } - vector = new DenseVector(_data); + vector = new DenseVector(this._data); vector = 2.0 * vector; - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i] * 2.0, vector[i]); + Assert.AreEqual(this._data[i] * 2.0, vector[i]); } vector = 1.0 * vector; - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i] * 2.0, vector[i]); + Assert.AreEqual(this._data[i] * 2.0, vector[i]); } } @@ -239,361 +236,30 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [MultipleAsserts] public void CanDivideDenseVectorByScalarUsingOperators() { - var vector = new DenseVector(_data); + var vector = new DenseVector(this._data); vector = vector / 2.0; - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i] / 2.0, vector[i]); + Assert.AreEqual(this._data[i] / 2.0, vector[i]); } vector = vector / 1.0; - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i] / 2.0, vector[i]); + Assert.AreEqual(this._data[i] / 2.0, vector[i]); } } [Test] - public void CanFindAbsoluteMinimumIndexInDenseVector() - { - DenseVector source = new DenseVector(_data); - int expected = 0; - int actual = source.AbsoluteMinimumIndex(); - Assert.AreEqual(expected, actual); - } - - [Test] - public void CanFindAbsoluteMinimumInDenseVector() - { - DenseVector source = new DenseVector(_data); - double expected = 1; - double actual = source.AbsoluteMinimum(); - Assert.AreEqual(expected, actual); - - } - - [Test] - [Row(0, 5)] - [Row(2, 2)] - [Row(1, 4)] - [Row(6, 10, ExpectedException = typeof(ArgumentOutOfRangeException))] - [Row(1, 10, ExpectedException = typeof(ArgumentOutOfRangeException))] - [Row(1, -10, ExpectedException = typeof(ArgumentOutOfRangeException))] - public void CanCalculateSubVector(int index, int length) + public void CanCalculateDyadicProductForDenseVector() { - DenseVector vector = new DenseVector(_data); - Vector sub = vector.SubVector(index, length); - Assert.AreEqual(length, sub.Count); - for (int i = 0; i < length; i++) + var vector1 = this.CreateVector(this._data); + var vector2 = this.CreateVector(this._data); + Matrix m = Vector.DyadicProduct(vector1, vector2); + for (var i = 0; i < vector1.Count; i++) { - Assert.AreEqual(vector[i + index], sub[i]); - } - } - - [Test] - public void CanFindMaximumIndexInDenseVector() - { - DenseVector vector = new DenseVector(_data); - - int expected = 4; - int actual = vector.MaximumIndex(); - - Assert.AreEqual(expected, actual); - } - - [Test] - public void CanFindMaximumInDenseVector() - { - DenseVector vector = new DenseVector(_data); - double expected = 5; - double actual = vector.Maximum(); - Assert.AreEqual(expected, actual); - } - - [Test] - public void CanFindMinimumIndexOfDenseVector() - { - DenseVector vector = new DenseVector(_data); - int expected = 0; - int actual = vector.MinimumIndex(); - Assert.AreEqual(expected, actual); - } - - [Test] - public void CanFindMinimumOfDenseVector() - { - DenseVector vector = new DenseVector(_data); - double expected = 1; - double actual = vector.Minimum(); - Assert.AreEqual(expected, actual); - } - - [Test] - public void SumOfDenseVector() - { - double[] testData = { -20, -10, 10, 20, 30, }; - DenseVector vector = new DenseVector(testData); - double actual = vector.Sum(); - double expected = 30; - Assert.AreEqual(expected, actual); - } - - [Test] - public void SumMagnitudesOfDenseVector() - { - double[] testData = { -20, -10, 10, 20, 30, }; - DenseVector vector = new DenseVector(testData); - double actual = vector.SumMagnitudes(); - double expected = 90; - Assert.AreEqual(expected, actual); - } - - [Test] - [ExpectedArgumentNullException] - public new void SetValuesWithNullParameterShouldThrowException() - { - DenseVector vector = new DenseVector(_data); - vector.SetValues(null); - } - - [Test] - [ExpectedArgumentException] - public new void SetValuesWithNonEqualDataLengthShouldThrowException() - { - DenseVector vector = new DenseVector(_data.Length + 2); - vector.SetValues(_data); - } - - [Test] - public new void PointWiseMultiply() - { - DenseVector vector1 = new DenseVector(_data); - DenseVector vector2 = new DenseVector(_data); - DenseVector result = new DenseVector(vector1.Count); - vector1.PointWiseMultiply(vector2, result); - for (int i = 0; i < vector1.Count; i++) - { - Assert.AreEqual(_data[i] * _data[i], result[i]); - } - } - - [Test] - [ExpectedArgumentNullException] - public new void PointWiseMultiplyWithOtherNullShouldThrowException() - { - DenseVector vector1 = new DenseVector(_data); - DenseVector vector2 = null; - DenseVector result = new DenseVector(vector1.Count); - vector1.PointWiseMultiply(vector2, result); - } - - [Test] - [ExpectedArgumentNullException] - public new void PointWiseMultiplyWithResultNullShouldThrowException() - { - DenseVector vector1 = new DenseVector(_data); - DenseVector vector2 = new DenseVector(_data); - DenseVector result = null; - vector1.PointWiseMultiply(vector2, result); - } - - [Test] - [ExpectedArgumentException] - public new void PointWiseMultiplyWithInvalidResultLengthShouldThrowException() - { - Vector vector1 = new DenseVector(_data); - Vector vector2 = new DenseVector(_data); - Vector result = CreateVector(vector1.Count + 1); - vector1.PointWiseMultiply(vector2, result); - } - - [Test] - public new void PointWiseMultiplyWithResult() - { - Vector vector1 = new DenseVector(_data); - Vector vector2 = new DenseVector(_data); - Vector result = vector1.PointWiseMultiply(vector2); - for (int i = 0; i < vector1.Count; i++) - { - Assert.AreEqual(_data[i] * _data[i], result[i]); - } - } - - [Test] - public new void PointWiseAdd() - { - DenseVector vector1 = new DenseVector(_data); - DenseVector vector2 = new DenseVector(_data); - DenseVector result = new DenseVector(vector1.Count); - vector1.PointWiseAdd(vector2, result); - for (int i = 0; i < vector1.Count; i++) - { - Assert.AreEqual(_data[i] + _data[i], result[i]); - } - } - - [Test] - [ExpectedArgumentNullException] - public new void PointWiseAddWithOtherNullShouldThrowException() - { - DenseVector vector1 = new DenseVector(_data); - DenseVector vector2 = null; - DenseVector result = new DenseVector(vector1.Count); - vector1.PointWiseAdd(vector2, result); - } - - [Test] - [ExpectedArgumentNullException] - public new void PointWiseAddWithResultNullShouldThrowException() - { - DenseVector vector1 = new DenseVector(_data); - DenseVector vector2 = new DenseVector(_data); - DenseVector result = null; - vector1.PointWiseAdd(vector2, result); - } - - [Test] - [ExpectedArgumentException] - public new void PointWiseAddWithInvalidResultLengthShouldThrowException() - { - DenseVector vector1 = new DenseVector(_data); - DenseVector vector2 = new DenseVector(_data); - DenseVector result = new DenseVector(vector1.Count + 1); - vector1.PointWiseAdd(vector2, result); - } - - [Test] - public new void PointWiseAddWithResult() - { - DenseVector vector1 = new DenseVector(_data); - DenseVector vector2 = new DenseVector(_data); - DenseVector result = vector1.PointWiseAdd(vector2); - for (int i = 0; i < vector1.Count; i++) - { - Assert.AreEqual(_data[i] + _data[i], result[i]); - } - } - - [Test] - public new void PointWiseSubtract() - { - DenseVector vector1 = new DenseVector(_data); - DenseVector vector2 = new DenseVector(_data); - DenseVector result = new DenseVector(vector1.Count); - vector1.PointWiseSubtract(vector2, result); - for (int i = 0; i < vector1.Count; i++) - { - Assert.AreEqual(_data[i] - _data[i], result[i]); - } - } - - [Test] - [ExpectedArgumentNullException] - public new void PointWiseSubtractWithOtherNullShouldThrowException() - { - DenseVector vector1 = new DenseVector(_data); - DenseVector vector2 = null; - DenseVector result = new DenseVector(vector1.Count); - vector1.PointWiseSubtract(vector2, result); - } - - [Test] - [ExpectedArgumentNullException] - public new void PointWiseSubtractWithResultNullShouldThrowException() - { - DenseVector vector1 = new DenseVector(_data); - DenseVector vector2 = new DenseVector(_data); - Vector result = null; - vector1.PointWiseSubtract(vector2, result); - } - - [Test] - [ExpectedArgumentException] - public new void PointWiseSubtractWithInvalidResultLengthShouldThrowException() - { - DenseVector vector1 = new DenseVector(_data); - DenseVector vector2 = new DenseVector(_data); - DenseVector result = new DenseVector(vector1.Count + 1); - vector1.PointWiseSubtract(vector2, result); - } - - [Test] - public new void PointWiseSubtractWithResult() - { - DenseVector vector1 = new DenseVector(_data); - DenseVector vector2 = new DenseVector(_data); - DenseVector result = vector1.PointWiseSubtract(vector2); - for (int i = 0; i < vector1.Count; i++) - { - Assert.AreEqual(_data[i] - _data[i], result[i]); - } - } - - [Test] - public new void PointWiseDivide() - { - DenseVector vector1 = new DenseVector(_data); - DenseVector vector2 = new DenseVector(_data); - DenseVector result = new DenseVector(vector1.Count); - vector1.PointWiseDivide(vector2, result); - for (int i = 0; i < vector1.Count; i++) - { - Assert.AreEqual(_data[i] / _data[i], result[i]); - } - } - - [Test] - [ExpectedArgumentNullException] - public new void PointWiseDivideWithOtherNullShouldThrowException() - { - DenseVector vector1 = new DenseVector(_data); - DenseVector vector2 = null; - DenseVector result = new DenseVector(vector1.Count); - vector1.PointWiseDivide(vector2, result); - } - - [Test] - [ExpectedArgumentNullException] - public new void PointWiseDivideWithResultNullShouldThrowException() - { - DenseVector vector1 = new DenseVector(_data); - DenseVector vector2 = new DenseVector(_data); - DenseVector result = null; - vector1.PointWiseDivide(vector2, result); - } - - [Test] - [ExpectedArgumentException] - public new void PointWiseDivideWithInvalidResultLengthShouldThrowException() - { - DenseVector vector1 = new DenseVector(_data); - DenseVector vector2 = new DenseVector(_data); - DenseVector result = new DenseVector(vector1.Count + 1); - vector1.PointWiseDivide(vector2, result); - } - - [Test] - public new void PointWiseDivideWithResult() - { - DenseVector vector1 = new DenseVector(_data); - DenseVector vector2 = new DenseVector(_data); - DenseVector result = vector1.PointWiseDivide(vector2); - for (int i = 0; i < vector1.Count; i++) - { - Assert.AreEqual(_data[i] / _data[i], result[i]); - } - } - - [Test] - public new void CanCalculateDyadicProduct() - { - DenseVector vector1 = new DenseVector(_data); - DenseVector vector2 = new DenseVector(_data); - Matrix m = DenseVector.DyadicProduct(vector1, vector2); - for (int i = 0; i < vector1.Count; i++) - { - for (int j = 0; j < vector2.Count; j++) + for (var j = 0; j < vector2.Count; j++) { Assert.AreEqual(m[i, j], vector1[i] * vector2[j]); } @@ -602,53 +268,20 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [Test] [ExpectedArgumentNullException] - public new void DyadicProductWithFirstParameterNullShouldThrowException() + public void DyadicProducForDenseVectortWithFirstParameterNullShouldThrowException() { DenseVector vector1 = null; - DenseVector vector2 = new DenseVector(_data); - DenseVector.DyadicProduct(vector1, vector2); - } - - [Test] - [ExpectedArgumentNullException] - public new void DyadicProductWithSecondParameterNullShouldThrowException() - { - DenseVector vector1 = new DenseVector(_data); - DenseVector vector2 = null; + var vector2 = this.CreateVector(this._data); Vector.DyadicProduct(vector1, vector2); } - [Test] - public new void CanCalculateTensorMultiply() - { - DenseVector vector1 = new DenseVector(_data); - DenseVector vector2 = new DenseVector(_data); - Matrix m = vector1.TensorMultiply(vector2); - for (int i = 0; i < vector1.Count; i++) - { - for (int j = 0; j < vector2.Count; j++) - { - Assert.AreEqual(m[i, j], vector1[i] * vector2[j]); - } - } - } - [Test] [ExpectedArgumentNullException] - public new void TensorMultiplyWithNullParameterNullShouldThrowException() + public void DyadicProductForDenseVectorWithSecondParameterNullShouldThrowException() { - DenseVector vector1 = new DenseVector(_data); + var vector1 = this.CreateVector(this._data); DenseVector vector2 = null; - vector1.TensorMultiply(vector2); - } - - [Test] - [ExpectedArgumentException] - public new void RandomWithNumberOfElementsLessThanZeroShouldThrowException() - { - - DenseVector vector = new DenseVector(4); - vector = vector.Random(-3); + Vector.DyadicProduct(vector1, vector2); } } -} +} \ No newline at end of file diff --git a/src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs b/src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs index 08efb8b1..f13f6792 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs @@ -3,9 +3,7 @@ // 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 @@ -14,10 +12,8 @@ // 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 @@ -30,16 +26,16 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double { - using System; - using MathNet.Numerics.LinearAlgebra.Double; - using MbUnit.Framework; + using System; + using LinearAlgebra.Double; + using MbUnit.Framework; public abstract partial class VectorTests { [Test] public void CanCallPlus() { - var vector = CreateVector(_data); + var vector = this.CreateVector(this._data); var other = vector.Plus(); Assert.AreSame(vector, other, "Should be the same vector"); } @@ -55,7 +51,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [Test] public void CanCallUnaryPlusOperator() { - var vector = CreateVector(_data); + var vector = this.CreateVector(this._data); var other = +vector; Assert.AreSame(vector, other, "Should be the same vector"); } @@ -64,18 +60,18 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [MultipleAsserts] public void CanAddScalarToVector() { - var vector = CreateVector(_data); + var vector = this.CreateVector(this._data); vector.Add(2.0); - for( var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i]+2.0, vector[i]); + Assert.AreEqual(this._data[i] + 2.0, vector[i]); } vector.Add(0.0); - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i] + 2.0, vector[i]); + Assert.AreEqual(this._data[i] + 2.0, vector[i]); } } @@ -83,67 +79,67 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [MultipleAsserts] public void CanAddScalarToVectorUsingResultVector() { - var vector = CreateVector(_data); - var result = CreateVector(_data.Length); + var vector = this.CreateVector(this._data); + var result = this.CreateVector(this._data.Length); vector.Add(2.0, result); - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._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]); + Assert.AreEqual(this._data[i], vector[i], "Making sure the original vector wasn't modified."); + Assert.AreEqual(this._data[i] + 2.0, result[i]); } vector.Add(0.0, result); - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i], result[i]); + Assert.AreEqual(this._data[i], result[i]); } } [Test] public void ThrowsArgumentNullExceptionWhenAddingScalarWithNullResultVector() { - var vector = CreateVector(_data.Length); + var vector = this.CreateVector(this._data.Length); Assert.Throws(() => vector.Add(0.0, null)); } [Test] public void ThrowsArgumentExceptionWhenAddingScalarWithWrongSizeResultVector() { - var vector = CreateVector(_data.Length); - var result = CreateVector(_data.Length + 1); + var vector = this.CreateVector(this._data.Length); + var result = this.CreateVector(this._data.Length + 1); Assert.Throws(() => vector.Add(0.0, result)); } [Test] public void ThrowsArgumentNullExceptionWhenAddingTwoVectorsAndOneIsNull() { - var vector = CreateVector(_data); + var vector = this.CreateVector(this._data); Assert.Throws(() => vector.Add(null)); } [Test] public void ThrowsArgumentExceptionWhenAddingTwoVectorsOfDifferingSize() { - var vector = CreateVector(_data.Length); - var other = CreateVector(_data.Length +1); + var vector = this.CreateVector(this._data.Length); + var other = this.CreateVector(this._data.Length + 1); Assert.Throws(() => vector.Add(other)); } [Test] public void ThrowsArgumentNullExceptionWhenAddingTwoVectorsAndResultIsNull() { - var vector = CreateVector(_data.Length); - var other = CreateVector(_data.Length+1); - Assert.Throws(() => vector.Add(other,null)); + var vector = this.CreateVector(this._data.Length); + var other = this.CreateVector(this._data.Length + 1); + Assert.Throws(() => vector.Add(other, null)); } [Test] public void ThrowsArgumentExceptionWhenAddingTwoVectorsAndResultIsDifferentSize() { - var vector = CreateVector(_data.Length); - var other = CreateVector(_data.Length); - var result = CreateVector(_data.Length + 1); + var vector = this.CreateVector(this._data.Length); + var other = this.CreateVector(this._data.Length); + var result = this.CreateVector(this._data.Length + 1); Assert.Throws(() => vector.Add(other, result)); } @@ -151,8 +147,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double public void AdditionOperatorThrowsArgumentNullExpectionIfAVectorIsNull() { Vector a = null; - var b = CreateVector(_data.Length); - Assert.Throws(()=> a += b); + var b = this.CreateVector(this._data.Length); + Assert.Throws(() => a += b); a = b; b = null; @@ -162,21 +158,21 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [Test] public void AdditionOperatorThrowsArgumentExpectionIfVectorsAreDifferentSize() { - var a = CreateVector(_data.Length); - var b = CreateVector(_data.Length + 1); + var a = this.CreateVector(this._data.Length); + var b = this.CreateVector(this._data.Length + 1); Assert.Throws(() => a += b); } [Test] public void CanAddTwoVectors() { - var vector = CreateVector(_data); - var other = CreateVector(_data); + var vector = this.CreateVector(this._data); + var other = this.CreateVector(this._data); vector.Add(other); - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i] * 2.0, vector[i]); + Assert.AreEqual(this._data[i] * 2.0, vector[i]); } } @@ -184,16 +180,16 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [MultipleAsserts] public void CanAddTwoVectorsUsingResultVector() { - var vector = CreateVector(_data); - var other = CreateVector(_data); - var result = CreateVector(_data.Length); + var vector = this.CreateVector(this._data); + var other = this.CreateVector(this._data); + var result = this.CreateVector(this._data.Length); vector.Add(other, result); - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i], vector[i], "Making sure the original vector wasn't modified."); - Assert.AreEqual(_data[i], other[i], "Making sure the original vector wasn't modified."); - Assert.AreEqual(_data[i] * 2.0, result[i]); + Assert.AreEqual(this._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(this._data[i] * 2.0, result[i]); } } @@ -201,27 +197,27 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [MultipleAsserts] public void CanAddTwoVectorsUsingOperator() { - var vector = CreateVector(_data); - var other = CreateVector(_data); + var vector = this.CreateVector(this._data); + var other = this.CreateVector(this._data); var result = vector + other; - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i], vector[i], "Making sure the original vector wasn't modified."); - Assert.AreEqual(_data[i], other[i], "Making sure the original vector wasn't modified."); - Assert.AreEqual(_data[i] * 2.0, result[i]); + Assert.AreEqual(this._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(this._data[i] * 2.0, result[i]); } } [Test] public void CanAddVectorToItself() { - var vector = CreateVector(_data); + var vector = this.CreateVector(this._data); vector.Add(vector); - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i] * 2.0, vector[i]); + Assert.AreEqual(this._data[i] * 2.0, vector[i]); } } @@ -229,14 +225,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [MultipleAsserts] public void CanAddVectorToItselfUsingResultVector() { - var vector = CreateVector(_data); - var result = CreateVector(_data.Length); + var vector = this.CreateVector(this._data); + var result = this.CreateVector(this._data.Length); vector.Add(vector, result); - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._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]); + Assert.AreEqual(this._data[i], vector[i], "Making sure the original vector wasn't modified."); + Assert.AreEqual(this._data[i] * 2.0, result[i]); } } @@ -244,25 +240,25 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [MultipleAsserts] public void CanAddTwoVectorsUsingItselfAsResultVector() { - var vector = CreateVector(_data); - var other = CreateVector(_data); + var vector = this.CreateVector(this._data); + var other = this.CreateVector(this._data); vector.Add(other, vector); - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i], other[i], "Making sure the original vector wasn't modified."); - Assert.AreEqual(_data[i] * 2.0, vector[i]); + Assert.AreEqual(this._data[i], other[i], "Making sure the original vector wasn't modified."); + Assert.AreEqual(this._data[i] * 2.0, vector[i]); } } [Test] public void CanCallNegate() { - var vector = CreateVector(_data); + var vector = this.CreateVector(this._data); var other = vector.Negate(); - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(-_data[i], other[i]); + Assert.AreEqual(-this._data[i], other[i]); } } @@ -277,11 +273,11 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [Test] public void CanCallUnaryNegationOperator() { - var vector = CreateVector(_data); + var vector = this.CreateVector(this._data); var other = -vector; - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(-_data[i], other[i]); + Assert.AreEqual(-this._data[i], other[i]); } } @@ -289,18 +285,18 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [MultipleAsserts] public void CanSubtractScalarFromVector() { - var vector = CreateVector(_data); + var vector = this.CreateVector(this._data); vector.Subtract(2.0); - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i] - 2.0, vector[i]); + Assert.AreEqual(this._data[i] - 2.0, vector[i]); } vector.Subtract(0.0); - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i] - 2.0, vector[i]); + Assert.AreEqual(this._data[i] - 2.0, vector[i]); } } @@ -308,67 +304,67 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [MultipleAsserts] public void CanSubtractScalarFromVectorUsingResultVector() { - var vector = CreateVector(_data); - var result = CreateVector(_data.Length); + var vector = this.CreateVector(this._data); + var result = this.CreateVector(this._data.Length); vector.Subtract(2.0, result); - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._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]); + Assert.AreEqual(this._data[i], vector[i], "Making sure the original vector wasn't modified."); + Assert.AreEqual(this._data[i] - 2.0, result[i]); } vector.Subtract(0.0, result); - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i], result[i]); + Assert.AreEqual(this._data[i], result[i]); } } [Test] public void ThrowsArgumentNullExceptionWhenSubtractingScalarWithNullResultVector() { - var vector = CreateVector(_data.Length); + var vector = this.CreateVector(this._data.Length); Assert.Throws(() => vector.Subtract(0.0, null)); } [Test] public void ThrowsArgumentExceptionWhenSubtractingScalarWithWrongSizeResultVector() { - var vector = CreateVector(_data.Length); - var result = CreateVector(_data.Length + 1); + var vector = this.CreateVector(this._data.Length); + var result = this.CreateVector(this._data.Length + 1); Assert.Throws(() => vector.Subtract(0.0, result)); } [Test] public void ThrowsArgumentNullExceptionWhenSubtractingTwoVectorsAndOneIsNull() { - var vector = CreateVector(_data); + var vector = this.CreateVector(this._data); Assert.Throws(() => vector.Subtract(null)); } [Test] public void ThrowsArgumentExceptionWhenSubtractingTwoVectorsOfDifferingSize() { - var vector = CreateVector(_data.Length); - var other = CreateVector(_data.Length + 1); + var vector = this.CreateVector(this._data.Length); + var other = this.CreateVector(this._data.Length + 1); Assert.Throws(() => vector.Subtract(other)); } [Test] public void ThrowsArgumentNullExceptionWhenSubtractingTwoVectorsAndResultIsNull() { - var vector = CreateVector(_data.Length); - var other = CreateVector(_data.Length + 1); + var vector = this.CreateVector(this._data.Length); + var other = this.CreateVector(this._data.Length + 1); Assert.Throws(() => vector.Subtract(other, null)); } [Test] public void ThrowsArgumentExceptionWhenSubtractingTwoVectorsAndResultIsDifferentSize() { - var vector = CreateVector(_data.Length); - var other = CreateVector(_data.Length); - var result = CreateVector(_data.Length + 1); + var vector = this.CreateVector(this._data.Length); + var other = this.CreateVector(this._data.Length); + var result = this.CreateVector(this._data.Length + 1); Assert.Throws(() => vector.Subtract(other, result)); } @@ -376,7 +372,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double public void SubtractionOperatorThrowsArgumentNullExpectionIfAVectorIsNull() { Vector a = null; - var b = CreateVector(_data.Length); + var b = this.CreateVector(this._data.Length); Assert.Throws(() => a -= b); a = b; @@ -387,19 +383,19 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [Test] public void SubtractionOperatorThrowsArgumentExpectionIfVectorsAreDifferentSize() { - var a = CreateVector(_data.Length); - var b = CreateVector(_data.Length + 1); + var a = this.CreateVector(this._data.Length); + var b = this.CreateVector(this._data.Length + 1); Assert.Throws(() => a -= b); } [Test] public void CanSubtractTwoVectors() { - var vector = CreateVector(_data); - var other = CreateVector(_data); + var vector = this.CreateVector(this._data); + var other = this.CreateVector(this._data); vector.Subtract(other); - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { Assert.AreEqual(0.0, vector[i]); } @@ -409,15 +405,15 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [MultipleAsserts] public void CanSubtractTwoVectorsUsingResultVector() { - var vector = CreateVector(_data); - var other = CreateVector(_data); - var result = CreateVector(_data.Length); + var vector = this.CreateVector(this._data); + var other = this.CreateVector(this._data); + var result = this.CreateVector(this._data.Length); vector.Subtract(other, result); - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i], vector[i], "Making sure the original vector wasn't modified."); - Assert.AreEqual(_data[i], other[i], "Making sure the original vector wasn't modified."); + Assert.AreEqual(this._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(0.0, result[i]); } } @@ -426,14 +422,14 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [MultipleAsserts] public void CanSubtractTwoVectorsUsingOperator() { - var vector = CreateVector(_data); - var other = CreateVector(_data); + var vector = this.CreateVector(this._data); + var other = this.CreateVector(this._data); var result = vector - other; - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i], vector[i], "Making sure the original vector wasn't modified."); - Assert.AreEqual(_data[i], other[i], "Making sure the original vector wasn't modified."); + Assert.AreEqual(this._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(0.0, result[i]); } } @@ -441,10 +437,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [Test] public void CanSubtractVectorFromItself() { - var vector = CreateVector(_data); + var vector = this.CreateVector(this._data); vector.Subtract(vector); - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { Assert.AreEqual(0.0, vector[i]); } @@ -454,13 +450,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [MultipleAsserts] public void CanSubtractVectorFromItselfUsingResultVector() { - var vector = CreateVector(_data); - var result = CreateVector(_data.Length); + var vector = this.CreateVector(this._data); + var result = this.CreateVector(this._data.Length); vector.Subtract(vector, result); - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i], vector[i], "Making sure the original vector wasn't modified."); + Assert.AreEqual(this._data[i], vector[i], "Making sure the original vector wasn't modified."); Assert.AreEqual(0.0, result[i]); } } @@ -469,13 +465,13 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [MultipleAsserts] public void CanSubtractTwoVectorsUsingItselfAsResultVector() { - var vector = CreateVector(_data); - var other = CreateVector(_data); + var vector = this.CreateVector(this._data); + var other = this.CreateVector(this._data); vector.Subtract(other, vector); - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i], other[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(0.0, vector[i]); } } @@ -484,18 +480,18 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [MultipleAsserts] public void CanDivideVectorByScalar() { - var vector = CreateVector(_data); + var vector = this.CreateVector(this._data); vector.Divide(2.0); - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i] / 2.0, vector[i]); + Assert.AreEqual(this._data[i] / 2.0, vector[i]); } vector.Divide(1.0); - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i] / 2.0, vector[i]); + Assert.AreEqual(this._data[i] / 2.0, vector[i]); } } @@ -503,20 +499,20 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [MultipleAsserts] public void CanDivideVectorByScalarUsingResultVector() { - var vector = CreateVector(_data); - var result = CreateVector(_data.Length); + var vector = this.CreateVector(this._data); + var result = this.CreateVector(this._data.Length); vector.Divide(2.0, result); - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._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]); + Assert.AreEqual(this._data[i], vector[i], "Making sure the original vector wasn't modified."); + Assert.AreEqual(this._data[i] / 2.0, result[i]); } vector.Divide(1.0, result); - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i], result[i]); + Assert.AreEqual(this._data[i], result[i]); } } @@ -524,18 +520,18 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [MultipleAsserts] public void CanMultiplyVectorByScalar() { - var vector = CreateVector(_data); + var vector = this.CreateVector(this._data); vector.Multiply(2.0); - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i] * 2.0, vector[i]); + Assert.AreEqual(this._data[i] * 2.0, vector[i]); } vector.Multiply(1.0); - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i] * 2.0, vector[i]); + Assert.AreEqual(this._data[i] * 2.0, vector[i]); } } @@ -543,50 +539,50 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [MultipleAsserts] public void CanMultiplyVectorByScalarUsingResultVector() { - var vector = CreateVector(_data); - var result = CreateVector(_data.Length); + var vector = this.CreateVector(this._data); + var result = this.CreateVector(this._data.Length); vector.Multiply(2.0, result); - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._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]); + Assert.AreEqual(this._data[i], vector[i], "Making sure the original vector wasn't modified."); + Assert.AreEqual(this._data[i] * 2.0, result[i]); } vector.Multiply(1.0, result); - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i], result[i]); + Assert.AreEqual(this._data[i], result[i]); } } [Test] public void ThrowsArgumentNullExceptionWhenMultiplyingScalarWithNullResultVector() { - var vector = CreateVector(_data.Length); + var vector = this.CreateVector(this._data.Length); Assert.Throws(() => vector.Multiply(1.0, null)); } [Test] public void ThrowsArgumentNullExceptionWhenDividingScalarWithNullResultVector() { - var vector = CreateVector(_data.Length); + var vector = this.CreateVector(this._data.Length); Assert.Throws(() => vector.Divide(1.0, null)); } [Test] public void ThrowsArgumentExceptionWhenMultiplyingScalarWithWrongSizeResultVector() { - var vector = CreateVector(_data.Length); - var result = CreateVector(_data.Length + 1); + var vector = this.CreateVector(this._data.Length); + var result = this.CreateVector(this._data.Length + 1); Assert.Throws(() => vector.Multiply(0.0, result)); } [Test] public void ThrowsArgumentExceptionWhenDividingScalarWithWrongSizeResultVector() { - var vector = CreateVector(_data.Length); - var result = CreateVector(_data.Length + 1); + var vector = this.CreateVector(this._data.Length); + var result = this.CreateVector(this._data.Length + 1); Assert.Throws(() => vector.Divide(0.0, result)); } @@ -594,32 +590,32 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [MultipleAsserts] public void CanMultiplyVectorByScalarUsingOperators() { - var vector = CreateVector(_data); + var vector = this.CreateVector(this._data); vector = vector * 2.0; - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i] * 2.0, vector[i]); + Assert.AreEqual(this._data[i] * 2.0, vector[i]); } vector = vector * 1.0; - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i] * 2.0, vector[i]); + Assert.AreEqual(this._data[i] * 2.0, vector[i]); } - vector = CreateVector(_data); + vector = this.CreateVector(this._data); vector = 2.0 * vector; - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i] * 2.0, vector[i]); + Assert.AreEqual(this._data[i] * 2.0, vector[i]); } vector = 1.0 * vector; - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i] * 2.0, vector[i]); + Assert.AreEqual(this._data[i] * 2.0, vector[i]); } } @@ -627,18 +623,18 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [MultipleAsserts] public void CanDivideVectorByScalarUsingOperators() { - var vector = CreateVector(_data); + var vector = this.CreateVector(this._data); vector = vector / 2.0; - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i] / 2.0, vector[i]); + Assert.AreEqual(this._data[i] / 2.0, vector[i]); } vector = vector / 1.0; - for (var i = 0; i < _data.Length; i++) + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(_data[i] / 2.0, vector[i]); + Assert.AreEqual(this._data[i] / 2.0, vector[i]); } } @@ -662,8 +658,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [Test] public void CanDotProduct() { - Vector dataA = CreateVector(_data); - Vector dataB = CreateVector(_data); + var dataA = this.CreateVector(this._data); + var dataB = this.CreateVector(this._data); Assert.AreEqual(55.0, dataA.DotProduct(dataB)); } @@ -672,7 +668,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [ExpectedArgumentNullException] public void DotProductThrowsExceptionWhenArgumentIsNull() { - Vector dataA = CreateVector(_data); + var dataA = this.CreateVector(this._data); Vector dataB = null; dataA.DotProduct(dataB); @@ -682,8 +678,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [ExpectedArgumentException] public void DotProductThrowsExceptionWhenArgumentHasDifferentSize() { - Vector dataA = CreateVector(_data); - Vector dataB = CreateVector(new double[] {1, 2, 3, 4, 5, 6}); + var dataA = this.CreateVector(this._data); + var dataB = this.CreateVector(new double[] { 1, 2, 3, 4, 5, 6 }); dataA.DotProduct(dataB); } @@ -691,8 +687,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [Test] public void CanDotProductUsingOperator() { - Vector dataA = CreateVector(_data); - Vector dataB = CreateVector(_data); + var dataA = this.CreateVector(this._data); + var dataB = this.CreateVector(this._data); Assert.AreEqual(55.0, dataA * dataB); } @@ -701,10 +697,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [ExpectedArgumentNullException] public void OperatorDotProductThrowsExceptionWhenLeftArgumentIsNull() { - Vector dataA = CreateVector(_data); + var dataA = this.CreateVector(this._data); Vector dataB = null; - double d = dataA * dataB; + var d = dataA * dataB; } [Test] @@ -712,31 +708,31 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double public void OperatorDotProductThrowsExceptionWhenRightArgumentIsNull() { Vector dataA = null; - Vector dataB = CreateVector(_data); + var dataB = this.CreateVector(this._data); - double d = dataA * dataB; + var d = dataA * dataB; } [Test] [ExpectedArgumentException] public void OperatorDotProductThrowsExceptionWhenArgumentHasDifferentSize() { - Vector dataA = CreateVector(_data); - Vector dataB = CreateVector(new double[] { 1, 2, 3, 4, 5, 6 }); + var dataA = this.CreateVector(this._data); + var dataB = this.CreateVector(new double[] { 1, 2, 3, 4, 5, 6 }); - double d = dataA * dataB; + var d = dataA * dataB; } [Test] public void PointWiseMultiply() { - Vector vector1 = CreateVector(_data); - Vector vector2 = vector1.Clone(); - Vector result = CreateVector(vector1.Count); + var vector1 = this.CreateVector(this._data); + var vector2 = vector1.Clone(); + var result = CreateVector(vector1.Count); vector1.PointWiseMultiply(vector2, result); - for (int i = 0; i < vector1.Count; i++) + for (var i = 0; i < vector1.Count; i++) { - Assert.AreEqual(_data[i] * _data[i], result[i]); + Assert.AreEqual(this._data[i] * this._data[i], result[i]); } } @@ -744,9 +740,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [ExpectedArgumentNullException] public void PointWiseMultiplyWithOtherNullShouldThrowException() { - Vector vector1 = CreateVector(_data); + var vector1 = this.CreateVector(this._data); Vector vector2 = null; - Vector result = CreateVector(vector1.Count); + var result = CreateVector(vector1.Count); vector1.PointWiseMultiply(vector2, result); } @@ -754,8 +750,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [ExpectedArgumentNullException] public void PointWiseMultiplyWithResultNullShouldThrowException() { - Vector vector1 = CreateVector(_data); - Vector vector2 = vector1.Clone(); + var vector1 = this.CreateVector(this._data); + var vector2 = vector1.Clone(); Vector result = null; vector1.PointWiseMultiply(vector2, result); } @@ -764,34 +760,34 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [ExpectedArgumentException] public void PointWiseMultiplyWithInvalidResultLengthShouldThrowException() { - Vector vector1 = CreateVector(_data); - Vector vector2 = vector1.Clone(); - Vector result = CreateVector(vector1.Count + 1); + var vector1 = this.CreateVector(this._data); + var vector2 = vector1.Clone(); + var result = this.CreateVector(vector1.Count + 1); vector1.PointWiseMultiply(vector2, result); } [Test] public void PointWiseMultiplyWithResult() { - Vector vector1 = CreateVector(_data); - Vector vector2 = vector1.Clone(); - Vector result = vector1.PointWiseMultiply(vector2); - for (int i = 0; i < vector1.Count; i++) + var vector1 = this.CreateVector(this._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]); + Assert.AreEqual(this._data[i] * this._data[i], result[i]); } } [Test] public void PointWiseAdd() { - Vector vector1 = CreateVector(_data); - Vector vector2 = vector1.Clone(); - Vector result = CreateVector(vector1.Count); + var vector1 = this.CreateVector(this._data); + var vector2 = vector1.Clone(); + var result = CreateVector(vector1.Count); vector1.PointWiseAdd(vector2, result); - for (int i = 0; i < vector1.Count; i++) + for (var i = 0; i < vector1.Count; i++) { - Assert.AreEqual(_data[i] + _data[i], result[i]); + Assert.AreEqual(this._data[i] + this._data[i], result[i]); } } @@ -799,9 +795,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [ExpectedArgumentNullException] public void PointWiseAddWithOtherNullShouldThrowException() { - Vector vector1 = CreateVector(_data); + var vector1 = this.CreateVector(this._data); Vector vector2 = null; - Vector result = CreateVector(vector1.Count); + var result = CreateVector(vector1.Count); vector1.PointWiseAdd(vector2, result); } @@ -809,8 +805,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [ExpectedArgumentNullException] public void PointWiseAddWithResultNullShouldThrowException() { - Vector vector1 = CreateVector(_data); - Vector vector2 = vector1.Clone(); + var vector1 = this.CreateVector(this._data); + var vector2 = vector1.Clone(); Vector result = null; vector1.PointWiseAdd(vector2, result); } @@ -819,34 +815,34 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [ExpectedArgumentException] public void PointWiseAddWithInvalidResultLengthShouldThrowException() { - Vector vector1 = CreateVector(_data); - Vector vector2 = vector1.Clone(); - Vector result = CreateVector(vector1.Count + 1); + var vector1 = this.CreateVector(this._data); + var vector2 = vector1.Clone(); + var result = this.CreateVector(vector1.Count + 1); vector1.PointWiseAdd(vector2, result); } [Test] public void PointWiseAddWithResult() { - Vector vector1 = CreateVector(_data); - Vector vector2 = vector1.Clone(); - Vector result = vector1.PointWiseAdd(vector2); - for (int i = 0; i < vector1.Count; i++) + var vector1 = this.CreateVector(this._data); + var vector2 = vector1.Clone(); + var result = vector1.PointWiseAdd(vector2); + for (var i = 0; i < vector1.Count; i++) { - Assert.AreEqual(_data[i] + _data[i], result[i]); + Assert.AreEqual(this._data[i] + this._data[i], result[i]); } } [Test] public void PointWiseSubtract() { - Vector vector1 = CreateVector(_data); - Vector vector2 = vector1.Clone(); - Vector result = CreateVector(vector1.Count); + var vector1 = this.CreateVector(this._data); + var vector2 = vector1.Clone(); + var result = CreateVector(vector1.Count); vector1.PointWiseSubtract(vector2, result); - for (int i = 0; i < vector1.Count; i++) + for (var i = 0; i < vector1.Count; i++) { - Assert.AreEqual(_data[i] - _data[i], result[i]); + Assert.AreEqual(this._data[i] - this._data[i], result[i]); } } @@ -854,9 +850,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [ExpectedArgumentNullException] public void PointWiseSubtractWithOtherNullShouldThrowException() { - Vector vector1 = CreateVector(_data); + var vector1 = this.CreateVector(this._data); Vector vector2 = null; - Vector result = CreateVector(vector1.Count); + var result = CreateVector(vector1.Count); vector1.PointWiseSubtract(vector2, result); } @@ -864,8 +860,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [ExpectedArgumentNullException] public void PointWiseSubtractWithResultNullShouldThrowException() { - Vector vector1 = CreateVector(_data); - Vector vector2 = vector1.Clone(); + var vector1 = this.CreateVector(this._data); + var vector2 = vector1.Clone(); Vector result = null; vector1.PointWiseSubtract(vector2, result); } @@ -874,34 +870,34 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [ExpectedArgumentException] public void PointWiseSubtractWithInvalidResultLengthShouldThrowException() { - Vector vector1 = CreateVector(_data); - Vector vector2 = vector1.Clone(); - Vector result = CreateVector(vector1.Count + 1); + var vector1 = this.CreateVector(this._data); + var vector2 = vector1.Clone(); + var result = this.CreateVector(vector1.Count + 1); vector1.PointWiseSubtract(vector2, result); } [Test] public void PointWiseSubtractWithResult() { - Vector vector1 = CreateVector(_data); - Vector vector2 = vector1.Clone(); - Vector result = vector1.PointWiseSubtract(vector2); - for (int i = 0; i < vector1.Count; i++) + var vector1 = this.CreateVector(this._data); + var vector2 = vector1.Clone(); + var result = vector1.PointWiseSubtract(vector2); + for (var i = 0; i < vector1.Count; i++) { - Assert.AreEqual(_data[i] - _data[i], result[i]); + Assert.AreEqual(this._data[i] - this._data[i], result[i]); } } [Test] public void PointWiseDivide() { - Vector vector1 = CreateVector(_data); - Vector vector2 = vector1.Clone(); - Vector result = CreateVector(vector1.Count); + var vector1 = this.CreateVector(this._data); + var vector2 = vector1.Clone(); + var result = CreateVector(vector1.Count); vector1.PointWiseDivide(vector2, result); - for (int i = 0; i < vector1.Count; i++) + for (var i = 0; i < vector1.Count; i++) { - Assert.AreEqual(_data[i] / _data[i], result[i]); + Assert.AreEqual(this._data[i] / this._data[i], result[i]); } } @@ -909,9 +905,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [ExpectedArgumentNullException] public void PointWiseDivideWithOtherNullShouldThrowException() { - Vector vector1 = CreateVector(_data); + var vector1 = this.CreateVector(this._data); Vector vector2 = null; - Vector result = CreateVector(vector1.Count); + var result = CreateVector(vector1.Count); vector1.PointWiseDivide(vector2, result); } @@ -919,8 +915,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [ExpectedArgumentNullException] public void PointWiseDivideWithResultNullShouldThrowException() { - Vector vector1 = CreateVector(_data); - Vector vector2 = vector1.Clone(); + var vector1 = this.CreateVector(this._data); + var vector2 = vector1.Clone(); Vector result = null; vector1.PointWiseDivide(vector2, result); } @@ -929,134 +925,33 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [ExpectedArgumentException] public void PointWiseDivideWithInvalidResultLengthShouldThrowException() { - Vector vector1 = CreateVector(_data); - Vector vector2 = vector1.Clone(); - Vector result = CreateVector(vector1.Count + 1); + var vector1 = this.CreateVector(this._data); + var vector2 = vector1.Clone(); + var result = this.CreateVector(vector1.Count + 1); vector1.PointWiseDivide(vector2, result); } [Test] public void PointWiseDivideWithResult() { - Vector vector1 = CreateVector(_data); - Vector vector2 = vector1.Clone(); - Vector result = vector1.PointWiseDivide(vector2); - for (int i = 0; i < vector1.Count; i++) - { - Assert.AreEqual(_data[i] / _data[i], result[i]); - } - } - - [Test] - public void CanFindAbsoluteMinimumIndex() - { - Vector source = CreateVector(_data); - int expected = 0; - int actual = source.AbsoluteMinimumIndex(); - Assert.AreEqual(expected, actual); - } - - [Test] - public void CanFindAbsoluteMinimum() - { - Vector source = CreateVector(_data); - double expected = 1; - double actual = source.AbsoluteMinimum(); - Assert.AreEqual(expected, actual); - - } - - [Test] - public void CanFindMaximumIndex() - { - Vector vector = CreateVector(_data); - - int expected = 4; - int actual = vector.MaximumIndex(); - - Assert.AreEqual(expected, actual); - } - - [Test] - public void CanFindMaximum() - { - Vector vector = CreateVector(_data); - - double expected = 5; - double actual = vector.Maximum(); - - Assert.AreEqual(expected, actual); - } - - [Test] - public void CanFindMinimumIndex() - { - Vector vector = CreateVector(_data); - - int expected = 0; - int actual = vector.MinimumIndex(); - - Assert.AreEqual(expected, actual); - } - - [Test] - public void CanFindMinimum() - { - Vector vector = CreateVector(_data); - - double expected = 1; - double actual = vector.Minimum(); - - Assert.AreEqual(expected, actual); - } - - [Test] - [Row(0, 5)] - [Row(2, 2)] - [Row(1, 4)] - [Row(6, 10, ExpectedException = typeof(ArgumentOutOfRangeException))] - [Row(1, 10, ExpectedException = typeof(ArgumentOutOfRangeException))] - [Row(1, -10, ExpectedException = typeof(ArgumentOutOfRangeException))] - public void SubVector(int index, int length) - { - Vector vector = CreateVector(_data); - Vector sub = vector.SubVector(index, length); - Assert.AreEqual(length, sub.Count); - for (int i = 0; i < length; i++) + var vector1 = this.CreateVector(this._data); + var vector2 = vector1.Clone(); + var result = vector1.PointWiseDivide(vector2); + for (var i = 0; i < vector1.Count; i++) { - Assert.AreEqual(vector[i + index], sub[i]); + Assert.AreEqual(this._data[i] / this._data[i], result[i]); } } - [Test] - public void Sum() - { - double[] testData = { -20, -10, 10, 20, 30, }; - Vector vector = CreateVector(testData); - double actual = vector.Sum(); - double expected = 30; - Assert.AreEqual(expected, actual); - } - - [Test] - public void SumMagnitudes() - { - double[] testData = { -20, -10, 10, 20, 30, }; - Vector vector = CreateVector(testData); - double actual = vector.SumMagnitudes(); - double expected = 90; - Assert.AreEqual(expected, actual); - } - [Test] public void CanCalculateDyadicProduct() { - Vector vector1 = CreateVector(_data); - Vector vector2 = CreateVector(_data); - Matrix m = Vector.DyadicProduct(vector1,vector2); - for (int i = 0; i < vector1.Count; i++) + var vector1 = this.CreateVector(this._data); + var vector2 = this.CreateVector(this._data); + Matrix m = Vector.DyadicProduct(vector1, vector2); + for (var i = 0; i < vector1.Count; i++) { - for (int j = 0; j < vector2.Count; j++) + for (var j = 0; j < vector2.Count; j++) { Assert.AreEqual(m[i, j], vector1[i] * vector2[j]); } @@ -1068,7 +963,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double public void DyadicProductWithFirstParameterNullShouldThrowException() { Vector vector1 = null; - Vector vector2 = CreateVector(_data); + var vector2 = this.CreateVector(this._data); Vector.DyadicProduct(vector1, vector2); } @@ -1076,7 +971,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [ExpectedArgumentNullException] public void DyadicProductWithSecondParameterNullShouldThrowException() { - Vector vector1 = CreateVector(_data); + var vector1 = this.CreateVector(this._data); Vector vector2 = null; Vector.DyadicProduct(vector1, vector2); } @@ -1084,12 +979,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [Test] public void CanCalculateTensorMultiply() { - Vector vector1 = CreateVector(_data); - Vector vector2 = CreateVector(_data); - Matrix m = vector1.TensorMultiply(vector2); - for (int i = 0; i < vector1.Count; i++) + var vector1 = this.CreateVector(this._data); + var vector2 = this.CreateVector(this._data); + var m = vector1.TensorMultiply(vector2); + for (var i = 0; i < vector1.Count; i++) { - for (int j = 0; j < vector2.Count; j++) + for (var j = 0; j < vector2.Count; j++) { Assert.AreEqual(m[i, j], vector1[i] * vector2[j]); } @@ -1100,19 +995,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [ExpectedArgumentNullException] public void TensorMultiplyWithNullParameterNullShouldThrowException() { - Vector vector1 = CreateVector(_data); + var vector1 = this.CreateVector(this._data); Vector vector2 = null; vector1.TensorMultiply(vector2); } - - [Test] - [ExpectedArgumentException] - public void RandomWithNumberOfElementsLessThanZeroShouldThrowException() - { - Vector vector1 = CreateVector(_data); - Vector vector2 = vector1.Random(-5); - } - - } } \ No newline at end of file diff --git a/src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs b/src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs index d7a626bd..2505df18 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs @@ -3,9 +3,7 @@ // 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 @@ -14,10 +12,8 @@ // 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 @@ -30,28 +26,29 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double { - using System; - using System.Collections.Generic; - using System.Globalization; - using LinearAlgebra.Double; - using MbUnit.Framework; + using System; using System.Collections; + using System.Collections.Generic; + using System.Globalization; + using Distributions; + using LinearAlgebra.Double; + using MbUnit.Framework; [TestFixture] public abstract partial class VectorTests { - protected readonly double[] _data = {1, 2, 3, 4, 5}; + protected readonly double[] _data = { 1, 2, 3, 4, 5 }; [Test] [MultipleAsserts] public void CanCloneVector() { - var vector = CreateVector(_data); + var vector = this.CreateVector(this._data); var clone = vector.Clone(); Assert.AreNotSame(vector, clone); Assert.AreEqual(vector.Count, clone.Count); - for (var index = 0; index < _data.Length; index++) + for (var index = 0; index < this._data.Length; index++) { Assert.AreEqual(vector[index], clone[index]); } @@ -61,12 +58,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [MultipleAsserts] public void CanCloneVectorUsingICloneable() { - var vector = CreateVector(_data); + var vector = this.CreateVector(this._data); var clone = (Vector)((ICloneable)vector).Clone(); Assert.AreNotSame(vector, clone); Assert.AreEqual(vector.Count, clone.Count); - for (var index = 0; index < _data.Length; index++) + for (var index = 0; index < this._data.Length; index++) { Assert.AreEqual(vector[index], clone[index]); } @@ -75,7 +72,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [Test] public void CanConvertVectorToString() { - var vector = CreateVector(_data); + var vector = this.CreateVector(this._data); var str = vector.ToString(); var sep = CultureInfo.CurrentCulture.TextInfo.ListSeparator; Assert.AreEqual(string.Format("1{0}2{0}3{0}4{0}5", sep), str); @@ -85,8 +82,8 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [MultipleAsserts] public void CanCopyPartialVectorToAnother() { - var vector = CreateVector(_data); - var other = CreateVector(_data.Length); + var vector = this.CreateVector(this._data); + var other = this.CreateVector(this._data.Length); vector.CopyTo(other, 2, 2, 2); @@ -101,7 +98,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [MultipleAsserts] public void CanCopyPartialVectorToSelf() { - var vector = CreateVector(_data); + var vector = this.CreateVector(this._data); vector.CopyTo(vector, 0, 2, 2); Assert.AreEqual(1.0, vector[0]); @@ -115,12 +112,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [MultipleAsserts] public void CanCopyVectorToAnother() { - var vector = CreateVector(_data); - var other = CreateVector(_data.Length); + var vector = this.CreateVector(this._data); + var other = this.CreateVector(this._data.Length); vector.CopyTo(other); - for (var index = 0; index < _data.Length; index++) + for (var index = 0; index < this._data.Length; index++) { Assert.AreEqual(vector[index], other[index]); } @@ -135,7 +132,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [Test] public void CanCreateVector() { - var expected = CreateVector(5); + var expected = this.CreateVector(5); var actual = expected.CreateVector(5); Assert.AreEqual(expected.GetType(), actual.GetType(), "vectors are same type."); } @@ -143,20 +140,19 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [Test] public void CanEnumerateOverVector() { - var vector = CreateVector(_data); - Assert.AreElementsEqual(_data, vector); - + var vector = this.CreateVector(this._data); + Assert.AreElementsEqual(this._data, vector); } [Test] [MultipleAsserts] public void CanEnumerateOverVectorUsingIEnumerable() { - var enumerable = (IEnumerable)CreateVector(_data); + var enumerable = (IEnumerable)this.CreateVector(this._data); var index = 0; foreach (var element in enumerable) { - Assert.AreEqual(_data[index++], (double)element); + Assert.AreEqual(this._data[index++], (double)element); } } @@ -164,9 +160,9 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [MultipleAsserts] public void CanEquateVectors() { - var vector1 = CreateVector(_data); - var vector2 = CreateVector(_data); - var vector3 = CreateVector(4); + var vector1 = this.CreateVector(this._data); + var vector2 = this.CreateVector(this._data); + var vector3 = this.CreateVector(4); Assert.IsTrue(vector1.Equals(vector1)); Assert.IsTrue(vector1.Equals(vector2)); Assert.IsFalse(vector1.Equals(vector3)); @@ -177,71 +173,71 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double [MultipleAsserts] public void ThrowsArgumentExceptionIfSizeIsNotPositive() { - Assert.Throws(() => CreateVector(-1)); - Assert.Throws(() => CreateVector(0)); + Assert.Throws(() => this.CreateVector(-1)); + Assert.Throws(() => this.CreateVector(0)); } [Test] public void TestingForEqualityWithNonVectorReturnsFalse() { - var vector = CreateVector(_data); + var vector = this.CreateVector(this._data); Assert.IsFalse(vector.Equals(2)); } [Test] public void CanTestForEqualityUsingObjectEquals() { - var vector1 = CreateVector(_data); - var vector2 = CreateVector(_data); + var vector1 = this.CreateVector(this._data); + var vector2 = this.CreateVector(this._data); Assert.IsTrue(vector1.Equals((object)vector2)); } [Test] public void VectorGetHashCode() { - var vector = CreateVector(new double[] { 1, 2, 3, 4 }); + var vector = this.CreateVector(new double[] { 1, 2, 3, 4 }); Assert.AreEqual(2145910784, vector.GetHashCode()); } [Test] public void GetIndexedEnumerator() { - Vector vector = CreateVector(_data); - foreach (KeyValuePair pair in vector.GetIndexedEnumerator()) + var vector = this.CreateVector(this._data); + foreach (var pair in vector.GetIndexedEnumerator()) { - Assert.AreEqual(_data[pair.Key], pair.Value); + Assert.AreEqual(this._data[pair.Key], pair.Value); } } [Test] public void CanConvertVectorToArray() { - var vector = CreateVector(_data); + var vector = this.CreateVector(this._data); var array = vector.ToArray(); Assert.AreElementsEqual(vector, array); } - [Test] + [Test, Ignore] [MultipleAsserts] public void CanConvertVectorToColumnMatrix() { - var vector = CreateVector(_data); + var vector = this.CreateVector(this._data); var matrix = vector.ToColumnMatrix(); - + Assert.AreEqual(vector.Count, matrix.RowCount); Assert.AreEqual(1, matrix.ColumnCount); - - for(var i = 0; i < vector.Count; i++) + + for (var i = 0; i < vector.Count; i++) { Assert.AreEqual(vector[i], matrix[i, 0]); } } - [Test] + [Test, Ignore] [MultipleAsserts] public void CanConvertVectorToRowMatrix() { - var vector = CreateVector(_data); + var vector = this.CreateVector(this._data); var matrix = vector.ToRowMatrix(); Assert.AreEqual(vector.Count, matrix.ColumnCount); @@ -252,32 +248,159 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double Assert.AreEqual(vector[i], matrix[0, i]); } } - + [Test] public void CanSetValues() { - Vector vector = CreateVector(_data); - vector.SetValues(_data); - for (int i = 0; i < _data.Length; i++) + var vector = this.CreateVector(this._data); + vector.SetValues(this._data); + for (var i = 0; i < this._data.Length; i++) { - Assert.AreEqual(vector[i], _data[i]); + Assert.AreEqual(vector[i], this._data[i]); } } + [Test] + [Row(0, 5)] + [Row(2, 2)] + [Row(1, 4)] + [Row(6, 10, ExpectedException = typeof(ArgumentOutOfRangeException))] + [Row(1, 10, ExpectedException = typeof(ArgumentOutOfRangeException))] + [Row(1, -10, ExpectedException = typeof(ArgumentOutOfRangeException))] + public void CanCalculateSubVector(int index, int length) + { + var vector = this.CreateVector(this._data); + var sub = vector.SubVector(index, length); + Assert.AreEqual(length, sub.Count); + for (var i = 0; i < length; i++) + { + Assert.AreEqual(vector[i + index], sub[i]); + } + } + + [Test] + public void CanFindAbsoluteMinimumIndex() + { + var source = this.CreateVector(this._data); + var expected = 0; + var actual = source.AbsoluteMinimumIndex(); + Assert.AreEqual(expected, actual); + } + + [Test] + public void CanFindAbsoluteMinimum() + { + var source = this.CreateVector(this._data); + double expected = 1; + var actual = source.AbsoluteMinimum(); + Assert.AreEqual(expected, actual); + } + + [Test] + public void CanFindMaximumIndex() + { + var vector = this.CreateVector(this._data); + + var expected = 4; + var actual = vector.MaximumIndex(); + + Assert.AreEqual(expected, actual); + } + + [Test] + public void CanFindMaximum() + { + var vector = this.CreateVector(this._data); + + double expected = 5; + var actual = vector.Maximum(); + + Assert.AreEqual(expected, actual); + } + + [Test] + public void CanFindMinimumIndex() + { + var vector = this.CreateVector(this._data); + + var expected = 0; + var actual = vector.MinimumIndex(); + + Assert.AreEqual(expected, actual); + } + + [Test] + public void CanFindMinimum() + { + var vector = this.CreateVector(this._data); + + double expected = 1; + var actual = vector.Minimum(); + + Assert.AreEqual(expected, actual); + } + + [Test] + [Row(0, 5)] + [Row(2, 2)] + [Row(1, 4)] + [Row(6, 10, ExpectedException = typeof(ArgumentOutOfRangeException))] + [Row(1, 10, ExpectedException = typeof(ArgumentOutOfRangeException))] + [Row(1, -10, ExpectedException = typeof(ArgumentOutOfRangeException))] + public void CanGetSubVector(int index, int length) + { + var vector = this.CreateVector(this._data); + var sub = vector.SubVector(index, length); + Assert.AreEqual(length, sub.Count); + for (var i = 0; i < length; i++) + { + Assert.AreEqual(vector[i + index], sub[i]); + } + } + + [Test] + public void CanSum() + { + double[] testData = { -20, -10, 10, 20, 30, }; + var vector = CreateVector(testData); + var actual = vector.Sum(); + double expected = 30; + Assert.AreEqual(expected, actual); + } + + [Test] + public void CanSumMagnitudes() + { + double[] testData = { -20, -10, 10, 20, 30, }; + var vector = CreateVector(testData); + var actual = vector.SumMagnitudes(); + double expected = 90; + Assert.AreEqual(expected, actual); + } + [Test] [ExpectedArgumentNullException] - public void SetValuesWithNullParameterShouldThrowException() + public void CanSetValuesWithNullParameterShouldThrowException() { - Vector vector = CreateVector(_data); + var vector = this.CreateVector(this._data); vector.SetValues(null); } [Test] [ExpectedArgumentException] - public void SetValuesWithNonEqualDataLengthShouldThrowException() + public void CanSetValuesWithNonEqualDataLengthShouldThrowException() + { + var vector = this.CreateVector(this._data.Length + 2); + vector.SetValues(this._data); + } + + + [Test] + [ExpectedArgumentException] + public void RandomWithNumberOfElementsLessThanZeroShouldThrowException() { - Vector vector = CreateVector(_data.Length + 2); - vector.SetValues(_data); + var vector = this.CreateVector(4); + vector = vector.Random(-3, new ContinuousUniform()); } protected abstract Vector CreateVector(int size);