Math.NET Numerics


IF# Notebook

iPython provides a rich browser-based interactive notebook with support for code, text, mathematical expressions, inline plots and other rich media. IfSharp, developed by Bayard Rock, is an F# profile for iPython with IntelliSense and embedded FSharp.Charting. Thanks to its NuGet support it can load other packages like Math.NET Numerics on demand.

Screenshot

Installing IF# Notebook

Follow the instructions at IfSharp/Installation.

Essentially:

  1. Install Anaconda

  2. In a shell, run

    conda update conda conda update ipython

  3. Install IfSharp.

Display Printers for Matrices and Vectors

By itself IfSharp does not know how to display matrices and vectors in a nice way, but we can tell it how to do so by providing our own display printers for them. Since v3.3 the Math.NET Numerics F# package includes a script MathNet.Numerics.IfSharp.fsx to do so. Unfortunately loading this script requires the exact version in the path - if you know a way to avoid this please let us know.

Screenshot

Alternatively you can also use the code below and adapt it to your needs, e.g. if you want it to show more rows.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
open MathNet.Numerics.LinearAlgebra

let inline (|Float|_|) (v:obj) =
    if v :? float then Some(v :?> float) else None
let inline (|Float32|_|) (v:obj) =
    if v :? float32 then Some(v :?> float32) else None
