Skip to content

Commit

Permalink
Revert "Signature of extension methods Compile of string type is fixe…
Browse files Browse the repository at this point in the history
…d. (#71)"

This reverts commit ffd2bb4.
  • Loading branch information
AntonovAnton committed Oct 12, 2024
1 parent 50cd106 commit ad9c8a0
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 33 deletions.
4 changes: 2 additions & 2 deletions MathEvaluation.Benchmarks/CompilationBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,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 Down Expand Up @@ -102,7 +102,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: 20 additions & 4 deletions MathEvaluation/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,36 +66,52 @@ 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)
=> new MathExpression(mathString, context).Compile();

/// <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)
=> new MathExpression(mathString, context).CompileDecimal();

/// <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)
=> new MathExpression(mathString, context).CompileBoolean();

/// <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)
=> new MathExpression(mathString, context).CompileComplex();

/// <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

0 comments on commit ad9c8a0

Please sign in to comment.