我想在 F# 项目中使用库 Math.NET Symbolics。但是当我运行简单的代码时:
open MathNet.Symbolics
open MathNet.Symbolics.Operators
...
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
我有:
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.
在讨论中,我认为不可能,因为我只有.Net 4.5和VS2012(因此我不能使用F#3.1)。
但我不明白,如果在 .Net 4.0 中一切正常,为什么我不能在 .Net 4.5 中正常使用。兼容版本呢?
问题:这甚至可能吗?而且,如果可能的话,怎么做?
编辑:
当 app.config 有:
<bindingRedirect oldVersion="0.0.0.0-4.3.1.0" newVersion="4.3.1.0" />
<bindingRedirect oldVersion="2.3.5.0" newVersion="4.3.0.0" />
<bindingRedirect oldVersion="2.0.0.0" newVersion="4.3.0.0" />
此代码工作正常:
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)
但我需要使用解析器来解析数学表达式。因此,这个选项不适合我。
更新:
回答:
编译失败后,Visual Studio 被更改为从标准 FSharp.Core 4.3.0.0 的包文件夹中引用 FSharp.Core。当我设置属性“复制本地”=“真”时,问题就解决了。
现在编码
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
给出另一个例外:
System.TypeInitializationException: The type initializer for '<StartupCode$MathNet-Symbolics>.$Infix'
threw an exception. ---> System.Security.VerificationException: Operation could destabilize the runtime
我正在寻找一个没有的错误。问题不在于依赖。因为不使用 FParces 的代码有效!
在包的页面上写如下:
“这个包使用 FParsec 的基本“低信任”配置,它不使用任何无法验证的代码,并针对最大的可移植性进行了优化。如果您需要解析非常大的文件,或者如果您将 FParsec 用于性能关键的工作,请考虑使用备用“大数据版”NuGet 包(请参阅 nuget.org/packages/fparsec-big-data-edition)。”
所以,我在FParsec(大数据版)上更改了 FParsec ,一切正常!
PS我尝试更改绑定重定向没有意义=)只需写:
<bindingRedirect oldVersion="0.0.0.0-4.3.1.0" newVersion="4.3.1.0" />