From 38fea05dcfa9cf772dabb555f87ed02aa43b23ff Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Mon, 14 Apr 2014 04:05:07 +0200 Subject: [PATCH] LA: make non-arithmetic instance members available as functions in F# --- src/FSharp/LinearAlgebra.Matrix.fs | 41 ++++++++++++++++++++++-------- src/FSharp/LinearAlgebra.Vector.fs | 18 ++++++++++++- 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/src/FSharp/LinearAlgebra.Matrix.fs b/src/FSharp/LinearAlgebra.Matrix.fs index 2ed48a50..d1a84d21 100644 --- a/src/FSharp/LinearAlgebra.Matrix.fs +++ b/src/FSharp/LinearAlgebra.Matrix.fs @@ -4,7 +4,7 @@ // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com // -// Copyright (c) 2009-2013 Math.NET +// Copyright (c) 2009-2014 Math.NET // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation @@ -169,13 +169,13 @@ module Matrix = /// Checks whether there is a column in the matrix that satisfies a predicate. let inline existsCol p (m: #Matrix<_>) = m.EnumerateColumns() |> Seq.exists p - + /// Checks whether there is a column in the matrix that satisfies a position dependent predicate. let inline existsiCol p (m: #Matrix<_>) = m.EnumerateColumnsIndexed() |> Seq.exists (fun (j,x) -> p j x) - + /// Checks whether there is a row in the matrix that satisfies a predicate. let inline existsRow p (m: #Matrix<_>) = m.EnumerateRows() |> Seq.exists p - + /// Checks whether there is a row in the matrix that satisfies a position dependent predicate. let inline existsiRow p (m: #Matrix<_>) = m.EnumerateRowsIndexed() |> Seq.exists (fun (i,x) -> p i x) @@ -194,13 +194,13 @@ module Matrix = /// Checks whether all columns in the matrix that satisfy a predicate. let inline forallCols p (m: #Matrix<_>) = m.EnumerateColumns() |> Seq.forall p - + /// Checks whether all columns in the matrix that satisfy a position dependent predicate. let inline foralliCols p (m: #Matrix<_>) = m.EnumerateColumnsIndexed() |> Seq.forall (fun (j,x) -> p j x) - + /// Checks whether all rows in the matrix that satisfy a predicate. let inline forallRows p (m: #Matrix<_>) = m.EnumerateRows() |> Seq.forall p - + /// Checks whether all rows in the matrix that satisfy a position dependent predicate. let inline foralliRows p (m: #Matrix<_>) = m.EnumerateRowsIndexed() |> Seq.forall (fun (i,x) -> p i x) @@ -316,7 +316,6 @@ module Matrix = let inline subInPlace (v: #Matrix<_>) (w: #Matrix<_>) = v.Subtract(w, v) - /// Returns the sum of all elements of a matrix. let inline sum (A: #Matrix<'a>) = A |> foldSkipZeros (+) Matrix<'a>.Zero @@ -329,6 +328,26 @@ module Matrix = A.EnumerateRowsIndexed() |> Seq.map (fun (i,row) -> f i row) |> Seq.reduce (+) + let inline rowCount (A: #Matrix<_>) = A.RowCount + let inline columnCount (A: #Matrix<_>) = A.ColumnCount + + let inline transpose (A: #Matrix<_>) = A.Transpose() + let inline conjugate (A: #Matrix<_>) = A.Conjugate() + let inline conjugateTranspose (A: #Matrix<_>) = A.ConjugateTranspose() + let inline inverse (A: #Matrix<_>) = A.Inverse() + + let inline norm (A: #Matrix<_>) = A.L2Norm() + let inline frobenius (A: #Matrix<_>) = A.FrobeniusNorm() + let inline rank (A: #Matrix<_>) = A.Rank() + let inline trace (A: #Matrix<_>) = A.Trace() + let inline determinant (A: #Matrix<_>) = A.Determinant() + let inline condition (A: #Matrix<_>) = A.ConditionNumber() + let inline nullity (A: #Matrix<_>) = A.Nullity() + let inline kernel (A: #Matrix<_>) = A.Kernel() + let inline range (A: #Matrix<_>) = A.Range() + let inline symmetric (A: #Matrix<_>) = A.IsSymmetric + + /// A module which helps constructing generic dense matrices. [] @@ -349,7 +368,7 @@ module DenseMatrix = /// Create a random matrix with the given dimension and value distribution. 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<'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) @@ -363,7 +382,7 @@ module DenseMatrix = /// Create a matrix with the given dimension and set all diagonal values to x. All other values are zero. let inline diag (order: int) (x: 'T) = Matrix<'T>.Build.DenseDiagonal(order, x) - + /// Create a matrix with the given dimension and set all diagonal values to x. All other values are zero. let inline diag2 (rows: int) (cols: int) (x: 'T) = Matrix<'T>.Build.DenseDiagonal(rows, cols, x) @@ -458,7 +477,7 @@ module SparseMatrix = /// Create a matrix with the given dimension and set all diagonal values to x. All other values are zero. let inline diag (order: int) (x: 'T) = Matrix<'T>.Build.SparseDiagonal(order, x) - + /// Create a matrix with the given dimension and set all diagonal values to x. All other values are zero. let inline diag2 (rows: int) (cols: int) (x: 'T) = Matrix<'T>.Build.SparseDiagonal(rows, cols, x) diff --git a/src/FSharp/LinearAlgebra.Vector.fs b/src/FSharp/LinearAlgebra.Vector.fs index b184cd2e..aad763f6 100644 --- a/src/FSharp/LinearAlgebra.Vector.fs +++ b/src/FSharp/LinearAlgebra.Vector.fs @@ -4,7 +4,7 @@ // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com // -// Copyright (c) 2009-2013 Math.NET +// Copyright (c) 2009-2014 Math.NET // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation @@ -203,6 +203,22 @@ module Vector = let inline subInPlace (v: #Vector<_>) (w: #Vector<_>) = v.Subtract(w, v) + let inline length (A: #Vector<_>) = A.Count + + let inline conjugate (A: #Vector<_>) = A.Conjugate() + let inline norm (A: #Vector<_>) = A.L2Norm() + let inline sum (A: #Vector<_>) = A.Sum() + + let inline min (A: #Vector<_>) = A.Minimum() + let inline max (A: #Vector<_>) = A.Maximum() + let inline minIndex (A: #Vector<_>) = A.MinimumIndex() + let inline maxIndex (A: #Vector<_>) = A.MaximumIndex() + let inline minAbs (A: #Vector<_>) = A.AbsoluteMinimum() + let inline maxAbs (A: #Vector<_>) = A.AbsoluteMaximum() + let inline minAbsIndex (A: #Vector<_>) = A.AbsoluteMinimumIndex() + let inline maxAbsIndex (A: #Vector<_>) = A.AbsoluteMaximumIndex() + + /// A module which helps constructing generic dense vectors. []