Browse Source

LA: split builders to vector/matrix, also allows shorter member names #139

pull/163/head
Christoph Ruegg 13 years ago
parent
commit
f9d8009858
  1. 84
      src/FSharp/LinearAlgebra.Matrix.fs
  2. 36
      src/FSharp/LinearAlgebra.Vector.fs
  3. 596
      src/Numerics/LinearAlgebra/Builder.cs
  4. 2
      src/Numerics/LinearAlgebra/Factorization/LU.cs
  5. 4
      src/Numerics/LinearAlgebra/Matrix.Arithmetic.cs
  6. 14
      src/Numerics/LinearAlgebra/Matrix.Solve.cs
  7. 10
      src/Numerics/LinearAlgebra/Matrix.cs
  8. 2
      src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs
  9. 2
      src/Numerics/LinearAlgebra/Storage/VectorStorage.cs
  10. 4
      src/Numerics/LinearAlgebra/Vector.Arithmetic.cs
  11. 10
      src/Numerics/LinearAlgebra/Vector.cs
  12. 18
      src/Numerics/LinearRegression/MultipleRegression.cs
  13. 12
      src/Numerics/LinearRegression/WeightedRegression.cs

84
src/FSharp/LinearAlgebra.Matrix.fs

@ -351,43 +351,43 @@ module Matrix =
module DenseMatrix = module DenseMatrix =
/// Create a matrix that directly binds to a raw storage array in column-major (column by column) format, without copying. /// 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. /// 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. /// 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. /// 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. /// 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. /// 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. /// 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. /// 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. /// 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. /// Create an identity matrix with the given dimension.
let inline identity (rows: int) (cols: int) = createDiag rows cols Matrix<'T>.Build.One 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. /// 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. /// 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. /// 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. /// 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 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 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. /// 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<seq<'T>>) = Matrix<'T>.Build.DenseMatrixOfRows(rows, cols, seqOfRows) let inline ofRowSeq2 (rows: int) (cols: int) (seqOfRows: #seq<seq<'T>>) = Matrix<'T>.Build.DenseOfRows(rows, cols, seqOfRows)
/// Create a matrix from a list of column vectors. /// 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. /// 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. /// 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 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 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. /// 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<seq<'T>>) = Matrix<'T>.Build.DenseMatrixOfColumns(rows, cols, seqOfCols) let inline ofColumnSeq2 (rows: int) (cols: int) (seqOfCols: #seq<seq<'T>>) = Matrix<'T>.Build.DenseOfColumns(rows, cols, seqOfCols)
/// Create a matrix with a given dimension from an indexed list of row, column, value tuples. /// 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<int * int * 'T>) = Matrix<'T>.Build.DenseMatrixOfIndexed(rows, cols, Seq.ofList indexed) let inline ofListi (rows: int) (cols: int) (indexed: list<int * int * 'T>) = 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. /// 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<int * int * 'T>) = Matrix<'T>.Build.DenseMatrixOfIndexed(rows, cols, indexed) let inline ofSeqi (rows: int) (cols: int) (indexed: #seq<int * int * 'T>) = Matrix<'T>.Build.DenseOfIndexed(rows, cols, indexed)
/// Create a square matrix with the vector elements on the diagonal. /// 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. /// 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. /// 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. /// 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. /// A module which helps constructing generic sparse matrices.
@ -437,37 +437,37 @@ module DenseMatrix =
module SparseMatrix = module SparseMatrix =
/// Create an all-zero matrix with the given dimension. /// 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. /// 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. /// 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. /// 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. /// 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. /// 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. /// 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. /// Create an identity matrix with the given dimension.
let inline identity (rows: int) (cols: int) = createDiag rows cols Matrix<'T>.Build.One 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. /// 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. /// 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. /// 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. /// 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 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 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. /// 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<seq<'T>>) = Matrix<'T>.Build.SparseMatrixOfRows(rows, cols, seqOfRows) let inline ofRowSeq2 (rows: int) (cols: int) (seqOfRows: #seq<seq<'T>>) = Matrix<'T>.Build.SparseOfRows(rows, cols, seqOfRows)
/// Create a matrix from a list of column vectors. /// 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. /// 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. /// 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 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 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. /// 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<seq<'T>>) = Matrix<'T>.Build.SparseMatrixOfColumns(rows, cols, seqOfCols) let inline ofColumnSeq2 (rows: int) (cols: int) (seqOfCols: #seq<seq<'T>>) = Matrix<'T>.Build.SparseOfColumns(rows, cols, seqOfCols)
/// Create a matrix with a given dimension from an indexed list of row, column, value tuples. /// 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<int * int * 'T>) = Matrix<'T>.Build.SparseMatrixOfIndexed(rows, cols, Seq.ofList indexed) let inline ofListi (rows: int) (cols: int) (indexed: list<int * int * 'T>) = 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. /// 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<int * int * 'T>) = Matrix<'T>.Build.SparseMatrixOfIndexed(rows, cols, indexed) let inline ofSeqi (rows: int) (cols: int) (indexed: #seq<int * int * 'T>) = Matrix<'T>.Build.SparseOfIndexed(rows, cols, indexed)
/// Create a square matrix with the vector elements on the diagonal. /// 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. /// 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. /// 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. /// 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 /// Module that contains implementation of useful F#-specific extension members for generic matrices

