Math.NET Symbolics for WPF F# project


#1

Hello all
I want to use Math.Net.Symbolics in F# WPF project.
I download NuGet package:

Next I tried run simple example:

type MainViewModel() = 
    inherit ViewModelBase()   
    
    let expr = Infix.parseOrThrow("sin(x) * y")
    let symbols = Map.ofList [ "x", Real 2.0; "y", Real 3.0 ]
    let res = Evaluate.evaluate symbols expr 

    member self.Result with get() = res |> string

But…I get warning:

warning MSB3247: Found conflicts between different versions of the same dependent Assembly.

and exception:

Could not load file or Assembly FSharp.Core, Version=4.3.1.0…

Sorry for my English.


(Christoph Rüegg) #2

How do you currently reference FSharp.Core (i.e. before Math.NET Symbolics was added)? Assuming you do need 4.3.1.0, maybe this can be fixed with binding redirects from 3.1.2.5 to 4.3.1.0.


#3

Currently standard version:

(Version has not changed after adding Math.NET Symbolics)

if I remove link to it and added to FSharp.Core.dll from package folders - get exception (without warning):

Could not load file or Assembly ' FSharp.Core, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a\" or one of the dependent components. The system cannot find the file specified.

As far as I know, FSharp.Core 4.3.1.0 need for F#3.1, but I have only F# 3.0


#4

Update.
I tried create F# console application with .Net 4.5.
Still don’t want to work.

  1. Link to \packages\FSharp.Core.3.1.2.5\lib\net40
System.TypeInitializationException: The type initializer for '<StartupCode$MathNet-Symbolics>.$Infix'
 threw an exception. ---> System.Security.VerificationException: Operation could destabilize the runtime
  1. Link to \packages\FSharp.Core.3.1.2.5\lib\portable-net45+netcore45

The type ‘System.Object’ is defined in an assembly that is not referenced.
You must add a reference to assembly 'System.Runtime, Version=4.0.0.0

  1. Link to standard FSharp.Core 4.3.0.0 or 4.0.0.0

The type ‘System.Object’ is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0


Update2. This code work fine:

type MainViewModel() = 
    inherit ViewModelBase()   
    
    let symbols = Map.ofList [ "x", Real 2.0; "y", Real 3.0 ]
    let x = symbol "x"
    let y = symbol "y"
    let res = Evaluate.evaluate symbols (sin(x) + y) 
    member self.Result with get() = res.RealValue |> string

So, I think, problem in FParsec. But changes on FParsec.1.0.1 did not help.


(Christoph Rüegg) #5

Thanks for the updates. I’ll do a test series with different configurations as well, and post my findings here.


(Christoph Rüegg) #6

Code:

open MathNet.Symbolics
open MathNet.Symbolics.Operators

// verify it works
let expr = Infix.parseOrThrow("sin(x) * y")
let symbols = Map.ofList [ "x", Real 2.0; "y", Real 3.0 ]
let res = Evaluate.evaluate symbols expr
printfn "Result: %A" res

// actually used versions:
let prefixes = ["MathNet"; "FSharp"; "FParsec"]
System.AppDomain.CurrentDomain.GetAssemblies()
|> Seq.map (fun a -> a.GetName())
|> Seq.filter (fun a -> prefixes |> List.exists (fun p -> a.Name.StartsWith p))
|> Seq.iter (fun a -> printfn "%s: %O" a.Name a.Version)

A) .Net 4.5, F# 3.1, FSharp.Core 4.3.1.0, FParsec 1.0.1
Repro: in VS2015, create a .Net 4.5 F# Console App and install MathNet.Symbolics with NuGet

Resulting packages.config:

<packages>
  <package id="FParsec" version="1.0.1" targetFramework="net45" />
  <package id="FSharp.Core" version="3.1.2.5" targetFramework="net45" />
  <package id="MathNet.Numerics" version="3.8.0" targetFramework="net45" />
  <package id="MathNet.Numerics.FSharp" version="3.8.0" targetFramework="net45" />
  <package id="MathNet.Symbolics" version="0.7.1" targetFramework="net45" />
</packages>

Output:

Result: Real 2.72789228
MathNet.Symbolics-CompatibilityTest: 1.0.0.0
FSharp.Core: 4.3.1.0
MathNet.Symbolics: 0.7.1.0
FParsec: 1.0.0.0
FParsecCS: 1.0.0.0
MathNet.Numerics.FSharp: 3.8.0.0
MathNet.Numerics: 3.8.0.0

B) .Net 4.5, F# 4.0, FSharp.Core 4.4.0.0, FParsec 1.0.1
Repro: take A, update the FSharp.Core NuGet package to v4.0.0.1 with NuGet

Resulting packages.config:

