From 3259ce6ba28ac24c229066e13468402906ffc0a5 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sun, 1 Dec 2013 15:53:48 +0100 Subject: [PATCH] LA: extend diagonal matrix builder --- src/Numerics/LinearAlgebra/Builder.cs | 54 +++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/Numerics/LinearAlgebra/Builder.cs b/src/Numerics/LinearAlgebra/Builder.cs index 0a8907e0..e668e7e3 100644 --- a/src/Numerics/LinearAlgebra/Builder.cs +++ b/src/Numerics/LinearAlgebra/Builder.cs @@ -1101,6 +1101,60 @@ namespace MathNet.Numerics.LinearAlgebra /// public abstract Matrix Diagonal(DiagonalMatrixStorage storage); + /// + /// Create a new diagonal matrix with the given number of rows and columns. + /// All cells of the matrix will be initialized to zero. + /// Zero-length matrices are not supported. + /// + public Matrix Diagonal(int rows, int columns) + { + return Diagonal(new DiagonalMatrixStorage(rows, columns)); + } + + /// + /// Create a new diagonal matrix with the given number of rows and columns directly binding to a raw array. + /// The array is assumed to represent the diagonal values and is used directly without copying. + /// Very efficient, but changes to the array and the matrix will affect each other. + /// + /// + public Matrix Diagonal(int rows, int columns, T[] storage) + { + return Diagonal(new DiagonalMatrixStorage(rows, columns, storage)); + } + + /// + /// Create a new diagonal matrix and initialize each diagonal value to the same provided value. + /// + public Matrix Diagonal(int rows, int columns, T value) + { + if (Zero.Equals(value)) return Diagonal(rows, columns); + return Diagonal(DiagonalMatrixStorage.OfInit(rows, columns, i => value)); + } + + /// + /// Create a new diagonal matrix and initialize each diagonal value using the provided init function. + /// + public Matrix Diagonal(int rows, int columns, Func init) + { + return Diagonal(DiagonalMatrixStorage.OfInit(rows, columns, init)); + } + + /// + /// Create a new diagonal identity matrix with a one-diagonal. + /// + public Matrix DiagonalIdentity(int rows, int columns) + { + return Diagonal(DiagonalMatrixStorage.OfInit(rows, columns, i => One)); + } + + /// + /// Create a new diagonal identity matrix with a one-diagonal. + /// + public Matrix DiagonalIdentity(int order) + { + return Diagonal(DiagonalMatrixStorage.OfInit(order, order, i => One)); + } + public abstract IIterationStopCriterium[] IterativeSolverStopCriteria(int maxIterations = 1000); }