36
src/FSharp/LinearAlgebra.Vector.fs

@ -222,34 +222,34 @@ module Vector =
module DenseVector = module DenseVector =
/// Create a vector that directly binds to a raw storage array, without copying. /// 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. /// 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. /// 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. /// 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. /// 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). /// 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. /// 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. /// 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. /// Create a vector with a given dimension from an indexed list of index, value pairs.
let inline ofListi (n: int) (fl: list<int * 'T>) = Vector<'T>.Build.DenseVectorOfIndexed(n, Seq.ofList fl) let inline ofListi (n: int) (fl: list<int * 'T>) = Vector<'T>.Build.DenseOfIndexed(n, Seq.ofList fl)
/// Create a vector with a given dimension from an indexed sequences of index, value pairs. /// Create a vector with a given dimension from an indexed sequences of index, value pairs.
let inline ofSeqi (n: int) (fs: #seq<int * 'T>) = Vector<'T>.Build.DenseVectorOfIndexed(n, fs) let inline ofSeqi (n: int) (fs: #seq<int * 'T>) = Vector<'T>.Build.DenseOfIndexed(n, fs)
/// Create a vector with integer entries in the given range. /// 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 |] 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 = module SparseVector =
/// Initialize an all-zero vector with the given dimension. /// 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. /// 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. /// 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. /// 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. /// 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. /// 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. /// Create a sparse vector with a given dimension from an indexed list of index, value pairs.
let inline ofListi (n: int) (fl: list<int * 'T>) = Vector<'T>.Build.SparseVectorOfIndexed(n, Seq.ofList fl) let inline ofListi (n: int) (fl: list<int * 'T>) = Vector<'T>.Build.SparseOfIndexed(n, Seq.ofList fl)
/// Create a sparse vector with a given dimension from an indexed sequence of index, value pairs. /// Create a sparse vector with a given dimension from an indexed sequence of index, value pairs.
let inline ofSeqi (n: int) (fs: #seq<int * 'T>) = Vector<'T>.Build.SparseVectorOfIndexed(n, fs) let inline ofSeqi (n: int) (fs: #seq<int * 'T>) = Vector<'T>.Build.SparseOfIndexed(n, fs)
/// Module that contains implementation of useful F#-specific extension members for generic vectors /// Module that contains implementation of useful F#-specific extension members for generic vectors

596
src/Numerics/LinearAlgebra/Builder.cs

File diff suppressed because it is too large

2
src/Numerics/LinearAlgebra/Factorization/LU.cs

@ -46,7 +46,7 @@ namespace MathNet.Numerics.LinearAlgebra.Factorization
public abstract class LU<T> : ISolver<T> public abstract class LU<T> : ISolver<T>
where T : struct, IEquatable<T>, IFormattable where T : struct, IEquatable<T>, IFormattable
{ {
static readonly T One = BuilderInstance<T>.Instance.One; static readonly T One = BuilderInstance<T>.Matrix.One;
readonly Lazy<Matrix<T>> _lazyL; readonly Lazy<Matrix<T>> _lazyL;
readonly Lazy<Matrix<T>> _lazyU; readonly Lazy<Matrix<T>> _lazyU;

4
src/Numerics/LinearAlgebra/Matrix.Arithmetic.cs

@ -41,12 +41,12 @@ namespace MathNet.Numerics.LinearAlgebra
/// <summary> /// <summary>
/// The value of 1.0. /// The value of 1.0.
/// </summary> /// </summary>
public static readonly T One = BuilderInstance<T>.Instance.One; public static readonly T One = BuilderInstance<T>.Matrix.One;
/// <summary> /// <summary>
/// The value of 0.0. /// The value of 0.0.
/// </summary> /// </summary>
public static readonly T Zero = BuilderInstance<T>.Instance.Zero; public static readonly T Zero = BuilderInstance<T>.Matrix.Zero;
/// <summary> /// <summary>
/// Negate each element of this matrix and place the results into the result matrix. /// Negate each element of this matrix and place the results into the result matrix.

14
src/Numerics/LinearAlgebra/Matrix.Solve.cs

@ -201,7 +201,7 @@ namespace MathNet.Numerics.LinearAlgebra
for (var column = 0; column < input.ColumnCount; column++) for (var column = 0; column < input.ColumnCount; column++)
{ {
var solution = Build.DenseVector(RowCount); var solution = Vector<T>.Build.Dense(RowCount);
solver.Solve(this, input.Column(column), solution, iterator, preconditioner); solver.Solve(this, input.Column(column), solution, iterator, preconditioner);
@ -283,7 +283,7 @@ namespace MathNet.Numerics.LinearAlgebra
/// <returns>The result vector <c>x</c>.</returns> /// <returns>The result vector <c>x</c>.</returns>
public Vector<T> SolveIterative(Vector<T> input, IIterativeSolver<T> solver, Iterator<T> iterator = null, IPreconditioner<T> preconditioner = null) public Vector<T> SolveIterative(Vector<T> input, IIterativeSolver<T> solver, Iterator<T> iterator = null, IPreconditioner<T> preconditioner = null)
{ {
var result = Build.DenseVector(RowCount); var result = Vector<T>.Build.Dense(RowCount);
TrySolveIterative(input, result, solver, iterator, preconditioner); TrySolveIterative(input, result, solver, iterator, preconditioner);
return result; return result;
} }
@ -298,7 +298,7 @@ namespace MathNet.Numerics.LinearAlgebra
/// <returns>The result matrix <c>X</c>.</returns> /// <returns>The result matrix <c>X</c>.</returns>
public Matrix<T> SolveIterative(Matrix<T> input, IIterativeSolver<T> solver, Iterator<T> iterator = null, IPreconditioner<T> preconditioner = null) public Matrix<T> SolveIterative(Matrix<T> input, IIterativeSolver<T> solver, Iterator<T> iterator = null, IPreconditioner<T> preconditioner = null)
{ {
var result = Build.DenseMatrix(input.RowCount, input.ColumnCount); var result = Build.Dense(input.RowCount, input.ColumnCount);
TrySolveIterative(input, result, solver, iterator, preconditioner); TrySolveIterative(input, result, solver, iterator, preconditioner);
return result; return result;
} }
@ -313,7 +313,7 @@ namespace MathNet.Numerics.LinearAlgebra
/// <returns>The result vector <c>x</c>.</returns> /// <returns>The result vector <c>x</c>.</returns>
public Vector<T> SolveIterative(Vector<T> input, IIterativeSolver<T> solver, IPreconditioner<T> preconditioner, params IIterationStopCriterium<T>[] stopCriteria) public Vector<T> SolveIterative(Vector<T> input, IIterativeSolver<T> solver, IPreconditioner<T> preconditioner, params IIterationStopCriterium<T>[] stopCriteria)
{ {
var result = Build.DenseVector(RowCount); var result = Vector<T>.Build.Dense(RowCount);
TrySolveIterative(input, result, solver, preconditioner, stopCriteria); TrySolveIterative(input, result, solver, preconditioner, stopCriteria);
return result; return result;
} }
@ -328,7 +328,7 @@ namespace MathNet.Numerics.LinearAlgebra
/// <returns>The result matrix <c>X</c>.</returns> /// <returns>The result matrix <c>X</c>.</returns>
public Matrix<T> SolveIterative(Matrix<T> input, IIterativeSolver<T> solver, IPreconditioner<T> preconditioner, params IIterationStopCriterium<T>[] stopCriteria) public Matrix<T> SolveIterative(Matrix<T> input, IIterativeSolver<T> solver, IPreconditioner<T> preconditioner, params IIterationStopCriterium<T>[] stopCriteria)
{ {
var result = Build.DenseMatrix(input.RowCount, input.ColumnCount); var result = Build.Dense(input.RowCount, input.ColumnCount);
TrySolveIterative(input, result, solver, preconditioner, stopCriteria); TrySolveIterative(input, result, solver, preconditioner, stopCriteria);
return result; return result;
} }
@ -342,7 +342,7 @@ namespace MathNet.Numerics.LinearAlgebra
/// <returns>The result vector <c>x</c>.</returns> /// <returns>The result vector <c>x</c>.</returns>
public Vector<T> SolveIterative(Vector<T> input, IIterativeSolver<T> solver, params IIterationStopCriterium<T>[] stopCriteria) public Vector<T> SolveIterative(Vector<T> input, IIterativeSolver<T> solver, params IIterationStopCriterium<T>[] stopCriteria)
{ {
var result = Build.DenseVector(RowCount); var result = Vector<T>.Build.Dense(RowCount);
TrySolveIterative(input, result, solver, stopCriteria); TrySolveIterative(input, result, solver, stopCriteria);
return result; return result;
} }
@ -356,7 +356,7 @@ namespace MathNet.Numerics.LinearAlgebra
/// <returns>The result matrix <c>X</c>.</returns> /// <returns>The result matrix <c>X</c>.</returns>
public Matrix<T> SolveIterative(Matrix<T> input, IIterativeSolver<T> solver, params IIterationStopCriterium<T>[] stopCriteria) public Matrix<T> SolveIterative(Matrix<T> input, IIterativeSolver<T> solver, params IIterationStopCriterium<T>[] stopCriteria)
{ {
var result = Build.DenseMatrix(input.RowCount, input.ColumnCount); var result = Build.Dense(input.RowCount, input.ColumnCount);
TrySolveIterative(input, result, solver, stopCriteria); TrySolveIterative(input, result, solver, stopCriteria);
return result; return result;
} }

10
src/Numerics/LinearAlgebra/Matrix.cs

@ -58,7 +58,7 @@ namespace MathNet.Numerics.LinearAlgebra
ColumnCount = storage.ColumnCount; ColumnCount = storage.ColumnCount;
} }
public static readonly Builder<T> Build = BuilderInstance<T>.Instance; public static readonly MatrixBuilder<T> Build = BuilderInstance<T>.Matrix;
/// <summary> /// <summary>
/// Gets the raw matrix data storage. /// Gets the raw matrix data storage.
@ -244,8 +244,8 @@ namespace MathNet.Numerics.LinearAlgebra
public Matrix<T> CreateMatrix(int rows, int columns) public Matrix<T> CreateMatrix(int rows, int columns)
{ {
return Storage.IsDense return Storage.IsDense
? Build.DenseMatrix(rows, columns) ? Build.Dense(rows, columns)
: Build.SparseMatrix(rows, columns); : Build.Sparse(rows, columns);
} }
/// <summary> /// <summary>
@ -256,8 +256,8 @@ namespace MathNet.Numerics.LinearAlgebra
public Vector<T> CreateVector(int size) public Vector<T> CreateVector(int size)
{ {
return Storage.IsDense return Storage.IsDense
? Build.DenseVector(size) ? Vector<T>.Build.Dense(size)
: Build.SparseVector(size); : Vector<T>.Build.Sparse(size);
} }
/// <summary> /// <summary>

2
src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs

@ -40,7 +40,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
{ {
// [ruegg] public fields are OK here // [ruegg] public fields are OK here
protected static readonly T Zero = BuilderInstance<T>.Instance.Zero; protected static readonly T Zero = BuilderInstance<T>.Matrix.Zero;
public readonly int RowCount; public readonly int RowCount;
public readonly int ColumnCount; public readonly int ColumnCount;

2
src/Numerics/LinearAlgebra/Storage/VectorStorage.cs

@ -40,7 +40,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
{ {
// [ruegg] public fields are OK here // [ruegg] public fields are OK here
protected static readonly T Zero = BuilderInstance<T>.Instance.Zero; protected static readonly T Zero = BuilderInstance<T>.Vector.Zero;
public readonly int Length; public readonly int Length;
protected VectorStorage(int length) protected VectorStorage(int length)

4
src/Numerics/LinearAlgebra/Vector.Arithmetic.cs

@ -38,12 +38,12 @@ namespace MathNet.Numerics.LinearAlgebra
/// <summary> /// <summary>
/// The zero value for type T. /// The zero value for type T.
/// </summary> /// </summary>
public static readonly T Zero = BuilderInstance<T>.Instance.Zero; public static readonly T Zero = BuilderInstance<T>.Vector.Zero;
/// <summary> /// <summary>
/// The value of 1.0 for type T. /// The value of 1.0 for type T.
/// </summary> /// </summary>
public static readonly T One = BuilderInstance<T>.Instance.One; public static readonly T One = BuilderInstance<T>.Vector.One;
/// <summary> /// <summary>
/// Negates vector and save result to <paramref name="result"/> /// Negates vector and save result to <paramref name="result"/>

10
src/Numerics/LinearAlgebra/Vector.cs

@ -58,7 +58,7 @@ namespace MathNet.Numerics.LinearAlgebra
Count = storage.Length; Count = storage.Length;
} }
public static readonly Builder<T> Build = BuilderInstance<T>.Instance; public static readonly VectorBuilder<T> Build = BuilderInstance<T>.Vector;
/// <summary> /// <summary>
/// Gets the raw vector data storage. /// Gets the raw vector data storage.
@ -141,8 +141,8 @@ namespace MathNet.Numerics.LinearAlgebra
public Matrix<T> CreateMatrix(int rows, int columns) public Matrix<T> CreateMatrix(int rows, int columns)
{ {
return Storage.IsDense return Storage.IsDense
? Build.DenseMatrix(rows, columns) ? Matrix<T>.Build.Dense(rows, columns)
: Build.SparseMatrix(rows, columns); : Matrix<T>.Build.Sparse(rows, columns);
} }
/// <summary> /// <summary>
@ -153,8 +153,8 @@ namespace MathNet.Numerics.LinearAlgebra
public Vector<T> CreateVector(int size) public Vector<T> CreateVector(int size)
{ {
return Storage.IsDense return Storage.IsDense
? Build.DenseVector(size) ? Build.Dense(size)
: Build.SparseVector(size); : Build.Sparse(size);
} }
/// <summary> /// <summary>

18
src/Numerics/LinearRegression/MultipleRegression.cs

@ -70,12 +70,12 @@ namespace MathNet.Numerics.LinearRegression
/// <returns>Best fitting list of model parameters β for each element in the predictor-arrays.</returns> /// <returns>Best fitting list of model parameters β for each element in the predictor-arrays.</returns>
public static T[] NormalEquations<T>(T[][] x, T[] y, bool intercept = false) where T : struct, IEquatable<T>, IFormattable public static T[] NormalEquations<T>(T[][] x, T[] y, bool intercept = false) where T : struct, IEquatable<T>, IFormattable
{ {
var predictor = Matrix<T>.Build.DenseMatrixOfRowArrays(x); var predictor = Matrix<T>.Build.DenseOfRowArrays(x);
if (intercept) if (intercept)
{ {
predictor = predictor.InsertColumn(0, Vector<T>.Build.DenseVector(predictor.RowCount, Vector<T>.One)); predictor = predictor.InsertColumn(0, Vector<T>.Build.Dense(predictor.RowCount, Vector<T>.One));
} }
var response = Matrix<T>.Build.DenseVector(y); var response = Vector<T>.Build.Dense(y);
return predictor.TransposeThisAndMultiply(predictor).Cholesky().Solve(predictor.Transpose()*response).ToArray(); return predictor.TransposeThisAndMultiply(predictor).Cholesky().Solve(predictor.Transpose()*response).ToArray();
} }
@ -126,12 +126,12 @@ namespace MathNet.Numerics.LinearRegression
/// <returns>Best fitting list of model parameters β for each element in the predictor-arrays.</returns> /// <returns>Best fitting list of model parameters β for each element in the predictor-arrays.</returns>
public static T[] QR<T>(T[][] x, T[] y, bool intercept = false) where T : struct, IEquatable<T>, IFormattable public static T[] QR<T>(T[][] x, T[] y, bool intercept = false) where T : struct, IEquatable<T>, IFormattable
{ {
var predictor = Matrix<T>.Build.DenseMatrixOfRowArrays(x); var predictor = Matrix<T>.Build.DenseOfRowArrays(x);
if (intercept) if (intercept)
{ {
predictor = predictor.InsertColumn(0, Vector<T>.Build.DenseVector(predictor.RowCount, Vector<T>.One)); predictor = predictor.InsertColumn(0, Vector<T>.Build.Dense(predictor.RowCount, Vector<T>.One));
} }
return predictor.QR().Solve(Matrix<T>.Build.DenseVector(y)).ToArray(); return predictor.QR().Solve(Vector<T>.Build.Dense(y)).ToArray();
} }
/// <summary> /// <summary>
@ -181,12 +181,12 @@ namespace MathNet.Numerics.LinearRegression
/// <returns>Best fitting list of model parameters β for each element in the predictor-arrays.</returns> /// <returns>Best fitting list of model parameters β for each element in the predictor-arrays.</returns>
public static T[] Svd<T>(T[][] x, T[] y, bool intercept = false) where T : struct, IEquatable<T>, IFormattable public static T[] Svd<T>(T[][] x, T[] y, bool intercept = false) where T : struct, IEquatable<T>, IFormattable
{ {
var predictor = Matrix<T>.Build.DenseMatrixOfRowArrays(x); var predictor = Matrix<T>.Build.DenseOfRowArrays(x);
if (intercept) if (intercept)
{ {
predictor = predictor.InsertColumn(0, Vector<T>.Build.DenseVector(predictor.RowCount, Vector<T>.One)); predictor = predictor.InsertColumn(0, Vector<T>.Build.Dense(predictor.RowCount, Vector<T>.One));
} }
return predictor.Svd().Solve(Matrix<T>.Build.DenseVector(y)).ToArray(); return predictor.Svd().Solve(Vector<T>.Build.Dense(y)).ToArray();
} }
/// <summary> /// <summary>

12
src/Numerics/LinearRegression/WeightedRegression.cs

@ -59,13 +59,13 @@ namespace MathNet.Numerics.LinearRegression
/// <param name="intercept">True if an intercept should be added as first artificial perdictor value. Default = false.</param> /// <param name="intercept">True if an intercept should be added as first artificial perdictor value. Default = false.</param>
public static T[] Weighted<T>(T[][] x, T[] y, T[] w, bool intercept = false) where T : struct, IEquatable<T>, IFormattable public static T[] Weighted<T>(T[][] x, T[] y, T[] w, bool intercept = false) where T : struct, IEquatable<T>, IFormattable
{ {
var predictor = Matrix<T>.Build.DenseMatrixOfRowArrays(x); var predictor = Matrix<T>.Build.DenseOfRowArrays(x);
if (intercept) if (intercept)
{ {
predictor = predictor.InsertColumn(0, Vector<T>.Build.DenseVector(predictor.RowCount, Vector<T>.One)); predictor = predictor.InsertColumn(0, Vector<T>.Build.Dense(predictor.RowCount, Vector<T>.One));
} }
var response = Matrix<T>.Build.DenseVector(y); var response = Vector<T>.Build.Dense(y);
var weights = Matrix<T>.Build.DiagonalMatrix(new DiagonalMatrixStorage<T>(predictor.RowCount, predictor.RowCount, w)); var weights = Matrix<T>.Build.Diagonal(new DiagonalMatrixStorage<T>(predictor.RowCount, predictor.RowCount, w));
return predictor.TransposeThisAndMultiply(weights*predictor).Cholesky().Solve(predictor.Transpose()*(weights*response)).ToArray(); return predictor.TransposeThisAndMultiply(weights*predictor).Cholesky().Solve(predictor.Transpose()*(weights*response)).ToArray();
} }
@ -85,7 +85,7 @@ namespace MathNet.Numerics.LinearRegression
public static Vector<T> Local<T>(Matrix<T> x, Vector<T> y, Vector<T> t, double radius, Func<double, T> kernel) where T : struct, IEquatable<T>, IFormattable public static Vector<T> Local<T>(Matrix<T> x, Vector<T> y, Vector<T> t, double radius, Func<double, T> kernel) where T : struct, IEquatable<T>, IFormattable
{ {
// TODO: Weird kernel definition // TODO: Weird kernel definition
var w = Matrix<T>.Build.DenseMatrix(x.RowCount, x.RowCount); var w = Matrix<T>.Build.Dense(x.RowCount, x.RowCount);
for (int i = 0; i < x.RowCount; i++) for (int i = 0; i < x.RowCount; i++)
{ {
w.At(i, i, kernel(Distance.Euclidean(t, x.Row(i))/radius)); w.At(i, i, kernel(Distance.Euclidean(t, x.Row(i))/radius));
@ -99,7 +99,7 @@ namespace MathNet.Numerics.LinearRegression
public static Matrix<T> Local<T>(Matrix<T> x, Matrix<T> y, Vector<T> t, double radius, Func<double, T> kernel) where T : struct, IEquatable<T>, IFormattable public static Matrix<T> Local<T>(Matrix<T> x, Matrix<T> y, Vector<T> t, double radius, Func<double, T> kernel) where T : struct, IEquatable<T>, IFormattable
{ {
// TODO: Weird kernel definition // TODO: Weird kernel definition
var w = Matrix<T>.Build.DenseMatrix(x.RowCount, x.RowCount); var w = Matrix<T>.Build.Dense(x.RowCount, x.RowCount);
for (int i = 0; i < x.RowCount; i++) for (int i = 0; i < x.RowCount; i++)
{ {
w.At(i, i, kernel(Distance.Euclidean(t, x.Row(i))/radius)); w.At(i, i, kernel(Distance.Euclidean(t, x.Row(i))/radius));

Loading…
Cancel
Save