Browse Source

LA: Initialize sparse vector by enumerable #104

pull/112/head
Christoph Ruegg 13 years ago
parent
commit
0f2a289373
  1. 12
      src/Numerics/LinearAlgebra/Complex/SparseVector.cs
  2. 12
      src/Numerics/LinearAlgebra/Complex32/SparseVector.cs
  3. 12
      src/Numerics/LinearAlgebra/Double/SparseVector.cs
  4. 12
      src/Numerics/LinearAlgebra/Single/SparseVector.cs
  5. 31
      src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs

12
src/Numerics/LinearAlgebra/Complex/SparseVector.cs

@ -119,17 +119,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
}
/// <summary>
/// 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.
/// </summary>
public SparseVector(IList<Complex> array)
: this(new SparseVectorStorage<Complex>(array.Count))
public SparseVector(IEnumerable<Complex> other)
: this(SparseVectorStorage<Complex>.FromEnumerable(other))
{
for (var i = 0; i < array.Count; i++)
{
Storage.At(i, array[i]);
}
}
/// <summary>

12
src/Numerics/LinearAlgebra/Complex32/SparseVector.cs

@ -119,17 +119,13 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
}
/// <summary>
/// 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.
/// </summary>
public SparseVector(IList<Complex32> array)
: this(new SparseVectorStorage<Complex32>(array.Count))
public SparseVector(IEnumerable<Complex32> other)
: this(SparseVectorStorage<Complex32>.FromEnumerable(other))
{
for (var i = 0; i < array.Count; i++)
{
Storage.At(i, array[i]);
}
}
/// <summary>

12
src/Numerics/LinearAlgebra/Double/SparseVector.cs

@ -119,17 +119,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
/// <summary>
/// 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.
/// </summary>
public SparseVector(IList<double> array)
: this(new SparseVectorStorage<double>(array.Count))
public SparseVector(IEnumerable<double> other)
: this(SparseVectorStorage<double>.FromEnumerable(other))
{
for (var i = 0; i < array.Count; i++)
{
Storage.At(i, array[i]);
}
}
/// <summary>

12
src/Numerics/LinearAlgebra/Single/SparseVector.cs

@ -119,17 +119,13 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
/// <summary>
/// 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.
/// </summary>
public SparseVector(IList<float> array)
: this(new SparseVectorStorage<float>(array.Count))
public SparseVector(IEnumerable<float> other)
: this(SparseVectorStorage<float>.FromEnumerable(other))
{
for (var i = 0; i < array.Count; i++)
{
Storage.At(i, array[i]);
}
}
/// <summary>

31
src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs

@ -282,6 +282,37 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return hash;
}
// INITIALIZATION
public static SparseVectorStorage<T> FromEnumerable(IEnumerable<T> data)
{
if (data == null)
{
throw new ArgumentNullException("data");
}
var indices = new List<int>();
var values = new List<T>();
int length = 0;
foreach (T item in data)
{
if (!Zero.Equals(item))
{
values.Add(item);
indices.Add(length);
}
length++;
}
return new SparseVectorStorage<T>(length)
{
Indices = indices.ToArray(),
Values = values.ToArray(),
ValueCount = values.Count
};
}
// ENUMERATION
public override IEnumerable<T> Enumerate()

Loading…
Cancel
Save