From c62ba47988e4ca0eb62a7bd046eec8bd0549dad0 Mon Sep 17 00:00:00 2001 From: Marcus Cuda Date: Sun, 27 Jun 2010 15:44:21 +0800 Subject: [PATCH] merged Andriy's sparse optimizations a little clean up --- .../LinearAlgebra/Double/DenseMatrix.cs | 62 +- .../LinearAlgebra/Double/DenseVector.cs | 202 +++--- .../LinearAlgebra/Double/Matrix.Arithmetic.cs | 149 +++-- src/Numerics/LinearAlgebra/Double/Matrix.cs | 384 ++++++------ .../LinearAlgebra/Double/SparseMatrix.cs | 583 +++++++++--------- .../LinearAlgebra/Double/SparseVector.cs | 309 +++++----- src/Numerics/LinearAlgebra/Double/Vector.cs | 204 +++--- .../Double/SparseVectorTest.cs | 6 - .../LinearAlgebraTests/Double/VectorTests.cs | 12 + 9 files changed, 974 insertions(+), 937 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs index 1d6c375d..7e6e1829 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs @@ -44,7 +44,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double public DenseMatrix(int order) : base(order) { - this.Data = new double[order * order]; + Data = new double[order * order]; } /// @@ -59,7 +59,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double public DenseMatrix(int rows, int columns) : base(rows, columns) { - this.Data = new double[rows * columns]; + Data = new double[rows * columns]; } /// @@ -75,10 +75,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double public DenseMatrix(int rows, int columns, double value) : base(rows, columns) { - this.Data = new double[rows * columns]; - for (var i = 0; i < this.Data.Length; i++) + Data = new double[rows * columns]; + for (var i = 0; i < Data.Length; i++) { - this.Data[i] = value; + Data[i] = value; } } @@ -92,7 +92,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double public DenseMatrix(int rows, int columns, double[] array) : base(rows, columns) { - this.Data = array; + Data = array; } /// @@ -105,12 +105,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double { var rows = array.GetLength(0); var columns = array.GetLength(1); - this.Data = new double[rows * columns]; + Data = new double[rows * columns]; for (var i = 0; i < rows; i++) { for (var j = 0; j < columns; j++) { - this.Data[(j * rows) + i] = array[i, j]; + Data[(j * rows) + i] = array[i, j]; } } } @@ -168,7 +168,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public override double At(int row, int column) { - return this.Data[(column * this.RowCount) + row]; + return Data[(column * RowCount) + row]; } /// @@ -185,7 +185,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public override void At(int row, int column, double value) { - this.Data[(column * this.RowCount) + row] = value; + Data[(column * RowCount) + row] = value; } /// @@ -193,7 +193,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public override void Clear() { - Array.Clear(this.Data, 0, this.Data.Length); + Array.Clear(Data, 0, Data.Length); } /// @@ -202,13 +202,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The transpose of this matrix. public override Matrix Transpose() { - var ret = new DenseMatrix(this.ColumnCount, this.RowCount); - for (var j = 0; j < this.ColumnCount; j++) + var ret = new DenseMatrix(ColumnCount, RowCount); + for (var j = 0; j < ColumnCount; j++) { - var index = j * this.RowCount; - for (var i = 0; i < this.RowCount; i++) + var index = j * RowCount; + for (var i = 0; i < RowCount; i++) { - ret.Data[(i * this.ColumnCount) + j] = this.Data[index + i]; + ret.Data[(i * ColumnCount) + j] = Data[index + i]; } } @@ -232,7 +232,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double } else { - this.Add(m); + Add(m); } } @@ -249,12 +249,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (other.RowCount != this.RowCount || other.ColumnCount != this.ColumnCount) + if (other.RowCount != RowCount || other.ColumnCount != ColumnCount) { throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions); } - Control.LinearAlgebraProvider.AddArrays(this.Data, other.Data, this.Data); + Control.LinearAlgebraProvider.AddArrays(Data, other.Data, Data); } /// @@ -272,7 +272,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double } else { - this.Subtract(m); + Subtract(m); } } @@ -289,12 +289,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (other.RowCount != this.RowCount || other.ColumnCount != this.ColumnCount) + if (other.RowCount != RowCount || other.ColumnCount != ColumnCount) { throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions); } - Control.LinearAlgebraProvider.SubtractArrays(this.Data, other.Data, this.Data); + Control.LinearAlgebraProvider.SubtractArrays(Data, other.Data, Data); } /// @@ -303,7 +303,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The scalar to multiply with. public override void Multiply(double scalar) { - Control.LinearAlgebraProvider.ScaleArray(scalar, this.Data); + Control.LinearAlgebraProvider.ScaleArray(scalar, Data); } /// @@ -327,12 +327,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (this.ColumnCount != other.RowCount) + if (ColumnCount != other.RowCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions); } - if (result.RowCount != this.RowCount || result.ColumnCount != other.ColumnCount) + if (result.RowCount != RowCount || result.ColumnCount != other.ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions); } @@ -347,9 +347,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double else { Control.LinearAlgebraProvider.MatrixMultiply( - this.Data, - this.RowCount, - this.ColumnCount, + Data, + RowCount, + ColumnCount, m.Data, m.RowCount, m.ColumnCount, @@ -371,7 +371,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (this.ColumnCount != other.RowCount) + if (ColumnCount != other.RowCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions); } @@ -382,7 +382,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double return base.Multiply(other); } - var result = (DenseMatrix)this.CreateMatrix(this.RowCount, other.ColumnCount); + var result = (DenseMatrix)CreateMatrix(RowCount, other.ColumnCount); Multiply(other, result); return result; } @@ -440,4 +440,4 @@ namespace MathNet.Numerics.LinearAlgebra.Double #endregion } -} \ No newline at end of file +} diff --git a/src/Numerics/LinearAlgebra/Double/DenseVector.cs b/src/Numerics/LinearAlgebra/Double/DenseVector.cs index aa9dcab3..cee08c40 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseVector.cs @@ -51,7 +51,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double public DenseVector(int size) : base(size) { - this.Data = new double[size]; + Data = new double[size]; } /// @@ -70,9 +70,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double public DenseVector(int size, double value) : this(size) { - for (var index = 0; index < this.Data.Length; index++) + for (var index = 0; index < Data.Length; index++) { - this.Data[index] = value; + Data[index] = value; } } @@ -91,12 +91,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double { CommonParallel.For( 0, - this.Data.Length, + Data.Length, index => this[index] = other[index]); } else { - Buffer.BlockCopy(vector.Data, 0, this.Data, 0, this.Data.Length * Constants.SizeOfDouble); + Buffer.BlockCopy(vector.Data, 0, Data, 0, Data.Length * Constants.SizeOfDouble); } } @@ -110,7 +110,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double public DenseVector(DenseVector other) : this(other.Count) { - Buffer.BlockCopy(other.Data, 0, this.Data, 0, this.Data.Length * Constants.SizeOfDouble); + Buffer.BlockCopy(other.Data, 0, Data, 0, Data.Length * Constants.SizeOfDouble); } /// @@ -121,7 +121,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// changes to the vector will also change the array. public DenseVector(double[] array) : base(array.Length) { - this.Data = array; + Data = array; } /// @@ -158,7 +158,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// /// The array to bind to the DenseVector object. /// - /// A DenseVector whose values are bound to the given array. + /// A DenseVector whose values are bound to the given array. /// public static implicit operator DenseVector(double[] array) { @@ -176,10 +176,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// This vector as a column matrix. public override Matrix ToColumnMatrix() { - var matrix = new DenseMatrix(this.Count, 1); - for (var i = 0; i < this.Data.Length; i++) + var matrix = new DenseMatrix(Count, 1); + for (var i = 0; i < Data.Length; i++) { - matrix[i, 0] = this.Data[i]; + matrix[i, 0] = Data[i]; } return matrix; @@ -191,10 +191,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// This vector as a row matrix. public override Matrix ToRowMatrix() { - var matrix = new DenseMatrix(1, this.Count); - for (var i = 0; i < this.Data.Length; i++) + var matrix = new DenseMatrix(1, Count); + for (var i = 0; i < Data.Length; i++) { - matrix[0, i] = this.Data[i]; + matrix[0, i] = Data[i]; } return matrix; @@ -209,12 +209,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double { get { - return this.Data[index]; + return Data[index]; } set { - this.Data[index] = value; + Data[index] = value; } } @@ -270,7 +270,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("target"); } - if (this.Count != target.Count) + if (Count != target.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target"); } @@ -285,12 +285,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double { CommonParallel.For( 0, - this.Data.Length, - index => target[index] = this.Data[index]); + Data.Length, + index => target[index] = Data[index]); } else { - Buffer.BlockCopy(this.Data, 0, otherVector.Data, 0, this.Data.Length * Constants.SizeOfDouble); + Buffer.BlockCopy(Data, 0, otherVector.Data, 0, Data.Length * Constants.SizeOfDouble); } } @@ -307,8 +307,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double CommonParallel.For( 0, - this.Data.Length, - index => this.Data[index] += scalar); + Data.Length, + index => Data[index] += scalar); } /// @@ -325,12 +325,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (this.Count != result.Count) + if (Count != result.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } - this.CopyTo(result); + CopyTo(result); result.Add(scalar); } @@ -347,7 +347,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (this.Count != other.Count) + if (Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } @@ -360,7 +360,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double } else { - Control.LinearAlgebraProvider.AddVectorToScaledVector(this.Data, 1.0, denseVector.Data); + Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, 1.0, denseVector.Data); } } @@ -380,12 +380,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (this.Count != other.Count) + if (Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } - if (this.Count != result.Count) + if (Count != result.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } @@ -398,7 +398,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double } else { - this.CopyTo(result); + CopyTo(result); result.Add(other); } } @@ -463,8 +463,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double CommonParallel.For( 0, - this.Data.Length, - index => this.Data[index] -= scalar); + Data.Length, + index => Data[index] -= scalar); } /// @@ -481,12 +481,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (this.Count != result.Count) + if (Count != result.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } - this.CopyTo(result); + CopyTo(result); result.Subtract(scalar); } @@ -503,7 +503,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (this.Count != other.Count) + if (Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } @@ -516,7 +516,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double } else { - Control.LinearAlgebraProvider.AddVectorToScaledVector(this.Data, -1.0, denseVector.Data); + Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, -1.0, denseVector.Data); } } @@ -536,12 +536,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (this.Count != other.Count) + if (Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } - if (this.Count != result.Count) + if (Count != result.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } @@ -554,7 +554,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double } else { - this.CopyTo(result); + CopyTo(result); result.Subtract(other); } } @@ -612,11 +612,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// Added as an alternative to the unary negation operator. public override Vector Negate() { - var result = new DenseVector(this.Count); + var result = new DenseVector(Count); CommonParallel.For( 0, - this.Data.Length, - index => result[index] = -this.Data[index]); + Data.Length, + index => result[index] = -Data[index]); return result; } @@ -632,7 +632,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double return; } - Control.LinearAlgebraProvider.ScaleArray(scalar, this.Data); + Control.LinearAlgebraProvider.ScaleArray(scalar, Data); } /// @@ -649,7 +649,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (this.Count != other.Count) + if (Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } @@ -661,7 +661,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double return base.DotProduct(other); } - return Control.LinearAlgebraProvider.DotProduct(this.Data, denseVector.Data); + return Control.LinearAlgebraProvider.DotProduct(Data, denseVector.Data); } /// @@ -756,10 +756,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double public override int AbsoluteMinimumIndex() { var index = 0; - var min = Math.Abs(this.Data[index]); - for (var i = 1; i < this.Count; i++) + var min = Math.Abs(Data[index]); + for (var i = 1; i < Count; i++) { - var test = Math.Abs(this.Data[i]); + var test = Math.Abs(Data[i]); if (test < min) { index = i; @@ -776,7 +776,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The value of the absolute minimum element. public override double AbsoluteMinimum() { - return Math.Abs(this.Data[this.AbsoluteMinimumIndex()]); + return Math.Abs(Data[AbsoluteMinimumIndex()]); } /// @@ -785,7 +785,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The value of the absolute maximum element. public override double AbsoluteMaximum() { - return Math.Abs(this.Data[this.AbsoluteMaximumIndex()]); + return Math.Abs(Data[AbsoluteMaximumIndex()]); } /// @@ -795,10 +795,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double public override int AbsoluteMaximumIndex() { var index = 0; - var max = Math.Abs(this.Data[index]); - for (var i = 1; i < this.Count; i++) + var max = Math.Abs(Data[index]); + for (var i = 1; i < Count; i++) { - var test = Math.Abs(this.Data[i]); + var test = Math.Abs(Data[i]); if (test > max) { index = i; @@ -822,7 +822,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// If is not positive. public override Vector SubVector(int index, int length) { - if (index < 0 || index >= this.Count) + if (index < 0 || index >= Count) { throw new ArgumentOutOfRangeException("index"); } @@ -832,7 +832,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentOutOfRangeException("length"); } - if (index + length > this.Count) + if (index + length > Count) { throw new ArgumentOutOfRangeException("length"); } @@ -842,7 +842,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double CommonParallel.For( index, index + length, - i => result.Data[i - index] = this.Data[i]); + i => result.Data[i - index] = Data[i]); return result; } @@ -859,7 +859,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("values"); } - if (values.Length != this.Count) + if (values.Length != Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "values"); } @@ -867,7 +867,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double CommonParallel.For( 0, values.Length, - i => this.Data[i] = values[i]); + i => Data[i] = values[i]); } /// @@ -877,13 +877,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double public override int MaximumIndex() { var index = 0; - var max = this.Data[0]; - for (var i = 1; i < this.Count; i++) + var max = Data[0]; + for (var i = 1; i < Count; i++) { - if (max < this.Data[i]) + if (max < Data[i]) { index = i; - max = this.Data[i]; + max = Data[i]; } } @@ -897,13 +897,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double public override int MinimumIndex() { var index = 0; - var min = this.Data[0]; - for (var i = 1; i < this.Count; i++) + var min = Data[0]; + for (var i = 1; i < Count; i++) { - if (min > this.Data[i]) + if (min > Data[i]) { index = i; - min = this.Data[i]; + min = Data[i]; } } @@ -917,9 +917,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double public override double Sum() { double result = 0; - for (var i = 0; i < this.Count; i++) + for (var i = 0; i < Count; i++) { - result += this.Data[i]; + result += Data[i]; } return result; @@ -932,9 +932,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double public override double SumMagnitudes() { double result = 0; - for (var i = 0; i < this.Count; i++) + for (var i = 0; i < Count; i++) { - result += Math.Abs(this.Data[i]); + result += Math.Abs(Data[i]); } return result; @@ -953,7 +953,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (this.Count != other.Count) + if (Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } @@ -968,7 +968,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double { CommonParallel.For( 0, - this.Count, + Count, index => this[index] *= other[index]); } } @@ -994,12 +994,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (this.Count != other.Count) + if (Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } - if (this.Count != result.Count) + if (Count != result.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } @@ -1007,12 +1007,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) { var tmp = result.CreateVector(result.Count); - this.PointwiseMultiply(other, tmp); + PointwiseMultiply(other, tmp); tmp.CopyTo(result); } else { - this.CopyTo(result); + CopyTo(result); result.PointwiseMultiply(other); } } @@ -1030,7 +1030,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (this.Count != other.Count) + if (Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } @@ -1045,7 +1045,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double { CommonParallel.For( 0, - this.Count, + Count, index => this[index] /= other[index]); } } @@ -1071,12 +1071,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (this.Count != other.Count) + if (Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } - if (this.Count != result.Count) + if (Count != result.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } @@ -1084,12 +1084,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) { var tmp = result.CreateVector(result.Count); - this.PointwiseDivide(other, tmp); + PointwiseDivide(other, tmp); tmp.CopyTo(result); } else { - this.CopyTo(result); + CopyTo(result); result.PointwiseDivide(other); } } @@ -1116,11 +1116,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double var matrix = new DenseMatrix(u.Count, v.Count); CommonParallel.For( - 0, - u.Count, + 0, + u.Count, i => { - for (int j = 0; j < v.Count; j++) + for (var j = 0; j < v.Count; j++) { matrix.At(i, j, u.Data[i] * v.Data[j]); } @@ -1145,7 +1145,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); } - var v = (DenseVector)this.CreateVector(length); + var v = (DenseVector)CreateVector(length); for (var index = 0; index < v.Data.Length; index++) { v.Data[index] = randomDistribution.Sample(); @@ -1171,7 +1171,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); } - var v = (DenseVector)this.CreateVector(length); + var v = (DenseVector)CreateVector(length); for (var index = 0; index < v.Data.Length; index++) { v.Data[index] = randomDistribution.Sample(); @@ -1203,9 +1203,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double { var sum = 0.0; - for (var i = 0; i < this.Data.Length; i++) + for (var i = 0; i < Data.Length; i++) { - sum = SpecialFunctions.Hypotenuse(sum, this.Data[i]); + sum = SpecialFunctions.Hypotenuse(sum, Data[i]); } return sum; @@ -1219,8 +1219,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double { return CommonParallel.Aggregate( 0, - this.Count, - index => Math.Abs(this.Data[index])); + Count, + index => Math.Abs(Data[index])); } /// @@ -1237,18 +1237,18 @@ namespace MathNet.Numerics.LinearAlgebra.Double if (1 == p) { - return this.Norm1(); + return Norm1(); } if (2 == p) { - return this.Norm(); + return Norm(); } var sum = CommonParallel.Aggregate( 0, - this.Count, - index => Math.Pow(Math.Abs(this.Data[index]), p)); + Count, + index => Math.Pow(Math.Abs(Data[index]), p)); return Math.Pow(sum, 1.0 / p); } @@ -1261,8 +1261,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double { return CommonParallel.Select( 0, - this.Count, - (index, localData) => localData = Math.Max(localData, Math.Abs(this.Data[index])), + Count, + (index, localData) => localData = Math.Max(localData, Math.Abs(Data[index])), Math.Max); } @@ -1426,5 +1426,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double } #endregion + + /// + /// Resets all values to zero. + /// + public override void Clear() + { + Array.Clear(Data, 0, Data.Length); + } } -} \ No newline at end of file +} diff --git a/src/Numerics/LinearAlgebra/Double/Matrix.Arithmetic.cs b/src/Numerics/LinearAlgebra/Double/Matrix.Arithmetic.cs index fdee91b3..357fb0a8 100644 --- a/src/Numerics/LinearAlgebra/Double/Matrix.Arithmetic.cs +++ b/src/Numerics/LinearAlgebra/Double/Matrix.Arithmetic.cs @@ -49,19 +49,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (other.RowCount != this.RowCount || other.ColumnCount != this.ColumnCount) + if (other.RowCount != RowCount || other.ColumnCount != ColumnCount) { throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions); } CommonParallel.For( 0, - this.RowCount, + RowCount, i => { - for (var j = 0; j < this.ColumnCount; j++) + for (var j = 0; j < ColumnCount; j++) { - this.At(i, j, this.At(i, j) + other.At(i, j)); + At(i, j, At(i, j) + other.At(i, j)); } }); } @@ -79,19 +79,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (other.RowCount != this.RowCount || other.ColumnCount != this.ColumnCount) + if (other.RowCount != RowCount || other.ColumnCount != ColumnCount) { throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions); } CommonParallel.For( 0, - this.RowCount, + RowCount, i => { - for (var j = 0; j < this.ColumnCount; j++) + for (var j = 0; j < ColumnCount; j++) { - this.At(i, j, this.At(i, j) - other.At(i, j)); + At(i, j, At(i, j) - other.At(i, j)); } }); } @@ -109,12 +109,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double CommonParallel.For( 0, - this.RowCount, + RowCount, i => { - for (var j = 0; j < this.ColumnCount; j++) + for (var j = 0; j < ColumnCount; j++) { - this.At(i, j, this.At(i, j) * scalar); + At(i, j, At(i, j) * scalar); } }); } @@ -133,17 +133,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (result.RowCount != this.RowCount) + if (result.RowCount != RowCount) { throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "result"); } - if (result.ColumnCount != this.ColumnCount) + if (result.ColumnCount != ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension, "result"); } - this.CopyTo(result); + CopyTo(result); result.Multiply(scalar); } @@ -156,7 +156,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// If this.ColumnCount != rightSide.Count. public virtual Vector Multiply(Vector rightSide) { - var ret = this.CreateVector(this.RowCount); + var ret = CreateVector(RowCount); Multiply(rightSide, ret); return ret; } @@ -177,7 +177,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("rightSide"); } - if (this.ColumnCount != rightSide.Count) + if (ColumnCount != rightSide.Count) { throw new ArgumentException(Resources.ArgumentMatrixDimensions, "rightSide"); } @@ -187,7 +187,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (this.RowCount != result.Count) + if (RowCount != result.Count) { throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); } @@ -202,13 +202,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double { CommonParallel.For( 0, - this.RowCount, + RowCount, i => { double s = 0; - for (var j = 0; j != this.ColumnCount; j++) + for (var j = 0; j != ColumnCount; j++) { - s += this.At(i, j) * rightSide[j]; + s += At(i, j) * rightSide[j]; } result[i] = s; @@ -225,8 +225,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// If this.RowCount != .Count. public virtual Vector LeftMultiply(Vector leftSide) { - var ret = this.CreateVector(this.ColumnCount); - this.LeftMultiply(leftSide, ret); + var ret = CreateVector(ColumnCount); + LeftMultiply(leftSide, ret); return ret; } @@ -246,7 +246,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("leftSide"); } - if (this.RowCount != leftSide.Count) + if (RowCount != leftSide.Count) { throw new ArgumentException(Resources.ArgumentMatrixDimensions, "leftSide"); } @@ -256,7 +256,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (this.ColumnCount != result.Count) + if (ColumnCount != result.Count) { throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); } @@ -264,20 +264,20 @@ namespace MathNet.Numerics.LinearAlgebra.Double if (ReferenceEquals(leftSide, result)) { var tmp = result.CreateVector(result.Count); - this.LeftMultiply(leftSide, tmp); + LeftMultiply(leftSide, tmp); tmp.CopyTo(result); } else { CommonParallel.For( 0, - this.RowCount, + RowCount, j => { double s = 0; for (var i = 0; i != leftSide.Count; i++) { - s += leftSide[i] * this.At(i, j); + s += leftSide[i] * At(i, j); } result[j] = s; @@ -306,12 +306,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (this.ColumnCount != other.RowCount) + if (ColumnCount != other.RowCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions); } - if (result.RowCount != this.RowCount || result.ColumnCount != other.ColumnCount) + if (result.RowCount != RowCount || result.ColumnCount != other.ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions); } @@ -326,15 +326,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double { CommonParallel.For( 0, - this.RowCount, + RowCount, j => { for (var i = 0; i != other.ColumnCount; i++) { double s = 0; - for (var l = 0; l < this.ColumnCount; l++) + for (var l = 0; l < ColumnCount; l++) { - s += this.At(j, l) * other.At(l, i); + s += At(j, l) * other.At(l, i); } result.At(j, i, s); @@ -357,12 +357,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (this.ColumnCount != other.RowCount) + if (ColumnCount != other.RowCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions); } - var result = this.CreateMatrix(this.RowCount, other.ColumnCount); + var result = CreateMatrix(RowCount, other.ColumnCount); Multiply(other, result); return result; } @@ -372,7 +372,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public virtual void Negate() { - this.Multiply(-1); + Multiply(-1); } /// @@ -388,12 +388,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (result.RowCount != this.RowCount || result.ColumnCount != this.ColumnCount) + if (result.RowCount != RowCount || result.ColumnCount != ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions); } - this.CopyTo(result); + CopyTo(result); result.Negate(); } @@ -614,13 +614,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (this.ColumnCount != other.ColumnCount || this.RowCount != other.RowCount) + if (ColumnCount != other.ColumnCount || RowCount != other.RowCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions, "other"); } - var result = this.CreateMatrix(this.RowCount, this.ColumnCount); - this.PointwiseMultiply(other, result); + var result = CreateMatrix(RowCount, ColumnCount); + PointwiseMultiply(other, result); return result; } @@ -645,24 +645,24 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (this.ColumnCount != other.ColumnCount || this.RowCount != other.RowCount) + if (ColumnCount != other.ColumnCount || RowCount != other.RowCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); } - if (this.ColumnCount != result.ColumnCount || this.RowCount != result.RowCount) + if (ColumnCount != result.ColumnCount || RowCount != result.RowCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); } CommonParallel.For( 0, - this.ColumnCount, + ColumnCount, j => { - for (var i = 0; i < this.RowCount; i++) + for (var i = 0; i < RowCount; i++) { - result.At(i, j, this.At(i, j) * other.At(i, j)); + result.At(i, j, At(i, j) * other.At(i, j)); } }); } @@ -681,13 +681,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (this.ColumnCount != other.ColumnCount || this.RowCount != other.RowCount) + if (ColumnCount != other.ColumnCount || RowCount != other.RowCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions, "other"); } - var result = this.CreateMatrix(this.RowCount, this.ColumnCount); - this.PointwiseDivide(other, result); + var result = CreateMatrix(RowCount, ColumnCount); + PointwiseDivide(other, result); return result; } @@ -712,24 +712,24 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (this.ColumnCount != other.ColumnCount || this.RowCount != other.RowCount) + if (ColumnCount != other.ColumnCount || RowCount != other.RowCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); } - if (this.ColumnCount != result.ColumnCount || this.RowCount != result.RowCount) + if (ColumnCount != result.ColumnCount || RowCount != result.RowCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); } CommonParallel.For( 0, - this.ColumnCount, + ColumnCount, j => { - for (var i = 0; i < this.RowCount; i++) + for (var i = 0; i < RowCount; i++) { - result.At(i, j, this.At(i, j) / other.At(i, j)); + result.At(i, j, At(i, j) / other.At(i, j)); } }); } @@ -757,10 +757,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentException(Resources.ArgumentMustBePositive, "numberOfColumns"); } - var matrix = this.CreateMatrix(numberOfRows, numberOfColumns); + var matrix = CreateMatrix(numberOfRows, numberOfColumns); CommonParallel.For( 0, - this.ColumnCount, + ColumnCount, j => { for (var i = 0; i < matrix.RowCount; i++) @@ -795,10 +795,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentException(Resources.ArgumentMustBePositive, "numberOfColumns"); } - var matrix = this.CreateMatrix(numberOfRows, numberOfColumns); + var matrix = CreateMatrix(numberOfRows, numberOfColumns); CommonParallel.For( 0, - this.ColumnCount, + ColumnCount, j => { for (var i = 0; i < matrix.RowCount; i++) @@ -817,12 +817,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// If the matrix is not square public virtual double Trace() { - if (this.RowCount != this.ColumnCount) + if (RowCount != ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixSquare); } - return CommonParallel.Aggregate(0, this.RowCount, i => this[i, i]); + return CommonParallel.Aggregate(0, RowCount, i => this[i, i]); } /// @@ -863,8 +863,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - var result = this.CreateMatrix(this.RowCount * other.RowCount, this.ColumnCount * other.ColumnCount); - this.KroneckerProduct(other, result); + var result = CreateMatrix(RowCount * other.RowCount, ColumnCount * other.ColumnCount); + KroneckerProduct(other, result); return result; } @@ -889,19 +889,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (result.RowCount != (this.RowCount * other.RowCount) || result.ColumnCount != (this.ColumnCount * other.ColumnCount)) + if (result.RowCount != (RowCount * other.RowCount) || result.ColumnCount != (ColumnCount * other.ColumnCount)) { throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); } CommonParallel.For( 0, - this.ColumnCount, + ColumnCount, j => { - for (var i = 0; i < this.RowCount; i++) + for (var i = 0; i < RowCount; i++) { - result.SetSubMatrix(i * other.RowCount, other.RowCount, j * other.ColumnCount, other.ColumnCount, this.At(i, j) * other); + result.SetSubMatrix(i * other.RowCount, other.RowCount, j * other.ColumnCount, other.ColumnCount, At(i, j) * other); } }); } @@ -919,15 +919,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentOutOfRangeException("p", Resources.ArgumentMustBePositive); } - var ret = this.Clone(); + var ret = Clone(); CommonParallel.For( 0, - this.ColumnCount, + ColumnCount, i => { - var coli = this.Column(i); + var coli = Column(i); var norm = coli.NormP(p); - for (var j = 0; j < this.RowCount; j++) + for (var j = 0; j < RowCount; j++) { ret[j, i] = coli[j] / norm; } @@ -948,21 +948,20 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentOutOfRangeException("p", Resources.ArgumentMustBePositive); } - var ret = this.Clone(); + var ret = Clone(); CommonParallel.For( 0, - this.ColumnCount, + ColumnCount, j => { - var rowj = this.Row(j); + var rowj = Row(j); var norm = rowj.NormP(p); - for (var i = 0; i < this.RowCount; i++) + for (var i = 0; i < RowCount; i++) { ret[i, j] = rowj[j] / norm; } }); - return ret; } } -} \ No newline at end of file +} diff --git a/src/Numerics/LinearAlgebra/Double/Matrix.cs b/src/Numerics/LinearAlgebra/Double/Matrix.cs index e7eda7da..f5888801 100644 --- a/src/Numerics/LinearAlgebra/Double/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Double/Matrix.cs @@ -64,8 +64,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentOutOfRangeException(Resources.MatrixColumnsMustBePositive); } - this.RowCount = rows; - this.ColumnCount = columns; + RowCount = rows; + ColumnCount = columns; } /// @@ -81,8 +81,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentOutOfRangeException(Resources.MatrixRowsOrColumnsMustBePositive); } - this.RowCount = order; - this.ColumnCount = order; + RowCount = order; + ColumnCount = order; } /// @@ -121,14 +121,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double { get { - this.RangeCheck(row, column); - return this.At(row, column); + RangeCheck(row, column); + return At(row, column); } set { - this.RangeCheck(row, column); - this.At(row, column, value); + RangeCheck(row, column); + At(row, column, value); } } @@ -168,8 +168,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public virtual Matrix Clone() { - var result = this.CreateMatrix(this.RowCount, this.ColumnCount); - this.CopyTo(result); + var result = CreateMatrix(RowCount, ColumnCount); + CopyTo(result); return result; } @@ -197,16 +197,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double return; } - if (this.RowCount != target.RowCount || this.ColumnCount != target.ColumnCount) + if (RowCount != target.RowCount || ColumnCount != target.ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions, "target"); } - for (var i = 0; i < this.RowCount; i++) + for (var i = 0; i < RowCount; i++) { - for (var j = 0; j < this.ColumnCount; j++) + for (var j = 0; j < ColumnCount; j++) { - target.At(i, j, this.At(i, j)); + target.At(i, j, At(i, j)); } } } @@ -248,7 +248,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public override string ToString() { - return this.ToString(null, null); + return ToString(null, null); } /// @@ -260,8 +260,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// or greater than or equal to the number of rows. public virtual Vector Row(int index) { - var ret = this.CreateVector(this.ColumnCount); - this.Row(index, 0, this.ColumnCount, ret); + var ret = CreateVector(ColumnCount); + Row(index, 0, ColumnCount, ret); return ret; } @@ -276,7 +276,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// If this.Columns != result.Count. public virtual void Row(int index, Vector result) { - this.Row(index, 0, this.ColumnCount, result); + Row(index, 0, ColumnCount, result); } /// @@ -295,8 +295,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// If is not positive. public virtual Vector Row(int rowIndex, int columnIndex, int length) { - var ret = this.CreateVector(length); - this.Row(rowIndex, columnIndex, length, ret); + var ret = CreateVector(length); + Row(rowIndex, columnIndex, length, ret); return ret; } @@ -323,17 +323,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (rowIndex >= this.RowCount || rowIndex < 0) + if (rowIndex >= RowCount || rowIndex < 0) { throw new ArgumentOutOfRangeException("rowIndex"); } - if (columnIndex >= this.ColumnCount || columnIndex < 0) + if (columnIndex >= ColumnCount || columnIndex < 0) { throw new ArgumentOutOfRangeException("columnIndex"); } - if (columnIndex + length > this.ColumnCount) + if (columnIndex + length > ColumnCount) { throw new ArgumentOutOfRangeException("length"); } @@ -350,7 +350,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double for (int i = columnIndex, j = 0; i < columnIndex + length; i++, j++) { - result[j] = this.At(rowIndex, i); + result[j] = At(rowIndex, i); } } @@ -363,8 +363,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// or greater than or equal to the number of columns. public virtual Vector Column(int index) { - var result = this.CreateVector(this.RowCount); - this.Column(index, 0, this.RowCount, result); + var result = CreateVector(RowCount); + Column(index, 0, RowCount, result); return result; } @@ -379,7 +379,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// If this.Rows != result.Count. public virtual void Column(int index, Vector result) { - this.Column(index, 0, this.RowCount, result); + Column(index, 0, RowCount, result); } /// @@ -399,8 +399,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// If is not positive. public virtual Vector Column(int columnIndex, int rowIndex, int length) { - var result = this.CreateVector(length); - this.Column(columnIndex, rowIndex, length, result); + var result = CreateVector(length); + Column(columnIndex, rowIndex, length, result); return result; } @@ -427,17 +427,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (columnIndex >= this.ColumnCount || columnIndex < 0) + if (columnIndex >= ColumnCount || columnIndex < 0) { throw new ArgumentOutOfRangeException("columnIndex"); } - if (rowIndex >= this.RowCount || rowIndex < 0) + if (rowIndex >= RowCount || rowIndex < 0) { throw new ArgumentOutOfRangeException("rowIndex"); } - if (rowIndex + length > this.RowCount) + if (rowIndex + length > RowCount) { throw new ArgumentOutOfRangeException("length"); } @@ -454,7 +454,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double for (int i = rowIndex, j = 0; i < rowIndex + length; i++, j++) { - result[j] = this.At(i, columnIndex); + result[j] = At(i, columnIndex); } } @@ -464,15 +464,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The lower triangle of this matrix. public virtual Matrix LowerTriangle() { - var ret = this.CreateMatrix(this.RowCount, this.ColumnCount); + var ret = CreateMatrix(RowCount, ColumnCount); CommonParallel.For( 0, - this.ColumnCount, + ColumnCount, j => { - for (var i = j; i < this.RowCount; i++) + for (var i = j; i < RowCount; i++) { - ret.At(i, j, this.At(i, j)); + ret.At(i, j, At(i, j)); } }); return ret; @@ -491,19 +491,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (result.RowCount != this.RowCount || result.ColumnCount != this.ColumnCount) + if (result.RowCount != RowCount || result.ColumnCount != ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); } CommonParallel.For( 0, - this.ColumnCount, + ColumnCount, j => { - for (var i = 0; i < this.RowCount; i++) + for (var i = 0; i < RowCount; i++) { - result.At(i, j, i >= j ? this.At(i, j) : 0); + result.At(i, j, i >= j ? At(i, j) : 0); } }); } @@ -514,17 +514,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The upper triangle of this matrix. public virtual Matrix UpperTriangle() { - var ret = this.CreateMatrix(this.RowCount, this.ColumnCount); + var ret = CreateMatrix(RowCount, ColumnCount); CommonParallel.For( 0, - this.ColumnCount, + ColumnCount, j => { - for (var i = 0; i < this.RowCount; i++) + for (var i = 0; i < RowCount; i++) { if (i <= j) { - ret.At(i, j, this.At(i, j)); + ret.At(i, j, At(i, j)); } } }); @@ -544,19 +544,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (result.RowCount != this.RowCount || result.ColumnCount != this.ColumnCount) + if (result.RowCount != RowCount || result.ColumnCount != ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); } CommonParallel.For( 0, - this.ColumnCount, + ColumnCount, j => { - for (var i = 0; i < this.RowCount; i++) + for (var i = 0; i < RowCount; i++) { - result.At(i, j, i <= j ? this.At(i, j) : 0); + result.At(i, j, i <= j ? At(i, j) : 0); } }); } @@ -579,12 +579,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// is not positive. public virtual Matrix SubMatrix(int rowIndex, int rowLength, int columnIndex, int columnLength) { - if (rowIndex >= this.RowCount || rowIndex < 0) + if (rowIndex >= RowCount || rowIndex < 0) { throw new ArgumentOutOfRangeException("rowIndex"); } - if (columnIndex >= this.ColumnCount || columnIndex < 0) + if (columnIndex >= ColumnCount || columnIndex < 0) { throw new ArgumentOutOfRangeException("columnIndex"); } @@ -602,17 +602,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double var colMax = columnIndex + columnLength; var rowMax = rowIndex + rowLength; - if (rowMax > this.RowCount) + if (rowMax > RowCount) { throw new ArgumentOutOfRangeException("rowLength"); } - if (colMax > this.ColumnCount) + if (colMax > ColumnCount) { throw new ArgumentOutOfRangeException("columnLength"); } - var result = this.CreateMatrix(rowLength, columnLength); + var result = CreateMatrix(rowLength, columnLength); CommonParallel.For( columnIndex, @@ -621,7 +621,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double { for (int i = rowIndex, ii = 0; i < rowMax; i++, ii++) { - result.At(ii, j - columnIndex, this.At(i, j)); + result.At(ii, j - columnIndex, At(i, j)); } }); return result; @@ -634,9 +634,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public virtual IEnumerable> ColumnEnumerator() { - for (var i = 0; i < this.ColumnCount; i++) + for (var i = 0; i < ColumnCount; i++) { - yield return new KeyValuePair(i, this.Column(i)); + yield return new KeyValuePair(i, Column(i)); } } @@ -655,12 +655,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// If is not positive. public virtual IEnumerable> ColumnEnumerator(int index, int length) { - if (index >= this.ColumnCount || index < 0) + if (index >= ColumnCount || index < 0) { throw new ArgumentOutOfRangeException("index"); } - if (index + length > this.ColumnCount) + if (index + length > ColumnCount) { throw new ArgumentOutOfRangeException("length"); } @@ -673,7 +673,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double var maxIndex = index + length; for (var i = index; i < maxIndex; i++) { - yield return new KeyValuePair(i, this.Column(i)); + yield return new KeyValuePair(i, Column(i)); } } @@ -691,12 +691,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// If is not positive. public virtual IEnumerable> RowEnumerator(int index, int length) { - if (index >= this.RowCount || index < 0) + if (index >= RowCount || index < 0) { throw new ArgumentOutOfRangeException("index"); } - if (index + length > this.RowCount) + if (index + length > RowCount) { throw new ArgumentOutOfRangeException("length"); } @@ -709,7 +709,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double var maxi = index + length; for (var i = index; i < maxi; i++) { - yield return new KeyValuePair(i, this.Row(i)); + yield return new KeyValuePair(i, Row(i)); } } @@ -720,9 +720,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public virtual IEnumerable> RowEnumerator() { - for (var i = 0; i < this.RowCount; i++) + for (var i = 0; i < RowCount; i++) { - yield return new KeyValuePair(i, this.Row(i)); + yield return new KeyValuePair(i, Row(i)); } } @@ -734,12 +734,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// i == j (i is the row index, and j is the column index). public virtual Vector Diagonal() { - var min = Math.Min(this.RowCount, this.ColumnCount); - var diagonal = this.CreateVector(min); + var min = Math.Min(RowCount, ColumnCount); + var diagonal = CreateVector(min); CommonParallel.For( 0, min, - i => { diagonal[i] = this.At(i, i); }); + i => { diagonal[i] = At(i, i); }); return diagonal; } @@ -750,17 +750,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The lower triangle of this matrix. public virtual Matrix StrictlyLowerTriangle() { - var result = this.CreateMatrix(this.RowCount, this.ColumnCount); + var result = CreateMatrix(RowCount, ColumnCount); CommonParallel.For( 0, - this.RowCount, + RowCount, i => { - for (var j = 0; j < this.ColumnCount; j++) + for (var j = 0; j < ColumnCount; j++) { if (i > j) { - result.At(i, j, this.At(i, j)); + result.At(i, j, At(i, j)); } } }); @@ -780,19 +780,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (result.RowCount != this.RowCount || result.ColumnCount != this.ColumnCount) + if (result.RowCount != RowCount || result.ColumnCount != ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); } CommonParallel.For( 0, - this.RowCount, + RowCount, i => { - for (var j = 0; j < this.ColumnCount; j++) + for (var j = 0; j < ColumnCount; j++) { - result.At(i, j, i > j ? this.At(i, j) : 0); + result.At(i, j, i > j ? At(i, j) : 0); } }); } @@ -804,17 +804,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The upper triangle of this matrix. public virtual Matrix StrictlyUpperTriangle() { - var result = this.CreateMatrix(this.RowCount, this.ColumnCount); + var result = CreateMatrix(RowCount, ColumnCount); CommonParallel.For( 0, - this.RowCount, + RowCount, i => { - for (var j = 0; j < this.ColumnCount; j++) + for (var j = 0; j < ColumnCount; j++) { if (i < j) { - result.At(i, j, this.At(i, j)); + result.At(i, j, At(i, j)); } } }); @@ -837,28 +837,28 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("column"); } - if (columnIndex < 0 || columnIndex > this.ColumnCount) + if (columnIndex < 0 || columnIndex > ColumnCount) { throw new ArgumentOutOfRangeException("columnIndex"); } - if (column.Count != this.RowCount) + if (column.Count != RowCount) { throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "column"); } - var result = this.CreateMatrix(this.RowCount, this.ColumnCount + 1); + var result = CreateMatrix(RowCount, ColumnCount + 1); for (var i = 0; i < columnIndex; i++) { - result.SetColumn(i, this.Column(i)); + result.SetColumn(i, Column(i)); } result.SetColumn(columnIndex, column); - for (var i = columnIndex + 1; i < this.ColumnCount + 1; i++) + for (var i = columnIndex + 1; i < ColumnCount + 1; i++) { - result.SetColumn(i, this.Column(i - 1)); + result.SetColumn(i, Column(i - 1)); } return result; @@ -878,7 +878,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// equal the number of rows of this Matrix. public virtual void SetColumn(int columnIndex, double[] column) { - if (columnIndex < 0 || columnIndex >= this.ColumnCount) + if (columnIndex < 0 || columnIndex >= ColumnCount) { throw new ArgumentOutOfRangeException("columnIndex"); } @@ -888,15 +888,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("column"); } - if (column.Length != this.RowCount) + if (column.Length != RowCount) { throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "column"); } CommonParallel.For( 0, - this.RowCount, - i => this.At(i, columnIndex, column[i])); + RowCount, + i => At(i, columnIndex, column[i])); } /// @@ -911,7 +911,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// equal the number of rows of this Matrix. public virtual void SetColumn(int columnIndex, Vector column) { - if (columnIndex < 0 || columnIndex >= this.ColumnCount) + if (columnIndex < 0 || columnIndex >= ColumnCount) { throw new ArgumentOutOfRangeException("columnIndex"); } @@ -921,15 +921,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("column"); } - if (column.Count != this.RowCount) + if (column.Count != RowCount) { throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "column"); } CommonParallel.For( 0, - this.RowCount, - i => this.At(i, columnIndex, column[i])); + RowCount, + i => At(i, columnIndex, column[i])); } /// @@ -948,28 +948,28 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("row"); } - if (rowIndex < 0 || rowIndex > this.RowCount) + if (rowIndex < 0 || rowIndex > RowCount) { throw new ArgumentOutOfRangeException("rowIndex"); } - if (row.Count != this.ColumnCount) + if (row.Count != ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "row"); } - var result = this.CreateMatrix(this.RowCount + 1, this.ColumnCount); + var result = CreateMatrix(RowCount + 1, ColumnCount); for (var i = 0; i < rowIndex; i++) { - result.SetRow(i, this.Row(i)); + result.SetRow(i, Row(i)); } result.SetRow(rowIndex, row); - for (var i = rowIndex + 1; i < this.RowCount; i++) + for (var i = rowIndex + 1; i < RowCount; i++) { - result.SetRow(i, this.Row(i - 1)); + result.SetRow(i, Row(i - 1)); } return result; @@ -987,7 +987,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// equal the number of columns of this Matrix. public virtual void SetRow(int rowIndex, Vector row) { - if (rowIndex < 0 || rowIndex >= this.RowCount) + if (rowIndex < 0 || rowIndex >= RowCount) { throw new ArgumentOutOfRangeException("rowIndex"); } @@ -997,15 +997,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("row"); } - if (row.Count != this.ColumnCount) + if (row.Count != ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "row"); } CommonParallel.For( 0, - this.ColumnCount, - i => this.At(rowIndex, i, row[i])); + ColumnCount, + i => At(rowIndex, i, row[i])); } /// @@ -1020,7 +1020,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// equal the number of columns of this Matrix. public virtual void SetRow(int rowIndex, double[] row) { - if (rowIndex < 0 || rowIndex >= this.RowCount) + if (rowIndex < 0 || rowIndex >= RowCount) { throw new ArgumentOutOfRangeException("rowIndex"); } @@ -1030,15 +1030,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("row"); } - if (row.Length != this.ColumnCount) + if (row.Length != ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "row"); } CommonParallel.For( 0, - this.ColumnCount, - i => this.At(rowIndex, i, row[i])); + ColumnCount, + i => At(rowIndex, i, row[i])); } /// @@ -1061,12 +1061,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// is not positive. public virtual void SetSubMatrix(int rowIndex, int rowLength, int columnIndex, int columnLength, Matrix subMatrix) { - if (rowIndex >= this.RowCount || rowIndex < 0) + if (rowIndex >= RowCount || rowIndex < 0) { throw new ArgumentOutOfRangeException("rowIndex"); } - if (columnIndex >= this.ColumnCount || columnIndex < 0) + if (columnIndex >= ColumnCount || columnIndex < 0) { throw new ArgumentOutOfRangeException("columnIndex"); } @@ -1099,12 +1099,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double var colMax = columnIndex + columnLength; var rowMax = rowIndex + rowLength; - if (rowMax > this.RowCount) + if (rowMax > RowCount) { throw new ArgumentOutOfRangeException("rowLength"); } - if (colMax > this.ColumnCount) + if (colMax > ColumnCount) { throw new ArgumentOutOfRangeException("columnLength"); } @@ -1116,7 +1116,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double { for (int i = rowIndex, ii = 0; i < rowMax; i++, ii++) { - this.At(i, j, subMatrix[ii, j - columnIndex]); + At(i, j, subMatrix[ii, j - columnIndex]); } }); } @@ -1138,7 +1138,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("source"); } - var min = Math.Min(this.RowCount, this.ColumnCount); + var min = Math.Min(RowCount, ColumnCount); if (source.Count != min) { @@ -1148,7 +1148,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double CommonParallel.For( 0, min, - i => this.At(i, i, source[i])); + i => At(i, i, source[i])); } /// @@ -1168,7 +1168,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("source"); } - var min = Math.Min(this.RowCount, this.ColumnCount); + var min = Math.Min(RowCount, ColumnCount); if (source.Length != min) { @@ -1178,7 +1178,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double CommonParallel.For( 0, min, - i => this.At(i, i, source[i])); + i => At(i, i, source[i])); } /// @@ -1194,19 +1194,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (result.RowCount != this.RowCount || result.ColumnCount != this.ColumnCount) + if (result.RowCount != RowCount || result.ColumnCount != ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); } CommonParallel.For( 0, - this.RowCount, + RowCount, i => { - for (var j = 0; j < this.ColumnCount; j++) + for (var j = 0; j < ColumnCount; j++) { - result.At(i, j, i < j ? this.At(i, j) : 0); + result.At(i, j, i < j ? At(i, j) : 0); } }); } @@ -1217,15 +1217,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// A multidimensional containing the values of this matrix. public virtual double[,] ToArray() { - var ret = new double[this.RowCount, this.ColumnCount]; + var ret = new double[RowCount, ColumnCount]; CommonParallel.For( 0, - this.ColumnCount, + ColumnCount, j => { - for (var i = 0; i < this.RowCount; i++) + for (var i = 0; i < RowCount; i++) { - ret[i, j] = this.At(i, j); + ret[i, j] = At(i, j); } }); return ret; @@ -1242,10 +1242,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// An array containing the matrix's elements. public virtual double[] ToColumnWiseArray() { - var ret = new double[this.RowCount * this.ColumnCount]; - foreach (var column in this.ColumnEnumerator()) + var ret = new double[RowCount * ColumnCount]; + foreach (var column in ColumnEnumerator()) { - var columnIndex = column.Key * this.RowCount; + var columnIndex = column.Key * RowCount; foreach (var element in column.Value.GetIndexedEnumerator()) { ret[columnIndex + element.Key] = element.Value; @@ -1266,11 +1266,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// An array containing the matrix's elements. public virtual double[] ToRowWiseArray() { - var ret = new double[this.RowCount * this.ColumnCount]; + var ret = new double[RowCount * ColumnCount]; - foreach (var row in this.RowEnumerator()) + foreach (var row in RowEnumerator()) { - var rowIndex = row.Key * this.ColumnCount; + var rowIndex = row.Key * ColumnCount; foreach (var element in row.Value.GetIndexedEnumerator()) { ret[rowIndex + element.Key] = element.Value; @@ -1294,7 +1294,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// object ICloneable.Clone() { - return this.Clone(); + return Clone(); } #endregion @@ -1320,7 +1320,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double return false; } - if (this.ColumnCount != other.ColumnCount || this.RowCount != other.RowCount) + if (ColumnCount != other.ColumnCount || RowCount != other.RowCount) { return false; } @@ -1332,11 +1332,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double } // If all else fails, perform element wise comparison. - for (var row = 0; row < this.RowCount; row++) + for (var row = 0; row < RowCount; row++) { - for (var column = 0; column < this.ColumnCount; column++) + for (var column = 0; column < ColumnCount; column++) { - if (this.At(row, column) != other.At(row, column)) + if (At(row, column) != other.At(row, column)) { return false; } @@ -1365,18 +1365,18 @@ namespace MathNet.Numerics.LinearAlgebra.Double public string ToString(string format, IFormatProvider formatProvider) { var stringBuilder = new StringBuilder(); - for (var row = 0; row < this.RowCount; row++) + for (var row = 0; row < RowCount; row++) { - for (var column = 0; column < this.ColumnCount; column++) + for (var column = 0; column < ColumnCount; column++) { - stringBuilder.Append(this.At(row, column).ToString(format, formatProvider)); - if (column != this.ColumnCount - 1) + stringBuilder.Append(At(row, column).ToString(format, formatProvider)); + if (column != ColumnCount - 1) { stringBuilder.Append(formatProvider.GetTextInfo().ListSeparator); } } - if (row != this.RowCount - 1) + if (row != RowCount - 1) { stringBuilder.Append(Environment.NewLine); } @@ -1400,12 +1400,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// private void RangeCheck(int row, int column) { - if (row < 0 || row >= this.RowCount) + if (row < 0 || row >= RowCount) { throw new ArgumentOutOfRangeException("row"); } - if (column < 0 || column >= this.ColumnCount) + if (column < 0 || column >= ColumnCount) { throw new ArgumentOutOfRangeException("column"); } @@ -1422,7 +1422,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public override bool Equals(object obj) { - return this.Equals(obj as Matrix); + return Equals(obj as Matrix); } /// @@ -1433,12 +1433,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public override int GetHashCode() { - var hashNum = Math.Min(this.RowCount * this.ColumnCount, 25); + var hashNum = Math.Min(RowCount * ColumnCount, 25); long hash = 0; for (var i = 0; i < hashNum; i++) { - var col = i % this.ColumnCount; - var row = (i - col) / this.RowCount; + var col = i % ColumnCount; + var row = (i - col) / RowCount; #if SILVERLIGHT hash ^= Precision.DoubleToInt64Bits(this[row, col]); @@ -1457,11 +1457,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public virtual void Clear() { - for (var i = 0; i < this.RowCount; i++) + for (var i = 0; i < RowCount; i++) { - for (var j = 0; j < this.ColumnCount; j++) + for (var j = 0; j < ColumnCount; j++) { - this.At(i, j, 0); + At(i, j, 0); } } } @@ -1472,12 +1472,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The transpose of this matrix. public virtual Matrix Transpose() { - var ret = this.CreateMatrix(this.ColumnCount, this.RowCount); - for (var j = 0; j < this.ColumnCount; j++) + var ret = CreateMatrix(ColumnCount, RowCount); + for (var j = 0; j < ColumnCount; j++) { - for (var i = 0; i < this.RowCount; i++) + for (var i = 0; i < RowCount; i++) { - ret.At(j, i, this.At(i, j)); + ret.At(j, i, At(i, j)); } } @@ -1490,7 +1490,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The row permutation to apply to this matrix. public virtual void PermuteRows(Permutation p) { - if (p.Dimension != this.RowCount) + if (p.Dimension != RowCount) { throw new ArgumentException(Resources.ArgumentArraysSameLength, "p"); } @@ -1503,11 +1503,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double if (inv[i] != i) { var q = inv[i]; - for (var j = 0; j < this.ColumnCount; j++) + for (var j = 0; j < ColumnCount; j++) { - var temp = this.At(q, j); - this.At(q, j, this.At(i, j)); - this.At(i, j, temp); + var temp = At(q, j); + At(q, j, At(i, j)); + At(i, j, temp); } } } @@ -1519,7 +1519,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The column permutation to apply to this matrix. public virtual void PermuteColumns(Permutation p) { - if (p.Dimension != this.ColumnCount) + if (p.Dimension != ColumnCount) { throw new ArgumentException(Resources.ArgumentArraysSameLength, "p"); } @@ -1532,11 +1532,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double if (inv[i] != i) { var q = inv[i]; - for (var j = 0; j < this.RowCount; j++) + for (var j = 0; j < RowCount; j++) { - var temp = this.At(j, q); - this.At(j, q, this.At(j, i)); - this.At(j, i, temp); + var temp = At(j, q); + At(j, q, At(j, i)); + At(j, i, temp); } } } @@ -1554,13 +1554,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("right"); } - if (right.RowCount != this.RowCount) + if (right.RowCount != RowCount) { throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension); } - var result = this.CreateMatrix(this.RowCount, this.ColumnCount + right.ColumnCount); - this.Append(right, result); + var result = CreateMatrix(RowCount, ColumnCount + right.ColumnCount); + Append(right, result); return result; } @@ -1576,7 +1576,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("right"); } - if (right.RowCount != this.RowCount) + if (right.RowCount != RowCount) { throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension); } @@ -1586,7 +1586,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (result.ColumnCount != (this.ColumnCount + right.ColumnCount) || result.RowCount != this.RowCount) + if (result.ColumnCount != (ColumnCount + right.ColumnCount) || result.RowCount != RowCount) { throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension); } @@ -1594,21 +1594,21 @@ namespace MathNet.Numerics.LinearAlgebra.Double CommonParallel.Invoke( () => { - for (var i = 0; i < this.RowCount; i++) + for (var i = 0; i < RowCount; i++) { - for (var j = 0; j < this.ColumnCount; j++) + for (var j = 0; j < ColumnCount; j++) { - result.At(i, j, this.At(i, j)); + result.At(i, j, At(i, j)); } } }, () => { - for (var i = 0; i < this.RowCount; i++) + for (var i = 0; i < RowCount; i++) { for (var j = 0; j < right.ColumnCount; j++) { - result.At(i, j + this.ColumnCount, right.At(i, j)); + result.At(i, j + ColumnCount, right.At(i, j)); } } }); @@ -1628,13 +1628,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("lower"); } - if (lower.ColumnCount != this.ColumnCount) + if (lower.ColumnCount != ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension, "lower"); } - var result = this.CreateMatrix(this.RowCount + lower.RowCount, this.ColumnCount); - this.Stack(lower, result); + var result = CreateMatrix(RowCount + lower.RowCount, ColumnCount); + Stack(lower, result); return result; } @@ -1652,7 +1652,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("lower"); } - if (lower.ColumnCount != this.ColumnCount) + if (lower.ColumnCount != ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension, "lower"); } @@ -1662,7 +1662,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (result.RowCount != (this.RowCount + lower.RowCount) || result.ColumnCount != this.ColumnCount) + if (result.RowCount != (RowCount + lower.RowCount) || result.ColumnCount != ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); } @@ -1670,11 +1670,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double CommonParallel.Invoke( () => { - for (var i = 0; i < this.RowCount; i++) + for (var i = 0; i < RowCount; i++) { - for (var j = 0; j < this.ColumnCount; j++) + for (var j = 0; j < ColumnCount; j++) { - result.At(i, j, this.At(i, j)); + result.At(i, j, At(i, j)); } } }, @@ -1682,9 +1682,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double { for (var i = 0; i < lower.RowCount; i++) { - for (var j = 0; j < this.ColumnCount; j++) + for (var j = 0; j < ColumnCount; j++) { - result.At(i + this.RowCount, j, lower.At(i, j)); + result.At(i + RowCount, j, lower.At(i, j)); } } }); @@ -1705,8 +1705,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("lower"); } - var result = this.CreateMatrix(this.RowCount + lower.RowCount, this.ColumnCount + lower.ColumnCount); - this.DiagonalStack(lower, result); + var result = CreateMatrix(RowCount + lower.RowCount, ColumnCount + lower.ColumnCount); + DiagonalStack(lower, result); return result; } @@ -1730,7 +1730,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (result.RowCount != this.RowCount + lower.RowCount || result.ColumnCount != this.ColumnCount + lower.ColumnCount) + if (result.RowCount != RowCount + lower.RowCount || result.ColumnCount != ColumnCount + lower.ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); } @@ -1738,11 +1738,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double CommonParallel.Invoke( () => { - for (var i = 0; i < this.RowCount; i++) + for (var i = 0; i < RowCount; i++) { - for (var j = 0; j < this.ColumnCount; j++) + for (var j = 0; j < ColumnCount; j++) { - result.At(i, j, this.At(i, j)); + result.At(i, j, At(i, j)); } } }, @@ -1752,10 +1752,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double { for (var j = 0; j < lower.ColumnCount; j++) { - result.At(i + this.RowCount, j + this.ColumnCount, lower.At(i, j)); + result.At(i + RowCount, j + ColumnCount, lower.At(i, j)); } } }); } } -} \ No newline at end of file +} diff --git a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs index 8e94a045..fb541ad7 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs @@ -84,7 +84,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public SparseMatrix(int rows, int columns) : base(rows, columns) { - this._rowIndex = new int[rows]; + _rowIndex = new int[rows]; } /// @@ -116,10 +116,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double } NonZerosCount = rows * columns; - this._nonZeroValues = new double[NonZerosCount]; - this._columnIndices = new int[NonZerosCount]; + _nonZeroValues = new double[NonZerosCount]; + _columnIndices = new int[NonZerosCount]; - for (int i = 0, j = 0; i < this._nonZeroValues.Length; i++, j++) + for (int i = 0, j = 0; i < _nonZeroValues.Length; i++, j++) { // Reset column position to "0" if (j == columns) @@ -127,14 +127,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double j = 0; } - this._nonZeroValues[i] = value; - this._columnIndices[i] = j; + _nonZeroValues[i] = value; + _columnIndices[i] = j; } // Set proper row pointers - for (var i = 0; i < this._rowIndex.Length; i++) + for (var i = 0; i < _rowIndex.Length; i++) { - this._rowIndex[i] = ((i + 1) * columns) - columns; + _rowIndex[i] = ((i + 1) * columns) - columns; } } @@ -143,7 +143,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// /// The number of rows. /// The number of columns. - /// The one dimensional array to create this matrix from. This array should store the matrix in column-major order. + /// The one dimensional array to create this matrix from. This array should store the matrix in column-major order. /// If length is less than * . /// public SparseMatrix(int rows, int columns, double[] array) : this(rows, columns) @@ -157,7 +157,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double { for (var j = 0; j < columns; j++) { - this.SetValueAt(i, j, array[i + (j * rows)]); + SetValueAt(i, j, array[i + (j * rows)]); } } } @@ -175,7 +175,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double { for (var j = 0; j < columns; j++) { - this.SetValueAt(i, j, array[i, j]); + SetValueAt(i, j, array[i, j]); } } } @@ -215,20 +215,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The lower triangle of this matrix. public override Matrix LowerTriangle() { - var result = this.CreateMatrix(this.RowCount, this.ColumnCount); - for (var row = 0; row < result.RowCount; row++) - { - var startIndex = this._rowIndex[row]; - var endIndex = row < this._rowIndex.Length - 1 ? this._rowIndex[row + 1] : this.NonZerosCount; - for (var j = startIndex; j < endIndex; j++) - { - if (row >= this._columnIndices[j]) - { - result.At(row, this._columnIndices[j], this._nonZeroValues[j]); - } - } - } - + var result = CreateMatrix(RowCount, ColumnCount); + LowerTriangleImpl(result); return result; } @@ -245,7 +233,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (result.RowCount != this.RowCount || result.ColumnCount != this.ColumnCount) + if (result.RowCount != RowCount || result.ColumnCount != ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); } @@ -253,47 +241,44 @@ namespace MathNet.Numerics.LinearAlgebra.Double if (ReferenceEquals(this, result)) { var tmp = result.CreateMatrix(result.RowCount, result.ColumnCount); - this.LowerTriangle(tmp); + LowerTriangle(tmp); tmp.CopyTo(result); } else { result.Clear(); - for (var row = 0; row < result.RowCount; row++) - { - var startIndex = this._rowIndex[row]; - var endIndex = row < this._rowIndex.Length - 1 ? this._rowIndex[row + 1] : this.NonZerosCount; - for (var j = startIndex; j < endIndex; j++) - { - if (row >= this._columnIndices[j]) - { - result.At(row, this._columnIndices[j], this._nonZeroValues[j]); - } - } - } + LowerTriangleImpl(result); } } /// - /// Returns a new matrix containing the upper triangle of this matrix. + /// Puts the lower triangle of this matrix into the result matrix. /// - /// The upper triangle of this matrix. - public override Matrix UpperTriangle() + /// Where to store the lower triangle. + private void LowerTriangleImpl(Matrix result) { - var result = this.CreateMatrix(this.RowCount, this.ColumnCount); for (var row = 0; row < result.RowCount; row++) { - var startIndex = this._rowIndex[row]; - var endIndex = row < this._rowIndex.Length - 1 ? this._rowIndex[row + 1] : this.NonZerosCount; + var startIndex = _rowIndex[row]; + var endIndex = row < _rowIndex.Length - 1 ? _rowIndex[row + 1] : NonZerosCount; for (var j = startIndex; j < endIndex; j++) { - if (row <= this._columnIndices[j]) + if (row >= _columnIndices[j]) { - result.At(row, this._columnIndices[j], this._nonZeroValues[j]); + result.At(row, _columnIndices[j], _nonZeroValues[j]); } } } + } + /// + /// Returns a new matrix containing the upper triangle of this matrix. + /// + /// The upper triangle of this matrix. + public override Matrix UpperTriangle() + { + var result = CreateMatrix(RowCount, ColumnCount); + UpperTriangleImpl(result); return result; } @@ -310,7 +295,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (result.RowCount != this.RowCount || result.ColumnCount != this.ColumnCount) + if (result.RowCount != RowCount || result.ColumnCount != ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); } @@ -318,22 +303,31 @@ namespace MathNet.Numerics.LinearAlgebra.Double if (ReferenceEquals(this, result)) { var tmp = result.CreateMatrix(result.RowCount, result.ColumnCount); - this.UpperTriangle(tmp); + UpperTriangle(tmp); tmp.CopyTo(result); } else { result.Clear(); - for (var row = 0; row < result.RowCount; row++) + UpperTriangleImpl(result); + } + } + + /// + /// Puts the upper triangle of this matrix into the result matrix. + /// + /// Where to store the lower triangle. + private void UpperTriangleImpl(Matrix result) + { + for (var row = 0; row < result.RowCount; row++) + { + var startIndex = _rowIndex[row]; + var endIndex = row < _rowIndex.Length - 1 ? _rowIndex[row + 1] : NonZerosCount; + for (var j = startIndex; j < endIndex; j++) { - var startIndex = this._rowIndex[row]; - var endIndex = row < this._rowIndex.Length - 1 ? this._rowIndex[row + 1] : this.NonZerosCount; - for (var j = startIndex; j < endIndex; j++) + if (row <= _columnIndices[j]) { - if (row <= this._columnIndices[j]) - { - result.At(row, this._columnIndices[j], this._nonZeroValues[j]); - } + result.At(row, _columnIndices[j], _nonZeroValues[j]); } } } @@ -357,12 +351,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// is not positive. public override Matrix SubMatrix(int rowIndex, int rowLength, int columnIndex, int columnLength) { - if (rowIndex >= this.RowCount || rowIndex < 0) + if (rowIndex >= RowCount || rowIndex < 0) { throw new ArgumentOutOfRangeException("rowIndex"); } - if (columnIndex >= this.ColumnCount || columnIndex < 0) + if (columnIndex >= ColumnCount || columnIndex < 0) { throw new ArgumentOutOfRangeException("columnIndex"); } @@ -380,30 +374,30 @@ namespace MathNet.Numerics.LinearAlgebra.Double var colMax = columnIndex + columnLength; var rowMax = rowIndex + rowLength; - if (rowMax > this.RowCount) + if (rowMax > RowCount) { throw new ArgumentOutOfRangeException("rowLength"); } - if (colMax > this.ColumnCount) + if (colMax > ColumnCount) { throw new ArgumentOutOfRangeException("columnLength"); } - var result = this.CreateMatrix(rowLength, columnLength); + var result = (SparseMatrix)CreateMatrix(rowLength, columnLength); for (int i = rowIndex, row = 0; i < rowMax; i++, row++) { - var startIndex = this._rowIndex[i]; - var endIndex = row < this._rowIndex.Length - 1 ? this._rowIndex[i + 1] : this.NonZerosCount; + var startIndex = _rowIndex[i]; + var endIndex = row < _rowIndex.Length - 1 ? _rowIndex[i + 1] : NonZerosCount; for (int j = startIndex; j < endIndex; j++) { // check if the column index is in the range - if ((this._columnIndices[j] >= columnIndex) && (this._columnIndices[j] < columnIndex + columnLength)) + if ((_columnIndices[j] >= columnIndex) && (_columnIndices[j] < columnIndex + columnLength)) { - var column = this._columnIndices[j] - columnIndex; - result[row, column] = this._nonZeroValues[j]; + var column = _columnIndices[j] - columnIndex; + result.SetValueAt(row, column, _nonZeroValues[j]); } } } @@ -418,20 +412,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The lower triangle of this matrix. public override Matrix StrictlyLowerTriangle() { - var result = this.CreateMatrix(this.RowCount, this.ColumnCount); - for (var row = 0; row < result.RowCount; row++) - { - var startIndex = this._rowIndex[row]; - var endIndex = row < this._rowIndex.Length - 1 ? this._rowIndex[row + 1] : this.NonZerosCount; - for (var j = startIndex; j < endIndex; j++) - { - if (row > this._columnIndices[j]) - { - result.At(row, this._columnIndices[j], this._nonZeroValues[j]); - } - } - } - + var result = CreateMatrix(RowCount, ColumnCount); + StrictlyLowerTriangleImpl(result); return result; } @@ -448,7 +430,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (result.RowCount != this.RowCount || result.ColumnCount != this.ColumnCount) + if (result.RowCount != RowCount || result.ColumnCount != ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); } @@ -456,48 +438,45 @@ namespace MathNet.Numerics.LinearAlgebra.Double if (ReferenceEquals(this, result)) { var tmp = result.CreateMatrix(result.RowCount, result.ColumnCount); - this.StrictlyLowerTriangle(tmp); + StrictlyLowerTriangle(tmp); tmp.CopyTo(result); } else { result.Clear(); - for (var row = 0; row < result.RowCount; row++) - { - var startIndex = this._rowIndex[row]; - var endIndex = row < this._rowIndex.Length - 1 ? this._rowIndex[row + 1] : this.NonZerosCount; - for (var j = startIndex; j < endIndex; j++) - { - if (row > this._columnIndices[j]) - { - result.At(row, this._columnIndices[j], this._nonZeroValues[j]); - } - } - } + StrictlyLowerTriangleImpl(result); } } /// - /// Returns a new matrix containing the upper triangle of this matrix. The new matrix - /// does not contain the diagonal elements of this matrix. + /// Puts the strictly lower triangle of this matrix into the result matrix. /// - /// The upper triangle of this matrix. - public override Matrix StrictlyUpperTriangle() + /// Where to store the lower triangle. + private void StrictlyLowerTriangleImpl(Matrix result) { - var result = this.CreateMatrix(this.RowCount, this.ColumnCount); for (var row = 0; row < result.RowCount; row++) { - var startIndex = this._rowIndex[row]; - var endIndex = row < this._rowIndex.Length - 1 ? this._rowIndex[row + 1] : this.NonZerosCount; + var startIndex = _rowIndex[row]; + var endIndex = row < _rowIndex.Length - 1 ? _rowIndex[row + 1] : NonZerosCount; for (var j = startIndex; j < endIndex; j++) { - if (row < this._columnIndices[j]) + if (row > _columnIndices[j]) { - result.At(row, this._columnIndices[j], this._nonZeroValues[j]); + result.At(row, _columnIndices[j], _nonZeroValues[j]); } } } + } + /// + /// Returns a new matrix containing the upper triangle of this matrix. The new matrix + /// does not contain the diagonal elements of this matrix. + /// + /// The upper triangle of this matrix. + public override Matrix StrictlyUpperTriangle() + { + var result = CreateMatrix(RowCount, ColumnCount); + StrictlyUpperTriangleImpl(result); return result; } @@ -514,7 +493,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (result.RowCount != this.RowCount || result.ColumnCount != this.ColumnCount) + if (result.RowCount != RowCount || result.ColumnCount != ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); } @@ -522,27 +501,60 @@ namespace MathNet.Numerics.LinearAlgebra.Double if (ReferenceEquals(this, result)) { var tmp = result.CreateMatrix(result.RowCount, result.ColumnCount); - this.StrictlyUpperTriangle(tmp); + StrictlyUpperTriangle(tmp); tmp.CopyTo(result); } else { result.Clear(); - for (var row = 0; row < result.RowCount; row++) + StrictlyUpperTriangleImpl(result); + } + } + + /// + /// Puts the strictly upper triangle of this matrix into the result matrix. + /// + /// Where to store the lower triangle. + private void StrictlyUpperTriangleImpl(Matrix result) + { + for (var row = 0; row < result.RowCount; row++) + { + var startIndex = _rowIndex[row]; + var endIndex = row < _rowIndex.Length - 1 ? _rowIndex[row + 1] : NonZerosCount; + for (var j = startIndex; j < endIndex; j++) { - var startIndex = this._rowIndex[row]; - var endIndex = row < this._rowIndex.Length - 1 ? this._rowIndex[row + 1] : this.NonZerosCount; - for (var j = startIndex; j < endIndex; j++) + if (row < _columnIndices[j]) { - if (row < this._columnIndices[j]) - { - result.At(row, this._columnIndices[j], this._nonZeroValues[j]); - } + result.At(row, _columnIndices[j], _nonZeroValues[j]); } } } } + /// + /// Returns the matrix's elements as an array with the data laid out column-wise. + /// + ///
+        /// 1, 2, 3
+        /// 4, 5, 6  will be returned as  1, 4, 7, 2, 5, 8, 3, 6, 9
+        /// 7, 8, 9
+        /// 
+ /// An array containing the matrix's elements. + public override double[] ToColumnWiseArray() + { + var ret = new double[RowCount * ColumnCount]; + for (var j = 0; j < ColumnCount; j++) + { + for (var i = 0; i < RowCount; i++) + { + var index = FindItem(i, j); + ret[(j * RowCount) + i] = index >= 0 ? _nonZeroValues[index] : 0.0; + } + } + + return ret; + } + /// /// Retrieves the requested element without range checking. /// @@ -557,10 +569,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public override double At(int row, int column) { - lock (this._lockObject) + lock (_lockObject) { - var index = this.FindItem(row, column); - return index >= 0 ? this._nonZeroValues[index] : 0.0; + var index = FindItem(row, column); + return index >= 0 ? _nonZeroValues[index] : 0.0; } } @@ -578,9 +590,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public override void At(int row, int column, double value) { - lock (this._lockObject) + lock (_lockObject) { - this.SetValueAt(row, column, value); + SetValueAt(row, column, value); } } @@ -594,62 +606,62 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// WARNING: This method is not thread safe. Use "lock" with it and be sure to avoid deadlocks private void SetValueAt(int row, int column, double value) { - var index = this.FindItem(row, column); - if (index >= 0) + var index = FindItem(row, column); + if (index >= 0) + { + // Non-zero item found in matrix + if (value == 0.0) { - // Non-zero item found in matrix - if (value == 0.0) - { - // Delete existing item - this.DeleteItemByIndex(index, row); - } - else - { - // Update item - this._nonZeroValues[index] = value; - } + // Delete existing item + DeleteItemByIndex(index, row); } - else + else { - // Item not found. Add new value - if (value == 0.0) - { - return; - } + // Update item + _nonZeroValues[index] = value; + } + } + else + { + // Item not found. Add new value + if (value == 0.0) + { + return; + } - index = ~index; - - // Check if the storage needs to be increased - if ((this.NonZerosCount == this._nonZeroValues.Length) && (this.NonZerosCount < (this.RowCount * this.ColumnCount))) - { - // Value array is completely full so we increase the size - // Determine the increase in size. We will not grow beyond the size of the matrix - var size = Math.Min(this._nonZeroValues.Length + this.GrowthSize(), this.RowCount * this.ColumnCount); - Array.Resize(ref this._nonZeroValues, size); - Array.Resize(ref this._columnIndices, size); - } + index = ~index; - // Move all values (with an position larger than index) in the value array to the next position - // move all values (with an position larger than index) in the columIndices array to the next position - for (var i = this.NonZerosCount - 1; i > index - 1; i--) - { - this._nonZeroValues[i + 1] = this._nonZeroValues[i]; - this._columnIndices[i + 1] = this._columnIndices[i]; - } + // Check if the storage needs to be increased + if ((NonZerosCount == _nonZeroValues.Length) && (NonZerosCount < (RowCount * ColumnCount))) + { + // Value array is completely full so we increase the size + // Determine the increase in size. We will not grow beyond the size of the matrix + var size = Math.Min(_nonZeroValues.Length + GrowthSize(), RowCount * ColumnCount); + Array.Resize(ref _nonZeroValues, size); + Array.Resize(ref _columnIndices, size); + } - // Add the value and the column index - this._nonZeroValues[index] = value; - this._columnIndices[index] = column; + // Move all values (with an position larger than index) in the value array to the next position + // move all values (with an position larger than index) in the columIndices array to the next position + for (var i = NonZerosCount - 1; i > index - 1; i--) + { + _nonZeroValues[i + 1] = _nonZeroValues[i]; + _columnIndices[i + 1] = _columnIndices[i]; + } - // increase the number of non-zero numbers by one - this.NonZerosCount += 1; + // Add the value and the column index + _nonZeroValues[index] = value; + _columnIndices[index] = column; - // add 1 to all the row indices for rows bigger than rowIndex - // so that they point to the correct part of the value array again. - for (var i = row + 1; i < this._rowIndex.Length; i++) - { - this._rowIndex[i] += 1; - } + // increase the number of non-zero numbers by one + NonZerosCount += 1; + + // add 1 to all the row indices for rows bigger than rowIndex + // so that they point to the correct part of the value array again. + for (var i = row + 1; i < _rowIndex.Length; i++) + { + _rowIndex[i] += 1; + } } } @@ -663,26 +675,26 @@ namespace MathNet.Numerics.LinearAlgebra.Double { // Move all values (with an position larger than index) in the value array to the previous position // move all values (with an position larger than index) in the columIndices array to the previous position - for (var i = itemIndex + 1; i < this.NonZerosCount; i++) + for (var i = itemIndex + 1; i < NonZerosCount; i++) { - this._nonZeroValues[i - 1] = this._nonZeroValues[i]; - this._columnIndices[i - 1] = this._columnIndices[i]; + _nonZeroValues[i - 1] = _nonZeroValues[i]; + _columnIndices[i - 1] = _columnIndices[i]; } // Decrease value in Row - for (var i = row + 1; i < this._rowIndex.Length; i++) + for (var i = row + 1; i < _rowIndex.Length; i++) { - this._rowIndex[i] -= 1; + _rowIndex[i] -= 1; } - this.NonZerosCount -= 1; + NonZerosCount -= 1; // Check if the storage needs to be shrink. This is reasonable to do if // there are a lot of non-zero elements and storage is two times bigger - if ((this.NonZerosCount > 1024) && (this.NonZerosCount < this._nonZeroValues.Length / 2)) + if ((NonZerosCount > 1024) && (NonZerosCount < _nonZeroValues.Length / 2)) { - Array.Resize(ref this._nonZeroValues, this.NonZerosCount); - Array.Resize(ref this._columnIndices, this.NonZerosCount); + Array.Resize(ref _nonZeroValues, NonZerosCount); + Array.Resize(ref _columnIndices, NonZerosCount); } } @@ -696,9 +708,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double private int FindItem(int row, int column) { // Determin bounds in columnIndices array where this item should be searched (using rowIndex) - var startIndex = this._rowIndex[row]; - var endIndex = row < this._rowIndex.Length - 1 ? this._rowIndex[row + 1] : this.NonZerosCount; - return Array.BinarySearch(this._columnIndices, startIndex, endIndex - startIndex, column); + var startIndex = _rowIndex[row]; + var endIndex = row < _rowIndex.Length - 1 ? _rowIndex[row + 1] : NonZerosCount; + return Array.BinarySearch(_columnIndices, startIndex, endIndex - startIndex, column); } /// @@ -709,19 +721,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double private int GrowthSize() { int delta; - if (this._nonZeroValues.Length > 1024) + if (_nonZeroValues.Length > 1024) { - delta = this._nonZeroValues.Length / 4; + delta = _nonZeroValues.Length / 4; } else { - if (this._nonZeroValues.Length > 256) + if (_nonZeroValues.Length > 256) { delta = 512; } else { - delta = this._nonZeroValues.Length > 64 ? 128 : 32; + delta = _nonZeroValues.Length > 64 ? 128 : 32; } } @@ -734,8 +746,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public override void Clear() { - this.NonZerosCount = 0; - Array.Clear(this._rowIndex, 0, this._rowIndex.Length); + NonZerosCount = 0; + Array.Clear(_rowIndex, 0, _rowIndex.Length); } /// @@ -765,19 +777,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double return; } - if (this.RowCount != target.RowCount || this.ColumnCount != target.ColumnCount) + if (RowCount != target.RowCount || ColumnCount != target.ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions, "target"); } // Lets copy only needed data. Portion of needed data is determined by NonZerosCount value - sparseTarget._nonZeroValues = new double[this.NonZerosCount]; - sparseTarget._columnIndices = new int[this.NonZerosCount]; - sparseTarget.NonZerosCount = this.NonZerosCount; + sparseTarget._nonZeroValues = new double[NonZerosCount]; + sparseTarget._columnIndices = new int[NonZerosCount]; + sparseTarget.NonZerosCount = NonZerosCount; - Buffer.BlockCopy(this._nonZeroValues, 0, sparseTarget._nonZeroValues, 0, this.NonZerosCount * Constants.SizeOfDouble); - Buffer.BlockCopy(this._columnIndices, 0, sparseTarget._columnIndices, 0, this.NonZerosCount * Constants.SizeOfInt); - Buffer.BlockCopy(this._rowIndex, 0, sparseTarget._rowIndex, 0, this.RowCount * Constants.SizeOfInt); + Buffer.BlockCopy(_nonZeroValues, 0, sparseTarget._nonZeroValues, 0, NonZerosCount * Constants.SizeOfDouble); + Buffer.BlockCopy(_columnIndices, 0, sparseTarget._columnIndices, 0, NonZerosCount * Constants.SizeOfInt); + Buffer.BlockCopy(_rowIndex, 0, sparseTarget._rowIndex, 0, RowCount * Constants.SizeOfInt); } } @@ -799,21 +811,21 @@ namespace MathNet.Numerics.LinearAlgebra.Double return base.Equals(obj); } - // Accept if the argument is the same object as this. + // Accept if the argument is the same object as this if (ReferenceEquals(this, sparseMatrix)) { return true; } - if (this.ColumnCount != sparseMatrix.ColumnCount || this.RowCount != sparseMatrix.RowCount || this.NonZerosCount != sparseMatrix.NonZerosCount) + if (ColumnCount != sparseMatrix.ColumnCount || RowCount != sparseMatrix.RowCount || NonZerosCount != sparseMatrix.NonZerosCount) { return false; } // If all else fails, perform element wise comparison. - for (var index = 0; index < this.NonZerosCount; index++) + for (var index = 0; index < NonZerosCount; index++) { - if (!this._nonZeroValues[index].AlmostEqual(sparseMatrix._nonZeroValues[index]) || this._columnIndices[index] != sparseMatrix._columnIndices[index]) + if (!_nonZeroValues[index].AlmostEqual(sparseMatrix._nonZeroValues[index]) || _columnIndices[index] != sparseMatrix._columnIndices[index]) { return false; } @@ -830,14 +842,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public override int GetHashCode() { - var hashNum = Math.Min(this.NonZerosCount, 25); + var hashNum = Math.Min(NonZerosCount, 25); long hash = 0; for (var i = 0; i < hashNum; i++) { #if SILVERLIGHT - hash ^= Precision.DoubleToInt64Bits(this._nonZeroValues[i]); + hash ^= Precision.DoubleToInt64Bits(_nonZeroValues[i]); #else - hash ^= BitConverter.DoubleToInt64Bits(this._nonZeroValues[i]); + hash ^= BitConverter.DoubleToInt64Bits(_nonZeroValues[i]); #endif } @@ -850,14 +862,18 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The transpose of this matrix. public override Matrix Transpose() { - var ret = new SparseMatrix(this.ColumnCount, this.RowCount); - + var ret = new SparseMatrix(ColumnCount, RowCount) + { + _columnIndices = new int[NonZerosCount], + _nonZeroValues = new double[NonZerosCount] + }; + // Do an 'inverse' CopyTo iterate over the rows - for (var i = 0; i < this._rowIndex.Length; i++) + for (var i = 0; i < _rowIndex.Length; i++) { // Get the begin / end index for the current row - var startIndex = this._rowIndex[i]; - var endIndex = i < this._rowIndex.Length - 1 ? this._rowIndex[i + 1] : this.NonZerosCount; + var startIndex = _rowIndex[i]; + var endIndex = i < _rowIndex.Length - 1 ? _rowIndex[i + 1] : NonZerosCount; // Get the values for the current row if (startIndex == endIndex) @@ -868,7 +884,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double for (var j = startIndex; j < endIndex; j++) { - ret[this._columnIndices[j], i] = this._nonZeroValues[j]; + ret.SetValueAt(_columnIndices[j], i, _nonZeroValues[j]); } } @@ -898,17 +914,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (rowIndex >= this.RowCount || rowIndex < 0) + if (rowIndex >= RowCount || rowIndex < 0) { throw new ArgumentOutOfRangeException("rowIndex"); } - if (columnIndex >= this.ColumnCount || columnIndex < 0) + if (columnIndex >= ColumnCount || columnIndex < 0) { throw new ArgumentOutOfRangeException("columnIndex"); } - if (columnIndex + length > this.ColumnCount) + if (columnIndex + length > ColumnCount) { throw new ArgumentOutOfRangeException("length"); } @@ -923,22 +939,22 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } - // Determin bounds in columnIndices array where this item should be searched (using rowIndex) - var startIndex = this._rowIndex[rowIndex]; - var endIndex = rowIndex < this._rowIndex.Length - 1 ? this._rowIndex[rowIndex + 1] : this.NonZerosCount; + // Determine bounds in columnIndices array where this item should be searched (using rowIndex) + var startIndex = _rowIndex[rowIndex]; + var endIndex = rowIndex < _rowIndex.Length - 1 ? _rowIndex[rowIndex + 1] : NonZerosCount; if (startIndex == endIndex) { - // TODO: Maybe it is reasonable to add "Clear" method in Vector class? - // Only zero elements in rowIndex row. Clear the vector - result.Multiply(0); + result.Clear(); } else { // If there are non-zero elements use base class implementation for (int i = columnIndex, j = 0; i < columnIndex + length; i++, j++) { - result[j] = this.At(rowIndex, i); + // Copy code from At(row, column) to avoid unnecessary lock + var index = FindItem(rowIndex, i); + result[j] = index >= 0 ? _nonZeroValues[index] : 0.0; } } } @@ -950,7 +966,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The combined matrix /// If lower is . /// If the result matrix is . - /// If the result matrix's dimensions are not (this.Rows + lower.rows) x (this.Columns + lower.Columns). + /// If the result matrix's dimensions are not (Rows + lower.rows) x (Columns + lower.Columns). public override void DiagonalStack(Matrix lower, Matrix result) { var lowerSparseMatrix = lower as SparseMatrix; @@ -962,30 +978,30 @@ namespace MathNet.Numerics.LinearAlgebra.Double } else { - if (resultSparseMatrix.RowCount != this.RowCount + lowerSparseMatrix.RowCount || resultSparseMatrix.ColumnCount != this.ColumnCount + lowerSparseMatrix.ColumnCount) + if (resultSparseMatrix.RowCount != RowCount + lowerSparseMatrix.RowCount || resultSparseMatrix.ColumnCount != ColumnCount + lowerSparseMatrix.ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); } - resultSparseMatrix.NonZerosCount = this.NonZerosCount + lowerSparseMatrix.NonZerosCount; + resultSparseMatrix.NonZerosCount = NonZerosCount + lowerSparseMatrix.NonZerosCount; resultSparseMatrix._nonZeroValues = new double[resultSparseMatrix.NonZerosCount]; resultSparseMatrix._columnIndices = new int[resultSparseMatrix.NonZerosCount]; - Array.Copy(this._nonZeroValues, 0, resultSparseMatrix._nonZeroValues, 0, this.NonZerosCount); - Array.Copy(lowerSparseMatrix._nonZeroValues, 0, resultSparseMatrix._nonZeroValues, this.NonZerosCount, lowerSparseMatrix.NonZerosCount); + Array.Copy(_nonZeroValues, 0, resultSparseMatrix._nonZeroValues, 0, NonZerosCount); + Array.Copy(lowerSparseMatrix._nonZeroValues, 0, resultSparseMatrix._nonZeroValues, NonZerosCount, lowerSparseMatrix.NonZerosCount); - Array.Copy(this._columnIndices, 0, resultSparseMatrix._columnIndices, 0, this.NonZerosCount); - Array.Copy(this._rowIndex, 0, resultSparseMatrix._rowIndex, 0, this.RowCount); + Array.Copy(_columnIndices, 0, resultSparseMatrix._columnIndices, 0, NonZerosCount); + Array.Copy(_rowIndex, 0, resultSparseMatrix._rowIndex, 0, RowCount); // Copy and adjust lower column indices and rowIndex - for (int i = this.NonZerosCount, j = 0; i < resultSparseMatrix.NonZerosCount; i++, j++) + for (int i = NonZerosCount, j = 0; i < resultSparseMatrix.NonZerosCount; i++, j++) { - resultSparseMatrix._columnIndices[i] = lowerSparseMatrix._columnIndices[j] + this.ColumnCount; + resultSparseMatrix._columnIndices[i] = lowerSparseMatrix._columnIndices[j] + ColumnCount; } - for (int i = this.RowCount, j = 0; i < resultSparseMatrix.RowCount; i++, j++) + for (int i = RowCount, j = 0; i < resultSparseMatrix.RowCount; i++, j++) { - resultSparseMatrix._rowIndex[i] = lowerSparseMatrix._rowIndex[j] + this.NonZerosCount; + resultSparseMatrix._rowIndex[i] = lowerSparseMatrix._rowIndex[j] + NonZerosCount; } } } @@ -1002,7 +1018,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double { if (ReferenceEquals(this, other)) { - this.Multiply(2); + Multiply(2); return; } @@ -1030,7 +1046,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (other.RowCount != this.RowCount || other.ColumnCount != this.ColumnCount) + if (other.RowCount != RowCount || other.ColumnCount != ColumnCount) { throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions); } @@ -1043,21 +1059,21 @@ namespace MathNet.Numerics.LinearAlgebra.Double for (var j = startIndex; j < endIndex; j++) { - var index = this.FindItem(i, other._columnIndices[j]); + var index = FindItem(i, other._columnIndices[j]); if (index >= 0) { - if (this._nonZeroValues[index] + other._nonZeroValues[j] == 0.0) + if (_nonZeroValues[index] + other._nonZeroValues[j] == 0.0) { - this.DeleteItemByIndex(index, i); + DeleteItemByIndex(index, i); } else { - this._nonZeroValues[index] += other._nonZeroValues[j]; + _nonZeroValues[index] += other._nonZeroValues[j]; } } else { - this.SetValueAt(i, other._columnIndices[j], other._nonZeroValues[j]); + SetValueAt(i, other._columnIndices[j], other._nonZeroValues[j]); } } } @@ -1074,7 +1090,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double // We are substracting Matrix form itself if (ReferenceEquals(this, other)) { - this.Clear(); + Clear(); return; } @@ -1085,7 +1101,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double } else { - this.Subtract(m); + Subtract(m); } } @@ -1102,7 +1118,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (other.RowCount != this.RowCount || other.ColumnCount != this.ColumnCount) + if (other.RowCount != RowCount || other.ColumnCount != ColumnCount) { throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions); } @@ -1115,21 +1131,21 @@ namespace MathNet.Numerics.LinearAlgebra.Double for (var j = startIndex; j < endIndex; j++) { - var index = this.FindItem(i, other._columnIndices[j]); + var index = FindItem(i, other._columnIndices[j]); if (index >= 0) { - if (this._nonZeroValues[index] - other._nonZeroValues[j] == 0.0) + if (_nonZeroValues[index] - other._nonZeroValues[j] == 0.0) { - this.DeleteItemByIndex(index, i); + DeleteItemByIndex(index, i); } else { - this._nonZeroValues[index] -= other._nonZeroValues[j]; + _nonZeroValues[index] -= other._nonZeroValues[j]; } } else { - this.SetValueAt(i, other._columnIndices[j], -other._nonZeroValues[j]); + SetValueAt(i, other._columnIndices[j], -other._nonZeroValues[j]); } } } @@ -1148,11 +1164,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double if (0.0.AlmostEqualInDecimalPlaces(scalar, 15)) { - this.Clear(); + Clear(); return; } - Control.LinearAlgebraProvider.ScaleArray(scalar, this._nonZeroValues); + Control.LinearAlgebraProvider.ScaleArray(scalar, _nonZeroValues); } /// @@ -1162,8 +1178,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The result of the multiplication. /// If the other matrix is . /// If the result matrix is . - /// If this.Columns != other.Rows. - /// If the result matrix's dimensions are not the this.Rows x other.Columns. + /// If Columns != other.Rows. + /// If the result matrix's dimensions are not the Rows x other.Columns. public override void Multiply(Matrix other, Matrix result) { var otherSparseMatrix = other as SparseMatrix; @@ -1175,40 +1191,37 @@ namespace MathNet.Numerics.LinearAlgebra.Double } else { - if (this.ColumnCount != otherSparseMatrix.RowCount) + if (ColumnCount != otherSparseMatrix.RowCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions); } - if (resultSparseMatrix.RowCount != this.RowCount || resultSparseMatrix.ColumnCount != otherSparseMatrix.ColumnCount) + if (resultSparseMatrix.RowCount != RowCount || resultSparseMatrix.ColumnCount != otherSparseMatrix.ColumnCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions); } resultSparseMatrix.Clear(); - var columnVector = new SparseVector(otherSparseMatrix.RowCount); - for (var row = 0; row < this.RowCount; row++) + var columnVector = new DenseVector(otherSparseMatrix.RowCount); + for (var row = 0; row < RowCount; row++) { // Get the begin / end index for the current row - var startIndex = this._rowIndex[row]; - var endIndex = row < this._rowIndex.Length - 1 ? this._rowIndex[row + 1] : this.NonZerosCount; + var startIndex = _rowIndex[row]; + var endIndex = row < _rowIndex.Length - 1 ? _rowIndex[row + 1] : NonZerosCount; + if (startIndex == endIndex) + { + continue; + } + for (var column = 0; column < otherSparseMatrix.ColumnCount; column++) { - columnVector.Clear(); - otherSparseMatrix.Column(column, columnVector); - // Multiply row of matrix A on column of matrix B - var sum = 0.0; - if (startIndex != endIndex) - { - // If there are elements in that row, then calculate rowA x columnB - sum = CommonParallel.Aggregate( - startIndex, - endIndex, - index => this._nonZeroValues[index] * columnVector[this._columnIndices[index]]); - } - + otherSparseMatrix.Column(column, columnVector); + var sum = CommonParallel.Aggregate( + startIndex, + endIndex, + index => _nonZeroValues[index] * columnVector[_columnIndices[index]]); resultSparseMatrix.SetValueAt(row, column, sum); } } @@ -1219,7 +1232,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// Multiplies this matrix with another matrix and returns the result. /// /// The matrix to multiply with. - /// If this.Columns != other.Rows. + /// If Columns != other.Rows. /// If the other matrix is . /// The result of multiplication. public override Matrix Multiply(Matrix other) @@ -1230,16 +1243,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double return base.Multiply(other); } - if (this.ColumnCount != matrix.RowCount) + if (ColumnCount != matrix.RowCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions); } - var result = (SparseMatrix)this.CreateMatrix(this.RowCount, matrix.ColumnCount); - this.Multiply(matrix, result); + var result = (SparseMatrix)CreateMatrix(RowCount, matrix.ColumnCount); + Multiply(matrix, result); return result; } - + /// /// Multiplies two sparse matrices. /// @@ -1289,12 +1302,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (this.ColumnCount != other.ColumnCount || this.RowCount != other.RowCount) + if (ColumnCount != other.ColumnCount || RowCount != other.RowCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); } - if (this.ColumnCount != result.ColumnCount || this.RowCount != result.RowCount) + if (ColumnCount != result.ColumnCount || RowCount != result.RowCount) { throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); } @@ -1303,15 +1316,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double for (var i = 0; i < other.RowCount; i++) { // Get the begin / end index for the current row - var startIndex = this._rowIndex[i]; - var endIndex = i < this._rowIndex.Length - 1 ? this._rowIndex[i + 1] : this.NonZerosCount; + var startIndex = _rowIndex[i]; + var endIndex = i < _rowIndex.Length - 1 ? _rowIndex[i + 1] : NonZerosCount; for (var j = startIndex; j < endIndex; j++) { - var resVal = this._nonZeroValues[j] * other[i, this._columnIndices[j]]; + var resVal = _nonZeroValues[j] * other[i, _columnIndices[j]]; if (resVal != 0.0) { - result[i, this._columnIndices[j]] = resVal; + result[i, _columnIndices[j]] = resVal; } } } @@ -1340,12 +1353,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentException(Resources.ArgumentMustBePositive, "numberOfColumns"); } - var matrix = this.CreateMatrix(numberOfRows, numberOfColumns); - for (var i = 0; i < RowCount; i++) + var matrix = (SparseMatrix)CreateMatrix(numberOfRows, numberOfColumns); + for (var i = 0; i < matrix.RowCount; i++) { for (var j = 0; j < matrix.ColumnCount; j++) { - matrix[i, j] = distribution.Sample(); + var value = distribution.Sample(); + if (value != 0.0) + { + matrix.SetValueAt(i, j, value); + } } } @@ -1375,12 +1392,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentException(Resources.ArgumentMustBePositive, "numberOfColumns"); } - var matrix = this.CreateMatrix(numberOfRows, numberOfColumns); - for (var i = 0; i < RowCount; i++) + var matrix = (SparseMatrix)CreateMatrix(numberOfRows, numberOfColumns); + for (var i = 0; i < matrix.RowCount; i++) { for (var j = 0; j < matrix.ColumnCount; j++) { - matrix[i, j] = distribution.Sample(); + var value = distribution.Sample(); + if (value != 0.0) + { + matrix.SetValueAt(i, j, value); + } } } diff --git a/src/Numerics/LinearAlgebra/Double/SparseVector.cs b/src/Numerics/LinearAlgebra/Double/SparseVector.cs index dc5dec4b..6c77b4bf 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseVector.cs @@ -40,9 +40,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double public class SparseVector : Vector { /// - /// Lock ojbect for the indexer. + /// Lock object for the indexer. /// - private readonly object lockObject = new object(); + private readonly object _lockObject = new object(); /// /// Gets the vector's internal data. The array containing the actual values; only the non-zero values are stored. @@ -96,22 +96,22 @@ namespace MathNet.Numerics.LinearAlgebra.Double { if (value == 0.0) { -// Skip adding values + // Skip adding values return; } // We already know that this vector is "full", let's allocate all needed memory - this._nonZeroValues = new double[size]; - this._nonZeroIndices = new int[size]; - this.NonZerosCount = size; + _nonZeroValues = new double[size]; + _nonZeroIndices = new int[size]; + NonZerosCount = size; CommonParallel.For( 0, - this.Count, + Count, index => { - this._nonZeroValues[index] = value; - this._nonZeroIndices[index] = index; + _nonZeroValues[index] = value; + _nonZeroIndices[index] = index; }); } @@ -134,13 +134,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double } else { - this._nonZeroValues = new double[vector.NonZerosCount]; - this._nonZeroIndices = new int[vector.NonZerosCount]; - this.NonZerosCount = vector.NonZerosCount; + _nonZeroValues = new double[vector.NonZerosCount]; + _nonZeroIndices = new int[vector.NonZerosCount]; + NonZerosCount = vector.NonZerosCount; // Lets copy only needed data. Portion of needed data is determined by NonZerosCount value - Buffer.BlockCopy(vector._nonZeroValues, 0, this._nonZeroValues, 0, vector.NonZerosCount * Constants.SizeOfDouble); - Buffer.BlockCopy(vector._nonZeroIndices, 0, this._nonZeroIndices, 0, vector.NonZerosCount * Constants.SizeOfInt); + Buffer.BlockCopy(vector._nonZeroValues, 0, _nonZeroValues, 0, vector.NonZerosCount * Constants.SizeOfDouble); + Buffer.BlockCopy(vector._nonZeroIndices, 0, _nonZeroIndices, 0, vector.NonZerosCount * Constants.SizeOfInt); } } @@ -154,12 +154,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double public SparseVector(SparseVector other) : this(other.Count) { // Lets copy only needed data. Portion of needed data is determined by NonZerosCount value - this._nonZeroValues = new double[other.NonZerosCount]; - this._nonZeroIndices = new int[other.NonZerosCount]; - this.NonZerosCount = other.NonZerosCount; + _nonZeroValues = new double[other.NonZerosCount]; + _nonZeroIndices = new int[other.NonZerosCount]; + NonZerosCount = other.NonZerosCount; - Buffer.BlockCopy(other._nonZeroValues, 0, this._nonZeroValues, 0, other.NonZerosCount * Constants.SizeOfDouble); - Buffer.BlockCopy(other._nonZeroIndices, 0, this._nonZeroIndices, 0, other.NonZerosCount * Constants.SizeOfInt); + Buffer.BlockCopy(other._nonZeroValues, 0, _nonZeroValues, 0, other.NonZerosCount * Constants.SizeOfDouble); + Buffer.BlockCopy(other._nonZeroIndices, 0, _nonZeroIndices, 0, other.NonZerosCount * Constants.SizeOfInt); } /// @@ -183,10 +183,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// This vector as a column matrix. public override Matrix ToColumnMatrix() { - var matrix = new SparseMatrix(this.Count, 1); - for (var i = 0; i < this.NonZerosCount; i++) + var matrix = new SparseMatrix(Count, 1); + for (var i = 0; i < NonZerosCount; i++) { - matrix[this._nonZeroIndices[i], 0] = this._nonZeroValues[i]; + matrix[_nonZeroIndices[i], 0] = _nonZeroValues[i]; } return matrix; @@ -198,10 +198,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// This vector as a row matrix. public override Matrix ToRowMatrix() { - var matrix = new SparseMatrix(1, this.Count); - for (var i = 0; i < this.NonZerosCount; i++) + var matrix = new SparseMatrix(1, Count); + for (var i = 0; i < NonZerosCount; i++) { - matrix[0, this._nonZeroIndices[i]] = this._nonZeroValues[i]; + matrix[0, _nonZeroIndices[i]] = _nonZeroValues[i]; } return matrix; @@ -217,18 +217,18 @@ namespace MathNet.Numerics.LinearAlgebra.Double get { // If index is out of bounds - if ((index < 0) || (index >= this.Count)) + if ((index < 0) || (index >= Count)) { throw new IndexOutOfRangeException(); } - lock (this.lockObject) + lock (_lockObject) { // Search if item idex exists in NonZeroIndices array in range "0 - real nonzero values count" - var itemIndex = Array.BinarySearch(this._nonZeroIndices, 0, this.NonZerosCount, index); + var itemIndex = Array.BinarySearch(_nonZeroIndices, 0, NonZerosCount, index); if (itemIndex >= 0) { - return this._nonZeroValues[itemIndex]; + return _nonZeroValues[itemIndex]; } } @@ -238,14 +238,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double set { // If index is out of bounds - if ((index < 0) || (index >= this.Count)) + if ((index < 0) || (index >= Count)) { throw new IndexOutOfRangeException(); } - lock (this.lockObject) + lock (_lockObject) { - this.SetValue(index, value); + SetValue(index, value); } } } @@ -284,11 +284,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double } /// - /// Clears this instance. + /// Resets all values to zero. /// - public void Clear() + public override void Clear() { - this.NonZerosCount = 0; + NonZerosCount = 0; } /// @@ -310,7 +310,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("target"); } - if (this.Count != target.Count) + if (Count != target.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target"); } @@ -325,33 +325,23 @@ namespace MathNet.Numerics.LinearAlgebra.Double { CommonParallel.For( 0, - this.Count, + Count, index => target[index] = this[index]); } else { // Lets copy only needed data. Portion of needed data is determined by NonZerosCount value - otherVector._nonZeroValues = new double[this.NonZerosCount]; - otherVector._nonZeroIndices = new int[this.NonZerosCount]; - otherVector.NonZerosCount = this.NonZerosCount; + otherVector._nonZeroValues = new double[NonZerosCount]; + otherVector._nonZeroIndices = new int[NonZerosCount]; + otherVector.NonZerosCount = NonZerosCount; - Buffer.BlockCopy(this._nonZeroValues, 0, otherVector._nonZeroValues, 0, this.NonZerosCount * Constants.SizeOfDouble); - Buffer.BlockCopy(this._nonZeroIndices, 0, otherVector._nonZeroIndices, 0, this.NonZerosCount * Constants.SizeOfInt); + Buffer.BlockCopy(_nonZeroValues, 0, otherVector._nonZeroValues, 0, NonZerosCount * Constants.SizeOfDouble); + Buffer.BlockCopy(_nonZeroIndices, 0, otherVector._nonZeroIndices, 0, NonZerosCount * Constants.SizeOfInt); } } #region Operators and supplementary functions - // NOTE: There are no operators as: - // public static implicit operator SparseVector(double[] array) - // and - // public static implicit operator double[](SparseVector vector) - // as it is in DenseVector. Because when creating vector from double[] values are copied to internal storage and if user wants - // to get double[] he should call SparseVector.ToArray(), then double[] will be generated and returned to a user.\ - - // In DenseVector implementation reference to double[] is assigned to interanl storage when casting from double[] and returned - // when casting to double[] - /// /// Adds a scalar to each element of the vector. /// @@ -363,7 +353,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double return; } - for (var i = 0; i < this.Count; i++) + for (var i = 0; i < Count; i++) { this[i] += scalar; } @@ -383,12 +373,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (this.Count != result.Count) + if (Count != result.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } - this.CopyTo(result); + CopyTo(result); result.Add(scalar); } @@ -405,7 +395,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (this.Count != other.Count) + if (Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } @@ -418,7 +408,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double } else { - this.AddScaledSparceVector(1.0, sparseVector); + AddScaledSparceVector(1.0, sparseVector); } } @@ -434,7 +424,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (this.Count != other.Count) + if (Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } @@ -453,21 +443,21 @@ namespace MathNet.Numerics.LinearAlgebra.Double // To avoid such problem lets change values in internal storage of "this" if (alpha == 1.0) { - for (var i = 0; i < this.NonZerosCount; i++) + for (var i = 0; i < NonZerosCount; i++) { - this._nonZeroValues[i] += this._nonZeroValues[i]; + _nonZeroValues[i] += _nonZeroValues[i]; } } else if (alpha == -1.0) { - this.Clear(); // Vector is subtracted from itself + Clear(); // Vector is subtracted from itself return; } else { - for (var i = 0; i < this.NonZerosCount; i++) + for (var i = 0; i < NonZerosCount; i++) { - this._nonZeroValues[i] += alpha * this._nonZeroValues[i]; + _nonZeroValues[i] += alpha * _nonZeroValues[i]; } } } @@ -507,12 +497,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (this.Count != other.Count) + if (Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } - if (this.Count != result.Count) + if (Count != result.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } @@ -525,7 +515,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double } else { - this.CopyTo(result); + CopyTo(result); result.Add(other); } } @@ -588,7 +578,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double return; } - for (var i = 0; i < this.Count; i++) + for (var i = 0; i < Count; i++) { this[i] -= scalar; } @@ -608,12 +598,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (this.Count != result.Count) + if (Count != result.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } - this.CopyTo(result); + CopyTo(result); result.Subtract(scalar); } @@ -630,7 +620,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (this.Count != other.Count) + if (Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } @@ -643,7 +633,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double } else { - this.AddScaledSparceVector(-1.0, sparseVector); + AddScaledSparceVector(-1.0, sparseVector); } } @@ -663,12 +653,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (this.Count != other.Count) + if (Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } - if (this.Count != result.Count) + if (Count != result.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } @@ -681,7 +671,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double } else { - this.CopyTo(result); + CopyTo(result); result.Subtract(other); } } @@ -739,19 +729,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// Added as an alternative to the unary negation operator. public override Vector Negate() { - var result = new SparseVector(this.Count) + var result = new SparseVector(Count) { - _nonZeroValues = new double[this.NonZerosCount], - _nonZeroIndices = new int[this.NonZerosCount], - NonZerosCount = this.NonZerosCount + _nonZeroValues = new double[NonZerosCount], + _nonZeroIndices = new int[NonZerosCount], + NonZerosCount = NonZerosCount }; - Buffer.BlockCopy(this._nonZeroIndices, 0, result._nonZeroIndices, 0, this.NonZerosCount * Constants.SizeOfInt); + Buffer.BlockCopy(_nonZeroIndices, 0, result._nonZeroIndices, 0, NonZerosCount * Constants.SizeOfInt); CommonParallel.For( 0, - this.NonZerosCount, - index => result._nonZeroValues[index] = -this._nonZeroValues[index]); + NonZerosCount, + index => result._nonZeroValues[index] = -_nonZeroValues[index]); return result; } @@ -769,11 +759,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double if (scalar == 0) { - this.Clear(); // Set array empty + Clear(); // Set array empty return; } - Control.LinearAlgebraProvider.ScaleArray(scalar, this._nonZeroValues); + Control.LinearAlgebraProvider.ScaleArray(scalar, _nonZeroValues); } /// @@ -790,7 +780,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (this.Count != other.Count) + if (Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } @@ -798,9 +788,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double double result = 0; // base implementation iterates though all elements, but we need only take non-zeros - for (var i = 0; i < this.NonZerosCount; i++) + for (var i = 0; i < NonZerosCount; i++) { - result += this._nonZeroValues[i] * other[this._nonZeroIndices[i]]; + result += _nonZeroValues[i] * other[_nonZeroIndices[i]]; } return result; @@ -897,17 +887,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The index of absolute minimum element. public override int AbsoluteMinimumIndex() { - if (this.NonZerosCount == 0) + if (NonZerosCount == 0) { // No non-zero elements. Return 0 return 0; } var index = 0; - var min = Math.Abs(this._nonZeroValues[index]); - for (var i = 1; i < this.NonZerosCount; i++) + var min = Math.Abs(_nonZeroValues[index]); + for (var i = 1; i < NonZerosCount; i++) { - var test = Math.Abs(this._nonZeroValues[i]); + var test = Math.Abs(_nonZeroValues[i]); if (test < min) { index = i; @@ -915,7 +905,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double } } - return this._nonZeroIndices[index]; + return _nonZeroIndices[index]; } /// @@ -931,7 +921,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// If is not positive. public override Vector SubVector(int index, int length) { - if (index < 0 || index >= this.Count) + if (index < 0 || index >= Count) { throw new ArgumentOutOfRangeException("index"); } @@ -941,7 +931,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentOutOfRangeException("length"); } - if (index + length > this.Count) + if (index + length > Count) { throw new ArgumentOutOfRangeException("length"); } @@ -968,7 +958,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("values"); } - if (values.Length != this.Count) + if (values.Length != Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "values"); } @@ -985,23 +975,23 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The index of absolute maximum element. public override int MaximumIndex() { - if (this.NonZerosCount == 0) + if (NonZerosCount == 0) { return 0; } var index = 0; - var max = this._nonZeroValues[0]; - for (var i = 1; i < this.NonZerosCount; i++) + var max = _nonZeroValues[0]; + for (var i = 1; i < NonZerosCount; i++) { - if (max < this._nonZeroValues[i]) + if (max < _nonZeroValues[i]) { index = i; - max = this._nonZeroValues[i]; + max = _nonZeroValues[i]; } } - return this._nonZeroIndices[index]; + return _nonZeroIndices[index]; } /// @@ -1010,23 +1000,23 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The index of minimum element. public override int MinimumIndex() { - if (this.NonZerosCount == 0) + if (NonZerosCount == 0) { return 0; } var index = 0; - var min = this._nonZeroValues[0]; - for (var i = 1; i < this.NonZerosCount; i++) + var min = _nonZeroValues[0]; + for (var i = 1; i < NonZerosCount; i++) { - if (min > this._nonZeroValues[i]) + if (min > _nonZeroValues[i]) { index = i; - min = this._nonZeroValues[i]; + min = _nonZeroValues[i]; } } - return this._nonZeroIndices[index]; + return _nonZeroIndices[index]; } /// @@ -1036,9 +1026,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double public override double Sum() { double result = 0; - for (var i = 0; i < this.NonZerosCount; i++) + for (var i = 0; i < NonZerosCount; i++) { - result += this._nonZeroValues[i]; + result += _nonZeroValues[i]; } return result; @@ -1051,9 +1041,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double public override double SumMagnitudes() { double result = 0; - for (var i = 0; i < this.NonZerosCount; i++) + for (var i = 0; i < NonZerosCount; i++) { - result += Math.Abs(this._nonZeroValues[i]); + result += Math.Abs(_nonZeroValues[i]); } return result; @@ -1072,13 +1062,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (this.Count != other.Count) + if (Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } // We cannot iterate using NonZeroCount because the value may be changed (if multiply by 0) - for (var i = 0; i < this.Count; i++) + for (var i = 0; i < Count; i++) { this[i] *= other[i]; } @@ -1105,12 +1095,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (this.Count != other.Count) + if (Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } - if (this.Count != result.Count) + if (Count != result.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } @@ -1118,12 +1108,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) { var tmp = result.CreateVector(result.Count); - this.PointwiseMultiply(other, tmp); + PointwiseMultiply(other, tmp); tmp.CopyTo(result); } else { - this.CopyTo(result); + CopyTo(result); result.PointwiseMultiply(other); } } @@ -1141,15 +1131,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (this.Count != other.Count) + if (Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } // base implementation iterates though all elements, but we need only take non-zeros - for (var i = 0; i < this.NonZerosCount; i++) + for (var i = 0; i < NonZerosCount; i++) { - this[this._nonZeroIndices[i]] /= other[this._nonZeroIndices[i]]; + this[_nonZeroIndices[i]] /= other[_nonZeroIndices[i]]; } } @@ -1174,12 +1164,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (this.Count != other.Count) + if (Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } - if (this.Count != result.Count) + if (Count != result.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } @@ -1187,12 +1177,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) { var tmp = result.CreateVector(result.Count); - this.PointwiseDivide(other, tmp); + PointwiseDivide(other, tmp); tmp.CopyTo(result); } else { - this.CopyTo(result); + CopyTo(result); result.PointwiseDivide(other); } } @@ -1249,7 +1239,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); } - var v = (SparseVector)this.CreateVector(length); + var v = (SparseVector)CreateVector(length); for (var index = 0; index < v.Count; index++) { v[index] = randomDistribution.Sample(); @@ -1275,7 +1265,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); } - var v = (SparseVector)this.CreateVector(length); + var v = (SparseVector)CreateVector(length); for (var index = 0; index < v.Count; index++) { v[index] = randomDistribution.Sample(); @@ -1315,8 +1305,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double var sum = CommonParallel.Aggregate( 0, - this.NonZerosCount, - index => Math.Pow(Math.Abs(this._nonZeroValues[index]), p)); + NonZerosCount, + index => Math.Pow(Math.Abs(_nonZeroValues[index]), p)); return Math.Pow(sum, 1.0 / p); } @@ -1327,7 +1317,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// Scalar ret = max(abs(this[i])) public override double NormInfinity() { - return CommonParallel.Select(0, this.NonZerosCount, (index, localData) => localData = Math.Max(localData, Math.Abs(this._nonZeroValues[index])), Math.Max); + return CommonParallel.Select(0, NonZerosCount, (index, localData) => localData = Math.Max(localData, Math.Abs(_nonZeroValues[index])), Math.Max); } #endregion @@ -1500,7 +1490,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double private void SetValue(int index, double value) { // Search if "index" already exists in range "0 - real nonzero values count" - var itemIndex = Array.BinarySearch(this._nonZeroIndices, 0, this.NonZerosCount, index); + var itemIndex = Array.BinarySearch(_nonZeroIndices, 0, NonZerosCount, index); if (itemIndex >= 0) { @@ -1508,56 +1498,61 @@ namespace MathNet.Numerics.LinearAlgebra.Double if (value == 0.0) { // Value is zero. Let's delete it from Values and Indices array - for (var i = itemIndex + 1; i < this.NonZerosCount; i++) + for (var i = itemIndex + 1; i < NonZerosCount; i++) { - this._nonZeroValues[i - 1] = this._nonZeroValues[i]; - this._nonZeroIndices[i - 1] = this._nonZeroIndices[i]; + _nonZeroValues[i - 1] = _nonZeroValues[i]; + _nonZeroIndices[i - 1] = _nonZeroIndices[i]; } - this.NonZerosCount -= 1; + NonZerosCount -= 1; // Check if the storage needs to be shrink. This is reasonable to do if // there are a lot of non-zero elements and storage is two times bigger - if ((this.NonZerosCount > 1024) && (this.NonZerosCount < this._nonZeroIndices.Length / 2)) + if ((NonZerosCount > 1024) && (NonZerosCount < _nonZeroIndices.Length / 2)) { - Array.Resize(ref this._nonZeroValues, this.NonZerosCount); - Array.Resize(ref this._nonZeroIndices, this.NonZerosCount); + Array.Resize(ref _nonZeroValues, NonZerosCount); + Array.Resize(ref _nonZeroIndices, NonZerosCount); } } else { - this._nonZeroValues[itemIndex] = value; + _nonZeroValues[itemIndex] = value; } } else { + if (value == 0.0) + { + return; + } + itemIndex = ~itemIndex; // Index where to put new value // Check if the storage needs to be increased - if ((this.NonZerosCount == this._nonZeroValues.Length) && (this.NonZerosCount < this.Count)) + if ((NonZerosCount == _nonZeroValues.Length) && (NonZerosCount < Count)) { // Value and Indices arrays are completely full so we increase the size - var size = Math.Min(this._nonZeroValues.Length + this.GrowthSize(), this.Count); - Array.Resize(ref this._nonZeroValues, size); - Array.Resize(ref this._nonZeroIndices, size); + var size = Math.Min(_nonZeroValues.Length + GrowthSize(), Count); + Array.Resize(ref _nonZeroValues, size); + Array.Resize(ref _nonZeroIndices, size); } // Move all values (with an position larger than index) in the value array // to the next position // move all values (with an position larger than index) in the columIndices // array to the next position - for (var i = this.NonZerosCount - 1; i > itemIndex - 1; i--) + for (var i = NonZerosCount - 1; i > itemIndex - 1; i--) { - this._nonZeroValues[i + 1] = this._nonZeroValues[i]; - this._nonZeroIndices[i + 1] = this._nonZeroIndices[i]; + _nonZeroValues[i + 1] = _nonZeroValues[i]; + _nonZeroIndices[i + 1] = _nonZeroIndices[i]; } // Add the value and the column index - this._nonZeroValues[itemIndex] = value; - this._nonZeroIndices[itemIndex] = index; + _nonZeroValues[itemIndex] = value; + _nonZeroIndices[itemIndex] = index; // increase the number of non-zero numbers by one - this.NonZerosCount += 1; + NonZerosCount += 1; } } @@ -1569,19 +1564,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double private int GrowthSize() { int delta; - if (this._nonZeroValues.Length > 1024) + if (_nonZeroValues.Length > 1024) { - delta = this._nonZeroValues.Length / 4; + delta = _nonZeroValues.Length / 4; } else { - if (this._nonZeroValues.Length > 256) + if (_nonZeroValues.Length > 256) { delta = 512; } else { - delta = this._nonZeroValues.Length > 64 ? 128 : 32; + delta = _nonZeroValues.Length > 64 ? 128 : 32; } } @@ -1612,15 +1607,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double return true; } - if ((this.Count != sparseVector.Count) || (this.NonZerosCount != sparseVector.NonZerosCount)) + if ((Count != sparseVector.Count) || (NonZerosCount != sparseVector.NonZerosCount)) { return false; } // If all else fails, perform element wise comparison. - for (var index = 0; index < this.NonZerosCount; index++) + for (var index = 0; index < NonZerosCount; index++) { - if (!this._nonZeroValues[index].AlmostEqual(sparseVector._nonZeroValues[index]) || (this._nonZeroIndices[index] != sparseVector._nonZeroIndices[index])) + if (!_nonZeroValues[index].AlmostEqual(sparseVector._nonZeroValues[index]) || (_nonZeroIndices[index] != sparseVector._nonZeroIndices[index])) { return false; } @@ -1637,14 +1632,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public override int GetHashCode() { - var hashNum = Math.Min(this.NonZerosCount, 20); + var hashNum = Math.Min(NonZerosCount, 20); long hash = 0; for (var i = 0; i < hashNum; i++) { #if SILVERLIGHT hash ^= Precision.DoubleToInt64Bits(this._nonZeroValues[i]); #else - hash ^= BitConverter.DoubleToInt64Bits(this._nonZeroValues[i]); + hash ^= BitConverter.DoubleToInt64Bits(_nonZeroValues[i]); #endif } @@ -1653,4 +1648,4 @@ namespace MathNet.Numerics.LinearAlgebra.Double #endregion } -} \ No newline at end of file +} diff --git a/src/Numerics/LinearAlgebra/Double/Vector.cs b/src/Numerics/LinearAlgebra/Double/Vector.cs index 60068cf5..47699566 100644 --- a/src/Numerics/LinearAlgebra/Double/Vector.cs +++ b/src/Numerics/LinearAlgebra/Double/Vector.cs @@ -62,7 +62,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentException(Resources.ArgumentMustBePositive, "size"); } - this.Count = size; + Count = size; } /// @@ -129,7 +129,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double CommonParallel.For( 0, - this.Count, + Count, index => this[index] += scalar); } @@ -155,14 +155,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (this.Count != result.Count) + if (Count != result.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } if (!ReferenceEquals(this, result)) { - this.CopyTo(result); + CopyTo(result); } result.Add(scalar); @@ -201,14 +201,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (this.Count != other.Count) + if (Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } CommonParallel.For( 0, - this.Count, + Count, index => this[index] += other[index]); } @@ -240,7 +240,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (this.Count != result.Count) + if (Count != result.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } @@ -253,7 +253,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double } else { - this.CopyTo(result); + CopyTo(result); result.Add(other); } } @@ -273,7 +273,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double CommonParallel.For( 0, - this.Count, + Count, index => this[index] -= scalar); } @@ -299,14 +299,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (this.Count != result.Count) + if (Count != result.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } if (!ReferenceEquals(this, result)) { - this.CopyTo(result); + CopyTo(result); } result.Subtract(scalar); @@ -345,14 +345,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (this.Count != other.Count) + if (Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } CommonParallel.For( 0, - this.Count, + Count, index => this[index] -= other[index]); } @@ -384,7 +384,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (this.Count != result.Count) + if (Count != result.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } @@ -397,7 +397,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double } else { - this.CopyTo(result); + CopyTo(result); result.Subtract(other); } } @@ -417,7 +417,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double CommonParallel.For( 0, - this.Count, + Count, index => this[index] *= scalar); } @@ -443,14 +443,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (this.Count != result.Count) + if (Count != result.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } if (!ReferenceEquals(this, result)) { - this.CopyTo(result); + CopyTo(result); } result.Multiply(scalar); @@ -478,13 +478,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (this.Count != other.Count) + if (Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } var dot = 0.0; - for (var i = 0; i < this.Count; i++) + for (var i = 0; i < Count; i++) { dot += this[i] * other[i]; } @@ -505,7 +505,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double return; } - this.Multiply(1.0 / scalar); + Multiply(1.0 / scalar); } /// @@ -530,14 +530,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("result"); } - if (this.Count != result.Count) + if (Count != result.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } if (!ReferenceEquals(this, result)) { - this.CopyTo(result); + CopyTo(result); } result.Multiply(1.0 / scalar); @@ -556,14 +556,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (this.Count != other.Count) + if (Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } CommonParallel.For( 0, - this.Count, + Count, index => this[index] *= other[index]); } @@ -588,12 +588,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (this.Count != other.Count) + if (Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } - if (this.Count != result.Count) + if (Count != result.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } @@ -601,12 +601,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) { var tmp = result.CreateVector(result.Count); - this.PointwiseMultiply(other, tmp); + PointwiseMultiply(other, tmp); tmp.CopyTo(result); } else { - this.CopyTo(result); + CopyTo(result); result.PointwiseMultiply(other); } } @@ -624,14 +624,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (this.Count != other.Count) + if (Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } CommonParallel.For( 0, - this.Count, + Count, index => this[index] /= other[index]); } @@ -656,12 +656,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("other"); } - if (this.Count != other.Count) + if (Count != other.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); } - if (this.Count != result.Count) + if (Count != result.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); } @@ -669,12 +669,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) { var tmp = result.CreateVector(result.Count); - this.PointwiseDivide(other, tmp); + PointwiseDivide(other, tmp); tmp.CopyTo(result); } else { - this.CopyTo(result); + CopyTo(result); result.PointwiseDivide(other); } } @@ -701,11 +701,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double var matrix = new DenseMatrix(u.Count, v.Count); CommonParallel.For( - 0, - u.Count, + 0, + u.Count, i => { - for (int j = 0; j < v.Count; j++) + for (var j = 0; j < v.Count; j++) { matrix.At(i, j, u[i] * v[j]); } @@ -722,7 +722,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// A vector with n-random elements distributed according /// to the specified random distribution. /// - /// If the n vector is non poisitive. + /// If the n vector is non-positive. public virtual Vector Random(int length, IContinuousDistribution randomDistribution) { if (length < 1) @@ -730,7 +730,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); } - var v = this.CreateVector(length); + var v = CreateVector(length); for (var index = 0; index < v.Count; index++) { v[index] = randomDistribution.Sample(); @@ -748,7 +748,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// A vector with n-random elements distributed according /// to the specified random distribution. /// - /// If the n vector is non poisitive. + /// If the n vector is not positive. public virtual Vector Random(int length, IDiscreteDistribution randomDistribution) { if (length < 1) @@ -756,7 +756,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); } - var v = this.CreateVector(length); + var v = CreateVector(length); for (var index = 0; index < v.Count; index++) { v[index] = randomDistribution.Sample(); @@ -784,7 +784,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The value of the absolute minimum element. public virtual double AbsoluteMinimum() { - return Math.Abs(this[this.AbsoluteMinimumIndex()]); + return Math.Abs(this[AbsoluteMinimumIndex()]); } /// @@ -795,7 +795,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double { var index = 0; var min = Math.Abs(this[index]); - for (var i = 1; i < this.Count; i++) + for (var i = 1; i < Count; i++) { var test = Math.Abs(this[i]); if (test < min) @@ -814,7 +814,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The value of the absolute maximum element. public virtual double AbsoluteMaximum() { - return Math.Abs(this[this.AbsoluteMaximumIndex()]); + return Math.Abs(this[AbsoluteMaximumIndex()]); } /// @@ -825,7 +825,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double { var index = 0; var max = Math.Abs(this[index]); - for (var i = 1; i < this.Count; i++) + for (var i = 1; i < Count; i++) { var test = Math.Abs(this[i]); if (test > max) @@ -844,7 +844,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The value of maximum element. public virtual double Maximum() { - return this[this.MaximumIndex()]; + return this[MaximumIndex()]; } /// @@ -855,7 +855,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double { var index = 0; var max = this[0]; - for (var i = 1; i < this.Count; i++) + for (var i = 1; i < Count; i++) { if (max < this[i]) { @@ -873,7 +873,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The value of the minimum element. public virtual double Minimum() { - return this[this.MinimumIndex()]; + return this[MinimumIndex()]; } /// @@ -884,7 +884,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double { var index = 0; var min = this[0]; - for (var i = 1; i < this.Count; i++) + for (var i = 1; i < Count; i++) { if (min > this[i]) { @@ -903,7 +903,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double public virtual double Sum() { double result = 0; - for (var i = 0; i < this.Count; i++) + for (var i = 0; i < Count; i++) { result += this[i]; } @@ -918,7 +918,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double public virtual double SumMagnitudes() { double result = 0; - for (var i = 0; i < this.Count; i++) + for (var i = 0; i < Count; i++) { result += Math.Abs(this[i]); } @@ -931,7 +931,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double #region Arithmetic Operator Overloading /// - /// Returns a Vector containing the same values of rightSide. + /// Returns a Vector containing the same values of . /// /// This method is included for completeness. /// The vector to get the values from. @@ -978,7 +978,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double } /// - /// Returns a Vector containing the negated values of rightSide. + /// Returns a Vector containing the negated values of . /// /// The vector to get the values from. /// A vector containing the negated values as . @@ -1116,22 +1116,22 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// Euclidean Norm also known as 2-Norm. /// /// - /// Scalar ret = sqrt(sum(this[i]^2)) + /// Scalar ret = sqrt(sum(this[i]^2)) /// public virtual double Norm() { - return this.NormP(2); + return NormP(2); } /// /// Squared Euclidean 2-Norm. /// /// - /// Scalar ret = sum(this[i]^2) + /// Scalar ret = sum(this[i]^2) /// public virtual double SquaredNorm() { - var norm = this.Norm(); + var norm = Norm(); return norm * norm; } @@ -1139,11 +1139,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// 1-Norm also known as Manhattan Norm or Taxicab Norm. /// /// - /// Scalar ret = sum(abs(this[i])) + /// Scalar ret = sum(abs(this[i])) /// public virtual double Norm1() { - return this.NormP(1); + return NormP(1); } /// @@ -1153,7 +1153,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The p value. /// /// - /// Scalar ret = (sum(abs(this[i])^p))^(1/p) + /// Scalar ret = (sum(abs(this[i])^p))^(1/p) /// public virtual double NormP(int p) { @@ -1164,7 +1164,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double var sum = CommonParallel.Aggregate( 0, - this.Count, + Count, index => Math.Pow(Math.Abs(this[index]), p)); return Math.Pow(sum, 1.0 / p); @@ -1174,27 +1174,27 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// Infinity Norm. /// /// - /// Scalar ret = max(abs(this[i])) + /// Scalar ret = max(abs(this[i])) /// public virtual double NormInfinity() { return CommonParallel.Select( 0, - this.Count, + Count, (index, localData) => localData = Math.Max(localData, Math.Abs(this[index])), Math.Max); } /// - /// Normalizes this vector to a unit vector with respect to the Eucliden 2-Norm. + /// Normalizes this vector to a unit vector with respect to the Euclidean 2-Norm. /// /// - /// This vector normalized to a unit vector with respect to the Eucliden 2-Norm. + /// This vector normalized to a unit vector with respect to the Euclidean 2-Norm. /// public virtual Vector Normalize() { - var norm = this.Norm(); - var clone = this.Clone(); + var norm = Norm(); + var clone = Clone(); if (norm == 0.0) { return clone; @@ -1216,8 +1216,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public Vector Clone() { - var retrunVector = this.CreateVector(this.Count); - this.CopyTo(retrunVector); + var retrunVector = CreateVector(Count); + CopyTo(retrunVector); return retrunVector; } @@ -1240,7 +1240,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("target"); } - if (this.Count != target.Count) + if (Count != target.Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target"); } @@ -1252,7 +1252,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double CommonParallel.For( 0, - this.Count, + Count, index => target[index] = this[index]); } @@ -1278,12 +1278,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("destination"); } - if (offset >= this.Count) + if (offset >= Count) { throw new ArgumentOutOfRangeException("offset"); } - if (offset + count > this.Count) + if (offset + count > Count) { throw new ArgumentOutOfRangeException("count"); } @@ -1301,7 +1301,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double if (ReferenceEquals(this, destination)) { var tmpVector = destination.CreateVector(destination.Count); - this.CopyTo(tmpVector); + CopyTo(tmpVector); CommonParallel.For( 0, @@ -1325,7 +1325,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public virtual double[] ToArray() { - var ret = new double[this.Count]; + var ret = new double[Count]; for (var i = 0; i < ret.Length; i++) { ret[i] = this[i]; @@ -1342,8 +1342,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public virtual Matrix ToColumnMatrix() { - var matrix = this.CreateMatrix(this.Count, 1); - for (var i = 0; i < this.Count; i++) + var matrix = CreateMatrix(Count, 1); + for (var i = 0; i < Count; i++) { matrix[i, 0] = this[i]; } @@ -1359,8 +1359,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public virtual Matrix ToRowMatrix() { - var matrix = this.CreateMatrix(1, this.Count); - for (var i = 0; i < this.Count; i++) + var matrix = CreateMatrix(1, Count); + for (var i = 0; i < Count; i++) { matrix[0, i] = this[i]; } @@ -1381,7 +1381,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// If is not positive. public virtual Vector SubVector(int index, int length) { - if (index < 0 || index >= this.Count) + if (index < 0 || index >= Count) { throw new ArgumentOutOfRangeException("index"); } @@ -1391,12 +1391,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentOutOfRangeException("length"); } - if (index + length > this.Count) + if (index + length > Count) { throw new ArgumentOutOfRangeException("index"); } - var result = this.CreateVector(length); + var result = CreateVector(length); CommonParallel.For( index, @@ -1418,7 +1418,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double throw new ArgumentNullException("values"); } - if (values.Length != this.Count) + if (values.Length != Count) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "values"); } @@ -1445,7 +1445,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// object ICloneable.Clone() { - return this.Clone(); + return Clone(); } #endregion @@ -1462,7 +1462,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// IEnumerator IEnumerable.GetEnumerator() { - return this.GetEnumerator(); + return GetEnumerator(); } #endregion @@ -1477,7 +1477,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public virtual IEnumerator GetEnumerator() { - for (var index = 0; index < this.Count; index++) + for (var index = 0; index < Count; index++) { yield return this[index]; } @@ -1501,7 +1501,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public virtual IEnumerable> GetIndexedEnumerator() { - for (var i = 0; i < this.Count; i++) + for (var i = 0; i < Count; i++) { yield return new KeyValuePair(i, this[i]); } @@ -1516,7 +1516,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// An object to compare with this object. /// /// - /// true if the current object is equal to the parameter; otherwise, false. + /// true if the current object is equal to the parameter; otherwise, false. /// public bool Equals(Vector other) { @@ -1526,7 +1526,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double return false; } - if (this.Count != other.Count) + if (Count != other.Count) { return false; } @@ -1538,7 +1538,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double } // If all else fails, perform element wise comparison. - for (var index = 0; index < this.Count; index++) + for (var index = 0; index < Count; index++) { if (!this[index].AlmostEqual(other[index])) { @@ -1564,7 +1564,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public string ToString(IFormatProvider formatProvider) { - return this.ToString(null, formatProvider); + return ToString(null, formatProvider); } /// @@ -1582,10 +1582,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double public string ToString(string format, IFormatProvider formatProvider) { var stringBuilder = new StringBuilder(); - for (var index = 0; index < this.Count; index++) + for (var index = 0; index < Count; index++) { stringBuilder.Append(this[index].ToString(format, formatProvider)); - if (index != this.Count - 1) + if (index != Count - 1) { stringBuilder.Append(formatProvider.GetTextInfo().ListSeparator); } @@ -1611,7 +1611,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public override bool Equals(object obj) { - return this.Equals(obj as Vector); + return Equals(obj as Vector); } /// @@ -1622,7 +1622,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public override int GetHashCode() { - var hashNum = Math.Min(this.Count, 20); + var hashNum = Math.Min(Count, 20); long hash = 0; for (var i = 0; i < hashNum; i++) { @@ -1644,9 +1644,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public override string ToString() { - return this.ToString(null, null); + return ToString(null, null); } #endregion + + /// + /// Resets all values to zero. + /// + public virtual void Clear() + { + CommonParallel.For(0, Count, index => this[index] = 0); + } } -} \ No newline at end of file +} diff --git a/src/UnitTests/LinearAlgebraTests/Double/SparseVectorTest.cs b/src/UnitTests/LinearAlgebraTests/Double/SparseVectorTest.cs index cea7c1b8..28d12226 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/SparseVectorTest.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/SparseVectorTest.cs @@ -61,15 +61,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double System.Array.Copy(_data, data, _data.Length); var vector = new SparseVector(data); - //Assert.AreSame(data, vector.ToArray()); There is no way to cast SparseVector to double[], so "vector.ToArray()" and "array" have different references for (var i = 0; i < data.Length; i++) { Assert.AreEqual(data[i], vector[i]); } - - // vector and data are different instances actually - //vector[0] = 100.0; - //Assert.AreEqual(100.0, data[0]); } [Test] @@ -139,7 +134,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double var vector = new SparseVector(_data); var array = vector.ToArray(); Assert.IsInstanceOfType(typeof(double[]), array); - //Assert.AreSame(vector.ToArray(), array); There is no way to cast SparseVector to double[], so "vector.ToArray()" and "array" have different references Assert.AreElementsEqual(vector, array); } diff --git a/src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs b/src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs index 53adeb75..a340cc1f 100644 --- a/src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs @@ -421,6 +421,18 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double vector = vector.Random(-2, new ContinuousUniform()); } + [Test] + public void CanClearVector() + { + double[] testData = { -20, -10, 10, 20, 30, }; + var vector = CreateVector(testData); + vector.Clear(); + foreach (var element in vector) + { + Assert.AreEqual(0.0, element); + } + } + protected abstract Vector CreateVector(int size); protected abstract Vector CreateVector(IList data); }