From 0f2a2893732d6c30a3dd7193bfc0565a99be9d4e Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sat, 6 Apr 2013 18:08:06 +0200 Subject: [PATCH] LA: Initialize sparse vector by enumerable #104 --- .../LinearAlgebra/Complex/SparseVector.cs | 12 +++---- .../LinearAlgebra/Complex32/SparseVector.cs | 12 +++---- .../LinearAlgebra/Double/SparseVector.cs | 12 +++---- .../LinearAlgebra/Single/SparseVector.cs | 12 +++---- .../Storage/SparseVectorStorage.cs | 31 +++++++++++++++++++ 5 files changed, 47 insertions(+), 32 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs index cb68186b..c5227e10 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs @@ -119,17 +119,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } /// - /// Create a new sparse vector as a copy of the given array. - /// This new vector will be independent from the array. + /// Create a new sparse vector as a copy of the given enumerable. + /// This new vector will be independent from the enumerable. /// A new memory block will be allocated for storing the vector. /// - public SparseVector(IList array) - : this(new SparseVectorStorage(array.Count)) + public SparseVector(IEnumerable other) + : this(SparseVectorStorage.FromEnumerable(other)) { - for (var i = 0; i < array.Count; i++) - { - Storage.At(i, array[i]); - } } /// diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs index 37bb2c20..cb778fd8 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs @@ -119,17 +119,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } /// - /// Create a new sparse vector as a copy of the given array. - /// This new vector will be independent from the array. + /// Create a new sparse vector as a copy of the given enumerable. + /// This new vector will be independent from the enumerable. /// A new memory block will be allocated for storing the vector. /// - public SparseVector(IList array) - : this(new SparseVectorStorage(array.Count)) + public SparseVector(IEnumerable other) + : this(SparseVectorStorage.FromEnumerable(other)) { - for (var i = 0; i < array.Count; i++) - { - Storage.At(i, array[i]); - } } /// diff --git a/src/Numerics/LinearAlgebra/Double/SparseVector.cs b/src/Numerics/LinearAlgebra/Double/SparseVector.cs index fc323bfc..962898b9 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseVector.cs @@ -119,17 +119,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double } /// - /// Create a new sparse vector as a copy of the given array. - /// This new vector will be independent from the array. + /// Create a new sparse vector as a copy of the given enumerable. + /// This new vector will be independent from the enumerable. /// A new memory block will be allocated for storing the vector. /// - public SparseVector(IList array) - : this(new SparseVectorStorage(array.Count)) + public SparseVector(IEnumerable other) + : this(SparseVectorStorage.FromEnumerable(other)) { - for (var i = 0; i < array.Count; i++) - { - Storage.At(i, array[i]); - } } /// diff --git a/src/Numerics/LinearAlgebra/Single/SparseVector.cs b/src/Numerics/LinearAlgebra/Single/SparseVector.cs index dcf44748..71806e1c 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseVector.cs @@ -119,17 +119,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single } /// - /// Create a new sparse vector as a copy of the given array. - /// This new vector will be independent from the array. + /// Create a new sparse vector as a copy of the given enumerable. + /// This new vector will be independent from the enumerable. /// A new memory block will be allocated for storing the vector. /// - public SparseVector(IList array) - : this(new SparseVectorStorage(array.Count)) + public SparseVector(IEnumerable other) + : this(SparseVectorStorage.FromEnumerable(other)) { - for (var i = 0; i < array.Count; i++) - { - Storage.At(i, array[i]); - } } /// diff --git a/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs b/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs index 229b95f1..d19e1859 100644 --- a/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs @@ -282,6 +282,37 @@ namespace MathNet.Numerics.LinearAlgebra.Storage return hash; } + // INITIALIZATION + + public static SparseVectorStorage FromEnumerable(IEnumerable data) + { + if (data == null) + { + throw new ArgumentNullException("data"); + } + + var indices = new List(); + var values = new List(); + int length = 0; + + foreach (T item in data) + { + if (!Zero.Equals(item)) + { + values.Add(item); + indices.Add(length); + } + length++; + } + + return new SparseVectorStorage(length) + { + Indices = indices.ToArray(), + Values = values.ToArray(), + ValueCount = values.Count + }; + } + // ENUMERATION public override IEnumerable Enumerate()