From f2f702a0179aafb705afd1bcd2517d7b7dc166df Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Fri, 1 Mar 2013 20:29:21 +0100 Subject: [PATCH] LA: Vector enumeration migrated to storage, unified Zero definition --- .../LinearAlgebra/Complex/DiagonalMatrix.cs | 6 +-- .../LinearAlgebra/Complex/SparseMatrix.cs | 6 +-- .../LinearAlgebra/Complex/SparseVector.cs | 22 ---------- .../LinearAlgebra/Complex32/DiagonalMatrix.cs | 6 +-- .../LinearAlgebra/Complex32/SparseMatrix.cs | 6 +-- .../LinearAlgebra/Complex32/SparseVector.cs | 22 ---------- .../LinearAlgebra/Double/DiagonalMatrix.cs | 6 +-- .../LinearAlgebra/Double/SparseMatrix.cs | 6 +-- .../LinearAlgebra/Double/SparseVector.cs | 22 ---------- src/Numerics/LinearAlgebra/Generic/Common.cs | 36 +++++++++++++++-- .../LinearAlgebra/Generic/Factorization/LU.cs | 2 +- .../Generic/Matrix.Arithmetic.cs | 5 +-- src/Numerics/LinearAlgebra/Generic/Matrix.cs | 8 ++-- src/Numerics/LinearAlgebra/Generic/Vector.cs | 34 +++++----------- .../LinearAlgebra/Single/DiagonalMatrix.cs | 6 +-- .../LinearAlgebra/Single/SparseMatrix.cs | 6 +-- .../LinearAlgebra/Single/SparseVector.cs | 22 ---------- .../Storage/DenseVectorStorage.cs | 19 +++++++++ .../Storage/DiagonalMatrixStorage.cs | 13 +++--- .../LinearAlgebra/Storage/MatrixStorage.cs | 5 ++- .../SparseCompressedRowMatrixStorage.cs | 13 +++--- .../Storage/SparseVectorStorage.cs | 40 ++++++++++++++----- .../LinearAlgebra/Storage/VectorStorage.cs | 28 ++++++++++++- 23 files changed, 164 insertions(+), 175 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs index d382ff0b..9e5d9716 100644 --- a/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs @@ -71,7 +71,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// If is less than one. /// public DiagonalMatrix(int order) - : this(new DiagonalMatrixStorage(order, order, Complex.Zero)) + : this(new DiagonalMatrixStorage(order, order)) { } @@ -85,7 +85,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// The number of columns. /// public DiagonalMatrix(int rows, int columns) - : this(new DiagonalMatrixStorage(rows, columns, Complex.Zero)) + : this(new DiagonalMatrixStorage(rows, columns)) { } @@ -116,7 +116,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// The number of columns. /// The one dimensional array which contain diagonal elements. public DiagonalMatrix(int rows, int columns, Complex[] diagonalArray) - : this(new DiagonalMatrixStorage(rows, columns, Complex.Zero, diagonalArray)) + : this(new DiagonalMatrixStorage(rows, columns, diagonalArray)) { } diff --git a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs index d08298d0..85e24d40 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs @@ -75,7 +75,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// The number of columns. /// public SparseMatrix(int rows, int columns) - : this(new SparseCompressedRowMatrixStorage(rows, columns, Complex.Zero)) + : this(new SparseCompressedRowMatrixStorage(rows, columns)) { } @@ -494,7 +494,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex var values = _storage.Values; var valueCount = _storage.ValueCount; - var ret = new SparseCompressedRowMatrixStorage(ColumnCount, RowCount, Complex.Zero) + var ret = new SparseCompressedRowMatrixStorage(ColumnCount, RowCount) { ColumnIndices = new int[valueCount], Values = new Complex[valueCount] @@ -601,7 +601,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// public static SparseMatrix Identity(int order) { - var m = new SparseCompressedRowMatrixStorage(order, order, Complex.Zero) + var m = new SparseCompressedRowMatrixStorage(order, order) { ValueCount = order, Values = new Complex[order], diff --git a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs index 21e54b91..85dd9747 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs @@ -1044,27 +1044,5 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return base.ToString(format, formatProvider); } - - /// - /// Returns an that contains the position and value of the element. - /// - /// - /// An over this vector that contains the position and value of each - /// element. - /// - /// - /// The enumerator returns a - /// - /// with the first value being the element index and the second value - /// being the value of the element at that index. For sparse vectors, the enumerator will exclude all elements - /// with a zero value. - /// - public override IEnumerable> GetIndexedEnumerator() - { - for (var i = 0; i < _storage.ValueCount; i++) - { - yield return new Tuple(_storage.Indices[i], _storage.Values[i]); - } - } } } diff --git a/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs index 89fd1db0..e503bdfb 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs @@ -71,7 +71,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// If is less than one. /// public DiagonalMatrix(int order) - : this(new DiagonalMatrixStorage(order, order, Complex32.Zero)) + : this(new DiagonalMatrixStorage(order, order)) { } @@ -85,7 +85,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// The number of columns. /// public DiagonalMatrix(int rows, int columns) - : this(new DiagonalMatrixStorage(rows, columns, Complex32.Zero)) + : this(new DiagonalMatrixStorage(rows, columns)) { } @@ -116,7 +116,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// The number of columns. /// The one dimensional array which contain diagonal elements. public DiagonalMatrix(int rows, int columns, Complex32[] diagonalArray) - : this(new DiagonalMatrixStorage(rows, columns, Complex32.Zero, diagonalArray)) + : this(new DiagonalMatrixStorage(rows, columns, diagonalArray)) { } diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs index 11ee1e7d..27160e65 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs @@ -75,7 +75,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// The number of columns. /// public SparseMatrix(int rows, int columns) - : this(new SparseCompressedRowMatrixStorage(rows, columns, Complex32.Zero)) + : this(new SparseCompressedRowMatrixStorage(rows, columns)) { } @@ -494,7 +494,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 var values = _storage.Values; var valueCount = _storage.ValueCount; - var ret = new SparseCompressedRowMatrixStorage(ColumnCount, RowCount, Complex32.Zero) + var ret = new SparseCompressedRowMatrixStorage(ColumnCount, RowCount) { ColumnIndices = new int[valueCount], Values = new Complex32[valueCount] @@ -601,7 +601,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// public static SparseMatrix Identity(int order) { - var m = new SparseCompressedRowMatrixStorage(order, order, Complex32.Zero) + var m = new SparseCompressedRowMatrixStorage(order, order) { ValueCount = order, Values = new Complex32[order], diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs index 8693d914..08792875 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs @@ -1044,27 +1044,5 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return base.ToString(format, formatProvider); } - - /// - /// Returns an that contains the position and value of the element. - /// - /// - /// An over this vector that contains the position and value of each - /// element. - /// - /// - /// The enumerator returns a - /// - /// with the first value being the element index and the second value - /// being the value of the element at that index. For sparse vectors, the enumerator will exclude all elements - /// with a zero value. - /// - public override IEnumerable> GetIndexedEnumerator() - { - for (var i = 0; i < _storage.ValueCount; i++) - { - yield return new Tuple(_storage.Indices[i], _storage.Values[i]); - } - } } } diff --git a/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs index 105c783c..a7222099 100644 --- a/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs @@ -70,7 +70,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// If is less than one. /// public DiagonalMatrix(int order) - : this(new DiagonalMatrixStorage(order, order, 0d)) + : this(new DiagonalMatrixStorage(order, order)) { } @@ -84,7 +84,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The number of columns. /// public DiagonalMatrix(int rows, int columns) - : this(new DiagonalMatrixStorage(rows, columns, 0d)) + : this(new DiagonalMatrixStorage(rows, columns)) { } @@ -115,7 +115,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The number of columns. /// The one dimensional array which contain diagonal elements. public DiagonalMatrix(int rows, int columns, double[] diagonalArray) - : this(new DiagonalMatrixStorage(rows, columns, 0d, diagonalArray)) + : this(new DiagonalMatrixStorage(rows, columns, diagonalArray)) { } diff --git a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs index 2814dd89..93821d42 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs @@ -74,7 +74,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// The number of columns. /// public SparseMatrix(int rows, int columns) - : this(new SparseCompressedRowMatrixStorage(rows, columns, 0d)) + : this(new SparseCompressedRowMatrixStorage(rows, columns)) { } @@ -493,7 +493,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double var values = _storage.Values; var valueCount = _storage.ValueCount; - var ret = new SparseCompressedRowMatrixStorage(ColumnCount, RowCount, 0d) + var ret = new SparseCompressedRowMatrixStorage(ColumnCount, RowCount) { ColumnIndices = new int[valueCount], Values = new double[valueCount] @@ -599,7 +599,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// public static SparseMatrix Identity(int order) { - var m = new SparseCompressedRowMatrixStorage(order, order, 0d) + var m = new SparseCompressedRowMatrixStorage(order, order) { ValueCount = order, Values = new double[order], diff --git a/src/Numerics/LinearAlgebra/Double/SparseVector.cs b/src/Numerics/LinearAlgebra/Double/SparseVector.cs index 8fb6b1ef..22d03532 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseVector.cs @@ -1084,27 +1084,5 @@ namespace MathNet.Numerics.LinearAlgebra.Double return base.ToString(format, formatProvider); } - - /// - /// Returns an that contains the position and value of the element. - /// - /// - /// An over this vector that contains the position and value of each - /// element. - /// - /// - /// The enumerator returns a - /// - /// with the first value being the element index and the second value - /// being the value of the element at that index. For sparse vectors, the enumerator will exclude all elements - /// with a zero value. - /// - public override IEnumerable> GetIndexedEnumerator() - { - for (var i = 0; i < _storage.ValueCount; i++) - { - yield return new Tuple(_storage.Indices[i], _storage.Values[i]); - } - } } } diff --git a/src/Numerics/LinearAlgebra/Generic/Common.cs b/src/Numerics/LinearAlgebra/Generic/Common.cs index 002ba9a5..8ec5beaa 100644 --- a/src/Numerics/LinearAlgebra/Generic/Common.cs +++ b/src/Numerics/LinearAlgebra/Generic/Common.cs @@ -29,7 +29,7 @@ // -namespace MathNet.Numerics.LinearAlgebra.Generic +namespace MathNet.Numerics.LinearAlgebra { using System; @@ -43,7 +43,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// /// The type to return the value of 1.0 of. /// The value of 1.0 for type T. - public static T SetOne() + public static T OneOf() { if (typeof(T) == typeof(System.Numerics.Complex)) { @@ -57,7 +57,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic if (typeof(T) == typeof(double)) { - return (T)(object)1.0; + return (T)(object)1.0d; } if (typeof(T) == typeof(float)) @@ -67,5 +67,35 @@ namespace MathNet.Numerics.LinearAlgebra.Generic throw new NotSupportedException(); } + + /// + /// Sets the value of 0.0 for type T. + /// + /// The type to return the value of 0.0 of. + /// The value of 0.0 for type T. + public static T ZeroOf() + { + if (typeof(T) == typeof(System.Numerics.Complex)) + { + return (T)(object)System.Numerics.Complex.Zero; + } + + if (typeof(T) == typeof(Numerics.Complex32)) + { + return (T)(object)Numerics.Complex32.Zero; + } + + if (typeof(T) == typeof(double)) + { + return (T)(object)0.0d; + } + + if (typeof(T) == typeof(float)) + { + return (T)(object)0.0f; + } + + return default(T); + } } } diff --git a/src/Numerics/LinearAlgebra/Generic/Factorization/LU.cs b/src/Numerics/LinearAlgebra/Generic/Factorization/LU.cs index 2118ade4..2c351f3a 100644 --- a/src/Numerics/LinearAlgebra/Generic/Factorization/LU.cs +++ b/src/Numerics/LinearAlgebra/Generic/Factorization/LU.cs @@ -48,7 +48,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic.Factorization /// /// Value of one for T. /// - private static readonly T One = Common.SetOne(); + private static readonly T One = Common.OneOf(); /// /// Gets or sets both the L and U factors in the same matrix. diff --git a/src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs b/src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs index 5b59630b..e479875e 100644 --- a/src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs +++ b/src/Numerics/LinearAlgebra/Generic/Matrix.Arithmetic.cs @@ -32,7 +32,6 @@ namespace MathNet.Numerics.LinearAlgebra.Generic { using System; - using Distributions; using Factorization; using Properties; @@ -44,12 +43,12 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// /// The value of 1.0. /// - private static readonly T One = Common.SetOne(); + static readonly T One = Common.OneOf(); /// /// The value of 0.0. /// - private static readonly T Zero = default(T); + static readonly T Zero = Common.ZeroOf(); /// /// Adds another matrix to this matrix. diff --git a/src/Numerics/LinearAlgebra/Generic/Matrix.cs b/src/Numerics/LinearAlgebra/Generic/Matrix.cs index 94c0bd6f..4e72bca2 100644 --- a/src/Numerics/LinearAlgebra/Generic/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Generic/Matrix.cs @@ -586,7 +586,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic { for (var column = 0; column < ColumnCount; column++) { - result.At(row, column, row >= column ? At(row, column) : default(T)); + result.At(row, column, row >= column ? At(row, column) : Zero); } } } @@ -613,7 +613,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic { for (var column = 0; column < ColumnCount; column++) { - result.At(row, column, row <= column ? At(row, column) : default(T)); + result.At(row, column, row <= column ? At(row, column) : Zero); } } } @@ -702,7 +702,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic { for (var column = 0; column < ColumnCount; column++) { - result.At(row, column, row > column ? At(row, column) : default(T)); + result.At(row, column, row > column ? At(row, column) : Zero); } } } @@ -749,7 +749,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic { for (var column = 0; column < ColumnCount; column++) { - result.At(row, column, row < column ? At(row, column) : default(T)); + result.At(row, column, row < column ? At(row, column) : Zero); } } } diff --git a/src/Numerics/LinearAlgebra/Generic/Vector.cs b/src/Numerics/LinearAlgebra/Generic/Vector.cs index a5d68ada..d3c6deb2 100644 --- a/src/Numerics/LinearAlgebra/Generic/Vector.cs +++ b/src/Numerics/LinearAlgebra/Generic/Vector.cs @@ -43,22 +43,18 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// Supported data types are double, single, , and . [Serializable] public abstract partial class Vector : -#if PORTABLE IFormattable, IEnumerable, IEquatable> -#else - IFormattable, IEnumerable, IEquatable> -#endif where T : struct, IEquatable, IFormattable { /// /// The zero value for type T. /// - private static readonly T Zero = default(T); + private static readonly T Zero = Common.ZeroOf(); /// /// The value of 1.0 for type T. /// - private static readonly T One = Common.SetOne(); + private static readonly T One = Common.OneOf(); /// /// Initializes a new instance of the Vector class. @@ -1239,34 +1235,26 @@ namespace MathNet.Numerics.LinearAlgebra.Generic /// /// A that can be used to iterate through the collection. /// - public virtual IEnumerator GetEnumerator() + public IEnumerator GetEnumerator() { - for (var index = 0; index < Count; index++) - { - yield return this[index]; - } + return Storage.Enumerate().GetEnumerator(); } /// - /// Returns an that contains the position and value of the element. + /// Returns an that contains the position and value of the element, for all non-zero elements. /// /// - /// An over this vector that contains the position and value of each - /// element. + /// An over this vector that contains the position and value of each element. /// /// - /// The enumerator returns a - /// + /// The enumerator returns a /// with the first value being the element index and the second value - /// being the value of the element at that index. For sparse vectors, the enumerator will exclude all elements - /// with a zero value. + /// being the value of the element at that index. + /// The enumerator will exclude all elements with a zero value. /// - public virtual IEnumerable> GetIndexedEnumerator() + public IEnumerable> GetIndexedEnumerator() { - for (var i = 0; i < Count; i++) - { - yield return new Tuple(i, this[i]); - } + return Storage.EnumerateNonZero(); } /// diff --git a/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs b/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs index a4c0c375..b137b0bb 100644 --- a/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs @@ -70,7 +70,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// If is less than one. /// public DiagonalMatrix(int order) - : this(new DiagonalMatrixStorage(order, order, 0f)) + : this(new DiagonalMatrixStorage(order, order)) { } @@ -84,7 +84,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// The number of columns. /// public DiagonalMatrix(int rows, int columns) - : this(new DiagonalMatrixStorage(rows, columns, 0f)) + : this(new DiagonalMatrixStorage(rows, columns)) { } @@ -115,7 +115,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// The number of columns. /// The one dimensional array which contain diagonal elements. public DiagonalMatrix(int rows, int columns, float[] diagonalArray) - : this(new DiagonalMatrixStorage(rows, columns, 0f, diagonalArray)) + : this(new DiagonalMatrixStorage(rows, columns, diagonalArray)) { } diff --git a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs index ff9788aa..92179658 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs @@ -74,7 +74,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// The number of columns. /// public SparseMatrix(int rows, int columns) - : this(new SparseCompressedRowMatrixStorage(rows, columns, 0f)) + : this(new SparseCompressedRowMatrixStorage(rows, columns)) { } @@ -493,7 +493,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single var values = _storage.Values; var valueCount = _storage.ValueCount; - var ret = new SparseCompressedRowMatrixStorage(ColumnCount, RowCount, 0f) + var ret = new SparseCompressedRowMatrixStorage(ColumnCount, RowCount) { ColumnIndices = new int[valueCount], Values = new float[valueCount] @@ -599,7 +599,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// public static SparseMatrix Identity(int order) { - var mStorage = new SparseCompressedRowMatrixStorage(order, order, 0f) + var mStorage = new SparseCompressedRowMatrixStorage(order, order) { ValueCount = order, Values = new float[order], diff --git a/src/Numerics/LinearAlgebra/Single/SparseVector.cs b/src/Numerics/LinearAlgebra/Single/SparseVector.cs index 28971f17..7c0e6def 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseVector.cs @@ -1088,27 +1088,5 @@ namespace MathNet.Numerics.LinearAlgebra.Single return base.ToString(format, formatProvider); } - - /// - /// Returns an that contains the position and value of the element. - /// - /// - /// An over this vector that contains the position and value of each - /// element. - /// - /// - /// The enumerator returns a - /// - /// with the first value being the element index and the second value - /// being the value of the element at that index. For sparse vectors, the enumerator will exclude all elements - /// with a zero value. - /// - public override IEnumerable> GetIndexedEnumerator() - { - for (var i = 0; i < _storage.ValueCount; i++) - { - yield return new Tuple(_storage.Indices[i], _storage.Values[i]); - } - } } } diff --git a/src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs b/src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs index 822a49f3..ac745f37 100644 --- a/src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs @@ -29,6 +29,7 @@ // using System; +using System.Collections.Generic; using MathNet.Numerics.Properties; namespace MathNet.Numerics.LinearAlgebra.Storage @@ -89,6 +90,24 @@ namespace MathNet.Numerics.LinearAlgebra.Storage Array.Clear(Data, index, count); } + // ENUMERATION + + public override IEnumerable Enumerate() + { + return Data; + } + + public override IEnumerable> EnumerateNonZero() + { + for (var i = 0; i < Data.Length; i++) + { + if (!Zero.Equals(Data[i])) + { + yield return new Tuple(i, Data[i]); + } + } + } + // VECTOR COPY internal override void CopyToUnchecked(VectorStorage target, bool skipClearing = false) diff --git a/src/Numerics/LinearAlgebra/Storage/DiagonalMatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/DiagonalMatrixStorage.cs index b73b30be..1d7eb71d 100644 --- a/src/Numerics/LinearAlgebra/Storage/DiagonalMatrixStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/DiagonalMatrixStorage.cs @@ -40,17 +40,15 @@ namespace MathNet.Numerics.LinearAlgebra.Storage { // [ruegg] public fields are OK here - readonly T _zero; public readonly T[] Data; - internal DiagonalMatrixStorage(int rows, int columns, T zero) + internal DiagonalMatrixStorage(int rows, int columns) : base(rows, columns) { - _zero = zero; Data = new T[Math.Min(rows, columns)]; } - internal DiagonalMatrixStorage(int rows, int columns, T zero, T[] data) + internal DiagonalMatrixStorage(int rows, int columns, T[] data) : base(rows, columns) { if (data == null) @@ -63,7 +61,6 @@ namespace MathNet.Numerics.LinearAlgebra.Storage throw new ArgumentOutOfRangeException("data", string.Format(Resources.ArgumentArrayWrongLength, Math.Min(rows, columns))); } - _zero = zero; Data = data; } @@ -72,7 +69,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage /// public override T At(int row, int column) { - return row == column ? Data[row] : _zero; + return row == column ? Data[row] : Zero; } /// @@ -84,7 +81,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage { Data[row] = value; } - else if (!_zero.Equals(value)) + else if (!Zero.Equals(value)) { throw new IndexOutOfRangeException("Cannot set an off-diagonal element in a diagonal matrix."); } @@ -278,7 +275,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage { if (sourceRowIndex - sourceColumnIndex != targetRowIndex - targetColumnIndex) { - if (Data.Any(x => !_zero.Equals(x))) + if (Data.Any(x => !Zero.Equals(x))) { throw new NotSupportedException(); } diff --git a/src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs index 9d586e35..57eafc8d 100644 --- a/src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs @@ -39,6 +39,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage { // [ruegg] public fields are OK here + protected static readonly T Zero = Common.ZeroOf(); public readonly int RowCount; public readonly int ColumnCount; @@ -133,7 +134,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage { for (var j = 0; j < ColumnCount; j++) { - At(i, j, default(T)); + At(i, j, Zero); } } } @@ -144,7 +145,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage { for (var j = columnIndex; j < columnIndex + columnCount; j++) { - At(i, j, default(T)); + At(i, j, Zero); } } } diff --git a/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs index cdfdfb6c..131f3abf 100644 --- a/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs @@ -40,8 +40,6 @@ namespace MathNet.Numerics.LinearAlgebra.Storage { // [ruegg] public fields are OK here - readonly T _zero; - /// /// The array containing the row indices of the existing rows. Element "j" of the array gives the index of the /// element in the array that is first non-zero element in a row "j" @@ -66,10 +64,9 @@ namespace MathNet.Numerics.LinearAlgebra.Storage /// The number of non zero elements. public int ValueCount; - internal SparseCompressedRowMatrixStorage(int rows, int columns, T zero = default(T)) + internal SparseCompressedRowMatrixStorage(int rows, int columns) : base(rows, columns) { - _zero = zero; RowPointers = new int[rows]; ColumnIndices = new int[0]; Values = new T[0]; @@ -92,7 +89,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage public override T At(int row, int column) { var index = FindItem(row, column); - return index >= 0 ? Values[index] : _zero; + return index >= 0 ? Values[index] : Zero; } /// @@ -108,7 +105,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage if (index >= 0) { // Non-zero item found in matrix - if (_zero.Equals(value)) + if (Zero.Equals(value)) { // Delete existing item RemoveAtIndexUnchecked(index, row); @@ -122,7 +119,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage else { // Item not found. Add new value - if (_zero.Equals(value)) + if (Zero.Equals(value)) { return; } @@ -575,7 +572,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage for (int i = sourceColumnIndex, j = 0; i < sourceColumnIndex + columnCount; i++, j++) { var index = FindItem(rowIndex, i); - target.At(j, index >= 0 ? Values[index] : _zero); + target.At(j, index >= 0 ? Values[index] : Zero); } } diff --git a/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs b/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs index 4f029592..79c5c528 100644 --- a/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs @@ -29,6 +29,7 @@ // using System; +using System.Collections.Generic; using MathNet.Numerics.Properties; namespace MathNet.Numerics.LinearAlgebra.Storage @@ -39,8 +40,6 @@ namespace MathNet.Numerics.LinearAlgebra.Storage { // [ruegg] public fields are OK here - readonly T _zero; - /// /// Array that contains the indices of the non-zero values. /// @@ -56,10 +55,9 @@ namespace MathNet.Numerics.LinearAlgebra.Storage /// public int ValueCount; - internal SparseVectorStorage(int length, T zero = default(T)) + internal SparseVectorStorage(int length) : base(length) { - _zero = zero; Indices = new int[0]; Values = new T[0]; ValueCount = 0; @@ -72,7 +70,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage { // Search if item idex exists in NonZeroIndices array in range "0 - nonzero values count" var itemIndex = Array.BinarySearch(Indices, 0, ValueCount, index); - return itemIndex >= 0 ? Values[itemIndex] : _zero; + return itemIndex >= 0 ? Values[itemIndex] : Zero; } /// @@ -85,7 +83,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage if (itemIndex >= 0) { // Non-zero item found in matrix - if (_zero.Equals(value)) + if (Zero.Equals(value)) { // Delete existing item RemoveAtIndexUnchecked(itemIndex); @@ -99,7 +97,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage else { // Item not found. Add new value - if (!_zero.Equals(value)) + if (!Zero.Equals(value)) { InsertAtIndexUnchecked(~itemIndex, index, value); } @@ -235,7 +233,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage { if (j >= otherSparse.ValueCount || i < ValueCount && Indices[i] < otherSparse.Indices[j]) { - if (!_zero.Equals(Values[i++])) + if (!Zero.Equals(Values[i++])) { return false; } @@ -244,7 +242,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage if (i >= ValueCount || j < otherSparse.ValueCount && otherSparse.Indices[j] < Indices[i]) { - if (!_zero.Equals(otherSparse.Values[j++])) + if (!Zero.Equals(otherSparse.Values[j++])) { return false; } @@ -284,6 +282,30 @@ namespace MathNet.Numerics.LinearAlgebra.Storage return hash; } + // ENUMERATION + + public override IEnumerable Enumerate() + { + int k = 0; + for (int i = 0; i < Length; i++) + { + yield return k < ValueCount && Indices[k] == i + ? Values[k++] + : Zero; + } + } + + public override IEnumerable> EnumerateNonZero() + { + for (var i = 0; i < ValueCount; i++) + { + if (!Zero.Equals(Values[i])) + { + yield return new Tuple(Indices[i], Values[i]); + } + } + } + // VECTOR COPY internal override void CopyToUnchecked(VectorStorage target, bool skipClearing = false) diff --git a/src/Numerics/LinearAlgebra/Storage/VectorStorage.cs b/src/Numerics/LinearAlgebra/Storage/VectorStorage.cs index 1bb76f45..659365ad 100644 --- a/src/Numerics/LinearAlgebra/Storage/VectorStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/VectorStorage.cs @@ -29,6 +29,7 @@ // using System; +using System.Collections.Generic; using MathNet.Numerics.Properties; namespace MathNet.Numerics.LinearAlgebra.Storage @@ -39,6 +40,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage { // [ruegg] public fields are OK here + protected static readonly T Zero = Common.ZeroOf(); public readonly int Length; protected VectorStorage(int length) @@ -113,7 +115,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage { for (var i = 0; i < Length; i++) { - At(i, default(T)); + At(i, Zero); } } @@ -121,7 +123,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage { for (var i = index; i < index + count; i++) { - At(i, default(T)); + At(i, Zero); } } @@ -196,6 +198,28 @@ namespace MathNet.Numerics.LinearAlgebra.Storage return hash; } + // ENUMERATION + + public virtual IEnumerable Enumerate() + { + for (var i = 0; i < Length; i++) + { + yield return At(i); + } + } + + public virtual IEnumerable> EnumerateNonZero() + { + for (var i = 0; i < Length; i++) + { + var x = At(i); + if (!Zero.Equals(x)) + { + yield return new Tuple(i, x); + } + } + } + // VECTOR COPY public void CopyTo(VectorStorage target, bool skipClearing = false)