Browse Source

LA: F# matrix/vector zero random and identity functions with explicit generic type parameter

pull/222/head
Christoph Ruegg 12 years ago
parent
commit
ba3ba30a96
  1. 27
      src/FSharp/LinearAlgebra.Matrix.fs
  2. 18
      src/FSharp/LinearAlgebra.Vector.fs

27
src/FSharp/LinearAlgebra.Matrix.fs

@ -343,16 +343,20 @@ module DenseMatrix =
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 zero (rows: int) (cols: int) = Matrix<'T>.Build.Dense(rows, cols)
let inline zero<'T when 'T:struct and 'T :> ValueType and 'T: (new: unit ->'T) and 'T :> IEquatable<'T> and 'T :> IFormattable>
(rows: int) (cols: int) = Matrix<'T>.Build.Dense(rows, cols)
/// Create a random matrix with the given dimension and value distribution.
let inline random (rows: int) (cols: int) (dist: IContinuousDistribution) = Matrix<'T>.Build.Random(rows, cols, dist)
let inline random<'T when 'T:struct and 'T :> ValueType and 'T: (new: unit ->'T) and 'T :> IEquatable<'T> and 'T :> IFormattable>
(rows: int) (cols: int) (dist: IContinuousDistribution) = Matrix<'T>.Build.Random(rows, cols, dist)
/// Create a random matrix with the given dimension and standard distributed values.
let inline randomStandard (rows: int) (cols: int) = Matrix<'T>.Build.Random(rows, cols)
let inline randomStandard<'T when 'T:struct and 'T :> ValueType and 'T: (new: unit ->'T) and 'T :> IEquatable<'T> and 'T :> IFormattable>
(rows: int) (cols: int) = Matrix<'T>.Build.Random(rows, cols)
/// Create a random matrix with the given dimension and standard distributed values using the provided seed.
let inline randomSeed (rows: int) (cols: int) (seed: int) = Matrix<'T>.Build.Random(rows, cols, seed)
let inline randomSeed<'T when 'T:struct and 'T :> ValueType and 'T: (new: unit ->'T) and 'T :> IEquatable<'T> and 'T :> IFormattable>
(rows: int) (cols: int) (seed: int) = Matrix<'T>.Build.Random(rows, cols, seed)
/// 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.Dense(rows, cols, x)
@ -364,10 +368,12 @@ module DenseMatrix =
let inline diag2 (rows: int) (cols: int) (x: 'T) = Matrix<'T>.Build.DenseDiagonal(rows, cols, x)
/// Create an identity matrix with the given dimension.
let inline identity (order: int) = Matrix<'T>.Build.DenseIdentity(order)
let inline identity<'T when 'T:struct and 'T :> ValueType and 'T: (new: unit ->'T) and 'T :> IEquatable<'T> and 'T :> IFormattable>
(order: int) = Matrix<'T>.Build.DenseIdentity(order)
/// Create an identity matrix with the given dimension.
let inline identity2 (rows: int) (cols: int) = Matrix<'T>.Build.DenseIdentity(rows, cols)
let inline identity2<'T when 'T:struct and 'T :> ValueType and 'T: (new: unit ->'T) and 'T :> IEquatable<'T> and 'T :> IFormattable>
(rows: int) (cols: int) = Matrix<'T>.Build.DenseIdentity(rows, cols)
/// 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.Dense(rows, cols, fun i j -> f i j)
@ -444,7 +450,8 @@ module SparseMatrix =
let inline ofStorage storage = Matrix<'T>.Build.Sparse(storage)
/// Create an all-zero matrix with the given dimension.
let inline zero (rows: int) (cols: int) = Matrix<'T>.Build.Sparse(rows, cols)
let inline zero<'T when 'T:struct and 'T :> ValueType and 'T: (new: unit ->'T) and 'T :> IEquatable<'T> and 'T :> IFormattable>
(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.Sparse(rows, cols, x)
@ -456,10 +463,12 @@ module SparseMatrix =
let inline diag2 (rows: int) (cols: int) (x: 'T) = Matrix<'T>.Build.SparseDiagonal(rows, cols, x)
/// Create an identity matrix with the given dimension.
let inline identity (order: int) = Matrix<'T>.Build.SparseIdentity(order)
let inline identity<'T when 'T:struct and 'T :> ValueType and 'T: (new: unit ->'T) and 'T :> IEquatable<'T> and 'T :> IFormattable>
(order: int) = Matrix<'T>.Build.SparseIdentity(order)
/// Create an identity matrix with the given dimension.
let inline identity2 (rows: int) (cols: int) = Matrix<'T>.Build.SparseIdentity(rows, cols)
let inline identity2<'T when 'T:struct and 'T :> ValueType and 'T: (new: unit ->'T) and 'T :> IEquatable<'T> and 'T :> IFormattable>
(rows: int) (cols: int) = Matrix<'T>.Build.SparseIdentity(rows, cols)
/// 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.Sparse(rows, cols, fun n m -> f n m)

18
src/FSharp/LinearAlgebra.Vector.fs

@ -30,6 +30,9 @@
namespace MathNet.Numerics.LinearAlgebra
open System
open MathNet.Numerics.LinearAlgebra
/// A module which implements functional vector operations.
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
@ -214,16 +217,20 @@ module DenseVector =
let inline raw (raw: 'T[]) = Vector<'T>.Build.Dense(raw)
/// Initialize an all-zero vector with the given dimension.
let inline zero (n: int) = Vector<'T>.Build.Dense(n)
let inline zero<'T when 'T:struct and 'T :> ValueType and 'T: (new: unit ->'T) and 'T :> IEquatable<'T> and 'T :> IFormattable>
(n: int) = Vector<'T>.Build.Dense(n)
/// Initialize a random vector with the given dimension and distribution.
let inline random (n: int) (dist: IContinuousDistribution) = Vector<'T>.Build.Random(n, dist)
let inline random<'T when 'T:struct and 'T :> ValueType and 'T: (new: unit ->'T) and 'T :> IEquatable<'T> and 'T :> IFormattable>
(n: int) (dist: IContinuousDistribution) = Vector<'T>.Build.Random(n, dist)
/// Initialize a random vector with the given dimension and standard distributed values.
let inline randomStandard (n: int) = Vector<'T>.Build.Random(n)
let inline randomStandard<'T when 'T:struct and 'T :> ValueType and 'T: (new: unit ->'T) and 'T :> IEquatable<'T> and 'T :> IFormattable>
(n: int) = Vector<'T>.Build.Random(n)
/// Initialize a random vector with the given dimension and standard distributed values using the provided seed.
let inline randomSeed (n: int) (seed: int) = Vector<'T>.Build.Random(n, seed)
let inline randomSeed<'T when 'T:struct and 'T :> ValueType and 'T: (new: unit ->'T) and 'T :> IEquatable<'T> and 'T :> IFormattable>
(n: int) (seed: int) = Vector<'T>.Build.Random(n, seed)
/// Initialize an x-valued vector with the given dimension.
let inline create (n: int) (x: 'T) = Vector<'T>.Build.Dense(n, x)
@ -261,7 +268,8 @@ module SparseVector =
let inline ofStorage (storage: Storage.SparseVectorStorage<'T>) = Vector<'T>.Build.Sparse(storage)
/// Initialize an all-zero vector with the given dimension.
let inline zero (n: int) = Vector<'T>.Build.Sparse(n)
let inline zero<'T when 'T:struct and 'T :> ValueType and 'T: (new: unit ->'T) and 'T :> IEquatable<'T> and 'T :> IFormattable>
(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.Sparse(n, x)

Loading…
Cancel
Save