Browse Source

LA: add Vector.AsArray to directly return internal arrays if the schema is matching

v3
Christoph Ruegg 10 years ago
parent
commit
787e32562c
  1. 1
      src/Numerics/LinearAlgebra/Matrix.cs
  2. 14
      src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs
  3. 12
      src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs
  4. 17
      src/Numerics/LinearAlgebra/Storage/VectorStorage.cs
  5. 20
      src/Numerics/LinearAlgebra/Vector.cs

1
src/Numerics/LinearAlgebra/Matrix.cs

@ -1376,7 +1376,6 @@ namespace MathNet.Numerics.LinearAlgebra
/// Otherwise returns null. Changes to the returned array and the matrix will affect each other.
/// Use ToArray instead if you always need an independent array.
/// </summary>
/// <returns>A multidimensional containing the values of this matrix.</returns>
public T[,] AsArray()
{
return Storage.AsArray();

14
src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs

@ -329,6 +329,20 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
}
}
// EXTRACT
public override T[] ToArray()
{
var ret = new T[Data.Length];
Array.Copy(Data, 0, ret, 0, Data.Length);
return ret;
}
public override T[] AsArray()
{
return Data;
}
// ENUMERATION
public override IEnumerable<T> Enumerate()

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

@ -611,6 +611,18 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
}
}
// EXTRACT
public override T[] ToArray()
{
var ret = new T[Length];
for (int i = 0; i < ValueCount; i++)
{
ret[Indices[i]] = Values[i];
}
return ret;
}
// ENUMERATION
public override IEnumerable<T> Enumerate()

17
src/Numerics/LinearAlgebra/Storage/VectorStorage.cs

@ -374,6 +374,23 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
}
}
// EXTRACT
public virtual T[] ToArray()
{
var ret = new T[Length];
for (int i = 0; i < ret.Length; i++)
{
ret[i] = At(i);
}
return ret;
}
public virtual T[] AsArray()
{
return null;
}
// ENUMERATION
public virtual IEnumerable<T> Enumerate()

20
src/Numerics/LinearAlgebra/Vector.cs

@ -238,15 +238,23 @@ namespace MathNet.Numerics.LinearAlgebra
/// <summary>
/// Returns the data contained in the vector as an array.
/// The returned array will be independent from this vector.
/// A new memory block will be allocated for the array.
/// </summary>
/// <returns>
/// The vector's data as an array.
/// </returns>
/// <returns>The vector's data as an array.</returns>
public T[] ToArray()
{
var result = new DenseVectorStorage<T>(Count);
Storage.CopyToUnchecked(result, ExistingData.AssumeZeros);
return result.Data;
return Storage.ToArray();
}
/// <summary>
/// Returns the internal array of this vector if, and only if, this vector is stored by such an array internally.
/// Otherwise returns null. Changes to the returned array and the vector will affect each other.
/// Use ToArray instead if you always need an independent array.
/// </summary>
public T[] AsArray()
{
return Storage.AsArray();
}
/// <summary>

Loading…
Cancel
Save