let inline (|PositiveInfinity|_|) (v: ^T) =
    if (^T : (static member IsPositiveInfinity: 'T -> bool) (v))
    then Some PositiveInfinity else None
let inline (|NegativeInfinity|_|) (v: ^T) =
    if (^T : (static member IsNegativeInfinity: 'T -> bool) (v))
    then Some NegativeInfinity else None
let inline (|NaN|_|) (v: ^T) =
    if (^T : (static member IsNaN: 'T -> bool) (v))
    then Some NaN else None

let inline formatMathValue (floatFormat:string) = function
  | PositiveInfinity -> "\\infty"
  | NegativeInfinity -> "-\\infty"
  | NaN -> "\\times"
  | Float v -> v.ToString(floatFormat)
  | Float32 v -> v.ToString(floatFormat)
  | v -> v.ToString()

let inline formatMatrix (matrix: Matrix<'T>) =
  String.concat Environment.NewLine
    [ "\\begin{bmatrix}"
      matrix.ToMatrixString(10,4,7,2,"\\cdots","\\vdots","\\ddots",
        " & ", "\\\\ " + Environment.NewLine, (fun x -> formatMathValue "G4" x))
      "\\end{bmatrix}" ]

let inline formatVector (vector: Vector<'T>) =
  String.concat Environment.NewLine
    [ "\\begin{bmatrix}"
      vector.ToVectorString(12, 80, "\\vdots", " & ", "\\\\ " + Environment.NewLine,
        (fun x -> formatMathValue "G4" x))
      "\\end{bmatrix}" ]

App.AddDisplayPrinter (fun (x:Matrix<float>) ->
    { ContentType = "text/latex"; Data = formatMatrix x })
App.AddDisplayPrinter (fun (x:Matrix<float32>) ->
    { ContentType = "text/latex"; Data = formatMatrix x })
App.AddDisplayPrinter (fun (x:Vector<float>) ->
    { ContentType = "text/latex"; Data = formatVector x })
App.AddDisplayPrinter (fun (x:Vector<float32>) ->
    { ContentType = "text/latex"; Data = formatVector x })
namespace MathNet
namespace MathNet.Numerics
namespace MathNet.Numerics.LinearAlgebra
val v : obj
type obj = System.Object

Full name: Microsoft.FSharp.Core.obj
Multiple items
val float : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float = System.Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
union case Option.Some: Value: 'T -> Option<'T>
union case Option.None: Option<'T>
Multiple items
val float32 : value:'T -> float32 (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float32

--------------------
type float32 = System.Single

Full name: Microsoft.FSharp.Core.float32

--------------------
type float32<'Measure> = float32

Full name: Microsoft.FSharp.Core.float32<_>
val v : 'T (requires member IsPositiveInfinity)
type bool = System.Boolean

Full name: Microsoft.FSharp.Core.bool
val v : 'T (requires member IsNegativeInfinity)
val v : 'T (requires member IsNaN)
val formatMathValue : floatFormat:string -> _arg1:'a -> string (requires member IsNaN and member IsPositiveInfinity and member IsNegativeInfinity)

Full name: IFsharpNotebook.formatMathValue
val floatFormat : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
active recognizer PositiveInfinity: 'T -> unit option

Full name: IFsharpNotebook.( |PositiveInfinity|_| )
active recognizer NegativeInfinity: 'T -> unit option

Full name: IFsharpNotebook.( |NegativeInfinity|_| )
active recognizer NaN: 'T -> unit option

Full name: IFsharpNotebook.( |NaN|_| )
active recognizer Float: obj -> float option

Full name: IFsharpNotebook.( |Float|_| )
val v : float
System.Double.ToString() : string
System.Double.ToString(provider: System.IFormatProvider) : string
System.Double.ToString(format: string) : string
System.Double.ToString(format: string, provider: System.IFormatProvider) : string
active recognizer Float32: obj -> float32 option

Full name: IFsharpNotebook.( |Float32|_| )
val v : float32
System.Single.ToString() : string
System.Single.ToString(format: string) : string
System.Single.ToString(provider: System.IFormatProvider) : string
System.Single.ToString(format: string, provider: System.IFormatProvider) : string
val v : 'a (requires member IsNaN and member IsPositiveInfinity and member IsNegativeInfinity)
System.Object.ToString() : string
val formatMatrix : matrix:Matrix<'T> -> string (requires default constructor and value type and 'T :> System.IEquatable<'T> and 'T :> System.IFormattable and 'T :> System.ValueType and member IsNaN and member IsPositiveInfinity and member IsNegativeInfinity)

Full name: IFsharpNotebook.formatMatrix
val matrix : Matrix<'T> (requires default constructor and value type and 'T :> System.IEquatable<'T> and 'T :> System.IFormattable and 'T :> System.ValueType and member IsNaN and member IsPositiveInfinity and member IsNegativeInfinity)
type Matrix<'T (requires default constructor and value type and 'T :> IEquatable<'T> and 'T :> IFormattable and 'T :> ValueType)> =
  member Add : scalar:'T -> Matrix<'T> + 3 overloads
  member Append : right:Matrix<'T> -> Matrix<'T> + 1 overload
  member AsArray : unit -> 'T[,]
  member AsColumnArrays : unit -> 'T[][]
  member AsColumnMajorArray : unit -> 'T[]
  member AsRowArrays : unit -> 'T[][]
  member AsRowMajorArray : unit -> 'T[]
  member At : row:int * column:int -> 'T + 1 overload
  member Cholesky : unit -> Cholesky<'T>
  member Clear : unit -> unit
  ...

Full name: MathNet.Numerics.LinearAlgebra.Matrix<_>
module String

from Microsoft.FSharp.Core
val concat : sep:string -> strings:seq<string> -> string

Full name: Microsoft.FSharp.Core.String.concat
Matrix.ToMatrixString(?format: string, ?provider: System.IFormatProvider) : string
Matrix.ToMatrixString(maxRows: int, maxColumns: int, ?format: string, ?provider: System.IFormatProvider) : string
Matrix.ToMatrixString(upperRows: int, lowerRows: int, leftColumns: int, rightColumns: int, horizontalEllipsis: string, verticalEllipsis: string, diagonalEllipsis: string, columnSeparator: string, rowSeparator: string, formatValue: System.Func<'T,string>) : string
Matrix.ToMatrixString(upperRows: int, lowerRows: int, minLeftColumns: int, rightColumns: int, maxWidth: int, horizontalEllipsis: string, verticalEllipsis: string, diagonalEllipsis: string, columnSeparator: string, rowSeparator: string, formatValue: System.Func<'T,string>) : string
val x : 'T (requires default constructor and value type and 'T :> System.IEquatable<'T> and 'T :> System.IFormattable and 'T :> System.ValueType and member IsNaN and member IsPositiveInfinity and member IsNegativeInfinity)
val formatVector : vector:Vector<'T> -> string (requires default constructor and value type and 'T :> System.IEquatable<'T> and 'T :> System.IFormattable and 'T :> System.ValueType and member IsNaN and member IsPositiveInfinity and member IsNegativeInfinity)

Full name: IFsharpNotebook.formatVector
val vector : Vector<'T> (requires default constructor and value type and 'T :> System.IEquatable<'T> and 'T :> System.IFormattable and 'T :> System.ValueType and member IsNaN and member IsPositiveInfinity and member IsNegativeInfinity)
type Vector<'T (requires default constructor and value type and 'T :> IEquatable<'T> and 'T :> IFormattable and 'T :> ValueType)> =
  member AbsoluteMaximum : unit -> 'T
  member AbsoluteMaximumIndex : unit -> int
  member AbsoluteMinimum : unit -> 'T
  member AbsoluteMinimumIndex : unit -> int
  member Add : scalar:'T -> Vector<'T> + 3 overloads
  member AsArray : unit -> 'T[]
  member At : index:int -> 'T + 1 overload
  member Clear : unit -> unit
  member ClearSubVector : index:int * count:int -> unit
  member Clone : unit -> Vector<'T>
  ...

Full name: MathNet.Numerics.LinearAlgebra.Vector<_>
Vector.ToVectorString(?format: string, ?provider: System.IFormatProvider) : string
Vector.ToVectorString(maxPerColumn: int, maxCharactersWidth: int, ?format: string, ?provider: System.IFormatProvider) : string
Vector.ToVectorString(maxPerColumn: int, maxCharactersWidth: int, ellipsis: string, columnSeparator: string, rowSeparator: string, formatValue: System.Func<'T,string>) : string
namespace Microsoft.FSharp.Data