diff --git a/src/FSharp/LinearAlgebra.Matrix.fs b/src/FSharp/LinearAlgebra.Matrix.fs index 672263d5..2ed48a50 100644 --- a/src/FSharp/LinearAlgebra.Matrix.fs +++ b/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) diff --git a/src/FSharp/LinearAlgebra.Vector.fs b/src/FSharp/LinearAlgebra.Vector.fs index df2187c1..b184cd2e 100644 --- a/src/FSharp/LinearAlgebra.Vector.fs +++ b/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. [] @@ -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)