From 787e32562ca34cde8afacf5e1d8ee4782d9c6d11 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Tue, 15 Nov 2016 09:46:07 +0100 Subject: [PATCH] LA: add Vector.AsArray to directly return internal arrays if the schema is matching --- src/Numerics/LinearAlgebra/Matrix.cs | 1 - .../Storage/DenseVectorStorage.cs | 14 +++++++++++++ .../Storage/SparseVectorStorage.cs | 12 +++++++++++ .../LinearAlgebra/Storage/VectorStorage.cs | 17 ++++++++++++++++ src/Numerics/LinearAlgebra/Vector.cs | 20 +++++++++++++------ 5 files changed, 57 insertions(+), 7 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Matrix.cs b/src/Numerics/LinearAlgebra/Matrix.cs index 6eb2e3d3..f11423f7 100644 --- a/src/Numerics/LinearAlgebra/Matrix.cs +++ b/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. /// - /// A multidimensional containing the values of this matrix. public T[,] AsArray() { return Storage.AsArray(); diff --git a/src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs b/src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs index 1404b495..f5ba1401 100644 --- a/src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs +++ b/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 Enumerate() diff --git a/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs b/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs index d2f351b1..e474cedd 100644 --- a/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs +++ b/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 Enumerate() diff --git a/src/Numerics/LinearAlgebra/Storage/VectorStorage.cs b/src/Numerics/LinearAlgebra/Storage/VectorStorage.cs index 88a62bb7..913e72ab 100644 --- a/src/Numerics/LinearAlgebra/Storage/VectorStorage.cs +++ b/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 Enumerate() diff --git a/src/Numerics/LinearAlgebra/Vector.cs b/src/Numerics/LinearAlgebra/Vector.cs index b76b4bfc..b7a4b780 100644 --- a/src/Numerics/LinearAlgebra/Vector.cs +++ b/src/Numerics/LinearAlgebra/Vector.cs @@ -238,15 +238,23 @@ namespace MathNet.Numerics.LinearAlgebra /// /// 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. /// - /// - /// The vector's data as an array. - /// + /// The vector's data as an array. public T[] ToArray() { - var result = new DenseVectorStorage(Count); - Storage.CopyToUnchecked(result, ExistingData.AssumeZeros); - return result.Data; + return Storage.ToArray(); + } + + /// + /// 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. + /// + public T[] AsArray() + { + return Storage.AsArray(); } ///