MathEvaluator.FastExpressionCompiler
2.3.1
dotnet add package MathEvaluator.FastExpressionCompiler --version 2.3.1
NuGet\Install-Package MathEvaluator.FastExpressionCompiler -Version 2.3.1
<PackageReference Include="MathEvaluator.FastExpressionCompiler" Version="2.3.1" />
<PackageVersion Include="MathEvaluator.FastExpressionCompiler" Version="2.3.1" />
<PackageReference Include="MathEvaluator.FastExpressionCompiler" />
paket add MathEvaluator.FastExpressionCompiler --version 2.3.1
#r "nuget: MathEvaluator.FastExpressionCompiler, 2.3.1"
#addin nuget:?package=MathEvaluator.FastExpressionCompiler&version=2.3.1
#tool nuget:?package=MathEvaluator.FastExpressionCompiler&version=2.3.1
<img src="../logo.png" alt="logo" style="width:64px;height:64px;"/>
Fast Math Expression Compiler in .NET
NuGet packages:
MathEvaluator.FastExpressionCompiler is an extension of the MathEvaluator library that uses the FastExpressionCompiler library to provide high-performance compilation of mathematical expressions.
Compilation of expressions is up to 10-40x faster than the built-in .NET LambdaExpression.Compile()
method. This library includes all features of the MathEvaluator library. For more details, refer to the MathEvaluator GitHub repository.
How to use
Examples of using string extentions:
using MathEvaluation.Extensions;
// Compile a double expression
var compiled = "x * y + z".CompileFast(new { x = 0, y = 0, z = 0 });
double result = compiled(new { x = 5, y = 6, z = 7 }); // Result: 47
// Compile a decimal expression
var compiledDecimal = "x / y".CompileDecimalFast(new { x = 0m, y = 0m });
decimal decimalResult = compiledDecimal(new { x = 20m, y = 4m }); // Result: 5
// Compile a boolean expression
var compiledBoolean = "x > y".CompileBooleanFast(new { x = 0, y = 0 });
bool booleanResult = compiledBoolean(new { x = 2, y = 3 }); // Result: false
// Compile a complex expression
var compiledComplex = "x + y * i".CompileComplexFast(new { x = 0, y = 0 });
Complex complexResult = compiledComplex(new { x = 3, y = 4 }); // Result: 3 + 4i
Example of compilation with a Dictionary as a parameter (Added in version 2.3.0):
var dict = new Dictionary<string, double>();
dict.Add("x1", 3.0);
dict.Add("x2", 2.0);
var fn = "x1 + Math.Sin(x2) * 0.5"
.CompileFast(dict, new DotNetStandardMathContext());
var value = fn(dict);
Console.WriteLine(value); // 3.4546487...
FastMathExpression class
The FastMathExpression
class is a high-performance alternative to MathExpression
. It uses FastMathExpressionCompiler
by default for compiling expressions. Examples of using an instance of the FastMathExpression
class:
var dict = new Dictionary<string, double>();
dict.Add("x1", 3.0);
dict.Add("x2", 2.0);
var fn = new FastMathExpression("x1 + Math.Sin(x2) * 0.5", new DotNetStandardMathContext())
.Compile(dict);
var value = fn(dict);
Console.WriteLine(value); // 3.4546487...
FastMathExpressionCompiler class
The FastMathExpressionCompiler
class implements the IExpressionCompiler
interface (Added in version 2.3.1) and uses the FastExpressionCompiler to compile LINQ expressions into delegates.
Performance
By leveraging FastExpressionCompiler
, this library significantly reduces the time required to compile mathematical expressions. This makes it ideal for scenarios where expressions need to be compiled and executed repeatedly.
Contributing
Contributions are welcome! Please fork the repository and submit pull requests for any enhancements or bug fixes.
Looking to localize your project? Check out l10n.dev, an AI-powered localization service. Translate JSON files while preserving format, keys, and placeholders. Supports 165 languages with an easy-to-use API and UI. Get started for free!
License
This project is licensed under the Apache License, Version 2.0 - see the LICENSE file for details.
Contact
If you have any questions or suggestions, feel free to open an issue or contact me directly.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- FastExpressionCompiler (>= 5.2.0)
- MathEvaluator (>= 2.3.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
2.3.1 | 235 | a month ago |
It targets .NET Standard 2.1 and higher version.
Supports different mathematical contexts, such as scientific, programming, and other custom contexts.
Evaluates Boolean logic, as well as Double, Decimal, and Complex numbers.
Compiles a math expression string into executable code and produces a delegate that represents the math expression.
Provides variable support within math expressions.
Extensible with custom functions and operators.
Extended with the FastExpressionCompiler's CompileFast() methods, offering 10-40x faster performance compared to the traditional Compile() method.