diff --git a/src/FSharp/LinearAlgebra.Matrix.fs b/src/FSharp/LinearAlgebra.Matrix.fs index cd778a7b..ffa64af3 100644 --- a/src/FSharp/LinearAlgebra.Matrix.fs +++ b/src/FSharp/LinearAlgebra.Matrix.fs @@ -351,43 +351,43 @@ module Matrix = module DenseMatrix = /// Create a matrix that directly binds to a raw storage array in column-major (column by column) format, without copying. - let inline raw (rows: int) (cols: int) (columnMajor: 'T[]) = Matrix<'T>.Build.DenseMatrix(rows, cols, columnMajor) + let inline raw (rows: int) (cols: int) (columnMajor: 'T[]) = Matrix<'T>.Build.Dense(rows, cols, columnMajor) /// Create an all-zero matrix with the given dimension. - let inline zeroCreate (rows: int) (cols: int) = Matrix<'T>.Build.DenseMatrix(rows, cols) + let inline zeroCreate (rows: int) (cols: int) = Matrix<'T>.Build.Dense(rows, cols) /// Create a random matrix with the given dimension and value distribution. - let inline randomCreate (rows: int) (cols: int) dist = Matrix<'T>.Build.DenseMatrixRandom(rows, cols, dist) + let inline randomCreate (rows: int) (cols: int) dist = Matrix<'T>.Build.DenseRandom(rows, cols, dist) /// Create a matrix with the given dimension and set all values to x. - let inline create (rows: int) (cols: int) (x: 'T) = Matrix<'T>.Build.DenseMatrix(rows, cols, x) + let inline create (rows: int) (cols: int) (x: 'T) = Matrix<'T>.Build.Dense(rows, cols, x) /// Create a matrix with the given dimension and set all diagonal values to x. All other values are zero. - let inline createDiag (rows: int) (cols: int) (x: 'T) = Matrix<'T>.Build.DenseMatrixDiagonal(rows, cols, x) + let inline createDiag (rows: int) (cols: int) (x: 'T) = Matrix<'T>.Build.DenseDiagonal(rows, cols, x) /// Initialize a matrix by calling a construction function for every element. - let inline init (rows: int) (cols: int) (f: int -> int -> 'T) = Matrix<'T>.Build.DenseMatrix(rows, cols, fun i j -> f i j) + let inline init (rows: int) (cols: int) (f: int -> int -> 'T) = Matrix<'T>.Build.Dense(rows, cols, fun i j -> f i j) /// Initialize a matrix by calling a construction function for every row. - let inline initRows (rows: int) (f: int -> Vector<'T>) = Matrix<'T>.Build.DenseMatrixOfRowVectors(Array.init rows f) + let inline initRows (rows: int) (f: int -> Vector<'T>) = Matrix<'T>.Build.DenseOfRowVectors(Array.init rows f) /// Initialize a matrix by calling a construction function for every column. - let inline initColumns (cols: int) (f: int -> Vector<'T>) = Matrix<'T>.Build.DenseMatrixOfColumnVectors(Array.init cols f) + let inline initColumns (cols: int) (f: int -> Vector<'T>) = Matrix<'T>.Build.DenseOfColumnVectors(Array.init cols f) /// Initialize a matrix by calling a construction function for every diagonal element. All other values are zero. - let inline initDiag (rows: int) (cols: int) (f: int -> 'T) = Matrix<'T>.Build.DenseMatrixDiagonal(rows, cols, f) + let inline initDiag (rows: int) (cols: int) (f: int -> 'T) = Matrix<'T>.Build.DenseDiagonal(rows, cols, f) /// Create an identity matrix with the given dimension. let inline identity (rows: int) (cols: int) = createDiag rows cols Matrix<'T>.Build.One /// Create a matrix from a 2D array of floating point numbers. - let inline ofArray2 array = Matrix<'T>.Build.DenseMatrixOfArray(array) + let inline ofArray2 array = Matrix<'T>.Build.DenseOfArray(array) /// Create a matrix from a list of row vectors. - let inline ofRows (rows: Vector<'T> list) = Matrix<'T>.Build.DenseMatrixOfRowVectors(Array.ofList rows) + let inline ofRows (rows: Vector<'T> list) = Matrix<'T>.Build.DenseOfRowVectors(Array.ofList rows) /// Create a matrix from a list of row arrays. - let inline ofRowArrays (rows: 'T[][]) = Matrix<'T>.Build.DenseMatrixOfRowArrays(rows :> seq<'T[]>) // workaround params issue - impl detects it's actually an array + let inline ofRowArrays (rows: 'T[][]) = Matrix<'T>.Build.DenseOfRowArrays(rows :> seq<'T[]>) // workaround params issue - impl detects it's actually an array /// Create a matrix from a list of float lists. Every list in the master list specifies a row. let inline ofRowList (rows: 'T list list) = rows |> List.map List.toArray |> List.toArray |> ofRowArrays @@ -396,13 +396,13 @@ module DenseMatrix = let inline ofRowSeq (rows: #seq<#seq<'T>>) = rows |> Seq.map Seq.toArray |> Seq.toArray |> ofRowArrays /// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a row. - let inline ofRowSeq2 (rows: int) (cols: int) (seqOfRows: #seq>) = Matrix<'T>.Build.DenseMatrixOfRows(rows, cols, seqOfRows) + let inline ofRowSeq2 (rows: int) (cols: int) (seqOfRows: #seq>) = Matrix<'T>.Build.DenseOfRows(rows, cols, seqOfRows) /// Create a matrix from a list of column vectors. - let inline ofColumns (columns: Vector<'T> list) = Matrix<'T>.Build.DenseMatrixOfColumnVectors(Array.ofList columns) + let inline ofColumns (columns: Vector<'T> list) = Matrix<'T>.Build.DenseOfColumnVectors(Array.ofList columns) /// Create a matrix from a list of column arrays. - let inline ofColumnArrays (columns: 'T[][]) = Matrix<'T>.Build.DenseMatrixOfColumnArrays(columns :> seq<'T[]>) // workaround params issue - impl detects it's actually an array + let inline ofColumnArrays (columns: 'T[][]) = Matrix<'T>.Build.DenseOfColumnArrays(columns :> seq<'T[]>) // workaround params issue - impl detects it's actually an array /// Create a matrix from a list of float lists. Every list in the master list specifies a column. let inline ofColumnList (columns: 'T list list) = columns |> List.map List.toArray |> List.toArray |> ofColumnArrays @@ -411,25 +411,25 @@ module DenseMatrix = let inline ofColumnSeq (columns: #seq<#seq<'T>>) = columns |> Seq.map Seq.toArray |> Seq.toArray |> ofColumnArrays /// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a column. - let inline ofColumnSeq2 (rows: int) (cols: int) (seqOfCols: #seq>) = Matrix<'T>.Build.DenseMatrixOfColumns(rows, cols, seqOfCols) + let inline ofColumnSeq2 (rows: int) (cols: int) (seqOfCols: #seq>) = Matrix<'T>.Build.DenseOfColumns(rows, cols, seqOfCols) /// Create a matrix with a given dimension from an indexed list of row, column, value tuples. - let inline ofListi (rows: int) (cols: int) (indexed: list) = Matrix<'T>.Build.DenseMatrixOfIndexed(rows, cols, Seq.ofList indexed) + let inline ofListi (rows: int) (cols: int) (indexed: list) = Matrix<'T>.Build.DenseOfIndexed(rows, cols, Seq.ofList indexed) /// 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) = Matrix<'T>.Build.DenseMatrixOfIndexed(rows, cols, indexed) + let inline ofSeqi (rows: int) (cols: int) (indexed: #seq) = Matrix<'T>.Build.DenseOfIndexed(rows, cols, indexed) /// Create a square matrix with the vector elements on the diagonal. - let inline ofDiag (v: Vector<'T>) = Matrix<'T>.Build.DenseMatrixOfDiagonalVector(v) + let inline ofDiag (v: Vector<'T>) = Matrix<'T>.Build.DenseOfDiagonalVector(v) /// Create a matrix with the vector elements on the diagonal. - let inline ofDiag2 (rows: int) (cols: int) (v: Vector<'T>) = Matrix<'T>.Build.DenseMatrixOfDiagonalVector(rows, cols, v) + let inline ofDiag2 (rows: int) (cols: int) (v: Vector<'T>) = Matrix<'T>.Build.DenseOfDiagonalVector(rows, cols, v) /// Create a square matrix with the array elements on the diagonal. - let inline ofDiagArray (array: 'T array) = Matrix<'T>.Build.DenseMatrixOfDiagonalArray(array) + let inline ofDiagArray (array: 'T array) = Matrix<'T>.Build.DenseOfDiagonalArray(array) /// Create a matrix with the array elements on the diagonal. - let inline ofDiagArray2 (rows: int) (cols: int) (array: 'T array) = Matrix<'T>.Build.DenseMatrixOfDiagonalArray(rows, cols, array) + let inline ofDiagArray2 (rows: int) (cols: int) (array: 'T array) = Matrix<'T>.Build.DenseOfDiagonalArray(rows, cols, array) /// A module which helps constructing generic sparse matrices. @@ -437,37 +437,37 @@ module DenseMatrix = module SparseMatrix = /// Create an all-zero matrix with the given dimension. - let inline zeroCreate (rows: int) (cols: int) = Matrix<'T>.Build.SparseMatrix(rows, cols) + let inline zeroCreate (rows: int) (cols: int) = Matrix<'T>.Build.Sparse(rows, cols) /// 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: 'T) = Matrix<'T>.Build.SparseMatrix(rows, cols, x) + let inline create (rows: int) (cols: int) (x: 'T) = Matrix<'T>.Build.Sparse(rows, cols, x) /// Create a matrix with the given dimension and set all diagonal values to x. All other values are zero. - let inline createDiag (rows: int) (cols: int) (x: 'T) = Matrix<'T>.Build.SparseMatrixDiagonal(rows, cols, x) + let inline createDiag (rows: int) (cols: int) (x: 'T) = Matrix<'T>.Build.SparseDiagonal(rows, cols, x) /// Initialize a matrix by calling a construction function for every element. - let inline init (rows: int) (cols: int) (f: int -> int -> 'T) = Matrix<'T>.Build.SparseMatrix(rows, cols, fun n m -> f n m) + let inline init (rows: int) (cols: int) (f: int -> int -> 'T) = Matrix<'T>.Build.Sparse(rows, cols, fun n m -> f n m) /// Initialize a matrix by calling a construction function for every row. - let inline initRows (rows: int) (f: int -> Vector<'T>) = Matrix<'T>.Build.SparseMatrixOfRowVectors(Array.init rows f) + let inline initRows (rows: int) (f: int -> Vector<'T>) = Matrix<'T>.Build.SparseOfRowVectors(Array.init rows f) /// Initialize a matrix by calling a construction function for every column. - let inline initColumns (cols: int) (f: int -> Vector<'T>) = Matrix<'T>.Build.SparseMatrixOfColumnVectors(Array.init cols f) + let inline initColumns (cols: int) (f: int -> Vector<'T>) = Matrix<'T>.Build.SparseOfColumnVectors(Array.init cols f) /// Initialize a matrix by calling a construction function for every diagonal element. All other values are zero. - let inline initDiag (rows: int) (cols: int) (f: int -> 'T) = Matrix<'T>.Build.SparseMatrixDiagonal(rows, cols, f) + let inline initDiag (rows: int) (cols: int) (f: int -> 'T) = Matrix<'T>.Build.SparseDiagonal(rows, cols, f) /// Create an identity matrix with the given dimension. let inline identity (rows: int) (cols: int) = createDiag rows cols Matrix<'T>.Build.One /// Create a matrix from a 2D array of floating point numbers. - let inline ofArray2 array = Matrix<'T>.Build.SparseMatrixOfArray(array) + let inline ofArray2 array = Matrix<'T>.Build.SparseOfArray(array) /// Create a matrix from a list of row vectors. - let inline ofRows (rows: Vector<'T> list) = Matrix<'T>.Build.SparseMatrixOfRowVectors(Array.ofList rows) + let inline ofRows (rows: Vector<'T> list) = Matrix<'T>.Build.SparseOfRowVectors(Array.ofList rows) /// Create a matrix from a list of row arrays. - let inline ofRowArrays (rows: 'T[][]) = Matrix<'T>.Build.SparseMatrixOfRowArrays(rows :> seq<'T[]>) // workaround params issue - impl detects it's actually an array + let inline ofRowArrays (rows: 'T[][]) = Matrix<'T>.Build.SparseOfRowArrays(rows :> seq<'T[]>) // workaround params issue - impl detects it's actually an array /// Create a matrix from a list of float lists. Every list in the master list specifies a row. let inline ofRowList (rows: 'T list list) = rows |> List.map List.toArray |> List.toArray |> ofRowArrays @@ -476,13 +476,13 @@ module SparseMatrix = let inline ofRowSeq (rows: #seq<#seq<'T>>) = rows |> Seq.map Seq.toArray |> Seq.toArray |> ofRowArrays /// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a row. - let inline ofRowSeq2 (rows: int) (cols: int) (seqOfRows: #seq>) = Matrix<'T>.Build.SparseMatrixOfRows(rows, cols, seqOfRows) + let inline ofRowSeq2 (rows: int) (cols: int) (seqOfRows: #seq>) = Matrix<'T>.Build.SparseOfRows(rows, cols, seqOfRows) /// Create a matrix from a list of column vectors. - let inline ofColumns (columns: Vector<'T> list) = Matrix<'T>.Build.SparseMatrixOfColumnVectors(Array.ofList columns) + let inline ofColumns (columns: Vector<'T> list) = Matrix<'T>.Build.SparseOfColumnVectors(Array.ofList columns) /// Create a matrix from a list of column arrays. - let inline ofColumnArrays (columns: 'T[][]) = Matrix<'T>.Build.SparseMatrixOfColumnArrays(columns :> seq<'T[]>) // workaround params issue - impl detects it's actually an array + let inline ofColumnArrays (columns: 'T[][]) = Matrix<'T>.Build.SparseOfColumnArrays(columns :> seq<'T[]>) // workaround params issue - impl detects it's actually an array /// Create a matrix from a list of float lists. Every list in the master list specifies a column. let inline ofColumnList (columns: 'T list list) = columns |> List.map List.toArray |> List.toArray |> ofColumnArrays @@ -491,25 +491,25 @@ module SparseMatrix = let inline ofColumnSeq (columns: #seq<#seq<'T>>) = columns |> Seq.map Seq.toArray |> Seq.toArray |> ofColumnArrays /// Create a matrix from a list of sequences. Every sequence in the master sequence specifies a column. - let inline ofColumnSeq2 (rows: int) (cols: int) (seqOfCols: #seq>) = Matrix<'T>.Build.SparseMatrixOfColumns(rows, cols, seqOfCols) + let inline ofColumnSeq2 (rows: int) (cols: int) (seqOfCols: #seq>) = Matrix<'T>.Build.SparseOfColumns(rows, cols, seqOfCols) /// Create a matrix with a given dimension from an indexed list of row, column, value tuples. - let inline ofListi (rows: int) (cols: int) (indexed: list) = Matrix<'T>.Build.SparseMatrixOfIndexed(rows, cols, Seq.ofList indexed) + let inline ofListi (rows: int) (cols: int) (indexed: list) = Matrix<'T>.Build.SparseOfIndexed(rows, cols, Seq.ofList indexed) /// 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) = Matrix<'T>.Build.SparseMatrixOfIndexed(rows, cols, indexed) + let inline ofSeqi (rows: int) (cols: int) (indexed: #seq) = Matrix<'T>.Build.SparseOfIndexed(rows, cols, indexed) /// Create a square matrix with the vector elements on the diagonal. - let inline ofDiag (v: Vector<'T>) = Matrix<'T>.Build.SparseMatrixOfDiagonalVector(v) + let inline ofDiag (v: Vector<'T>) = Matrix<'T>.Build.SparseOfDiagonalVector(v) /// Create a matrix with the vector elements on the diagonal. - let inline ofDiag2 (rows: int) (cols: int) (v: Vector<'T>) = Matrix<'T>.Build.SparseMatrixOfDiagonalVector(rows, cols, v) + let inline ofDiag2 (rows: int) (cols: int) (v: Vector<'T>) = Matrix<'T>.Build.SparseOfDiagonalVector(rows, cols, v) /// Create a square matrix with the array elements on the diagonal. - let inline ofDiagArray (array: 'T array) = Matrix<'T>.Build.SparseMatrixOfDiagonalArray(array) + let inline ofDiagArray (array: 'T array) = Matrix<'T>.Build.SparseOfDiagonalArray(array) /// Create a matrix with the array elements on the diagonal. - let inline ofDiagArray2 (rows: int) (cols: int) (array: 'T array) = Matrix<'T>.Build.SparseMatrixOfDiagonalArray(rows, cols, array) + let inline ofDiagArray2 (rows: int) (cols: int) (array: 'T array) = Matrix<'T>.Build.SparseOfDiagonalArray(rows, cols, array) /// Module that contains implementation of useful F#-specific extension members for generic matrices diff --git a/src/FSharp/LinearAlgebra.Vector.fs b/src/FSharp/LinearAlgebra.Vector.fs index 612eec5e..0b06ff77 100644 --- a/src/FSharp/LinearAlgebra.Vector.fs +++ b/src/FSharp/LinearAlgebra.Vector.fs @@ -222,34 +222,34 @@ module Vector = module DenseVector = /// Create a vector that directly binds to a raw storage array, without copying. - let inline raw (raw: 'T[]) = Vector<'T>.Build.DenseVector(raw) + let inline raw (raw: 'T[]) = Vector<'T>.Build.Dense(raw) /// Initialize an all-zero vector with the given dimension. - let inline zeroCreate (n: int) = Vector<'T>.Build.DenseVector(n) + let inline zeroCreate (n: int) = Vector<'T>.Build.Dense(n) /// Initialize a random vector with the given dimension and distribution. - let inline randomCreate (n: int) dist = Vector<'T>.Build.DenseVectorRandom(n, dist) + let inline randomCreate (n: int) dist = Vector<'T>.Build.DenseRandom(n, dist) /// Initialize an x-valued vector with the given dimension. - let inline create (n: int) (x: 'T) = Vector<'T>.Build.DenseVector(n, x) + let inline create (n: int) (x: 'T) = Vector<'T>.Build.Dense(n, x) /// Initialize a vector by calling a construction function for every element. - let inline init (n: int) (f: int -> 'T) = Vector<'T>.Build.DenseVector(n, f) + let inline init (n: int) (f: int -> 'T) = Vector<'T>.Build.Dense(n, f) /// Create a vector from a float array (by copying - use raw instead if no copy is needed). - let inline ofArray (fl: 'T array) = Vector<'T>.Build.DenseVector(Array.copy fl) + let inline ofArray (fl: 'T array) = Vector<'T>.Build.Dense(Array.copy fl) /// Create a vector from a float list. - let inline ofList (fl: 'T list) = Vector<'T>.Build.DenseVector(Array.ofList fl) + let inline ofList (fl: 'T list) = Vector<'T>.Build.Dense(Array.ofList fl) /// Create a vector from a float sequence. - let inline ofSeq (fs: #seq<'T>) = Vector<'T>.Build.DenseVectorOfEnumerable(fs) + let inline ofSeq (fs: #seq<'T>) = Vector<'T>.Build.DenseOfEnumerable(fs) /// Create a vector with a given dimension from an indexed list of index, value pairs. - let inline ofListi (n: int) (fl: list) = Vector<'T>.Build.DenseVectorOfIndexed(n, Seq.ofList fl) + let inline ofListi (n: int) (fl: list) = Vector<'T>.Build.DenseOfIndexed(n, Seq.ofList fl) /// Create a vector with a given dimension from an indexed sequences of index, value pairs. - let inline ofSeqi (n: int) (fs: #seq) = Vector<'T>.Build.DenseVectorOfIndexed(n, fs) + let inline ofSeqi (n: int) (fs: #seq) = Vector<'T>.Build.DenseOfIndexed(n, fs) /// Create a vector with integer entries in the given range. let inline range (start: int) (step: int) (stop: int) = raw [| for i in start..step..stop -> float i |] @@ -263,28 +263,28 @@ module DenseVector = module SparseVector = /// Initialize an all-zero vector with the given dimension. - let inline zeroCreate (n: int) = Vector<'T>.Build.SparseVector(n) + let inline zeroCreate (n: int) = Vector<'T>.Build.Sparse(n) /// Initialize an x-valued vector with the given dimension. - let inline create (n: int) (x: 'T) = Vector<'T>.Build.SparseVector(n, x) + let inline create (n: int) (x: 'T) = Vector<'T>.Build.Sparse(n, x) /// Initialize a vector by calling a construction function for every element. - let inline init (n: int) (f: int -> 'T) = Vector<'T>.Build.SparseVector(n, f) + let inline init (n: int) (f: int -> 'T) = Vector<'T>.Build.Sparse(n, f) /// Create a sparse vector from a float array. - let inline ofArray (fl: 'T array) = Vector<'T>.Build.SparseVectorOfEnumerable(fl :> seq<'T>) + let inline ofArray (fl: 'T array) = Vector<'T>.Build.SparseOfEnumerable(fl :> seq<'T>) /// Create a sparse vector from a float list. - let inline ofList (fl: 'T list) = Vector<'T>.Build.SparseVectorOfEnumerable(Seq.ofList fl) + let inline ofList (fl: 'T list) = Vector<'T>.Build.SparseOfEnumerable(Seq.ofList fl) /// Create a sparse vector from a float sequence. - let inline ofSeq (fs: #seq<'T>) = Vector<'T>.Build.SparseVectorOfEnumerable(fs) + let inline ofSeq (fs: #seq<'T>) = Vector<'T>.Build.SparseOfEnumerable(fs) /// Create a sparse vector with a given dimension from an indexed list of index, value pairs. - let inline ofListi (n: int) (fl: list) = Vector<'T>.Build.SparseVectorOfIndexed(n, Seq.ofList fl) + let inline ofListi (n: int) (fl: list) = Vector<'T>.Build.SparseOfIndexed(n, Seq.ofList fl) /// Create a sparse vector with a given dimension from an indexed sequence of index, value pairs. - let inline ofSeqi (n: int) (fs: #seq) = Vector<'T>.Build.SparseVectorOfIndexed(n, fs) + let inline ofSeqi (n: int) (fs: #seq) = Vector<'T>.Build.SparseOfIndexed(n, fs) /// Module that contains implementation of useful F#-specific extension members for generic vectors diff --git a/src/Numerics/LinearAlgebra/Builder.cs b/src/Numerics/LinearAlgebra/Builder.cs index d46d4558..5d25e07b 100644 --- a/src/Numerics/LinearAlgebra/Builder.cs +++ b/src/Numerics/LinearAlgebra/Builder.cs @@ -35,11 +35,13 @@ using MathNet.Numerics.Distributions; using MathNet.Numerics.LinearAlgebra.Solvers; using MathNet.Numerics.LinearAlgebra.Storage; +// TODO: split up and move to proper folders + namespace MathNet.Numerics.LinearAlgebra.Double { using Solvers; - internal class Builder : Builder + internal class MatrixBuilder : MatrixBuilder { public override double Zero { @@ -51,50 +53,63 @@ namespace MathNet.Numerics.LinearAlgebra.Double get { return 1d; } } - public override Matrix DenseMatrix(DenseColumnMajorMatrixStorage storage) + public override Matrix Dense(DenseColumnMajorMatrixStorage storage) { return new DenseMatrix(storage); } - public override Matrix SparseMatrix(SparseCompressedRowMatrixStorage storage) + public override Matrix Sparse(SparseCompressedRowMatrixStorage storage) { return new SparseMatrix(storage); } - public override Matrix DiagonalMatrix(DiagonalMatrixStorage storage) + public override Matrix Diagonal(DiagonalMatrixStorage storage) { return new DiagonalMatrix(storage); } - public override Vector DenseVector(DenseVectorStorage storage) + public override Matrix DenseRandom(int rows, int columns, IContinuousDistribution distribution) { - return new DenseVector(storage); + return DenseMatrix.CreateRandom(rows, columns, distribution); } - public override Vector SparseVector(SparseVectorStorage storage) + public override IIterationStopCriterium[] IterativeSolverStopCriteria(int maxIterations = 1000) { - return new SparseVector(storage); + return new IIterationStopCriterium[] + { + new FailureStopCriterium(), + new DivergenceStopCriterium(), + new IterationCountStopCriterium(maxIterations), + new ResidualStopCriterium() + }; } + } - public override Matrix DenseMatrixRandom(int rows, int columns, IContinuousDistribution distribution) + internal class VectorBuilder : VectorBuilder + { + public override double Zero { - return Double.DenseMatrix.CreateRandom(rows, columns, distribution); + get { return 0d; } } - public override Vector DenseVectorRandom(int length, IContinuousDistribution distribution) + public override double One { - return new DenseVector(DenseVectorStorage.OfInit(length, i => distribution.Sample())); + get { return 1d; } } - public override IIterationStopCriterium[] IterativeSolverStopCriteria(int maxIterations = 1000) + public override Vector Dense(DenseVectorStorage storage) { - return new IIterationStopCriterium[] - { - new FailureStopCriterium(), - new DivergenceStopCriterium(), - new IterationCountStopCriterium(maxIterations), - new ResidualStopCriterium() - }; + return new DenseVector(storage); + } + + public override Vector Sparse(SparseVectorStorage storage) + { + return new SparseVector(storage); + } + + public override Vector DenseRandom(int length, IContinuousDistribution distribution) + { + return new DenseVector(DenseVectorStorage.OfInit(length, i => distribution.Sample())); } } } @@ -103,7 +118,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single { using Solvers; - internal class Builder : Builder + internal class MatrixBuilder : MatrixBuilder { public override float Zero { @@ -115,50 +130,63 @@ namespace MathNet.Numerics.LinearAlgebra.Single get { return 1f; } } - public override Matrix DenseMatrix(DenseColumnMajorMatrixStorage storage) + public override Matrix Dense(DenseColumnMajorMatrixStorage storage) { return new DenseMatrix(storage); } - public override Matrix SparseMatrix(SparseCompressedRowMatrixStorage storage) + public override Matrix Sparse(SparseCompressedRowMatrixStorage storage) { return new SparseMatrix(storage); } - public override Matrix DiagonalMatrix(DiagonalMatrixStorage storage) + public override Matrix Diagonal(DiagonalMatrixStorage storage) { return new DiagonalMatrix(storage); } - public override Vector DenseVector(DenseVectorStorage storage) + public override Matrix DenseRandom(int rows, int columns, IContinuousDistribution distribution) { - return new DenseVector(storage); + return DenseMatrix.CreateRandom(rows, columns, distribution); } - public override Vector SparseVector(SparseVectorStorage storage) + public override IIterationStopCriterium[] IterativeSolverStopCriteria(int maxIterations = 1000) { - return new SparseVector(storage); + return new IIterationStopCriterium[] + { + new FailureStopCriterium(), + new DivergenceStopCriterium(), + new IterationCountStopCriterium(maxIterations), + new ResidualStopCriterium() + }; } + } - public override Matrix DenseMatrixRandom(int rows, int columns, IContinuousDistribution distribution) + internal class VectorBuilder : VectorBuilder + { + public override float Zero { - return Single.DenseMatrix.CreateRandom(rows, columns, distribution); + get { return 0f; } } - public override Vector DenseVectorRandom(int length, IContinuousDistribution distribution) + public override float One { - return new DenseVector(DenseVectorStorage.OfInit(length, i => (float)distribution.Sample())); + get { return 1f; } } - public override IIterationStopCriterium[] IterativeSolverStopCriteria(int maxIterations = 1000) + public override Vector Dense(DenseVectorStorage storage) { - return new IIterationStopCriterium[] - { - new FailureStopCriterium(), - new DivergenceStopCriterium(), - new IterationCountStopCriterium(maxIterations), - new ResidualStopCriterium() - }; + return new DenseVector(storage); + } + + public override Vector Sparse(SparseVectorStorage storage) + { + return new SparseVector(storage); + } + + public override Vector DenseRandom(int length, IContinuousDistribution distribution) + { + return new DenseVector(DenseVectorStorage.OfInit(length, i => (float)distribution.Sample())); } } } @@ -171,10 +199,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex using Complex = Numerics.Complex; #else using Complex = System.Numerics.Complex; - #endif - internal class Builder : Builder + internal class MatrixBuilder : MatrixBuilder { public override Complex Zero { @@ -186,50 +213,63 @@ namespace MathNet.Numerics.LinearAlgebra.Complex get { return Complex.One; } } - public override Matrix DenseMatrix(DenseColumnMajorMatrixStorage storage) + public override Matrix Dense(DenseColumnMajorMatrixStorage storage) { return new DenseMatrix(storage); } - public override Matrix SparseMatrix(SparseCompressedRowMatrixStorage storage) + public override Matrix Sparse(SparseCompressedRowMatrixStorage storage) { return new SparseMatrix(storage); } - public override Matrix DiagonalMatrix(DiagonalMatrixStorage storage) + public override Matrix Diagonal(DiagonalMatrixStorage storage) { return new DiagonalMatrix(storage); } - public override Vector DenseVector(DenseVectorStorage storage) + public override Matrix DenseRandom(int rows, int columns, IContinuousDistribution distribution) { - return new DenseVector(storage); + return DenseMatrix.CreateRandom(rows, columns, distribution); } - public override Vector SparseVector(SparseVectorStorage storage) + public override IIterationStopCriterium[] IterativeSolverStopCriteria(int maxIterations = 1000) { - return new SparseVector(storage); + return new IIterationStopCriterium[] + { + new FailureStopCriterium(), + new DivergenceStopCriterium(), + new IterationCountStopCriterium(maxIterations), + new ResidualStopCriterium() + }; } + } - public override Matrix DenseMatrixRandom(int rows, int columns, IContinuousDistribution distribution) + internal class VectorBuilder : VectorBuilder + { + public override Complex Zero { - return LinearAlgebra.Complex.DenseMatrix.CreateRandom(rows, columns, distribution); + get { return Complex.Zero; } } - public override Vector DenseVectorRandom(int length, IContinuousDistribution distribution) + public override Complex One { - return new DenseVector(DenseVectorStorage.OfInit(length, i => new Complex(distribution.Sample(), distribution.Sample()))); + get { return Complex.One; } } - public override IIterationStopCriterium[] IterativeSolverStopCriteria(int maxIterations = 1000) + public override Vector Dense(DenseVectorStorage storage) { - return new IIterationStopCriterium[] - { - new FailureStopCriterium(), - new DivergenceStopCriterium(), - new IterationCountStopCriterium(maxIterations), - new ResidualStopCriterium() - }; + return new DenseVector(storage); + } + + public override Vector Sparse(SparseVectorStorage storage) + { + return new SparseVector(storage); + } + + public override Vector DenseRandom(int length, IContinuousDistribution distribution) + { + return new DenseVector(DenseVectorStorage.OfInit(length, i => new Complex(distribution.Sample(), distribution.Sample()))); } } } @@ -238,7 +278,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 { using Solvers; - internal class Builder : Builder + internal class MatrixBuilder : MatrixBuilder { public override Numerics.Complex32 Zero { @@ -250,50 +290,63 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 get { return Numerics.Complex32.One; } } - public override Matrix DenseMatrix(DenseColumnMajorMatrixStorage storage) + public override Matrix Dense(DenseColumnMajorMatrixStorage storage) { return new DenseMatrix(storage); } - public override Matrix SparseMatrix(SparseCompressedRowMatrixStorage storage) + public override Matrix Sparse(SparseCompressedRowMatrixStorage storage) { return new SparseMatrix(storage); } - public override Matrix DiagonalMatrix(DiagonalMatrixStorage storage) + public override Matrix Diagonal(DiagonalMatrixStorage storage) { return new DiagonalMatrix(storage); } - public override Vector DenseVector(DenseVectorStorage storage) + public override Matrix DenseRandom(int rows, int columns, IContinuousDistribution distribution) { - return new DenseVector(storage); + return DenseMatrix.CreateRandom(rows, columns, distribution); } - public override Vector SparseVector(SparseVectorStorage storage) + public override IIterationStopCriterium[] IterativeSolverStopCriteria(int maxIterations = 1000) { - return new SparseVector(storage); + return new IIterationStopCriterium[] + { + new FailureStopCriterium(), + new DivergenceStopCriterium(), + new IterationCountStopCriterium(maxIterations), + new ResidualStopCriterium() + }; } + } - public override Matrix DenseMatrixRandom(int rows, int columns, IContinuousDistribution distribution) + internal class VectorBuilder : VectorBuilder + { + public override Numerics.Complex32 Zero { - return Complex32.DenseMatrix.CreateRandom(rows, columns, distribution); + get { return Numerics.Complex32.Zero; } } - public override Vector DenseVectorRandom(int length, IContinuousDistribution distribution) + public override Numerics.Complex32 One { - return new DenseVector(DenseVectorStorage.OfInit(length, i => new Numerics.Complex32((float)distribution.Sample(), (float)distribution.Sample()))); + get { return Numerics.Complex32.One; } } - public override IIterationStopCriterium[] IterativeSolverStopCriteria(int maxIterations = 1000) + public override Vector Dense(DenseVectorStorage storage) { - return new IIterationStopCriterium[] - { - new FailureStopCriterium(), - new DivergenceStopCriterium(), - new IterationCountStopCriterium(maxIterations), - new ResidualStopCriterium() - }; + return new DenseVector(storage); + } + + public override Vector Sparse(SparseVectorStorage storage) + { + return new SparseVector(storage); + } + + public override Vector DenseRandom(int length, IContinuousDistribution distribution) + { + return new DenseVector(DenseVectorStorage.OfInit(length, i => new Numerics.Complex32((float)distribution.Sample(), (float)distribution.Sample()))); } } } @@ -313,7 +366,7 @@ namespace MathNet.Numerics.LinearAlgebra /// must be created in a generic way. Usage of generic builders should not be /// required in normal user code. /// - public abstract class Builder where T : struct, IEquatable, IFormattable + public abstract class MatrixBuilder where T : struct, IEquatable, IFormattable { /// /// Gets the value of 0.0 for type T. @@ -329,31 +382,16 @@ namespace MathNet.Numerics.LinearAlgebra /// Create a new matrix straight from an initialized matrix storage instance. /// If you have an instance of a discrete storage type instead, use their direct methods instead. /// - public Matrix Matrix(MatrixStorage storage) + public Matrix Storage(MatrixStorage storage) { var dense = storage as DenseColumnMajorMatrixStorage; - if (dense != null) return DenseMatrix(dense); + if (dense != null) return Dense(dense); var sparse = storage as SparseCompressedRowMatrixStorage; - if (sparse != null) return SparseMatrix(sparse); + if (sparse != null) return Sparse(sparse); var diagonal = storage as DiagonalMatrixStorage; - if (diagonal != null) return DiagonalMatrix(diagonal); - - throw new NotSupportedException(); - } - - /// - /// Create a new vector straight from an initialized matrix storage instance. - /// If you have an instance of a discrete storage type instead, use their direct methods instead. - /// - public Vector Vector(VectorStorage storage) - { - var dense = storage as DenseVectorStorage; - if (dense != null) return DenseVector(dense); - - var sparse = storage as SparseVectorStorage; - if (sparse != null) return SparseVector(sparse); + if (diagonal != null) return Diagonal(diagonal); throw new NotSupportedException(); } @@ -364,16 +402,16 @@ namespace MathNet.Numerics.LinearAlgebra /// Intended for advanced scenarios where you're working directly with /// storage for performance or interop reasons. /// - public abstract Matrix DenseMatrix(DenseColumnMajorMatrixStorage storage); + public abstract Matrix Dense(DenseColumnMajorMatrixStorage storage); /// /// Create a new dense 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 DenseMatrix(int rows, int columns) + public Matrix Dense(int rows, int columns) { - return DenseMatrix(new DenseColumnMajorMatrixStorage(rows, columns)); + return Dense(new DenseColumnMajorMatrixStorage(rows, columns)); } /// @@ -382,58 +420,58 @@ namespace MathNet.Numerics.LinearAlgebra /// Very efficient, but changes to the array and the matrix will affect each other. /// /// - public Matrix DenseMatrix(int rows, int columns, T[] storage) + public Matrix Dense(int rows, int columns, T[] storage) { - return DenseMatrix(new DenseColumnMajorMatrixStorage(rows, columns, storage)); + return Dense(new DenseColumnMajorMatrixStorage(rows, columns, storage)); } /// /// Create a new dense matrix and initialize each value to the same provided value. /// - public Matrix DenseMatrix(int rows, int columns, T value) + public Matrix Dense(int rows, int columns, T value) { - if (Zero.Equals(value)) return DenseMatrix(rows, columns); - return DenseMatrix(DenseColumnMajorMatrixStorage.OfInit(rows, columns, (i, j) => value)); + if (Zero.Equals(value)) return Dense(rows, columns); + return Dense(DenseColumnMajorMatrixStorage.OfInit(rows, columns, (i, j) => value)); } /// /// Create a new dense matrix and initialize each value using the provided init function. /// - public Matrix DenseMatrix(int rows, int columns, Func init) + public Matrix Dense(int rows, int columns, Func init) { - return DenseMatrix(DenseColumnMajorMatrixStorage.OfInit(rows, columns, init)); + return Dense(DenseColumnMajorMatrixStorage.OfInit(rows, columns, init)); } /// /// Create a new diagonal dense matrix and initialize each diagonal value to the same provided value. /// - public Matrix DenseMatrixDiagonal(int rows, int columns, T value) + public Matrix DenseDiagonal(int rows, int columns, T value) { - if (Zero.Equals(value)) return DenseMatrix(rows, columns); - return DenseMatrix(DenseColumnMajorMatrixStorage.OfDiagonalInit(rows, columns, i => value)); + if (Zero.Equals(value)) return Dense(rows, columns); + return Dense(DenseColumnMajorMatrixStorage.OfDiagonalInit(rows, columns, i => value)); } /// /// Create a new diagonal dense matrix and initialize each diagonal value using the provided init function. /// - public Matrix DenseMatrixDiagonal(int rows, int columns, Func init) + public Matrix DenseDiagonal(int rows, int columns, Func init) { - return DenseMatrix(DenseColumnMajorMatrixStorage.OfDiagonalInit(rows, columns, init)); + return Dense(DenseColumnMajorMatrixStorage.OfDiagonalInit(rows, columns, init)); } /// /// Create a new dense matrix with values sampled from the provided random distribution. /// - public abstract Matrix DenseMatrixRandom(int rows, int columns, IContinuousDistribution distribution); + public abstract Matrix DenseRandom(int rows, int columns, IContinuousDistribution distribution); /// /// Create a new dense matrix as a copy of the given other matrix. /// This new matrix will be independent from the other matrix. /// A new memory block will be allocated for storing the matrix. /// - public Matrix DenseMatrixOfMatrix(Matrix matrix) + public Matrix DenseOfMatrix(Matrix matrix) { - return DenseMatrix(DenseColumnMajorMatrixStorage.OfMatrix(matrix.Storage)); + return Dense(DenseColumnMajorMatrixStorage.OfMatrix(matrix.Storage)); } /// @@ -441,9 +479,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the provided array. /// A new memory block will be allocated for storing the matrix. /// - public Matrix DenseMatrixOfArray(T[,] array) + public Matrix DenseOfArray(T[,] array) { - return DenseMatrix(DenseColumnMajorMatrixStorage.OfArray(array)); + return Dense(DenseColumnMajorMatrixStorage.OfArray(array)); } /// @@ -452,9 +490,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the enumerable. /// A new memory block will be allocated for storing the matrix. /// - public Matrix DenseMatrixOfIndexed(int rows, int columns, IEnumerable> enumerable) + public Matrix DenseOfIndexed(int rows, int columns, IEnumerable> enumerable) { - return DenseMatrix(DenseColumnMajorMatrixStorage.OfIndexedEnumerable(rows, columns, enumerable)); + return Dense(DenseColumnMajorMatrixStorage.OfIndexedEnumerable(rows, columns, enumerable)); } /// @@ -463,9 +501,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the enumerable. /// A new memory block will be allocated for storing the matrix. /// - public Matrix DenseMatrixOfColumnMajor(int rows, int columns, IEnumerable columnMajor) + public Matrix DenseOfColumnMajor(int rows, int columns, IEnumerable columnMajor) { - return DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnMajorEnumerable(rows, columns, columnMajor)); + return Dense(DenseColumnMajorMatrixStorage.OfColumnMajorEnumerable(rows, columns, columnMajor)); } /// @@ -474,9 +512,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the enumerables. /// A new memory block will be allocated for storing the matrix. /// - public Matrix DenseMatrixOfColumns(IEnumerable> data) + public Matrix DenseOfColumns(IEnumerable> data) { - return DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnArrays(data.Select(v => v.ToArray()).ToArray())); + return Dense(DenseColumnMajorMatrixStorage.OfColumnArrays(data.Select(v => v.ToArray()).ToArray())); } /// @@ -485,9 +523,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the enumerables. /// A new memory block will be allocated for storing the matrix. /// - public Matrix DenseMatrixOfColumns(int rows, int columns, IEnumerable> data) + public Matrix DenseOfColumns(int rows, int columns, IEnumerable> data) { - return DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnEnumerables(rows, columns, data)); + return Dense(DenseColumnMajorMatrixStorage.OfColumnEnumerables(rows, columns, data)); } /// @@ -495,9 +533,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the arrays. /// A new memory block will be allocated for storing the matrix. /// - public Matrix DenseMatrixOfColumnArrays(params T[][] columns) + public Matrix DenseOfColumnArrays(params T[][] columns) { - return DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnArrays(columns)); + return Dense(DenseColumnMajorMatrixStorage.OfColumnArrays(columns)); } /// @@ -505,9 +543,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the arrays. /// A new memory block will be allocated for storing the matrix. /// - public Matrix DenseMatrixOfColumnArrays(IEnumerable columns) + public Matrix DenseOfColumnArrays(IEnumerable columns) { - return DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnArrays((columns as T[][]) ?? columns.ToArray())); + return Dense(DenseColumnMajorMatrixStorage.OfColumnArrays((columns as T[][]) ?? columns.ToArray())); } /// @@ -515,14 +553,14 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the vectors. /// A new memory block will be allocated for storing the matrix. /// - public Matrix DenseMatrixOfColumnVectors(params Vector[] columns) + public Matrix DenseOfColumnVectors(params Vector[] columns) { var storage = new VectorStorage[columns.Length]; for (int i = 0; i < columns.Length; i++) { storage[i] = columns[i].Storage; } - return DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnVectors(storage)); + return Dense(DenseColumnMajorMatrixStorage.OfColumnVectors(storage)); } /// @@ -530,9 +568,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the vectors. /// A new memory block will be allocated for storing the matrix. /// - public Matrix DenseMatrixOfColumnVectors(IEnumerable> columns) + public Matrix DenseOfColumnVectors(IEnumerable> columns) { - return DenseMatrix(DenseColumnMajorMatrixStorage.OfColumnVectors(columns.Select(c => c.Storage).ToArray())); + return Dense(DenseColumnMajorMatrixStorage.OfColumnVectors(columns.Select(c => c.Storage).ToArray())); } /// @@ -541,9 +579,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the enumerables. /// A new memory block will be allocated for storing the matrix. /// - public Matrix DenseMatrixOfRows(IEnumerable> data) + public Matrix DenseOfRows(IEnumerable> data) { - return DenseMatrix(DenseColumnMajorMatrixStorage.OfRowArrays(data.Select(v => v.ToArray()).ToArray())); + return Dense(DenseColumnMajorMatrixStorage.OfRowArrays(data.Select(v => v.ToArray()).ToArray())); } /// @@ -552,9 +590,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the enumerables. /// A new memory block will be allocated for storing the matrix. /// - public Matrix DenseMatrixOfRows(int rows, int columns, IEnumerable> data) + public Matrix DenseOfRows(int rows, int columns, IEnumerable> data) { - return DenseMatrix(DenseColumnMajorMatrixStorage.OfRowEnumerables(rows, columns, data)); + return Dense(DenseColumnMajorMatrixStorage.OfRowEnumerables(rows, columns, data)); } /// @@ -562,9 +600,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the arrays. /// A new memory block will be allocated for storing the matrix. /// - public Matrix DenseMatrixOfRowArrays(params T[][] rows) + public Matrix DenseOfRowArrays(params T[][] rows) { - return DenseMatrix(DenseColumnMajorMatrixStorage.OfRowArrays(rows)); + return Dense(DenseColumnMajorMatrixStorage.OfRowArrays(rows)); } /// @@ -572,9 +610,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the arrays. /// A new memory block will be allocated for storing the matrix. /// - public Matrix DenseMatrixOfRowArrays(IEnumerable rows) + public Matrix DenseOfRowArrays(IEnumerable rows) { - return DenseMatrix(DenseColumnMajorMatrixStorage.OfRowArrays((rows as T[][]) ?? rows.ToArray())); + return Dense(DenseColumnMajorMatrixStorage.OfRowArrays((rows as T[][]) ?? rows.ToArray())); } /// @@ -582,14 +620,14 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the vectors. /// A new memory block will be allocated for storing the matrix. /// - public Matrix DenseMatrixOfRowVectors(params Vector[] rows) + public Matrix DenseOfRowVectors(params Vector[] rows) { var storage = new VectorStorage[rows.Length]; for (int i = 0; i < rows.Length; i++) { storage[i] = rows[i].Storage; } - return DenseMatrix(DenseColumnMajorMatrixStorage.OfRowVectors(storage)); + return Dense(DenseColumnMajorMatrixStorage.OfRowVectors(storage)); } /// @@ -597,9 +635,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the vectors. /// A new memory block will be allocated for storing the matrix. /// - public Matrix DenseMatrixOfRowVectors(IEnumerable> rows) + public Matrix DenseOfRowVectors(IEnumerable> rows) { - return DenseMatrix(DenseColumnMajorMatrixStorage.OfRowVectors(rows.Select(r => r.Storage).ToArray())); + return Dense(DenseColumnMajorMatrixStorage.OfRowVectors(rows.Select(r => r.Storage).ToArray())); } /// @@ -607,9 +645,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the vector. /// A new memory block will be allocated for storing the matrix. /// - public Matrix DenseMatrixOfDiagonalVector(Vector diagonal) + public Matrix DenseOfDiagonalVector(Vector diagonal) { - var m = DenseMatrix(diagonal.Count, diagonal.Count); + var m = Dense(diagonal.Count, diagonal.Count); m.SetDiagonal(diagonal); return m; } @@ -619,9 +657,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the vector. /// A new memory block will be allocated for storing the matrix. /// - public Matrix DenseMatrixOfDiagonalVector(int rows, int columns, Vector diagonal) + public Matrix DenseOfDiagonalVector(int rows, int columns, Vector diagonal) { - var m = DenseMatrix(rows, columns); + var m = Dense(rows, columns); m.SetDiagonal(diagonal); return m; } @@ -631,9 +669,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the array. /// A new memory block will be allocated for storing the matrix. /// - public Matrix DenseMatrixOfDiagonalArray(T[] diagonal) + public Matrix DenseOfDiagonalArray(T[] diagonal) { - var m = DenseMatrix(diagonal.Length, diagonal.Length); + var m = Dense(diagonal.Length, diagonal.Length); m.SetDiagonal(diagonal); return m; } @@ -643,9 +681,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the array. /// A new memory block will be allocated for storing the matrix. /// - public Matrix DenseMatrixOfDiagonalArray(int rows, int columns, T[] diagonal) + public Matrix DenseOfDiagonalArray(int rows, int columns, T[] diagonal) { - var m = DenseMatrix(rows, columns); + var m = Dense(rows, columns); m.SetDiagonal(diagonal); return m; } @@ -656,50 +694,50 @@ namespace MathNet.Numerics.LinearAlgebra /// Intended for advanced scenarios where you're working directly with /// storage for performance or interop reasons. /// - public abstract Matrix SparseMatrix(SparseCompressedRowMatrixStorage storage); + public abstract Matrix Sparse(SparseCompressedRowMatrixStorage storage); /// /// Create a sparse matrix of T with the given number of rows and columns. /// /// The number of rows. /// The number of columns. - public Matrix SparseMatrix(int rows, int columns) + public Matrix Sparse(int rows, int columns) { - return SparseMatrix(new SparseCompressedRowMatrixStorage(rows, columns)); + return Sparse(new SparseCompressedRowMatrixStorage(rows, columns)); } /// /// Create a new sparse matrix and initialize each value to the same provided value. /// - public Matrix SparseMatrix(int rows, int columns, T value) + public Matrix Sparse(int rows, int columns, T value) { - if (Zero.Equals(value)) return SparseMatrix(rows, columns); - return SparseMatrix(SparseCompressedRowMatrixStorage.OfInit(rows, columns, (i, j) => value)); + if (Zero.Equals(value)) return Sparse(rows, columns); + return Sparse(SparseCompressedRowMatrixStorage.OfInit(rows, columns, (i, j) => value)); } /// /// Create a new sparse matrix and initialize each value using the provided init function. /// - public Matrix SparseMatrix(int rows, int columns, Func init) + public Matrix Sparse(int rows, int columns, Func init) { - return SparseMatrix(SparseCompressedRowMatrixStorage.OfInit(rows, columns, init)); + return Sparse(SparseCompressedRowMatrixStorage.OfInit(rows, columns, init)); } /// /// Create a new diagonal sparse matrix and initialize each diagonal value to the same provided value. /// - public Matrix SparseMatrixDiagonal(int rows, int columns, T value) + public Matrix SparseDiagonal(int rows, int columns, T value) { - if (Zero.Equals(value)) return SparseMatrix(rows, columns); - return SparseMatrix(SparseCompressedRowMatrixStorage.OfDiagonalInit(rows, columns, i => value)); + if (Zero.Equals(value)) return Sparse(rows, columns); + return Sparse(SparseCompressedRowMatrixStorage.OfDiagonalInit(rows, columns, i => value)); } /// /// Create a new diagonal sparse matrix and initialize each diagonal value using the provided init function. /// - public Matrix SparseMatrixDiagonal(int rows, int columns, Func init) + public Matrix SparseDiagonal(int rows, int columns, Func init) { - return SparseMatrix(SparseCompressedRowMatrixStorage.OfDiagonalInit(rows, columns, init)); + return Sparse(SparseCompressedRowMatrixStorage.OfDiagonalInit(rows, columns, init)); } /// @@ -707,9 +745,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the other matrix. /// A new memory block will be allocated for storing the matrix. /// - public Matrix SparseMatrixOfMatrix(Matrix matrix) + public Matrix SparseOfMatrix(Matrix matrix) { - return SparseMatrix(SparseCompressedRowMatrixStorage.OfMatrix(matrix.Storage)); + return Sparse(SparseCompressedRowMatrixStorage.OfMatrix(matrix.Storage)); } /// @@ -717,9 +755,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the provided array. /// A new memory block will be allocated for storing the matrix. /// - public Matrix SparseMatrixOfArray(T[,] array) + public Matrix SparseOfArray(T[,] array) { - return SparseMatrix(SparseCompressedRowMatrixStorage.OfArray(array)); + return Sparse(SparseCompressedRowMatrixStorage.OfArray(array)); } /// @@ -728,9 +766,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the enumerable. /// A new memory block will be allocated for storing the matrix. /// - public Matrix SparseMatrixOfIndexed(int rows, int columns, IEnumerable> enumerable) + public Matrix SparseOfIndexed(int rows, int columns, IEnumerable> enumerable) { - return SparseMatrix(SparseCompressedRowMatrixStorage.OfIndexedEnumerable(rows, columns, enumerable)); + return Sparse(SparseCompressedRowMatrixStorage.OfIndexedEnumerable(rows, columns, enumerable)); } /// @@ -740,9 +778,9 @@ namespace MathNet.Numerics.LinearAlgebra /// A new memory block will be allocated for storing the vector. /// /// - public Matrix SparseMatrixOfRowMajor(int rows, int columns, IEnumerable rowMajor) + public Matrix SparseOfRowMajor(int rows, int columns, IEnumerable rowMajor) { - return SparseMatrix(SparseCompressedRowMatrixStorage.OfRowMajorEnumerable(rows, columns, rowMajor)); + return Sparse(SparseCompressedRowMatrixStorage.OfRowMajorEnumerable(rows, columns, rowMajor)); } /// @@ -752,9 +790,9 @@ namespace MathNet.Numerics.LinearAlgebra /// A new memory block will be allocated for storing the matrix. /// /// - public Matrix SparseMatrixOfColumnMajor(int rows, int columns, IList columnMajor) + public Matrix SparseOfColumnMajor(int rows, int columns, IList columnMajor) { - return SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnMajorList(rows, columns, columnMajor)); + return Sparse(SparseCompressedRowMatrixStorage.OfColumnMajorList(rows, columns, columnMajor)); } /// @@ -763,9 +801,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the enumerables. /// A new memory block will be allocated for storing the matrix. /// - public Matrix SparseMatrixOfColumns(IEnumerable> data) + public Matrix SparseOfColumns(IEnumerable> data) { - return SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnArrays(data.Select(v => v.ToArray()).ToArray())); + return Sparse(SparseCompressedRowMatrixStorage.OfColumnArrays(data.Select(v => v.ToArray()).ToArray())); } /// @@ -774,9 +812,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the enumerables. /// A new memory block will be allocated for storing the matrix. /// - public Matrix SparseMatrixOfColumns(int rows, int columns, IEnumerable> data) + public Matrix SparseOfColumns(int rows, int columns, IEnumerable> data) { - return SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnEnumerables(rows, columns, data)); + return Sparse(SparseCompressedRowMatrixStorage.OfColumnEnumerables(rows, columns, data)); } /// @@ -784,9 +822,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the arrays. /// A new memory block will be allocated for storing the matrix. /// - public Matrix SparseMatrixOfColumnArrays(params T[][] columns) + public Matrix SparseOfColumnArrays(params T[][] columns) { - return SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnArrays(columns)); + return Sparse(SparseCompressedRowMatrixStorage.OfColumnArrays(columns)); } /// @@ -794,9 +832,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the arrays. /// A new memory block will be allocated for storing the matrix. /// - public Matrix SparseMatrixOfColumnArrays(IEnumerable columns) + public Matrix SparseOfColumnArrays(IEnumerable columns) { - return SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnArrays((columns as T[][]) ?? columns.ToArray())); + return Sparse(SparseCompressedRowMatrixStorage.OfColumnArrays((columns as T[][]) ?? columns.ToArray())); } /// @@ -804,14 +842,14 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the vectors. /// A new memory block will be allocated for storing the matrix. /// - public Matrix SparseMatrixOfColumnVectors(params Vector[] columns) + public Matrix SparseOfColumnVectors(params Vector[] columns) { var storage = new VectorStorage[columns.Length]; for (int i = 0; i < columns.Length; i++) { storage[i] = columns[i].Storage; } - return SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnVectors(storage)); + return Sparse(SparseCompressedRowMatrixStorage.OfColumnVectors(storage)); } /// @@ -819,9 +857,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the vectors. /// A new memory block will be allocated for storing the matrix. /// - public Matrix SparseMatrixOfColumnVectors(IEnumerable> columns) + public Matrix SparseOfColumnVectors(IEnumerable> columns) { - return SparseMatrix(SparseCompressedRowMatrixStorage.OfColumnVectors(columns.Select(c => c.Storage).ToArray())); + return Sparse(SparseCompressedRowMatrixStorage.OfColumnVectors(columns.Select(c => c.Storage).ToArray())); } /// @@ -830,9 +868,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the enumerables. /// A new memory block will be allocated for storing the matrix. /// - public Matrix SparseMatrixOfRows(IEnumerable> data) + public Matrix SparseOfRows(IEnumerable> data) { - return SparseMatrix(SparseCompressedRowMatrixStorage.OfRowArrays(data.Select(v => v.ToArray()).ToArray())); + return Sparse(SparseCompressedRowMatrixStorage.OfRowArrays(data.Select(v => v.ToArray()).ToArray())); } /// @@ -841,9 +879,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the enumerables. /// A new memory block will be allocated for storing the matrix. /// - public Matrix SparseMatrixOfRows(int rows, int columns, IEnumerable> data) + public Matrix SparseOfRows(int rows, int columns, IEnumerable> data) { - return SparseMatrix(SparseCompressedRowMatrixStorage.OfRowEnumerables(rows, columns, data)); + return Sparse(SparseCompressedRowMatrixStorage.OfRowEnumerables(rows, columns, data)); } /// @@ -851,9 +889,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the arrays. /// A new memory block will be allocated for storing the matrix. /// - public Matrix SparseMatrixOfRowArrays(params T[][] rows) + public Matrix SparseOfRowArrays(params T[][] rows) { - return SparseMatrix(SparseCompressedRowMatrixStorage.OfRowArrays(rows)); + return Sparse(SparseCompressedRowMatrixStorage.OfRowArrays(rows)); } /// @@ -861,9 +899,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the arrays. /// A new memory block will be allocated for storing the matrix. /// - public Matrix SparseMatrixOfRowArrays(IEnumerable rows) + public Matrix SparseOfRowArrays(IEnumerable rows) { - return SparseMatrix(SparseCompressedRowMatrixStorage.OfRowArrays((rows as T[][]) ?? rows.ToArray())); + return Sparse(SparseCompressedRowMatrixStorage.OfRowArrays((rows as T[][]) ?? rows.ToArray())); } /// @@ -871,14 +909,14 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the vectors. /// A new memory block will be allocated for storing the matrix. /// - public Matrix SparseMatrixOfRowVectors(params Vector[] rows) + public Matrix SparseOfRowVectors(params Vector[] rows) { var storage = new VectorStorage[rows.Length]; for (int i = 0; i < rows.Length; i++) { storage[i] = rows[i].Storage; } - return SparseMatrix(SparseCompressedRowMatrixStorage.OfRowVectors(storage)); + return Sparse(SparseCompressedRowMatrixStorage.OfRowVectors(storage)); } /// @@ -886,9 +924,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the vectors. /// A new memory block will be allocated for storing the matrix. /// - public Matrix SparseMatrixOfRowVectors(IEnumerable> rows) + public Matrix SparseOfRowVectors(IEnumerable> rows) { - return SparseMatrix(SparseCompressedRowMatrixStorage.OfRowVectors(rows.Select(r => r.Storage).ToArray())); + return Sparse(SparseCompressedRowMatrixStorage.OfRowVectors(rows.Select(r => r.Storage).ToArray())); } /// @@ -896,9 +934,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the vector. /// A new memory block will be allocated for storing the matrix. /// - public Matrix SparseMatrixOfDiagonalVector(Vector diagonal) + public Matrix SparseOfDiagonalVector(Vector diagonal) { - var m = SparseMatrix(diagonal.Count, diagonal.Count); + var m = Sparse(diagonal.Count, diagonal.Count); m.SetDiagonal(diagonal); return m; } @@ -908,9 +946,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the vector. /// A new memory block will be allocated for storing the matrix. /// - public Matrix SparseMatrixOfDiagonalVector(int rows, int columns, Vector diagonal) + public Matrix SparseOfDiagonalVector(int rows, int columns, Vector diagonal) { - var m = SparseMatrix(rows, columns); + var m = Sparse(rows, columns); m.SetDiagonal(diagonal); return m; } @@ -920,9 +958,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the array. /// A new memory block will be allocated for storing the matrix. /// - public Matrix SparseMatrixOfDiagonalArray(T[] diagonal) + public Matrix SparseOfDiagonalArray(T[] diagonal) { - var m = SparseMatrix(diagonal.Length, diagonal.Length); + var m = Sparse(diagonal.Length, diagonal.Length); m.SetDiagonal(diagonal); return m; } @@ -932,9 +970,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new matrix will be independent from the array. /// A new memory block will be allocated for storing the matrix. /// - public Matrix SparseMatrixOfDiagonalArray(int rows, int columns, T[] diagonal) + public Matrix SparseOfDiagonalArray(int rows, int columns, T[] diagonal) { - var m = SparseMatrix(rows, columns); + var m = Sparse(rows, columns); m.SetDiagonal(diagonal); return m; } @@ -945,7 +983,42 @@ namespace MathNet.Numerics.LinearAlgebra /// Intended for advanced scenarios where you're working directly with /// storage for performance or interop reasons. /// - public abstract Matrix DiagonalMatrix(DiagonalMatrixStorage storage); + public abstract Matrix Diagonal(DiagonalMatrixStorage storage); + + public abstract IIterationStopCriterium[] IterativeSolverStopCriteria(int maxIterations = 1000); + } + + /// + /// Generic linear algebra type builder, for situations where a matrix or vector + /// must be created in a generic way. Usage of generic builders should not be + /// required in normal user code. + /// + public abstract class VectorBuilder where T : struct, IEquatable, IFormattable + { + /// + /// Gets the value of 0.0 for type T. + /// + public abstract T Zero { get; } + + /// + /// Gets the value of 1.0 for type T. + /// + public abstract T One { get; } + + /// + /// Create a new vector straight from an initialized matrix storage instance. + /// If you have an instance of a discrete storage type instead, use their direct methods instead. + /// + public Vector Storage(VectorStorage storage) + { + var dense = storage as DenseVectorStorage; + if (dense != null) return Dense(dense); + + var sparse = storage as SparseVectorStorage; + if (sparse != null) return Sparse(sparse); + + throw new NotSupportedException(); + } /// /// Create a new dense vector straight from an initialized vector storage instance. @@ -953,55 +1026,55 @@ namespace MathNet.Numerics.LinearAlgebra /// Intended for advanced scenarios where you're working directly with /// storage for performance or interop reasons. /// - public abstract Vector DenseVector(DenseVectorStorage storage); + public abstract Vector Dense(DenseVectorStorage storage); /// /// Create a dense vector of T with the given size. /// /// The size of the vector. - public Vector DenseVector(int size) + public Vector Dense(int size) { - return DenseVector(new DenseVectorStorage(size)); + return Dense(new DenseVectorStorage(size)); } /// /// Create a dense vector of T that is directly bound to the specified array. /// - public Vector DenseVector(T[] array) + public Vector Dense(T[] array) { - return DenseVector(new DenseVectorStorage(array.Length, array)); + return Dense(new DenseVectorStorage(array.Length, array)); } /// /// Create a new dense vector and initialize each value using the provided value. /// - public Vector DenseVector(int length, T value) + public Vector Dense(int length, T value) { - if (Zero.Equals(value)) return DenseVector(length); - return DenseVector(DenseVectorStorage.OfInit(length, i => value)); + if (Zero.Equals(value)) return Dense(length); + return Dense(DenseVectorStorage.OfInit(length, i => value)); } /// /// Create a new dense vector and initialize each value using the provided init function. /// - public Vector DenseVector(int length, Func init) + public Vector Dense(int length, Func init) { - return DenseVector(DenseVectorStorage.OfInit(length, init)); + return Dense(DenseVectorStorage.OfInit(length, init)); } /// /// Create a new dense vector with values sampled from the provided random distribution. /// - public abstract Vector DenseVectorRandom(int length, IContinuousDistribution distribution); + public abstract Vector DenseRandom(int length, IContinuousDistribution distribution); /// /// 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 vector. /// - public Vector DenseVectorOfVector(Vector vector) + public Vector DenseOfVector(Vector vector) { - return DenseVector(DenseVectorStorage.OfVector(vector.Storage)); + return Dense(DenseVectorStorage.OfVector(vector.Storage)); } /// @@ -1009,9 +1082,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new vector will be independent from the enumerable. /// A new memory block will be allocated for storing the vector. /// - public Vector DenseVectorOfEnumerable(IEnumerable enumerable) + public Vector DenseOfEnumerable(IEnumerable enumerable) { - return DenseVector(DenseVectorStorage.OfEnumerable(enumerable)); + return Dense(DenseVectorStorage.OfEnumerable(enumerable)); } /// @@ -1020,9 +1093,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new vector will be independent from the enumerable. /// A new memory block will be allocated for storing the vector. /// - public Vector DenseVectorOfIndexed(int length, IEnumerable> enumerable) + public Vector DenseOfIndexed(int length, IEnumerable> enumerable) { - return DenseVector(DenseVectorStorage.OfIndexedEnumerable(length, enumerable)); + return Dense(DenseVectorStorage.OfIndexedEnumerable(length, enumerable)); } /// @@ -1031,32 +1104,32 @@ namespace MathNet.Numerics.LinearAlgebra /// Intended for advanced scenarios where you're working directly with /// storage for performance or interop reasons. /// - public abstract Vector SparseVector(SparseVectorStorage storage); + public abstract Vector Sparse(SparseVectorStorage storage); /// /// Create a sparse vector of T with the given size. /// /// The size of the vector. - public Vector SparseVector(int size) + public Vector Sparse(int size) { - return SparseVector(new SparseVectorStorage(size)); + return Sparse(new SparseVectorStorage(size)); } /// /// Create a new sparse vector and initialize each value using the provided value. /// - public Vector SparseVector(int length, T value) + public Vector Sparse(int length, T value) { - if (Zero.Equals(value)) return SparseVector(length); - return SparseVector(SparseVectorStorage.OfInit(length, i => value)); + if (Zero.Equals(value)) return Sparse(length); + return Sparse(SparseVectorStorage.OfInit(length, i => value)); } /// /// Create a new sparse vector and initialize each value using the provided init function. /// - public Vector SparseVector(int length, Func init) + public Vector Sparse(int length, Func init) { - return SparseVector(SparseVectorStorage.OfInit(length, init)); + return Sparse(SparseVectorStorage.OfInit(length, init)); } /// @@ -1064,9 +1137,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new vector will be independent from the other vector. /// A new memory block will be allocated for storing the vector. /// - public Vector SparseVectorOfVector(Vector vector) + public Vector SparseOfVector(Vector vector) { - return SparseVector(SparseVectorStorage.OfVector(vector.Storage)); + return Sparse(SparseVectorStorage.OfVector(vector.Storage)); } /// @@ -1074,9 +1147,9 @@ namespace MathNet.Numerics.LinearAlgebra /// This new vector will be independent from the enumerable. /// A new memory block will be allocated for storing the vector. /// - public Vector SparseVectorOfEnumerable(IEnumerable enumerable) + public Vector SparseOfEnumerable(IEnumerable enumerable) { - return SparseVector(SparseVectorStorage.OfEnumerable(enumerable)); + return Sparse(SparseVectorStorage.OfEnumerable(enumerable)); } /// @@ -1085,51 +1158,62 @@ namespace MathNet.Numerics.LinearAlgebra /// This new vector will be independent from the enumerable. /// A new memory block will be allocated for storing the vector. /// - public Vector SparseVectorOfIndexed(int length, IEnumerable> enumerable) + public Vector SparseOfIndexed(int length, IEnumerable> enumerable) { - return SparseVector(SparseVectorStorage.OfIndexedEnumerable(length, enumerable)); + return Sparse(SparseVectorStorage.OfIndexedEnumerable(length, enumerable)); } - - public abstract IIterationStopCriterium[] IterativeSolverStopCriteria(int maxIterations = 1000); } internal static class BuilderInstance where T : struct, IEquatable, IFormattable { - static Lazy> _singleton = new Lazy>(Create); + static Lazy, VectorBuilder>> _singleton = new Lazy, VectorBuilder>>(Create); - static Builder Create() + static Tuple, VectorBuilder> Create() { if (typeof (T) == typeof (Complex64)) { - return (Builder) (object) new Complex.Builder(); + return new Tuple, VectorBuilder>( + (MatrixBuilder) (object) new Complex.MatrixBuilder(), + (VectorBuilder) (object) new Complex.VectorBuilder()); } if (typeof (T) == typeof (Numerics.Complex32)) { - return (Builder) (object) new Complex32.Builder(); + return new Tuple, VectorBuilder>( + (MatrixBuilder) (object) new Complex32.MatrixBuilder(), + (VectorBuilder) (object) new Complex32.VectorBuilder()); } if (typeof (T) == typeof (double)) { - return (Builder) (object) new Double.Builder(); + return new Tuple, VectorBuilder>( + (MatrixBuilder) (object) new Double.MatrixBuilder(), + (VectorBuilder) (object) new Double.VectorBuilder()); } if (typeof (T) == typeof (float)) { - return (Builder) (object) new Single.Builder(); + return new Tuple, VectorBuilder>( + (MatrixBuilder) (object) new Single.MatrixBuilder(), + (VectorBuilder) (object) new Single.VectorBuilder()); } throw new NotSupportedException(); } - public static void Register(Builder builder) + public static void Register(MatrixBuilder matrixBuilder, VectorBuilder vectorBuilder) + { + _singleton = new Lazy, VectorBuilder>>(() => new Tuple, VectorBuilder>(matrixBuilder, vectorBuilder)); + } + + public static MatrixBuilder Matrix { - _singleton = new Lazy>(() => builder); + get { return _singleton.Value.Item1; } } - public static Builder Instance + public static VectorBuilder Vector { - get { return _singleton.Value; } + get { return _singleton.Value.Item2; } } } } diff --git a/src/Numerics/LinearAlgebra/Factorization/LU.cs b/src/Numerics/LinearAlgebra/Factorization/LU.cs index d83e6b24..abcf0ff2 100644 --- a/src/Numerics/LinearAlgebra/Factorization/LU.cs +++ b/src/Numerics/LinearAlgebra/Factorization/LU.cs @@ -46,7 +46,7 @@ namespace MathNet.Numerics.LinearAlgebra.Factorization public abstract class LU : ISolver where T : struct, IEquatable, IFormattable { - static readonly T One = BuilderInstance.Instance.One; + static readonly T One = BuilderInstance.Matrix.One; readonly Lazy> _lazyL; readonly Lazy> _lazyU; diff --git a/src/Numerics/LinearAlgebra/Matrix.Arithmetic.cs b/src/Numerics/LinearAlgebra/Matrix.Arithmetic.cs index ed53733d..ffb839cd 100644 --- a/src/Numerics/LinearAlgebra/Matrix.Arithmetic.cs +++ b/src/Numerics/LinearAlgebra/Matrix.Arithmetic.cs @@ -41,12 +41,12 @@ namespace MathNet.Numerics.LinearAlgebra /// /// The value of 1.0. /// - public static readonly T One = BuilderInstance.Instance.One; + public static readonly T One = BuilderInstance.Matrix.One; /// /// The value of 0.0. /// - public static readonly T Zero = BuilderInstance.Instance.Zero; + public static readonly T Zero = BuilderInstance.Matrix.Zero; /// /// Negate each element of this matrix and place the results into the result matrix. diff --git a/src/Numerics/LinearAlgebra/Matrix.Solve.cs b/src/Numerics/LinearAlgebra/Matrix.Solve.cs index f85cb443..5deafc03 100644 --- a/src/Numerics/LinearAlgebra/Matrix.Solve.cs +++ b/src/Numerics/LinearAlgebra/Matrix.Solve.cs @@ -201,7 +201,7 @@ namespace MathNet.Numerics.LinearAlgebra for (var column = 0; column < input.ColumnCount; column++) { - var solution = Build.DenseVector(RowCount); + var solution = Vector.Build.Dense(RowCount); solver.Solve(this, input.Column(column), solution, iterator, preconditioner); @@ -283,7 +283,7 @@ namespace MathNet.Numerics.LinearAlgebra /// The result vector x. public Vector SolveIterative(Vector input, IIterativeSolver solver, Iterator iterator = null, IPreconditioner preconditioner = null) { - var result = Build.DenseVector(RowCount); + var result = Vector.Build.Dense(RowCount); TrySolveIterative(input, result, solver, iterator, preconditioner); return result; } @@ -298,7 +298,7 @@ namespace MathNet.Numerics.LinearAlgebra /// The result matrix X. public Matrix SolveIterative(Matrix input, IIterativeSolver solver, Iterator iterator = null, IPreconditioner preconditioner = null) { - var result = Build.DenseMatrix(input.RowCount, input.ColumnCount); + var result = Build.Dense(input.RowCount, input.ColumnCount); TrySolveIterative(input, result, solver, iterator, preconditioner); return result; } @@ -313,7 +313,7 @@ namespace MathNet.Numerics.LinearAlgebra /// The result vector x. public Vector SolveIterative(Vector input, IIterativeSolver solver, IPreconditioner preconditioner, params IIterationStopCriterium[] stopCriteria) { - var result = Build.DenseVector(RowCount); + var result = Vector.Build.Dense(RowCount); TrySolveIterative(input, result, solver, preconditioner, stopCriteria); return result; } @@ -328,7 +328,7 @@ namespace MathNet.Numerics.LinearAlgebra /// The result matrix X. public Matrix SolveIterative(Matrix input, IIterativeSolver solver, IPreconditioner preconditioner, params IIterationStopCriterium[] stopCriteria) { - var result = Build.DenseMatrix(input.RowCount, input.ColumnCount); + var result = Build.Dense(input.RowCount, input.ColumnCount); TrySolveIterative(input, result, solver, preconditioner, stopCriteria); return result; } @@ -342,7 +342,7 @@ namespace MathNet.Numerics.LinearAlgebra /// The result vector x. public Vector SolveIterative(Vector input, IIterativeSolver solver, params IIterationStopCriterium[] stopCriteria) { - var result = Build.DenseVector(RowCount); + var result = Vector.Build.Dense(RowCount); TrySolveIterative(input, result, solver, stopCriteria); return result; } @@ -356,7 +356,7 @@ namespace MathNet.Numerics.LinearAlgebra /// The result matrix X. public Matrix SolveIterative(Matrix input, IIterativeSolver solver, params IIterationStopCriterium[] stopCriteria) { - var result = Build.DenseMatrix(input.RowCount, input.ColumnCount); + var result = Build.Dense(input.RowCount, input.ColumnCount); TrySolveIterative(input, result, solver, stopCriteria); return result; } diff --git a/src/Numerics/LinearAlgebra/Matrix.cs b/src/Numerics/LinearAlgebra/Matrix.cs index 76058d53..4f604fc6 100644 --- a/src/Numerics/LinearAlgebra/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Matrix.cs @@ -58,7 +58,7 @@ namespace MathNet.Numerics.LinearAlgebra ColumnCount = storage.ColumnCount; } - public static readonly Builder Build = BuilderInstance.Instance; + public static readonly MatrixBuilder Build = BuilderInstance.Matrix; /// /// Gets the raw matrix data storage. @@ -244,8 +244,8 @@ namespace MathNet.Numerics.LinearAlgebra public Matrix CreateMatrix(int rows, int columns) { return Storage.IsDense - ? Build.DenseMatrix(rows, columns) - : Build.SparseMatrix(rows, columns); + ? Build.Dense(rows, columns) + : Build.Sparse(rows, columns); } /// @@ -256,8 +256,8 @@ namespace MathNet.Numerics.LinearAlgebra public Vector CreateVector(int size) { return Storage.IsDense - ? Build.DenseVector(size) - : Build.SparseVector(size); + ? Vector.Build.Dense(size) + : Vector.Build.Sparse(size); } /// diff --git a/src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs index c5997dd4..724d8e8c 100644 --- a/src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs @@ -40,7 +40,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage { // [ruegg] public fields are OK here - protected static readonly T Zero = BuilderInstance.Instance.Zero; + protected static readonly T Zero = BuilderInstance.Matrix.Zero; public readonly int RowCount; public readonly int ColumnCount; diff --git a/src/Numerics/LinearAlgebra/Storage/VectorStorage.cs b/src/Numerics/LinearAlgebra/Storage/VectorStorage.cs index cf73ffbc..d096dc2c 100644 --- a/src/Numerics/LinearAlgebra/Storage/VectorStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/VectorStorage.cs @@ -40,7 +40,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage { // [ruegg] public fields are OK here - protected static readonly T Zero = BuilderInstance.Instance.Zero; + protected static readonly T Zero = BuilderInstance.Vector.Zero; public readonly int Length; protected VectorStorage(int length) diff --git a/src/Numerics/LinearAlgebra/Vector.Arithmetic.cs b/src/Numerics/LinearAlgebra/Vector.Arithmetic.cs index 35de213d..696355c7 100644 --- a/src/Numerics/LinearAlgebra/Vector.Arithmetic.cs +++ b/src/Numerics/LinearAlgebra/Vector.Arithmetic.cs @@ -38,12 +38,12 @@ namespace MathNet.Numerics.LinearAlgebra /// /// The zero value for type T. /// - public static readonly T Zero = BuilderInstance.Instance.Zero; + public static readonly T Zero = BuilderInstance.Vector.Zero; /// /// The value of 1.0 for type T. /// - public static readonly T One = BuilderInstance.Instance.One; + public static readonly T One = BuilderInstance.Vector.One; /// /// Negates vector and save result to diff --git a/src/Numerics/LinearAlgebra/Vector.cs b/src/Numerics/LinearAlgebra/Vector.cs index f35fe498..f4b5b8de 100644 --- a/src/Numerics/LinearAlgebra/Vector.cs +++ b/src/Numerics/LinearAlgebra/Vector.cs @@ -58,7 +58,7 @@ namespace MathNet.Numerics.LinearAlgebra Count = storage.Length; } - public static readonly Builder Build = BuilderInstance.Instance; + public static readonly VectorBuilder Build = BuilderInstance.Vector; /// /// Gets the raw vector data storage. @@ -141,8 +141,8 @@ namespace MathNet.Numerics.LinearAlgebra public Matrix CreateMatrix(int rows, int columns) { return Storage.IsDense - ? Build.DenseMatrix(rows, columns) - : Build.SparseMatrix(rows, columns); + ? Matrix.Build.Dense(rows, columns) + : Matrix.Build.Sparse(rows, columns); } /// @@ -153,8 +153,8 @@ namespace MathNet.Numerics.LinearAlgebra public Vector CreateVector(int size) { return Storage.IsDense - ? Build.DenseVector(size) - : Build.SparseVector(size); + ? Build.Dense(size) + : Build.Sparse(size); } /// diff --git a/src/Numerics/LinearRegression/MultipleRegression.cs b/src/Numerics/LinearRegression/MultipleRegression.cs index e84ed2aa..c215f28d 100644 --- a/src/Numerics/LinearRegression/MultipleRegression.cs +++ b/src/Numerics/LinearRegression/MultipleRegression.cs @@ -70,12 +70,12 @@ namespace MathNet.Numerics.LinearRegression /// Best fitting list of model parameters β for each element in the predictor-arrays. public static T[] NormalEquations(T[][] x, T[] y, bool intercept = false) where T : struct, IEquatable, IFormattable { - var predictor = Matrix.Build.DenseMatrixOfRowArrays(x); + var predictor = Matrix.Build.DenseOfRowArrays(x); if (intercept) { - predictor = predictor.InsertColumn(0, Vector.Build.DenseVector(predictor.RowCount, Vector.One)); + predictor = predictor.InsertColumn(0, Vector.Build.Dense(predictor.RowCount, Vector.One)); } - var response = Matrix.Build.DenseVector(y); + var response = Vector.Build.Dense(y); return predictor.TransposeThisAndMultiply(predictor).Cholesky().Solve(predictor.Transpose()*response).ToArray(); } @@ -126,12 +126,12 @@ namespace MathNet.Numerics.LinearRegression /// Best fitting list of model parameters β for each element in the predictor-arrays. public static T[] QR(T[][] x, T[] y, bool intercept = false) where T : struct, IEquatable, IFormattable { - var predictor = Matrix.Build.DenseMatrixOfRowArrays(x); + var predictor = Matrix.Build.DenseOfRowArrays(x); if (intercept) { - predictor = predictor.InsertColumn(0, Vector.Build.DenseVector(predictor.RowCount, Vector.One)); + predictor = predictor.InsertColumn(0, Vector.Build.Dense(predictor.RowCount, Vector.One)); } - return predictor.QR().Solve(Matrix.Build.DenseVector(y)).ToArray(); + return predictor.QR().Solve(Vector.Build.Dense(y)).ToArray(); } /// @@ -181,12 +181,12 @@ namespace MathNet.Numerics.LinearRegression /// Best fitting list of model parameters β for each element in the predictor-arrays. public static T[] Svd(T[][] x, T[] y, bool intercept = false) where T : struct, IEquatable, IFormattable { - var predictor = Matrix.Build.DenseMatrixOfRowArrays(x); + var predictor = Matrix.Build.DenseOfRowArrays(x); if (intercept) { - predictor = predictor.InsertColumn(0, Vector.Build.DenseVector(predictor.RowCount, Vector.One)); + predictor = predictor.InsertColumn(0, Vector.Build.Dense(predictor.RowCount, Vector.One)); } - return predictor.Svd().Solve(Matrix.Build.DenseVector(y)).ToArray(); + return predictor.Svd().Solve(Vector.Build.Dense(y)).ToArray(); } /// diff --git a/src/Numerics/LinearRegression/WeightedRegression.cs b/src/Numerics/LinearRegression/WeightedRegression.cs index 071e2033..15409756 100644 --- a/src/Numerics/LinearRegression/WeightedRegression.cs +++ b/src/Numerics/LinearRegression/WeightedRegression.cs @@ -59,13 +59,13 @@ namespace MathNet.Numerics.LinearRegression /// True if an intercept should be added as first artificial perdictor value. Default = false. public static T[] Weighted(T[][] x, T[] y, T[] w, bool intercept = false) where T : struct, IEquatable, IFormattable { - var predictor = Matrix.Build.DenseMatrixOfRowArrays(x); + var predictor = Matrix.Build.DenseOfRowArrays(x); if (intercept) { - predictor = predictor.InsertColumn(0, Vector.Build.DenseVector(predictor.RowCount, Vector.One)); + predictor = predictor.InsertColumn(0, Vector.Build.Dense(predictor.RowCount, Vector.One)); } - var response = Matrix.Build.DenseVector(y); - var weights = Matrix.Build.DiagonalMatrix(new DiagonalMatrixStorage(predictor.RowCount, predictor.RowCount, w)); + var response = Vector.Build.Dense(y); + var weights = Matrix.Build.Diagonal(new DiagonalMatrixStorage(predictor.RowCount, predictor.RowCount, w)); return predictor.TransposeThisAndMultiply(weights*predictor).Cholesky().Solve(predictor.Transpose()*(weights*response)).ToArray(); } @@ -85,7 +85,7 @@ namespace MathNet.Numerics.LinearRegression public static Vector Local(Matrix x, Vector y, Vector t, double radius, Func kernel) where T : struct, IEquatable, IFormattable { // TODO: Weird kernel definition - var w = Matrix.Build.DenseMatrix(x.RowCount, x.RowCount); + var w = Matrix.Build.Dense(x.RowCount, x.RowCount); for (int i = 0; i < x.RowCount; i++) { w.At(i, i, kernel(Distance.Euclidean(t, x.Row(i))/radius)); @@ -99,7 +99,7 @@ namespace MathNet.Numerics.LinearRegression public static Matrix Local(Matrix x, Matrix y, Vector t, double radius, Func kernel) where T : struct, IEquatable, IFormattable { // TODO: Weird kernel definition - var w = Matrix.Build.DenseMatrix(x.RowCount, x.RowCount); + var w = Matrix.Build.Dense(x.RowCount, x.RowCount); for (int i = 0; i < x.RowCount; i++) { w.At(i, i, kernel(Distance.Euclidean(t, x.Row(i))/radius));