Skip to content

Commit

Permalink
Fixes/extensions (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonovAnton authored Oct 12, 2024
1 parent 50cd106 commit 9f31d2a
Show file tree
Hide file tree
Showing 13 changed files with 41 additions and 61 deletions.
13 changes: 7 additions & 6 deletions MathEvaluation.Benchmarks/CompilationBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BenchmarkDotNet.Attributes;
using MathEvaluation;
using MathEvaluation.Context;
using MathEvaluation.Extensions;
using NCalc;
Expand Down Expand Up @@ -29,7 +30,7 @@ public CompilationBenchmarks()
[Benchmark(Description = "MathEvaluator: \"22888.32 * 30 / 323.34 / .5 - -1 / (2 + 22888.32) * 4 - 6\"")]
public Func<double> MathExpression_Compile()
{
return "22888.32 * 30 / 323.34 / .5 - -1 / (2 + 22888.32) * 4 - 6".Compile();
return new MathExpression("22888.32 * 30 / 323.34 / .5 - -1 / (2 + 22888.32) * 4 - 6").Compile();
}

[Benchmark(Description = "NCalc: \"22888.32 * 30 / 323.34 / .5 - -1 / (2 + 22888.32) * 4 - 6\"")]
Expand All @@ -44,8 +45,8 @@ public Func<double> NCalc_ToLambda()
[Benchmark(Description = "MathEvaluator: \"true or not false and (true or false)\"")]
public Func<bool> MathExpression_CompileBoolean()
{
return "true or not false and (true or false)"
.CompileBoolean(_programmingContext);
return new MathExpression("true or not false and (true or false)", _programmingContext)
.CompileBoolean();
}

[Benchmark(Description = "NCalc: \"true or not false and (true or false)\"")]
Expand All @@ -60,7 +61,7 @@ public Func<bool> NCalc_ToLambdaBoolean()
public Func<BooleanVariables, bool> MathExpression_CompileBoolean_HasVariables()
{
return "A or not B and (C or B)"
.CompileBoolean(_programmingContext, null, new BooleanVariables { A = true, B = false, C = true });
.CompileBoolean(new BooleanVariables { A = true, B = false, C = true }, _programmingContext);
}

[Benchmark(Description = "NCalc: \"A or not B and (C or B)\"")]
Expand All @@ -86,7 +87,7 @@ public bool NCalc_CompiledBoolean_HasVariables()
[Benchmark(Description = "MathEvaluator: \"Sin(pi/6) + Cos(pi/3)\"")]
public Func<double> MathExpression_CompileSinCos()
{
return "Sin(pi/6) + Cos(pi/3)".Compile(_scientificContext);
return new MathExpression("Sin(pi/6) + Cos(pi/3)", _scientificContext).Compile();
}

[Benchmark(Description = "NCalc: \"Sin(pi/6) + Cos(pi/3)\"")]
Expand All @@ -102,7 +103,7 @@ public Func<double> NCalc_ToLambdaSinCos()
[Benchmark(Description = "MathEvaluator: \"Sin(a) + Cos(b)\"")]
public Func<Variables, double> MathExpression_CompileSinCos_HasVariables()
{
return "Sin(a) + Cos(b)".Compile(_scientificContext, null, new Variables { a = a, b = b });
return "Sin(a) + Cos(b)".Compile(new Variables { a = a, b = b }, _scientificContext);
}

[Benchmark(Description = "NCalc: \"Sin(a) + Cos(b)\"")]
Expand Down
2 changes: 1 addition & 1 deletion MathEvaluation.Benchmarks/CompoundingInterestBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public double NCalc_Evaluate()
[Benchmark(Description = "MathEvaluator compilation")]
public Func<CompoundInterestFormulaParams, double> MathEvaluator_Compile()
{
return "P * (1 + r/n)^d".Compile(_mathContext, null, new CompoundInterestFormulaParams());
return "P * (1 + r/n)^d".Compile(new CompoundInterestFormulaParams(), _mathContext);
}

[Benchmark(Description = "NCalc compilation")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void MathExpression_CompileDecimalThenInvoke_HasPostfixIncrement_Expected
{
testOutputHelper.WriteLine($"{expression} = {expectedValue}");

var fn = expression.CompileDecimal(_context, null, new { a = 0 });
var fn = expression.CompileDecimal(new { a = 0 }, _context);
var value = fn(new { a = 5 });

testOutputHelper.WriteLine($"result: {value}");
Expand All @@ -69,7 +69,7 @@ public void MathExpression_CompileDecimalThenInvoke_HasPostfixDecrement_Expected
{
testOutputHelper.WriteLine($"{expression} = {expectedValue}");

var fn = expression.CompileDecimal(_context, null, new { a = 0 });
var fn = expression.CompileDecimal(new { a = 0 }, _context);
var value = fn(new { a = 5 });

testOutputHelper.WriteLine($"result: {value}");
Expand Down Expand Up @@ -417,7 +417,7 @@ public void MathExpression_CompileDecimalThenInvoke_HasVariable_ExpectedValue(st
testOutputHelper.WriteLine($"{expression} = {expectedValue}");
testOutputHelper.WriteLine($"variable value = {varValue}");

var fn = expression.CompileDecimal(_context, null, new { x = 0.0m, PI = 0.0m });
var fn = expression.CompileDecimal(new { x = 0.0m, PI = 0.0m }, _context);
var value = fn(new { x = (decimal)varValue, PI = (decimal)varValue });

testOutputHelper.WriteLine($"result: {value}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void MathExpression_CompileThenInvoke_HasPostfixIncrement_ExpectedValue(s
{
testOutputHelper.WriteLine($"{expression} = {expectedValue}");

var fn = expression.Compile(_context, null, new { a = 0 });
var fn = expression.Compile(new { a = 0 }, _context);
var value = fn(new { a = 5 });

testOutputHelper.WriteLine($"result: {value}");
Expand All @@ -89,7 +89,7 @@ public void MathExpression_CompileThenInvoke_HasPostfixDecrement_ExpectedValue(s
{
testOutputHelper.WriteLine($"{expression} = {expectedValue}");

var fn = expression.Compile(_context, null, new { a = 0 });
var fn = expression.Compile(new { a = 0 }, _context);
var value = fn(new { a = 5 });

testOutputHelper.WriteLine($"result: {value}");
Expand Down Expand Up @@ -501,7 +501,7 @@ public void MathExpression_CompileThenInvoke_HasVariable_ExpectedValue(string ex
testOutputHelper.WriteLine($"{expression} = {expectedValue}");
testOutputHelper.WriteLine($"variable value = {varValue}");

var fn = expression.Compile(_context, null, new { x = 0.0, PI = 0.0 });
var fn = expression.Compile(new { x = 0.0, PI = 0.0 }, _context);
var value = fn(new { x = varValue, PI = varValue });

testOutputHelper.WriteLine($"result: {value}");
Expand All @@ -517,7 +517,7 @@ public void MathExpression_CompileComplexThenInvoke_HasVariable_ExpectedValue(st
testOutputHelper.WriteLine($"{expression} = {expectedValue}");
testOutputHelper.WriteLine($"variable value = {varValue}");

var fn = expression.CompileComplex(_context, null, new { x = 0.0, PI = 0.0 });
var fn = expression.CompileComplex(new { x = 0.0, PI = 0.0 }, _context);
var value = fn(new { x = varValue, PI = varValue });

testOutputHelper.WriteLine($"result: {value}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ public void MathExpression_CompileThenInvoke_HasVariable_ExpectedValue(string ma

var parameters = new { x = varValue, PI = varValue };

var fn = mathString.Compile(_scientificContext, null, parameters);
var fn = mathString.Compile(parameters, _scientificContext);
var value = fn(parameters);

testOutputHelper.WriteLine($"result: {value}");
Expand All @@ -506,7 +506,7 @@ public void MathExpression_CompileThenInvoke_HasGetValueFns_ExpectedValue(string
var getX1 = () => x1;
var getX2 = () => x2;

var fn = mathString.Compile(_scientificContext, null, new { getX1, getX2 });
var fn = mathString.Compile(new { getX1, getX2 }, _scientificContext);
var value = fn(new { getX1, getX2 });

testOutputHelper.WriteLine($"result: {value}");
Expand All @@ -529,7 +529,7 @@ public void MathExpression_CompileThenInvoke_HasVariablesAndCustomFns_ExpectedVa
var context = new MathContext(new { sqrt, ln });
var parameters = new { x1, x2 };

var fn = mathString.Compile(context, null, parameters);
var fn = mathString.Compile(parameters, context);
var value = fn(parameters);

testOutputHelper.WriteLine($"result: {value}");
Expand All @@ -555,7 +555,7 @@ public void MathExpression_CompileThenInvoke_HasCustomMinFn_ExpectedValue(string
return minValue;
};

var fn = mathString.Compile(null, null, new { min });
var fn = mathString.Compile(new { min });
var value = fn(new { min });

testOutputHelper.WriteLine($"result: {value}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public void MathExpression_CompileBooleanThenInvoke_HasVariables_ExpectedValue(s
testOutputHelper.WriteLine($"{expression} = {expectedValue}");

var getC = () => c;
var fn = expression.CompileBoolean(_programmingContext, null, new { A = a, B = b, C = getC });
var fn = expression.CompileBoolean(new { A = a, B = b, C = getC }, _programmingContext);
var value = fn(new { A = a, B = b, C = getC });

testOutputHelper.WriteLine($"result: {value}");
Expand All @@ -161,7 +161,7 @@ public void MathExpression_CompileBooleanThenInvoke_HasCustomFunction_ExpectedVa
var context = new ProgrammingMathContext();
context.BindFunction((c, v1, v2) => c != 0.0 ? v1 : v2, "if");

var fn = expression.CompileBoolean(context, null, new { a });
var fn = expression.CompileBoolean(new { a }, context);
var value = fn(new { a });

testOutputHelper.WriteLine($"result: {value}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ public void MathExpression_CompileComplexThenInvoke_HasVariable_ExpectedValue(st

var parameters = new { x = varValue, PI = varValue };

var fn = mathString.CompileComplex(_scientificContext, null, parameters);
var fn = mathString.CompileComplex(parameters, _scientificContext);
var value = fn(parameters);

testOutputHelper.WriteLine($"result: {value}");
Expand All @@ -523,7 +523,7 @@ public void MathExpression_CompileComplexThenInvoke_HasGetValueFns_ExpectedValue
var getX1 = () => x1;
var getX2 = () => x2;

var fn = mathString.CompileComplex(_scientificContext, null, new { getX1, getX2 });
var fn = mathString.CompileComplex(new { getX1, getX2 }, _scientificContext);
var value = fn(new { getX1, getX2 });

testOutputHelper.WriteLine($"result: {value}");
Expand All @@ -546,7 +546,7 @@ public void MathExpression_CompileComplexThenInvoke_HasVariablesAndCustomFns_Exp
var context = new MathContext(new { sqrt, ln });
var parameters = new { x1, x2 };

var fn = mathString.CompileComplex(context, null, parameters);
var fn = mathString.CompileComplex(parameters, context);
var value = fn(parameters);

testOutputHelper.WriteLine($"result: {value}");
Expand All @@ -572,7 +572,7 @@ public void MathExpression_CompileComplexThenInvoke_HasCustomMinFn_ExpectedValue
return minValue;
};

var fn = mathString.CompileComplex(null, null, new { min });
var fn = mathString.CompileComplex(new { min });
var value = fn(new { min });

testOutputHelper.WriteLine($"result: {value}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void MathExpression_CompileComplexThenInvoke_HasVariables_ExpectedValue(s
testOutputHelper.WriteLine($"{expression} = {expectedValue}");

var getC = () => c;
var fn = expression.CompileComplex(_programmingContext, null, new { A = a, B = b, C = getC });
var fn = expression.CompileComplex(new { A = a, B = b, C = getC }, _programmingContext);
var value = fn(new { A = a, B = b, C = getC });

testOutputHelper.WriteLine($"result: {value}");
Expand All @@ -162,7 +162,7 @@ public void MathExpression_CompileComplexThenInvoke_HasCustomFunction_ExpectedVa
var context = new ProgrammingMathContext();
context.BindFunction((c, v1, v2) => c != 0.0 ? v1 : v2, "if");

var fn = expression.CompileComplex(context, null, new { a });
var fn = expression.CompileComplex(new { a }, context);
var value = fn(new { a });

testOutputHelper.WriteLine($"result: {value}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ public void MathExpression_CompileDecimalThenInvoke_HasVariable_ExpectedValue(st
testOutputHelper.WriteLine($"{mathString} = {expectedValue}");
testOutputHelper.WriteLine($"variable value = {varValue}");

var fn = mathString.CompileDecimal(_scientificContext, null, new { x = varValue, PI = varValue });
var fn = mathString.CompileDecimal(new { x = varValue, PI = varValue }, _scientificContext);
var value = fn(new { x = varValue, PI = varValue });

testOutputHelper.WriteLine($"result: {value}");
Expand All @@ -457,7 +457,7 @@ public void MathExpression_CompileDecimalThenInvoke_HasGetVariableFns_ExpectedVa
var getX1 = () => x1;
var getX2 = () => x2;

var fn = mathString.CompileDecimal(_scientificContext, null, new { getX1, getX2 });
var fn = mathString.CompileDecimal(new { getX1, getX2 }, _scientificContext);
var value = fn(new { getX1, getX2 });

testOutputHelper.WriteLine($"result: {value}");
Expand All @@ -480,7 +480,7 @@ public void MathExpression_CompileDecimalThenInvoke_HasVariablesAndCustomLnSqrtF
var context = new MathContext(new { sqrt, ln });
var parameters = new { x1, x2 };

var fn = mathString.CompileDecimal(context, null, parameters);
var fn = mathString.CompileDecimal(parameters, context);
var value = fn(parameters);

testOutputHelper.WriteLine($"result: {value}");
Expand All @@ -506,7 +506,7 @@ public void MathExpression_CompileDecimalThenInvoke_HasCustomMinFn_ExpectedValue
return minValue;
};

var fn = mathString.CompileDecimal(null, null, new { min });
var fn = mathString.CompileDecimal(new { min });
var value = fn(new { min });

testOutputHelper.WriteLine($"result: {value}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public void MathExpression_CompileDecimalThenInvoke_HasVariables_ExpectedValue(s
testOutputHelper.WriteLine($"{expression} = {expectedValue}");

var getC = () => c;
var fn = expression.CompileDecimal(_programmingContext, null, new { A = a, B = b, C = getC });
var fn = expression.CompileDecimal(new { A = a, B = b, C = getC }, _programmingContext);
var value = fn(new { A = a, B = b, C = getC });

testOutputHelper.WriteLine($"result: {value}");
Expand All @@ -161,7 +161,7 @@ public void MathExpression_CompileDecimalThenInvoke_HasCustomFunction_ExpectedVa
var context = new ProgrammingMathContext();
context.BindFunction((c, v1, v2) => c != 0.0 ? v1 : v2, "if");

var fn = expression.CompileDecimal(context, null, new { a });
var fn = expression.CompileDecimal(new { a }, context);
var value = fn(new { a });

testOutputHelper.WriteLine($"result: {value}");
Expand Down
24 changes: 4 additions & 20 deletions MathEvaluation/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,36 +66,20 @@ public static Complex EvaluateComplex(this string mathString,
IMathParameters? parameters, IMathContext? context = null, IFormatProvider? provider = null)
=> new MathExpression(mathString, context, provider).EvaluateComplex(parameters);

/// <inheritdoc cref="MathExpression.Compile()"/>
public static Func<double> Compile(this string mathString, IMathContext? context = null, IFormatProvider? provider = null)
=> new MathExpression(mathString, context, provider).Compile();

/// <inheritdoc cref="MathExpression.Compile{T}(T)"/>
public static Func<T, double> Compile<T>(this string mathString, IMathContext? context, IFormatProvider? provider, T parameters)
public static Func<T, double> Compile<T>(this string mathString, T parameters, IMathContext? context = null, IFormatProvider? provider = null)
=> new MathExpression(mathString, context, provider).Compile(parameters);

/// <inheritdoc cref="MathExpression.CompileDecimal()"/>
public static Func<decimal> CompileDecimal(this string mathString, IMathContext? context = null, IFormatProvider? provider = null)
=> new MathExpression(mathString, context, provider).CompileDecimal();

/// <inheritdoc cref="MathExpression.CompileDecimal{T}(T)"/>
public static Func<T, decimal> CompileDecimal<T>(this string mathString, IMathContext? context, IFormatProvider? provider, T parameters)
public static Func<T, decimal> CompileDecimal<T>(this string mathString, T parameters, IMathContext? context = null, IFormatProvider? provider = null)
=> new MathExpression(mathString, context, provider).CompileDecimal(parameters);

/// <inheritdoc cref="MathExpression.CompileBoolean()"/>
public static Func<bool> CompileBoolean(this string mathString, IMathContext? context = null, IFormatProvider? provider = null)
=> new MathExpression(mathString, context, provider).CompileBoolean();

/// <inheritdoc cref="MathExpression.CompileBoolean{T}(T)"/>
public static Func<T, bool> CompileBoolean<T>(this string mathString, IMathContext? context, IFormatProvider? provider, T parameters)
public static Func<T, bool> CompileBoolean<T>(this string mathString, T parameters, IMathContext? context = null, IFormatProvider? provider = null)
=> new MathExpression(mathString, context, provider).CompileBoolean(parameters);

/// <inheritdoc cref="MathExpression.CompileComplex()"/>
public static Func<Complex> CompileComplex(this string mathString, IMathContext? context = null, IFormatProvider? provider = null)
=> new MathExpression(mathString, context, provider).CompileComplex();

/// <inheritdoc cref="MathExpression.CompileComplex{T}(T)"/>
public static Func<T, Complex> CompileComplex<T>(this string mathString, IMathContext? context, IFormatProvider? provider, T parameters)
public static Func<T, Complex> CompileComplex<T>(this string mathString, T parameters, IMathContext? context = null, IFormatProvider? provider = null)
=> new MathExpression(mathString, context, provider).CompileComplex(parameters);

#region internal static Methods
Expand Down
2 changes: 1 addition & 1 deletion MathEvaluation/MathEvaluation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Provides variable support within math expressions.
Extensible with custom functions and operators.
</PackageReleaseNotes>
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
<Version>2.2.1</Version>
<Version>2.2.0</Version>
<PackageId>MathEvaluator</PackageId>
<Product>MathEvaluator</Product>
</PropertyGroup>
Expand Down
11 changes: 3 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,10 @@ Example of using custom context:

Examples of compilation:

Func<decimal> fn1 = "22’888.32 CHF * 30 / 323.34 / .5 - - 1 / (2 + 22’888.32 CHF) * 4 - 6"
.CompileDecimal(null, new CultureInfo("de-CH"));

var value1 = fn1();

var fn2 = "ln(1/x1 + √(1/(x2*x2) + 1))"
.Compile(new ScientificMathContext(), null, new { x1 = 0.0, x2 = 0.0 });
var fn = "ln(1/x1 + √(1/(x2*x2) + 1))"
.Compile(new { x1 = 0.0, x2 = 0.0 }, new ScientificMathContext());
var value2 = fn2(new { x1 = -0.5, x2 = 0.5 });
var value = fn(new { x1 = -0.5, x2 = 0.5 });

## How to debug or log

Expand Down

0 comments on commit 9f31d2a

Please sign in to comment.