diff --git a/src/Numerics/LinearAlgebra/Complex/DenseVector.cs b/src/Numerics/LinearAlgebra/Complex/DenseVector.cs index 3517c3c3..c05e6ff4 100644 --- a/src/Numerics/LinearAlgebra/Complex/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex/DenseVector.cs @@ -126,17 +126,21 @@ namespace MathNet.Numerics.LinearAlgebra.Complex { } + /// + /// Create a new dense vector and initialize each value using the provided init function. + /// + public static DenseVector Create(int length, Func init) + { + return new DenseVector(DenseVectorStorage.OfInit(length, init)); + } + /// /// Create a new dense vector with values sampled from the provided random distribution. /// - public static DenseVector CreateRandom(int size, IContinuousDistribution distribution) + public static DenseVector CreateRandom(int length, IContinuousDistribution distribution) { - var storage = new DenseVectorStorage(size); - for (var i = 0; i < storage.Data.Length; i++) - { - storage.Data[i] = new Complex(distribution.Sample(), distribution.Sample()); - } - return new DenseVector(storage); + return new DenseVector(DenseVectorStorage.OfInit(length, + i => new Complex(distribution.Sample(), distribution.Sample()))); } /// diff --git a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs index 11d8dc17..7eddd3b3 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs @@ -127,6 +127,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex { } + /// + /// Create a new sparse vector and initialize each value using the provided init function. + /// + public static SparseVector Create(int length, Func init) + { + return new SparseVector(SparseVectorStorage.OfInit(length, init)); + } + /// /// Creates a matrix with the given dimensions using the same storage type /// as this vector. diff --git a/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs b/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs index 7b987363..e2f7c379 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs @@ -126,17 +126,21 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 { } + /// + /// Create a new dense vector and initialize each value using the provided init function. + /// + public static DenseVector Create(int length, Func init) + { + return new DenseVector(DenseVectorStorage.OfInit(length, init)); + } + /// /// Create a new dense vector with values sampled from the provided random distribution. /// - public static DenseVector CreateRandom(int size, IContinuousDistribution distribution) + public static DenseVector CreateRandom(int length, IContinuousDistribution distribution) { - var storage = new DenseVectorStorage(size); - for (var i = 0; i < storage.Data.Length; i++) - { - storage.Data[i] = new Complex32((float)distribution.Sample(), (float)distribution.Sample()); - } - return new DenseVector(storage); + return new DenseVector(DenseVectorStorage.OfInit(length, + i => new Complex32((float) distribution.Sample(), (float) distribution.Sample()))); } /// diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs index 364d2c8b..cedaf48c 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs @@ -127,6 +127,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 { } + /// + /// Create a new sparse vector and initialize each value using the provided init function. + /// + public static SparseVector Create(int length, Func init) + { + return new SparseVector(SparseVectorStorage.OfInit(length, init)); + } + /// /// Creates a matrix with the given dimensions using the same storage type /// as this vector. diff --git a/src/Numerics/LinearAlgebra/Double/DenseVector.cs b/src/Numerics/LinearAlgebra/Double/DenseVector.cs index d1da3c27..f4110545 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseVector.cs @@ -127,17 +127,21 @@ namespace MathNet.Numerics.LinearAlgebra.Double { } + /// + /// Create a new dense vector and initialize each value using the provided init function. + /// + public static DenseVector Create(int length, Func init) + { + return new DenseVector(DenseVectorStorage.OfInit(length, init)); + } + /// /// Create a new dense vector with values sampled from the provided random distribution. /// - public static DenseVector CreateRandom(int size, IContinuousDistribution distribution) + public static DenseVector CreateRandom(int length, IContinuousDistribution distribution) { - var storage = new DenseVectorStorage(size); - for (var i = 0; i < storage.Data.Length; i++) - { - storage.Data[i] = distribution.Sample(); - } - return new DenseVector(storage); + return new DenseVector(DenseVectorStorage.OfInit(length, + i => distribution.Sample())); } /// diff --git a/src/Numerics/LinearAlgebra/Double/SparseVector.cs b/src/Numerics/LinearAlgebra/Double/SparseVector.cs index 87d21ecd..e3a55baf 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseVector.cs @@ -127,6 +127,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double { } + /// + /// Create a new sparse vector and initialize each value using the provided init function. + /// + public static SparseVector Create(int length, Func init) + { + return new SparseVector(SparseVectorStorage.OfInit(length, init)); + } + /// /// Creates a matrix with the given dimensions using the same storage type /// as this vector. diff --git a/src/Numerics/LinearAlgebra/Single/DenseVector.cs b/src/Numerics/LinearAlgebra/Single/DenseVector.cs index 9c1c3a26..ac4694f8 100644 --- a/src/Numerics/LinearAlgebra/Single/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/DenseVector.cs @@ -126,17 +126,21 @@ namespace MathNet.Numerics.LinearAlgebra.Single { } + /// + /// Create a new dense vector and initialize each value using the provided init function. + /// + public static DenseVector Create(int length, Func init) + { + return new DenseVector(DenseVectorStorage.OfInit(length, init)); + } + /// /// Create a new dense vector with values sampled from the provided random distribution. /// - public static DenseVector CreateRandom(int size, IContinuousDistribution distribution) + public static DenseVector CreateRandom(int length, IContinuousDistribution distribution) { - var storage = new DenseVectorStorage(size); - for (var i = 0; i < storage.Data.Length; i++) - { - storage.Data[i] = (float)distribution.Sample(); - } - return new DenseVector(storage); + return new DenseVector(DenseVectorStorage.OfInit(length, + i => (float) distribution.Sample())); } /// diff --git a/src/Numerics/LinearAlgebra/Single/SparseVector.cs b/src/Numerics/LinearAlgebra/Single/SparseVector.cs index c59b9953..1817a3d2 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseVector.cs @@ -127,6 +127,14 @@ namespace MathNet.Numerics.LinearAlgebra.Single { } + /// + /// Create a new sparse vector and initialize each value using the provided init function. + /// + public static SparseVector Create(int length, Func init) + { + return new SparseVector(SparseVectorStorage.OfInit(length, init)); + } + /// /// Creates a matrix with the given dimensions using the same storage type /// as this vector. diff --git a/src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs b/src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs index bda26619..fb4c1aab 100644 --- a/src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs @@ -99,6 +99,21 @@ namespace MathNet.Numerics.LinearAlgebra.Storage return storage; } + public static DenseVectorStorage OfInit(int length, Func init) + { + if (length < 1) + { + throw new ArgumentOutOfRangeException("length", string.Format(Resources.ArgumentLessThanOne, length)); + } + + var data = new T[length]; + for (int i = 0; i < data.Length; i++) + { + data[i] = init(i); + } + return new DenseVectorStorage(length, data); + } + public static DenseVectorStorage OfEnumerable(IEnumerable data) { if (data == null) diff --git a/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs b/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs index bc60705b..04f95483 100644 --- a/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs @@ -291,6 +291,32 @@ namespace MathNet.Numerics.LinearAlgebra.Storage return storage; } + public static SparseVectorStorage OfInit(int length, Func init) + { + if (length < 1) + { + throw new ArgumentOutOfRangeException("length", string.Format(Resources.ArgumentLessThanOne, length)); + } + + var indices = new List(); + var values = new List(); + for (int i = 0; i < length; i++) + { + var item = init(i); + if (!Zero.Equals(item)) + { + values.Add(item); + indices.Add(length); + } + } + return new SparseVectorStorage(length) + { + Indices = indices.ToArray(), + Values = values.ToArray(), + ValueCount = values.Count + }; + } + public static SparseVectorStorage OfEnumerable(IEnumerable data) { if (data == null)