From 1d0c49086c7b2aa04cac80fa5d65db505c5093b3 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sun, 1 Sep 2013 14:12:29 +0200 Subject: [PATCH] LA: rework create matrix from diagonal array or vector --- src/FSharp/LinearAlgebra.Double.Matrix.fs | 30 +++++++----- .../LinearAlgebra/Complex/DenseMatrix.cs | 48 +++++++++++++++++++ .../LinearAlgebra/Complex/SparseMatrix.cs | 48 +++++++++++++++++++ .../LinearAlgebra/Complex32/DenseMatrix.cs | 48 +++++++++++++++++++ .../LinearAlgebra/Complex32/SparseMatrix.cs | 48 +++++++++++++++++++ .../LinearAlgebra/Double/DenseMatrix.cs | 48 +++++++++++++++++++ .../LinearAlgebra/Double/SparseMatrix.cs | 48 +++++++++++++++++++ .../LinearAlgebra/Single/DenseMatrix.cs | 48 +++++++++++++++++++ .../LinearAlgebra/Single/SparseMatrix.cs | 48 +++++++++++++++++++ 9 files changed, 402 insertions(+), 12 deletions(-) diff --git a/src/FSharp/LinearAlgebra.Double.Matrix.fs b/src/FSharp/LinearAlgebra.Double.Matrix.fs index 779b1ce8..be854200 100644 --- a/src/FSharp/LinearAlgebra.Double.Matrix.fs +++ b/src/FSharp/LinearAlgebra.Double.Matrix.fs @@ -135,14 +135,17 @@ module DenseMatrix = /// Create a matrix with a given dimension from an indexed sequences of row, column, value tuples. let inline ofSeqi (rows: int) (cols: int) (indexed: #seq) = DenseMatrix.OfIndexed(rows, cols, indexed) :> _ Matrix + /// Create a square matrix with the vector elements on the diagonal. + let inline ofDiag (v: Vector) = DenseMatrix.OfDiagonalVector(v) :> _ Matrix + /// Create a matrix with the vector elements on the diagonal. - let ofDiag2 (rows: int) (cols: int) (v: Vector) = - let A = DenseMatrix(rows,cols) - A.SetDiagonal(v) - A :> _ Matrix + let inline ofDiag2 (rows: int) (cols: int) (v: Vector) = DenseMatrix.OfDiagonalVector(rows, cols, v) :> _ Matrix - /// Create a square matrix with the vector elements on the diagonal. - let inline ofDiag (v: Vector) = ofDiag2 v.Count v.Count v + /// Create a square matrix with the array elements on the diagonal. + let inline ofDiagArray (array: float array) = DenseMatrix.OfDiagonalArray(array) :> _ Matrix + + /// Create a matrix with the array elements on the diagonal. + let inline ofDiagArray2 (rows: int) (cols: int) (array: float array) = DenseMatrix.OfDiagonalArray(rows, cols, array) :> _ Matrix /// A module which implements functional sparse vector operations. @@ -212,11 +215,14 @@ module SparseMatrix = /// Create a matrix with a given dimension from an indexed sequences of row, column, value tuples. let inline ofSeqi (rows: int) (cols: int) (indexed: #seq) = SparseMatrix.OfIndexed(rows, cols, indexed) :> _ Matrix + /// Create a square matrix with the vector elements on the diagonal. + let inline ofDiag (v: Vector) = SparseMatrix.OfDiagonalVector(v) :> _ Matrix + /// Create a matrix with the vector elements on the diagonal. - let ofDiag2 (rows: int) (cols: int) (v: Vector) = - let A = SparseMatrix(rows,cols) - A.SetDiagonal(v) - A :> _ Matrix + let inline ofDiag2 (rows: int) (cols: int) (v: Vector) = SparseMatrix.OfDiagonalVector(rows, cols, v) :> _ Matrix - /// Create a square matrix with the vector elements on the diagonal. - let inline ofDiag (v: Vector) = ofDiag2 v.Count v.Count v + /// Create a square matrix with the array elements on the diagonal. + let inline ofDiagArray (array: float array) = SparseMatrix.OfDiagonalArray(array) :> _ Matrix + + /// Create a matrix with the array elements on the diagonal. + let inline ofDiagArray2 (rows: int) (cols: int) (array: float array) = SparseMatrix.OfDiagonalArray(rows, cols, array) :> _ Matrix diff --git a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs index 184acb48..717b67a8 100644 --- a/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs @@ -234,6 +234,54 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowVectors(storage)); } + /// + /// Create a new dense matrix with the diagonal as a copy of the given vector. + /// This new matrix will be independent from the vector. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfDiagonalVector(Vector diagonal) + { + var m = new DenseMatrix(diagonal.Count, diagonal.Count); + m.SetDiagonal(diagonal); + return m; + } + + /// + /// Create a new dense matrix with the diagonal as a copy of the given vector. + /// This new matrix will be independent from the vector. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfDiagonalVector(int rows, int columns, Vector diagonal) + { + var m = new DenseMatrix(rows, columns); + m.SetDiagonal(diagonal); + return m; + } + + /// + /// Create a new dense matrix with the diagonal as a copy of the given array. + /// This new matrix will be independent from the array. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfDiagonalArray(Complex[] diagonal) + { + var m = new DenseMatrix(diagonal.Length, diagonal.Length); + m.SetDiagonal(diagonal); + return m; + } + + /// + /// Create a new dense matrix with the diagonal as a copy of the given array. + /// This new matrix will be independent from the array. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfDiagonalArray(int rows, int columns, Complex[] diagonal) + { + var m = new DenseMatrix(rows, columns); + m.SetDiagonal(diagonal); + return m; + } + /// /// Create a new dense matrix and initialize each value to the same provided value. /// diff --git a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs index e7972017..51c6fd5c 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs @@ -223,6 +223,54 @@ namespace MathNet.Numerics.LinearAlgebra.Complex return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowVectors(storage)); } + /// + /// Create a new sparse matrix with the diagonal as a copy of the given vector. + /// This new matrix will be independent from the vector. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfDiagonalVector(Vector diagonal) + { + var m = new SparseMatrix(diagonal.Count, diagonal.Count); + m.SetDiagonal(diagonal); + return m; + } + + /// + /// Create a new sparse matrix with the diagonal as a copy of the given vector. + /// This new matrix will be independent from the vector. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfDiagonalVector(int rows, int columns, Vector diagonal) + { + var m = new SparseMatrix(rows, columns); + m.SetDiagonal(diagonal); + return m; + } + + /// + /// Create a new sparse matrix with the diagonal as a copy of the given array. + /// This new matrix will be independent from the array. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfDiagonalArray(Complex[] diagonal) + { + var m = new SparseMatrix(diagonal.Length, diagonal.Length); + m.SetDiagonal(diagonal); + return m; + } + + /// + /// Create a new sparse matrix with the diagonal as a copy of the given array. + /// This new matrix will be independent from the array. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfDiagonalArray(int rows, int columns, Complex[] diagonal) + { + var m = new SparseMatrix(rows, columns); + m.SetDiagonal(diagonal); + return m; + } + /// /// Create a new sparse matrix and initialize each value to the same provided value. /// diff --git a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs index 11001cee..b65906ed 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs @@ -229,6 +229,54 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowVectors(storage)); } + /// + /// Create a new dense matrix with the diagonal as a copy of the given vector. + /// This new matrix will be independent from the vector. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfDiagonalVector(Vector diagonal) + { + var m = new DenseMatrix(diagonal.Count, diagonal.Count); + m.SetDiagonal(diagonal); + return m; + } + + /// + /// Create a new dense matrix with the diagonal as a copy of the given vector. + /// This new matrix will be independent from the vector. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfDiagonalVector(int rows, int columns, Vector diagonal) + { + var m = new DenseMatrix(rows, columns); + m.SetDiagonal(diagonal); + return m; + } + + /// + /// Create a new dense matrix with the diagonal as a copy of the given array. + /// This new matrix will be independent from the array. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfDiagonalArray(Complex32[] diagonal) + { + var m = new DenseMatrix(diagonal.Length, diagonal.Length); + m.SetDiagonal(diagonal); + return m; + } + + /// + /// Create a new dense matrix with the diagonal as a copy of the given array. + /// This new matrix will be independent from the array. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfDiagonalArray(int rows, int columns, Complex32[] diagonal) + { + var m = new DenseMatrix(rows, columns); + m.SetDiagonal(diagonal); + return m; + } + /// /// Create a new dense matrix and initialize each value to the same provided value. /// diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs index 18e50277..e85ca541 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs @@ -218,6 +218,54 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowVectors(storage)); } + /// + /// Create a new sparse matrix with the diagonal as a copy of the given vector. + /// This new matrix will be independent from the vector. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfDiagonalVector(Vector diagonal) + { + var m = new SparseMatrix(diagonal.Count, diagonal.Count); + m.SetDiagonal(diagonal); + return m; + } + + /// + /// Create a new sparse matrix with the diagonal as a copy of the given vector. + /// This new matrix will be independent from the vector. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfDiagonalVector(int rows, int columns, Vector diagonal) + { + var m = new SparseMatrix(rows, columns); + m.SetDiagonal(diagonal); + return m; + } + + /// + /// Create a new sparse matrix with the diagonal as a copy of the given array. + /// This new matrix will be independent from the array. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfDiagonalArray(Complex32[] diagonal) + { + var m = new SparseMatrix(diagonal.Length, diagonal.Length); + m.SetDiagonal(diagonal); + return m; + } + + /// + /// Create a new sparse matrix with the diagonal as a copy of the given array. + /// This new matrix will be independent from the array. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfDiagonalArray(int rows, int columns, Complex32[] diagonal) + { + var m = new SparseMatrix(rows, columns); + m.SetDiagonal(diagonal); + return m; + } + /// /// Create a new sparse matrix and initialize each value to the same provided value. /// diff --git a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs index dc96f37f..3ec14b68 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseMatrix.cs @@ -227,6 +227,54 @@ namespace MathNet.Numerics.LinearAlgebra.Double return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowVectors(storage)); } + /// + /// Create a new dense matrix with the diagonal as a copy of the given vector. + /// This new matrix will be independent from the vector. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfDiagonalVector(Vector diagonal) + { + var m = new DenseMatrix(diagonal.Count, diagonal.Count); + m.SetDiagonal(diagonal); + return m; + } + + /// + /// Create a new dense matrix with the diagonal as a copy of the given vector. + /// This new matrix will be independent from the vector. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfDiagonalVector(int rows, int columns, Vector diagonal) + { + var m = new DenseMatrix(rows, columns); + m.SetDiagonal(diagonal); + return m; + } + + /// + /// Create a new dense matrix with the diagonal as a copy of the given array. + /// This new matrix will be independent from the array. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfDiagonalArray(double[] diagonal) + { + var m = new DenseMatrix(diagonal.Length, diagonal.Length); + m.SetDiagonal(diagonal); + return m; + } + + /// + /// Create a new dense matrix with the diagonal as a copy of the given array. + /// This new matrix will be independent from the array. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfDiagonalArray(int rows, int columns, double[] diagonal) + { + var m = new DenseMatrix(rows, columns); + m.SetDiagonal(diagonal); + return m; + } + /// /// Create a new dense matrix and initialize each value to the same provided value. /// diff --git a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs index ad0fd3c3..7eba1eae 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseMatrix.cs @@ -216,6 +216,54 @@ namespace MathNet.Numerics.LinearAlgebra.Double return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowVectors(storage)); } + /// + /// Create a new sparse matrix with the diagonal as a copy of the given vector. + /// This new matrix will be independent from the vector. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfDiagonalVector(Vector diagonal) + { + var m = new SparseMatrix(diagonal.Count, diagonal.Count); + m.SetDiagonal(diagonal); + return m; + } + + /// + /// Create a new sparse matrix with the diagonal as a copy of the given vector. + /// This new matrix will be independent from the vector. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfDiagonalVector(int rows, int columns, Vector diagonal) + { + var m = new SparseMatrix(rows, columns); + m.SetDiagonal(diagonal); + return m; + } + + /// + /// Create a new sparse matrix with the diagonal as a copy of the given array. + /// This new matrix will be independent from the array. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfDiagonalArray(double[] diagonal) + { + var m = new SparseMatrix(diagonal.Length, diagonal.Length); + m.SetDiagonal(diagonal); + return m; + } + + /// + /// Create a new sparse matrix with the diagonal as a copy of the given array. + /// This new matrix will be independent from the array. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfDiagonalArray(int rows, int columns, double[] diagonal) + { + var m = new SparseMatrix(rows, columns); + m.SetDiagonal(diagonal); + return m; + } + /// /// Create a new sparse matrix and initialize each value to the same provided value. /// diff --git a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs index 18a71bf6..b074d26d 100644 --- a/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/DenseMatrix.cs @@ -227,6 +227,54 @@ namespace MathNet.Numerics.LinearAlgebra.Single return new DenseMatrix(DenseColumnMajorMatrixStorage.OfRowVectors(storage)); } + /// + /// Create a new dense matrix with the diagonal as a copy of the given vector. + /// This new matrix will be independent from the vector. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfDiagonalVector(Vector diagonal) + { + var m = new DenseMatrix(diagonal.Count, diagonal.Count); + m.SetDiagonal(diagonal); + return m; + } + + /// + /// Create a new dense matrix with the diagonal as a copy of the given vector. + /// This new matrix will be independent from the vector. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfDiagonalVector(int rows, int columns, Vector diagonal) + { + var m = new DenseMatrix(rows, columns); + m.SetDiagonal(diagonal); + return m; + } + + /// + /// Create a new dense matrix with the diagonal as a copy of the given array. + /// This new matrix will be independent from the array. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfDiagonalArray(float[] diagonal) + { + var m = new DenseMatrix(diagonal.Length, diagonal.Length); + m.SetDiagonal(diagonal); + return m; + } + + /// + /// Create a new dense matrix with the diagonal as a copy of the given array. + /// This new matrix will be independent from the array. + /// A new memory block will be allocated for storing the matrix. + /// + public static DenseMatrix OfDiagonalArray(int rows, int columns, float[] diagonal) + { + var m = new DenseMatrix(rows, columns); + m.SetDiagonal(diagonal); + return m; + } + /// /// Create a new dense matrix and initialize each value to the same provided value. /// diff --git a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs index 92376672..d1354885 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseMatrix.cs @@ -216,6 +216,54 @@ namespace MathNet.Numerics.LinearAlgebra.Single return new SparseMatrix(SparseCompressedRowMatrixStorage.OfRowVectors(storage)); } + /// + /// Create a new sparse matrix with the diagonal as a copy of the given vector. + /// This new matrix will be independent from the vector. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfDiagonalVector(Vector diagonal) + { + var m = new SparseMatrix(diagonal.Count, diagonal.Count); + m.SetDiagonal(diagonal); + return m; + } + + /// + /// Create a new sparse matrix with the diagonal as a copy of the given vector. + /// This new matrix will be independent from the vector. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfDiagonalVector(int rows, int columns, Vector diagonal) + { + var m = new SparseMatrix(rows, columns); + m.SetDiagonal(diagonal); + return m; + } + + /// + /// Create a new sparse matrix with the diagonal as a copy of the given array. + /// This new matrix will be independent from the array. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfDiagonalArray(float[] diagonal) + { + var m = new SparseMatrix(diagonal.Length, diagonal.Length); + m.SetDiagonal(diagonal); + return m; + } + + /// + /// Create a new sparse matrix with the diagonal as a copy of the given array. + /// This new matrix will be independent from the array. + /// A new memory block will be allocated for storing the matrix. + /// + public static SparseMatrix OfDiagonalArray(int rows, int columns, float[] diagonal) + { + var m = new SparseMatrix(rows, columns); + m.SetDiagonal(diagonal); + return m; + } + /// /// Create a new sparse matrix and initialize each value to the same provided value. ///