diff --git a/src/FSharp/LinearAlgebra.Double.Matrix.fs b/src/FSharp/LinearAlgebra.Double.Matrix.fs index 55a35dc3..779b1ce8 100644 --- a/src/FSharp/LinearAlgebra.Double.Matrix.fs +++ b/src/FSharp/LinearAlgebra.Double.Matrix.fs @@ -76,13 +76,10 @@ module DenseMatrix = let inline randomCreate (rows: int) (cols: int) dist = DenseMatrix.CreateRandom(rows, cols, dist) :> _ Matrix /// Create a matrix with the given dimension and set all values to x. - let inline create (rows: int) (cols: int) x = DenseMatrix.Create(rows, cols, fun i j -> x) :> _ Matrix + let inline create (rows: int) (cols: int) (x: float) = DenseMatrix.Create(rows, cols, x) :> _ Matrix /// Create a matrix with the given dimension and set all diagonal values to x. All other values are zero. - let createDiag (rows: int) (cols: int) x = - let A = DenseMatrix(rows,cols) - for i=0 to min (rows-1) (cols-1) do A.At(i,i,x) - A :> _ Matrix + let inline createDiag (rows: int) (cols: int) (x: float) = DenseMatrix.CreateDiagonal(rows, cols, x) :> _ Matrix /// Initialize a matrix by calling a construction function for every element. let inline init (rows: int) (cols: int) (f: int -> int -> float) = DenseMatrix.Create(rows, cols, fun i j -> f i j) :> _ Matrix @@ -94,10 +91,7 @@ module DenseMatrix = let inline initColumns (cols: int) (f: int -> Vector) = DenseMatrix.OfColumnVectors(Array.init cols f) :> _ Matrix /// Initialize a matrix by calling a construction function for every diagonal element. All other values are zero. - let initDiag (rows: int) (cols: int) (f: int -> float) = - let A = DenseMatrix(rows,cols) - for i=0 to min (rows-1) (cols-1) do A.At(i,i,f i) - A :> _ Matrix + let inline initDiag (rows: int) (cols: int) (f: int -> float) = DenseMatrix.CreateDiagonal(rows, cols, f) :> _ Matrix /// Create an identity matrix with the given dimension. let inline identity (rows: int) (cols: int) = createDiag rows cols 1.0 @@ -158,11 +152,11 @@ module SparseMatrix = /// Create an all-zero matrix with the given dimension. let inline zeroCreate (rows: int) (cols: int) = SparseMatrix(rows, cols) :> _ Matrix + /// Create a matrix with the given dimension and set all values to x. Note that a dense matrix would likely be more appropriate. + let inline create (rows: int) (cols: int) (x: float) = SparseMatrix.Create(rows, cols, x) :> _ Matrix + /// Create a matrix with the given dimension and set all diagonal values to x. All other values are zero. - let createDiag (rows: int) (cols: int) x = - let A = SparseMatrix(rows,cols) - for i=0 to min (rows-1) (cols-1) do A.At(i,i,x) - A :> _ Matrix + let inline createDiag (rows: int) (cols: int) (x: float) = SparseMatrix.CreateDiagonal(rows, cols, x) :> _ Matrix /// Initialize a matrix by calling a construction function for every element. let inline init (rows: int) (cols: int) (f: int -> int -> float) = SparseMatrix.Create(rows, cols, fun n m -> f n m) :> _ Matrix @@ -174,10 +168,7 @@ module SparseMatrix = let inline initColumns (cols: int) (f: int -> Vector) = SparseMatrix.OfColumnVectors(Array.init cols f) :> _ Matrix /// Initialize a matrix by calling a construction function for every diagonal element. All other values are zero. - let initDiag (rows: int) (cols: int) (f: int -> float) = - let A = SparseMatrix(rows,cols) - for i=0 to min (rows-1) (cols-1) do A.At(i,i,f i) - A :> _ Matrix + let inline initDiag (rows: int) (cols: int) (f: int -> float) = SparseMatrix.CreateDiagonal(rows, cols, f) :> _ Matrix /// Create an identity matrix with the given dimension. let inline identity (rows: int) (cols: int) = createDiag rows cols 1.0 diff --git a/src/FSharp/LinearAlgebra.Double.Vector.fs b/src/FSharp/LinearAlgebra.Double.Vector.fs index e7f61568..4b3de758 100644 --- a/src/FSharp/LinearAlgebra.Double.Vector.fs +++ b/src/FSharp/LinearAlgebra.Double.Vector.fs @@ -62,10 +62,13 @@ module DenseVector = let inline randomCreate (n: int) dist = DenseVector.CreateRandom(n, dist) :> _ Vector /// Initialize an x-valued vector with the given dimension. - let inline create (n: int) x = DenseVector.Create(n, fun i -> x) :> _ Vector + let inline create (n: int) (x: float) = DenseVector.Create(n, x) :> _ Vector /// Initialize a vector by calling a construction function for every element. - let inline init (n: int) (f: int -> float) = DenseVector.Create(n, fun i -> f i) :> _ Vector + let inline init (n: int) (f: int -> float) = DenseVector.Create(n, f) :> _ Vector + + /// Create a vector from a float array (by copying - use raw instead if no copy is needed). + let inline ofArray (fl: float array) = DenseVector(Array.copy fl) :> _ Vector /// Create a vector from a float list. let inline ofList (fl: float list) = DenseVector(Array.ofList fl) :> _ Vector @@ -93,8 +96,14 @@ module SparseVector = /// Initialize an all-zero vector with the given dimension. let inline zeroCreate (n: int) = SparseVector(n) :> _ Vector + /// Initialize an x-valued vector with the given dimension. + let inline create (n: int) (x: float) = SparseVector.Create(n, x) :> _ Vector + /// Initialize a vector by calling a construction function for every element. - let inline init (n: int) (f: int -> float) = SparseVector.Create(n, fun i -> f i) :> _ Vector + let inline init (n: int) (f: int -> float) = SparseVector.Create(n, f) :> _ Vector + + /// Create a sparse vector from a float array. + let inline ofArray (fl: float array) = SparseVector.OfEnumerable(Seq.ofArray fl) :> _ Vector /// Create a sparse vector from a float list. let inline ofList (fl: float list) = SparseVector.OfEnumerable(Seq.ofList fl) :> _ Vector diff --git a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs index 34bca505..184acb48 100644 --- a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs @@ -234,6 +234,15 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowVectors(storage)); } + /// + /// Create a new dense matrix and initialize each value to the same provided value. + /// + public static DenseMatrix Create(int rows, int columns, Complex value) + { + if (value == Complex.Zero) return new DenseMatrix(rows, columns); + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfInit(rows, columns, (i, j) => value)); + } + /// /// Create a new dense matrix and initialize each value using the provided init function. /// @@ -242,6 +251,23 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return new DenseMatrix(DenseColumnMajorMatrixStorage.OfInit(rows, columns, init)); } + /// + /// Create a new diagonal dense matrix and initialize each diagonal value to the same provided value. + /// + public static DenseMatrix CreateDiagonal(int rows, int columns, Complex value) + { + if (value == Complex.Zero) return new DenseMatrix(rows, columns); + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfDiagonalInit(rows, columns, i => value)); + } + + /// + /// Create a new diagonal dense matrix and initialize each diagonal value using the provided init function. + /// + public static DenseMatrix CreateDiagonal(int rows, int columns, Func init) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfDiagonalInit(rows, columns, init)); + } + /// /// Create a new dense matrix with values sampled from the provided random distribution. /// diff --git a/src/Numerics/LinearAlgebra/Complex/DenseVector.cs b/src/Numerics/LinearAlgebra/Complex/DenseVector.cs index a0bcb0cc..2214c347 100644 --- a/src/Numerics/LinearAlgebra/Complex/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex/DenseVector.cs @@ -127,6 +127,15 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return new DenseVector(DenseVectorStorage.OfIndexedEnumerable(length, enumerable)); } + /// + /// Create a new dense vector and initialize each value using the provided value. + /// + public static DenseVector Create(int length, Complex value) + { + if (value == Complex.Zero) return new DenseVector(length); + return new DenseVector(DenseVectorStorage.OfInit(length, i => value)); + } + /// /// Create a new dense vector and initialize each value using the provided init function. /// diff --git a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs index 0294873f..e7972017 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs @@ -223,6 +223,15 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowVectors(storage)); } + /// + /// Create a new sparse matrix and initialize each value to the same provided value. + /// + public static SparseMatrix Create(int rows, int columns, Complex value) + { + if (value == Complex.Zero) return new SparseMatrix(rows, columns); + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfInit(rows, columns, (i, j) => value)); + } + /// /// Create a new sparse matrix and initialize each value using the provided init function. /// @@ -231,6 +240,23 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return new SparseMatrix(SparseCompressedRowMatrixStorage.OfInit(rows, columns, init)); } + /// + /// Create a new diagonal sparse matrix and initialize each diagonal value to the same provided value. + /// + public static SparseMatrix CreateDiagonal(int rows, int columns, Complex value) + { + if (value == Complex.Zero) return new SparseMatrix(rows, columns); + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfDiagonalInit(rows, columns, i => value)); + } + + /// + /// Create a new diagonal sparse matrix and initialize each diagonal value using the provided init function. + /// + public static SparseMatrix CreateDiagonal(int rows, int columns, Func init) + { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfDiagonalInit(rows, columns, init)); + } + /// /// Creates a SparseMatrix for the given number of rows and columns. /// diff --git a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs index a7c4cc98..e1251a79 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs @@ -116,6 +116,15 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return new SparseVector(SparseVectorStorage.OfIndexedEnumerable(length, enumerable)); } + /// + /// Create a new sparse vector and initialize each value using the provided value. + /// + public static SparseVector Create(int length, Complex value) + { + if (value == Complex.Zero) return new SparseVector(length); + return new SparseVector(SparseVectorStorage.OfInit(length, i => value)); + } + /// /// Create a new sparse vector and initialize each value using the provided init function. /// diff --git a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs index 76f912e0..11001cee 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs @@ -229,6 +229,15 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowVectors(storage)); } + /// + /// Create a new dense matrix and initialize each value to the same provided value. + /// + public static DenseMatrix Create(int rows, int columns, Complex32 value) + { + if (value == Complex32.Zero) return new DenseMatrix(rows, columns); + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfInit(rows, columns, (i, j) => value)); + } + /// /// Create a new dense matrix and initialize each value using the provided init function. /// @@ -237,6 +246,23 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return new DenseMatrix(DenseColumnMajorMatrixStorage.OfInit(rows, columns, init)); } + /// + /// Create a new diagonal dense matrix and initialize each diagonal value to the same provided value. + /// + public static DenseMatrix CreateDiagonal(int rows, int columns, Complex32 value) + { + if (value == Complex32.Zero) return new DenseMatrix(rows, columns); + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfDiagonalInit(rows, columns, i => value)); + } + + /// + /// Create a new diagonal dense matrix and initialize each diagonal value using the provided init function. + /// + public static DenseMatrix CreateDiagonal(int rows, int columns, Func init) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfDiagonalInit(rows, columns, init)); + } + /// /// Create a new dense matrix with values sampled from the provided random distribution. /// diff --git a/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs b/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs index bb52e1fc..6fab3dfa 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs @@ -122,6 +122,15 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return new DenseVector(DenseVectorStorage.OfIndexedEnumerable(length, enumerable)); } + /// + /// Create a new dense vector and initialize each value using the provided value. + /// + public static DenseVector Create(int length, Complex32 value) + { + if (value == Complex32.Zero) return new DenseVector(length); + return new DenseVector(DenseVectorStorage.OfInit(length, i => value)); + } + /// /// Create a new dense vector and initialize each value using the provided init function. /// diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs index be1eafe9..18e50277 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs @@ -218,6 +218,15 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowVectors(storage)); } + /// + /// Create a new sparse matrix and initialize each value to the same provided value. + /// + public static SparseMatrix Create(int rows, int columns, Complex32 value) + { + if (value == Complex32.Zero) return new SparseMatrix(rows, columns); + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfInit(rows, columns, (i, j) => value)); + } + /// /// Create a new sparse matrix and initialize each value using the provided init function. /// @@ -226,6 +235,23 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return new SparseMatrix(SparseCompressedRowMatrixStorage.OfInit(rows, columns, init)); } + /// + /// Create a new diagonal sparse matrix and initialize each diagonal value to the same provided value. + /// + public static SparseMatrix CreateDiagonal(int rows, int columns, Complex32 value) + { + if (value == Complex32.Zero) return new SparseMatrix(rows, columns); + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfDiagonalInit(rows, columns, i => value)); + } + + /// + /// Create a new diagonal sparse matrix and initialize each diagonal value using the provided init function. + /// + public static SparseMatrix CreateDiagonal(int rows, int columns, Func init) + { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfDiagonalInit(rows, columns, init)); + } + /// /// Creates a SparseMatrix for the given number of rows and columns. /// diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs index 2052f5cd..54a40d35 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs @@ -111,6 +111,15 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return new SparseVector(SparseVectorStorage.OfIndexedEnumerable(length, enumerable)); } + /// + /// Create a new sparse vector and initialize each value using the provided value. + /// + public static SparseVector Create(int length, Complex32 value) + { + if (value == Complex32.Zero) return new SparseVector(length); + return new SparseVector(SparseVectorStorage.OfInit(length, i => value)); + } + /// /// Create a new sparse vector and initialize each value using the provided init function. /// diff --git a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs index f5ed2cdc..dc96f37f 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs @@ -227,6 +227,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowVectors(storage)); } + /// + /// Create a new dense matrix and initialize each value to the same provided value. + /// + public static DenseMatrix Create(int rows, int columns, double value) + { + if (value == 0d) return new DenseMatrix(rows, columns); + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfInit(rows, columns, (i, j) => value)); + } + /// /// Create a new dense matrix and initialize each value using the provided init function. /// @@ -235,13 +244,29 @@ namespace MathNet.Numerics.LinearAlgebra.Double return new DenseMatrix(DenseColumnMajorMatrixStorage.OfInit(rows, columns, init)); } + /// + /// Create a new diagonal dense matrix and initialize each diagonal value to the same provided value. + /// + public static DenseMatrix CreateDiagonal(int rows, int columns, double value) + { + if (value == 0d) return new DenseMatrix(rows, columns); + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfDiagonalInit(rows, columns, i => value)); + } + + /// + /// Create a new diagonal dense matrix and initialize each diagonal value using the provided init function. + /// + public static DenseMatrix CreateDiagonal(int rows, int columns, Func init) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfDiagonalInit(rows, columns, init)); + } + /// /// Create a new dense matrix with values sampled from the provided random distribution. /// public static DenseMatrix CreateRandom(int rows, int columns, IContinuousDistribution distribution) { - return new DenseMatrix(DenseColumnMajorMatrixStorage.OfInit(rows, columns, - (i, j) => distribution.Sample())); + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfInit(rows, columns, (i, j) => distribution.Sample())); } /// diff --git a/src/Numerics/LinearAlgebra/Double/DenseVector.cs b/src/Numerics/LinearAlgebra/Double/DenseVector.cs index 07f3fa50..b7da46cd 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseVector.cs @@ -122,6 +122,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double return new DenseVector(DenseVectorStorage.OfIndexedEnumerable(length, enumerable)); } + /// + /// Create a new dense vector and initialize each value using the provided value. + /// + public static DenseVector Create(int length, double value) + { + if (value == 0d) return new DenseVector(length); + return new DenseVector(DenseVectorStorage.OfInit(length, i => value)); + } + /// /// Create a new dense vector and initialize each value using the provided init function. /// diff --git a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs index e16865ee..ad0fd3c3 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs @@ -216,6 +216,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowVectors(storage)); } + /// + /// Create a new sparse matrix and initialize each value to the same provided value. + /// + public static SparseMatrix Create(int rows, int columns, double value) + { + if (value == 0d) return new SparseMatrix(rows, columns); + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfInit(rows, columns, (i, j) => value)); + } + /// /// Create a new sparse matrix and initialize each value using the provided init function. /// @@ -224,6 +233,23 @@ namespace MathNet.Numerics.LinearAlgebra.Double return new SparseMatrix(SparseCompressedRowMatrixStorage.OfInit(rows, columns, init)); } + /// + /// Create a new diagonal sparse matrix and initialize each diagonal value to the same provided value. + /// + public static SparseMatrix CreateDiagonal(int rows, int columns, double value) + { + if (value == 0d) return new SparseMatrix(rows, columns); + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfDiagonalInit(rows, columns, i => value)); + } + + /// + /// Create a new diagonal sparse matrix and initialize each diagonal value using the provided init function. + /// + public static SparseMatrix CreateDiagonal(int rows, int columns, Func init) + { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfDiagonalInit(rows, columns, init)); + } + /// /// Creates a SparseMatrix for the given number of rows and columns. /// diff --git a/src/Numerics/LinearAlgebra/Double/SparseVector.cs b/src/Numerics/LinearAlgebra/Double/SparseVector.cs index 6c3bc299..0435aad2 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseVector.cs @@ -111,6 +111,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double return new SparseVector(SparseVectorStorage.OfIndexedEnumerable(length, enumerable)); } + /// + /// Create a new sparse vector and initialize each value using the provided value. + /// + public static SparseVector Create(int length, double value) + { + if (value == 0d) return new SparseVector(length); + return new SparseVector(SparseVectorStorage.OfInit(length, i => value)); + } + /// /// Create a new sparse vector and initialize each value using the provided init function. /// diff --git a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs index 209c6778..18a71bf6 100644 --- a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs @@ -227,6 +227,15 @@ namespace MathNet.Numerics.LinearAlgebra.Single return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowVectors(storage)); } + /// + /// Create a new dense matrix and initialize each value to the same provided value. + /// + public static DenseMatrix Create(int rows, int columns, float value) + { + if (value == 0f) return new DenseMatrix(rows, columns); + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfInit(rows, columns, (i, j) => value)); + } + /// /// Create a new dense matrix and initialize each value using the provided init function. /// @@ -235,13 +244,29 @@ namespace MathNet.Numerics.LinearAlgebra.Single return new DenseMatrix(DenseColumnMajorMatrixStorage.OfInit(rows, columns, init)); } + /// + /// Create a new diagonal dense matrix and initialize each diagonal value to the same provided value. + /// + public static DenseMatrix CreateDiagonal(int rows, int columns, float value) + { + if (value == 0f) return new DenseMatrix(rows, columns); + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfDiagonalInit(rows, columns, i => value)); + } + + /// + /// Create a new diagonal dense matrix and initialize each diagonal value using the provided init function. + /// + public static DenseMatrix CreateDiagonal(int rows, int columns, Func init) + { + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfDiagonalInit(rows, columns, init)); + } + /// /// Create a new dense matrix with values sampled from the provided random distribution. /// public static DenseMatrix CreateRandom(int rows, int columns, IContinuousDistribution distribution) { - return new DenseMatrix(DenseColumnMajorMatrixStorage.OfInit(rows, columns, - (i, j) => (float) distribution.Sample())); + return new DenseMatrix(DenseColumnMajorMatrixStorage.OfInit(rows, columns, (i, j) => (float) distribution.Sample())); } /// diff --git a/src/Numerics/LinearAlgebra/Single/DenseVector.cs b/src/Numerics/LinearAlgebra/Single/DenseVector.cs index e9e1684e..a665dc1b 100644 --- a/src/Numerics/LinearAlgebra/Single/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/DenseVector.cs @@ -121,6 +121,15 @@ namespace MathNet.Numerics.LinearAlgebra.Single return new DenseVector(DenseVectorStorage.OfIndexedEnumerable(length, enumerable)); } + /// + /// Create a new dense vector and initialize each value using the provided value. + /// + public static DenseVector Create(int length, float value) + { + if (value == 0f) return new DenseVector(length); + return new DenseVector(DenseVectorStorage.OfInit(length, i => value)); + } + /// /// Create a new dense vector and initialize each value using the provided init function. /// diff --git a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs index ce426e54..92376672 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs @@ -216,6 +216,15 @@ namespace MathNet.Numerics.LinearAlgebra.Single return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowVectors(storage)); } + /// + /// Create a new sparse matrix and initialize each value to the same provided value. + /// + public static SparseMatrix Create(int rows, int columns, float value) + { + if (value == 0f) return new SparseMatrix(rows, columns); + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfInit(rows, columns, (i, j) => value)); + } + /// /// Create a new sparse matrix and initialize each value using the provided init function. /// @@ -224,6 +233,23 @@ namespace MathNet.Numerics.LinearAlgebra.Single return new SparseMatrix(SparseCompressedRowMatrixStorage.OfInit(rows, columns, init)); } + /// + /// Create a new diagonal sparse matrix and initialize each diagonal value to the same provided value. + /// + public static SparseMatrix CreateDiagonal(int rows, int columns, float value) + { + if (value == 0f) return new SparseMatrix(rows, columns); + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfDiagonalInit(rows, columns, i => value)); + } + + /// + /// Create a new diagonal sparse matrix and initialize each diagonal value using the provided init function. + /// + public static SparseMatrix CreateDiagonal(int rows, int columns, Func init) + { + return new SparseMatrix(SparseCompressedRowMatrixStorage.OfDiagonalInit(rows, columns, init)); + } + /// /// Creates a SparseMatrix for the given number of rows and columns. /// diff --git a/src/Numerics/LinearAlgebra/Single/SparseVector.cs b/src/Numerics/LinearAlgebra/Single/SparseVector.cs index 2a0566e0..0c6416db 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseVector.cs @@ -111,6 +111,15 @@ namespace MathNet.Numerics.LinearAlgebra.Single return new SparseVector(SparseVectorStorage.OfIndexedEnumerable(length, enumerable)); } + /// + /// Create a new sparse vector and initialize each value using the provided value. + /// + public static SparseVector Create(int length, float value) + { + if (value == 0f) return new SparseVector(length); + return new SparseVector(SparseVectorStorage.OfInit(length, i => value)); + } + /// /// Create a new sparse vector and initialize each value using the provided init function. /// diff --git a/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs index ce4f0dd5..0ee45728 100644 --- a/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs @@ -114,9 +114,9 @@ namespace MathNet.Numerics.LinearAlgebra.Storage { var storage = new DenseColumnMajorMatrixStorage(rows, columns); int index = 0; - for (var j = 0; j < storage.ColumnCount; j++) + for (var j = 0; j < columns; j++) { - for (var i = 0; i < storage.RowCount; i++) + for (var i = 0; i < rows; i++) { storage.Data[index++] = init(i, j); } @@ -124,6 +124,19 @@ namespace MathNet.Numerics.LinearAlgebra.Storage return storage; } + public static DenseColumnMajorMatrixStorage OfDiagonalInit(int rows, int columns, Func init) + { + var storage = new DenseColumnMajorMatrixStorage(rows, columns); + int index = 0; + int stride = rows + 1; + for (var i = 0; i < Math.Min(rows, columns); i++) + { + storage.Data[index] = init(i); + index += stride; + } + return storage; + } + public static DenseColumnMajorMatrixStorage OfArray(T[,] array) { var storage = new DenseColumnMajorMatrixStorage(array.GetLength(0), array.GetLength(1)); diff --git a/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs index 9ea86671..f9cc29e2 100644 --- a/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs @@ -384,10 +384,10 @@ namespace MathNet.Numerics.LinearAlgebra.Storage rowPointers[row] = values.Count; for (int col = 0; col < columns; col++) { - var item = init(row, col); - if (!Zero.Equals(item)) + var x = init(row, col); + if (!Zero.Equals(x)) { - values.Add(item); + values.Add(x); columnIndices.Add(col); } } @@ -399,6 +399,30 @@ namespace MathNet.Numerics.LinearAlgebra.Storage return storage; } + public static SparseCompressedRowMatrixStorage OfDiagonalInit(int rows, int columns, Func init) + { + var storage = new SparseCompressedRowMatrixStorage(rows, columns); + var rowPointers = storage.RowPointers; + var columnIndices = new List(); + var values = new List(); + + for (int i = 0; i < Math.Min(rows, columns); i++) + { + rowPointers[i] = values.Count; + var x = init(i); + if (!Zero.Equals(x)) + { + values.Add(x); + columnIndices.Add(i); + } + } + + storage.ColumnIndices = columnIndices.ToArray(); + storage.Values = values.ToArray(); + storage.ValueCount = values.Count; + return storage; + } + public static SparseCompressedRowMatrixStorage OfArray(T[,] array) { var storage = new SparseCompressedRowMatrixStorage(array.GetLength(0), array.GetLength(1)); diff --git a/src/UnitTests/LinearAlgebraTests/Complex32/DenseMatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Complex32/DenseMatrixTests.cs index 70cf59c8..6687cbbb 100644 --- a/src/UnitTests/LinearAlgebraTests/Complex32/DenseMatrixTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Complex32/DenseMatrixTests.cs @@ -155,7 +155,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32 [Test] public void CanCreateMatrixWithUniformValues() { - var matrix = DenseMatrix.Create(10, 10, (i, j) => new Complex32(10.0f, 1)); + var matrix = DenseMatrix.Create(10, 10, new Complex32(10.0f, 1)); for (var i = 0; i < matrix.RowCount; i++) { for (var j = 0; j < matrix.ColumnCount; j++) diff --git a/src/UnitTests/LinearAlgebraTests/Single/DenseMatrixTests.cs b/src/UnitTests/LinearAlgebraTests/Single/DenseMatrixTests.cs index f0ca1cd6..b3e273e3 100644 --- a/src/UnitTests/LinearAlgebraTests/Single/DenseMatrixTests.cs +++ b/src/UnitTests/LinearAlgebraTests/Single/DenseMatrixTests.cs @@ -154,7 +154,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Single [Test] public void CanCreateMatrixWithUniformValues() { - var matrix = DenseMatrix.Create(10, 10, (i, j) => 10.0f); + var matrix = DenseMatrix.Create(10, 10, 10.0f); for (var i = 0; i < matrix.RowCount; i++) { for (var j = 0; j < matrix.ColumnCount; j++)