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()