diff --git a/MathNet.Numerics.sln b/MathNet.Numerics.sln index b3791751..bfb28cf0 100644 --- a/MathNet.Numerics.sln +++ b/MathNet.Numerics.sln @@ -45,6 +45,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Docs", "Docs", "{039229DA-A docs\content\Euclid.fsx = docs\content\Euclid.fsx docs\content\Functions.fsx = docs\content\Functions.fsx docs\content\Generate.fsx = docs\content\Generate.fsx + docs\content\IFsharpNotebook.fsx = docs\content\IFsharpNotebook.fsx docs\content\index.fsx = docs\content\index.fsx docs\content\IntegralTransforms.fsx = docs\content\IntegralTransforms.fsx docs\content\Integration.fsx = docs\content\Integration.fsx diff --git a/build.fsx b/build.fsx index e2eae04d..48400049 100644 --- a/build.fsx +++ b/build.fsx @@ -105,6 +105,7 @@ let fsharpPack = @"..\..\out\lib\Profile328\MathNet.Numerics.FSharp.*", Some libpcl328, None; @"..\..\out\lib\Profile328\MathNet.Numerics.FSharp.*", Some libpcl328, None; @"MathNet.Numerics.fsx", None, None; + @"MathNet.Numerics.IfSharp.fsx", None, None; @"..\..\src\FSharp\**\*.fs", Some "src/Common", None ] } let numericsSignedPack = @@ -124,6 +125,7 @@ let fsharpSignedPack = Dependencies = [{ FrameworkVersion=""; Dependencies=[ "MathNet.Numerics.Signed", RequireExactly packageVersion ] }] Files = [ @"..\..\out\lib-signed\Net40\MathNet.Numerics.FSharp.*", Some libnet40, None; @"MathNet.Numerics.fsx", None, None; + @"MathNet.Numerics.IfSharp.fsx", None, None; @"..\..\src\FSharp\**\*.fs", Some "src/Common", None ] } let coreBundle = @@ -371,12 +373,18 @@ let provideFsLoader includes path = let references = [ for assembly in assemblies -> sprintf "#r \"%s\"" assembly ] ReplaceFile (path @@ "MathNet.Numerics.fsx") (nowarn @ includes @ references @ extraScript |> toLines) +let provideFsIfSharpLoader path = + let fullScript = ReadFile "src/FSharp/MathNet.Numerics.IfSharp.fsx" |> Array.ofSeq + let startIndex = fullScript |> Seq.findIndex (fun s -> s.Contains "***MathNet.Numerics.IfSharp.fsx***") + ReplaceFile (path @@ "MathNet.Numerics.IfSharp.fsx") (fullScript .[startIndex + 1 ..] |> toLines) + let provideZipExtraFiles path (bundle:Bundle) = provideLicense path provideReadme (sprintf "%s v%s" bundle.Title bundle.Version) bundle.ReleaseNotesFile path if bundle.FsLoader then let includes = [ for root in [ ""; "../"; "../../" ] -> sprintf "#I \"%sNet40\"" root ] provideFsLoader includes path + provideFsIfSharpLoader path let provideNuGetExtraFiles path (bundle:Bundle) (pack:Package) = provideLicense path @@ -385,6 +393,7 @@ let provideNuGetExtraFiles path (bundle:Bundle) (pack:Package) = let includes = [ for root in [ ""; "../"; "../../"; "../../../" ] do for package in bundle.Packages -> sprintf "#I \"%spackages/%s.%s/lib/net40/\"" root package.Id package.Version ] provideFsLoader includes path + provideFsIfSharpLoader path // ZIP diff --git a/docs/content/IFsharpNotebook.fsx b/docs/content/IFsharpNotebook.fsx index 351d02ae..dc3f08e6 100644 --- a/docs/content/IFsharpNotebook.fsx +++ b/docs/content/IFsharpNotebook.fsx @@ -33,11 +33,12 @@ 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](img/IfSharp-MatrixVector.png) -Run the following snipped once within a notebook to render matrices and vectors with LaTeX/MathJax, and adapt it to your needs e.g. if you want it to show more rows. -Alternatively you can add it to an Includes.fsx file within the IfSharp installation to make it always available. +Alternatively you can also use the code below and adapt it to your needs, e.g. if you want it to show more rows. open MathNet.Numerics.LinearAlgebra @@ -86,5 +87,4 @@ Alternatively you can add it to an Includes.fsx file within the IfSharp installa App.AddDisplayPrinter (fun (x:Vector) -> { ContentType = "text/latex"; Data = formatVector x }) - *) diff --git a/docs/files/img/IfSharp-MatrixVector.png b/docs/files/img/IfSharp-MatrixVector.png index c842748f..3d69ed3c 100644 Binary files a/docs/files/img/IfSharp-MatrixVector.png and b/docs/files/img/IfSharp-MatrixVector.png differ diff --git a/src/FSharp/FSharp.fsproj b/src/FSharp/FSharp.fsproj index 934a562c..d8a81c65 100644 --- a/src/FSharp/FSharp.fsproj +++ b/src/FSharp/FSharp.fsproj @@ -74,6 +74,7 @@ + diff --git a/src/FSharp/MathNet.Numerics.IfSharp.fsx b/src/FSharp/MathNet.Numerics.IfSharp.fsx new file mode 100644 index 00000000..8e330813 --- /dev/null +++ b/src/FSharp/MathNet.Numerics.IfSharp.fsx @@ -0,0 +1,49 @@ +#I "../../out/lib/Net40" +#r "MathNet.Numerics.dll" +#r "MathNet.Numerics.FSharp.dll" + +// ***MathNet.Numerics.IfSharp.fsx*** (DO NOT REMOVE THIS COMMENT, everything below is copied to the output) + +// This file is intended for the IfSharp F# profile for iPython only. See: +// http://numerics.mathdotnet.com/docs/IFSharpNotebook.html +// http://github.com/BayardRock/IfSharp +// http://ipython.org/ + +// Assumption: MathNet.Numerics and MathNet.Numerics.FSharp have been referenced already, using +// #N "MathNet.Numerics" +// #N "MathNet.Numerics.FSharp" + +open System +open MathNet.Numerics +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) -> { ContentType = "text/latex"; Data = formatMatrix x }) +App.AddDisplayPrinter (fun (x:Matrix) -> { ContentType = "text/latex"; Data = formatMatrix x }) +App.AddDisplayPrinter (fun (x:Vector) -> { ContentType = "text/latex"; Data = formatVector x }) +App.AddDisplayPrinter (fun (x:Vector) -> { ContentType = "text/latex"; Data = formatVector x })