<packages>
  <package id="FParsec" version="1.0.1" targetFramework="net45" />
  <package id="FSharp.Core" version="4.0.0.1" targetFramework="net45" />
  <package id="MathNet.Numerics" version="3.8.0" targetFramework="net45" />
  <package id="MathNet.Numerics.FSharp" version="3.8.0" targetFramework="net45" />
  <package id="MathNet.Symbolics" version="0.7.1" targetFramework="net45" />
</packages>

Output:

Result: Real 2.72789228
MathNet.Symbolics-CompatibilityTest: 1.0.0.0
FSharp.Core: 4.4.0.0
MathNet.Symbolics: 0.7.1.0
FParsec: 1.0.0.0
FParsecCS: 1.0.0.0
MathNet.Numerics.FSharp: 3.8.0.0
MathNet.Numerics: 3.8.0.0

C) .Net 4.5, F# 4.0, FSharp.Core 4.4.0.0, FParsec 1.0.2
Repro: take B, update the FParsec NuGet package to v1.0.2 with NuGet

Resulting packages.config:

<packages>
  <package id="FParsec" version="1.0.2" targetFramework="net45" />
  <package id="FSharp.Core" version="4.0.0.1" targetFramework="net45" />
  <package id="MathNet.Numerics" version="3.8.0" targetFramework="net45" />
  <package id="MathNet.Numerics.FSharp" version="3.8.0" targetFramework="net45" />
  <package id="MathNet.Symbolics" version="0.7.1" targetFramework="net45" />
</packages>

Output:

Result: Real 2.72789228
MathNet.Symbolics-CompatibilityTest: 1.0.0.0
FSharp.Core: 4.4.0.0
MathNet.Symbolics: 0.7.1.0
FParsec: 1.0.0.0
FParsecCS: 1.0.0.0
MathNet.Numerics.FSharp: 3.8.0.0
MathNet.Numerics: 3.8.0.0

Note that the output shows the AssemblyVersion, which may be different to the FileVersion and the PackageVersion.

So all this configurations work fine on my machine. Note that I never did touch any versions or references regarding F# and FSharp.Core manually but let NuGet do its thing, controlling it by the version of the NuGet package. I wonder what’s different to your setup?


#7

After updating NuGet package I get exception:

<packages>
  <package id="FParsec" version="1.0.1" targetFramework="net45" />
  <package id="FSharp.Core" version="3.1.2.5" targetFramework="net45" />
  <package id="MathNet.Numerics" version="3.8.0" targetFramework="net45" />
  <package id="MathNet.Numerics.FSharp" version="3.8.0" targetFramework="net45" />
  <package id="MathNet.Symbolics" version="0.7.1" targetFramework="net45" />
</packages>

System.IO.FileLoadException: could not load file or Assembly “FSharp.Core, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a” or one of the dependent components. The found Assembly’s manifest definition does not match the Assembly reference.

So, I think the problem is that is required FSharp.Core 4.3.1.0. (F# 3.1)[VS13], but I have only F# 3.0 [VS12].

fsharp-core-notes

After install Visual F# Out of Band Release 3.1.1 I get exception:

System.TypeInitializationException: The type initializer for '<StartupCode$MathNet-Symbolics>.$Infix'
 threw an exception. ---> System.Security.VerificationException: Operation could destabilize the runtime

(Christoph Rüegg) #8

I still have VS11 (aka VS2012; v11.0.61219.00 Update 5) installed, for some reason it worked out of the box there as well, even though F# 3.1 is not officially supported. I’m not entirely sure what F# tooling is installed. I see I have Visual F# 3.1.2 but that is for VS12 only (aka VS2013).

I’ve uploaded that project, could you give it a spin, or see if there is anything different to your code? MathNet.Symbolics-Compat.VS2012.7z. It also includes the compiled binaries, would be interesting whether these work on your machine.

Is there no chance you could upgrade to VS2013 or newer?


#9

Yeah, working, but this .Net 4.0. I can`t used it, because F# WPF application requires .Net 4.5 =)

I’ll trying to update to VS15. Although I am not sure what happens.
Okay, thank you. Then I’ll write the results.


(Christoph Rüegg) #10

Ok, let me know whether it works for you.

I just tried to change the VS2012 project target to .Net 4.5 and reinstalled the packages (Update-Package -Reinstall), worked fine on my machine:

MathNet.Symbolics-Compat.VS2012.Net45.7z


#11

As expected - does not work.

I failed to update VS2012. I suppose that I not have the latest updates for Windows 7 to install even for VS2013 :frowning:

I will look for other options.

Happy New Year to all!


#12

I was looking for a mistake where it was not. The problem is not dependence. Because code without using FParces worked!

On the page for the package is written as follows:

“This package uses the basic “low-trust” configuration of FParsec, which does not use any unverifiable code and is optimized for maximum portability. If you need to parse very large files or if you employ FParsec for performance-critical jobs, consider using the alternate “Big Data Edition” NuGet package (see nuget.org/packages/fparsec-big-data-edition).”

So, I changed FParsec on FParsec (Big Data Edition) and everything works!