Browse Source

iPython IfSharp F# profile integration in the F# package; update docs

pull/248/head
Christoph Ruegg 12 years ago
parent
commit
46454cb5a3
  1. 1
      MathNet.Numerics.sln
  2. 9
      build.fsx
  3. 6
      docs/content/IFsharpNotebook.fsx
  4. BIN
      docs/files/img/IfSharp-MatrixVector.png
  5. 1
      src/FSharp/FSharp.fsproj
  6. 49
      src/FSharp/MathNet.Numerics.IfSharp.fsx

1
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

9
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

6
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<float32>) ->
{ ContentType = "text/latex"; Data = formatVector x })
*)

BIN
docs/files/img/IfSharp-MatrixVector.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 55 KiB

After

Width:  |  Height:  |  Size: 47 KiB

1
src/FSharp/FSharp.fsproj

@ -74,6 +74,7 @@
<Compile Include="FindRoots.fs" />
<Compile Include="RandomVariable.fs" />
<None Include="MathNet.Numerics.fsx" />
<None Include="MathNet.Numerics.IfSharp.fsx" />
</ItemGroup>
<ItemGroup>
<Reference Include="mscorlib" />

49
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<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 })
Loading…
Cancel
Save