From 5e07f34a8ee722fc2ece8f16e85654d2b4c0966c Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sat, 6 Apr 2013 17:48:26 +0200 Subject: [PATCH] LA: Initialize dense storage by enumerable #104 --- .../LinearAlgebra/Complex/DenseMatrix.cs | 12 ++++++++++ .../LinearAlgebra/Complex/DenseVector.cs | 12 +++++++++- .../LinearAlgebra/Complex32/DenseMatrix.cs | 12 ++++++++++ .../LinearAlgebra/Complex32/DenseVector.cs | 12 +++++++++- .../LinearAlgebra/Double/DenseMatrix.cs | 12 ++++++++++ .../LinearAlgebra/Double/DenseVector.cs | 13 +++++++++- .../LinearAlgebra/Single/DenseMatrix.cs | 12 ++++++++++ .../LinearAlgebra/Single/DenseVector.cs | 13 +++++++++- .../Storage/DenseColumnMajorMatrixStorage.cs | 24 +++++++++++++++++++ .../Storage/DenseVectorStorage.cs | 21 ++++++++++++++++ 10 files changed, 139 insertions(+), 4 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs index 9e2e9796..b1bfa986 100644 --- a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs @@ -36,6 +36,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex using Properties; using Storage; using System; + using System.Collections.Generic; using System.Diagnostics; using System.Numerics; @@ -144,6 +145,17 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } } } + + /// + /// Create a new dense matrix as a copy of the given enumerable. + /// The enumerable is assumed to be in column-major order. + /// This new matrix will be independent from the enumerable. + /// A new memory block will be allocated for storing the vector. + /// + public DenseMatrix(int rows, int columns, IEnumerable other) + : this(DenseColumnMajorMatrixStorage.FromColumnMajorEnumerable(rows, columns, other)) + { + } /// /// Create a new dense matrix as a copy of the given other matrix. diff --git a/src/Numerics/LinearAlgebra/Complex/DenseVector.cs b/src/Numerics/LinearAlgebra/Complex/DenseVector.cs index 4dc7a5e5..e3292794 100644 --- a/src/Numerics/LinearAlgebra/Complex/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex/DenseVector.cs @@ -99,7 +99,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex /// /// Create a new dense vector as a copy of the given other vector. /// This new vector will be independent from the other vector. - /// A new memory block will be allocated for storing the matrix. + /// A new memory block will be allocated for storing the vector. /// public DenseVector(Vector other) : this(other.Count) @@ -107,6 +107,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex other.Storage.CopyToUnchecked(Storage, skipClearing: true); } + /// + /// Create a new dense 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 DenseVector(IEnumerable other) + : this(DenseVectorStorage.FromEnumerable(other)) + { + } + /// /// Create a new dense vector directly binding to a raw array. /// The array is used directly without copying. diff --git a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs index d0448480..ccc4b224 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs @@ -37,6 +37,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 using Properties; using Storage; using System; + using System.Collections.Generic; using System.Diagnostics; /// @@ -144,6 +145,17 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } } } + + /// + /// Create a new dense matrix as a copy of the given enumerable. + /// The enumerable is assumed to be in column-major order. + /// This new matrix will be independent from the enumerable. + /// A new memory block will be allocated for storing the vector. + /// + public DenseMatrix(int rows, int columns, IEnumerable other) + : this(DenseColumnMajorMatrixStorage.FromColumnMajorEnumerable(rows, columns, other)) + { + } /// /// Create a new dense matrix as a copy of the given other matrix. diff --git a/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs b/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs index c86a9cd4..d412d767 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs @@ -99,7 +99,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 /// /// Create a new dense vector as a copy of the given other vector. /// This new vector will be independent from the other vector. - /// A new memory block will be allocated for storing the matrix. + /// A new memory block will be allocated for storing the vector. /// public DenseVector(Vector other) : this(other.Count) @@ -107,6 +107,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 other.Storage.CopyToUnchecked(Storage, skipClearing: true); } + /// + /// Create a new dense 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 DenseVector(IEnumerable other) + : this(DenseVectorStorage.FromEnumerable(other)) + { + } + /// /// Create a new dense vector directly binding to a raw array. /// The array is used directly without copying. diff --git a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs index 29398b2b..4524a03b 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs @@ -36,6 +36,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double using Properties; using Storage; using System; + using System.Collections.Generic; using System.Diagnostics; using Threading; @@ -144,6 +145,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double } } } + + /// + /// Create a new dense matrix as a copy of the given enumerable. + /// The enumerable is assumed to be in column-major order. + /// This new matrix will be independent from the enumerable. + /// A new memory block will be allocated for storing the vector. + /// + public DenseMatrix(int rows, int columns, IEnumerable other) + : this(DenseColumnMajorMatrixStorage.FromColumnMajorEnumerable(rows, columns, other)) + { + } /// /// Create a new dense matrix as a copy of the given other matrix. diff --git a/src/Numerics/LinearAlgebra/Double/DenseVector.cs b/src/Numerics/LinearAlgebra/Double/DenseVector.cs index cac1b120..b7b0c2dc 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseVector.cs @@ -35,6 +35,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double using Properties; using Storage; using System; + using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.Linq; @@ -99,7 +100,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double /// /// Create a new dense vector as a copy of the given other vector. /// This new vector will be independent from the other vector. - /// A new memory block will be allocated for storing the matrix. + /// A new memory block will be allocated for storing the vector. /// public DenseVector(Vector other) : this(other.Count) @@ -107,6 +108,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double other.Storage.CopyToUnchecked(Storage, skipClearing: true); } + /// + /// Create a new dense 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 DenseVector(IEnumerable other) + : this(DenseVectorStorage.FromEnumerable(other)) + { + } + /// /// Create a new dense vector directly binding to a raw array. /// The array is used directly without copying. diff --git a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs index 97f2ad9b..ea57ee8b 100644 --- a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs @@ -36,6 +36,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single using Properties; using Storage; using System; + using System.Collections.Generic; using System.Diagnostics; using Threading; @@ -145,6 +146,17 @@ namespace MathNet.Numerics.LinearAlgebra.Single } } + /// + /// Create a new dense matrix as a copy of the given enumerable. + /// The enumerable is assumed to be in column-major order. + /// This new matrix will be independent from the enumerable. + /// A new memory block will be allocated for storing the vector. + /// + public DenseMatrix(int rows, int columns, IEnumerable other) + : this(DenseColumnMajorMatrixStorage.FromColumnMajorEnumerable(rows, columns, other)) + { + } + /// /// Create a new dense matrix as a copy of the given other matrix. /// This new matrix will be independent from the other matrix. diff --git a/src/Numerics/LinearAlgebra/Single/DenseVector.cs b/src/Numerics/LinearAlgebra/Single/DenseVector.cs index 2e142c9d..2567f742 100644 --- a/src/Numerics/LinearAlgebra/Single/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/DenseVector.cs @@ -34,6 +34,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single using Generic; using Storage; using System; + using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.Linq; @@ -98,7 +99,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single /// /// Create a new dense vector as a copy of the given other vector. /// This new vector will be independent from the other vector. - /// A new memory block will be allocated for storing the matrix. + /// A new memory block will be allocated for storing the vector. /// public DenseVector(Vector other) : this(other.Count) @@ -106,6 +107,16 @@ namespace MathNet.Numerics.LinearAlgebra.Single other.Storage.CopyToUnchecked(Storage, skipClearing: true); } + /// + /// Create a new dense 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 DenseVector(IEnumerable other) + : this(DenseVectorStorage.FromEnumerable(other)) + { + } + /// /// Create a new dense vector directly binding to a raw array. /// The array is used directly without copying. diff --git a/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs index 90937964..309e9a9a 100644 --- a/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs @@ -29,6 +29,7 @@ // using System; +using System.Collections.Generic; using MathNet.Numerics.Properties; namespace MathNet.Numerics.LinearAlgebra.Storage @@ -98,6 +99,29 @@ namespace MathNet.Numerics.LinearAlgebra.Storage } } + // INITIALIZATION + + public static DenseColumnMajorMatrixStorage FromColumnMajorEnumerable(int rows, int columns, IEnumerable data) + { + if (data == null) + { + throw new ArgumentNullException("data"); + } + + var arrayData = data as T[]; + if (arrayData != null) + { + var copy = new T[arrayData.Length]; + Array.Copy(arrayData, copy, arrayData.Length); + return new DenseColumnMajorMatrixStorage(rows, columns, copy); + } + + var array = System.Linq.Enumerable.ToArray(data); + return new DenseColumnMajorMatrixStorage(rows, columns, array); + } + + // MATRIX COPY + internal override void CopyToUnchecked(MatrixStorage target, bool skipClearing = false) { var denseTarget = target as DenseColumnMajorMatrixStorage; diff --git a/src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs b/src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs index ac745f37..ab019454 100644 --- a/src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs @@ -90,6 +90,27 @@ namespace MathNet.Numerics.LinearAlgebra.Storage Array.Clear(Data, index, count); } + // INITIALIZATION + + public static DenseVectorStorage FromEnumerable(IEnumerable data) + { + if (data == null) + { + throw new ArgumentNullException("data"); + } + + var arrayData = data as T[]; + if (arrayData != null) + { + var copy = new T[arrayData.Length]; + Array.Copy(arrayData, copy, arrayData.Length); + return new DenseVectorStorage(copy.Length, copy); + } + + var array = System.Linq.Enumerable.ToArray(data); + return new DenseVectorStorage(array.Length, array); + } + // ENUMERATION public override IEnumerable Enumerate()