From 863d35ac45e1f6b73278e6486332215fcb2a2305 Mon Sep 17 00:00:00 2001 From: Anton Antonov Date: Fri, 11 Oct 2024 18:18:55 +0500 Subject: [PATCH] Signature of extension methods Compile of string type is fixed. --- .../CompilationBenchmarks.cs | 4 ++-- .../CompoundingInterestBenchmarks.cs | 2 +- .../DecimalDotNetStandartMathContextTests.cs | 6 ++--- .../DotNetStandartMathContextTests.cs | 8 +++---- .../MathExpressionTests.Compile.cs | 8 +++---- .../MathExpressionTests.CompileBoolean.cs | 4 ++-- .../MathExpressionTests_Complex.Compile.cs | 8 +++---- ...hExpressionTests_Complex.CompileBoolean.cs | 4 ++-- .../MathExpressionTests_Decimal.Compile.cs | 8 +++---- ...hExpressionTests_Decimal.CompileBoolean.cs | 4 ++-- MathEvaluation/Extensions/StringExtensions.cs | 24 ++++--------------- MathEvaluation/MathEvaluation.csproj | 2 +- 12 files changed, 33 insertions(+), 49 deletions(-) diff --git a/MathEvaluation.Benchmarks/CompilationBenchmarks.cs b/MathEvaluation.Benchmarks/CompilationBenchmarks.cs index 07b9293..ea35c10 100644 --- a/MathEvaluation.Benchmarks/CompilationBenchmarks.cs +++ b/MathEvaluation.Benchmarks/CompilationBenchmarks.cs @@ -60,7 +60,7 @@ public Func NCalc_ToLambdaBoolean() public Func MathExpression_CompileBoolean_HasVariables() { return "A or not B and (C or B)" - .CompileBoolean(new BooleanVariables { A = true, B = false, C = true }, _programmingContext); + .CompileBoolean(_programmingContext, null, new BooleanVariables { A = true, B = false, C = true }); } [Benchmark(Description = "NCalc: \"A or not B and (C or B)\"")] @@ -102,7 +102,7 @@ public Func NCalc_ToLambdaSinCos() [Benchmark(Description = "MathEvaluator: \"Sin(a) + Cos(b)\"")] public Func MathExpression_CompileSinCos_HasVariables() { - return "Sin(a) + Cos(b)".Compile(new Variables { a = a, b = b }, _scientificContext); + return "Sin(a) + Cos(b)".Compile(_scientificContext, null, new Variables { a = a, b = b }); } [Benchmark(Description = "NCalc: \"Sin(a) + Cos(b)\"")] diff --git a/MathEvaluation.Benchmarks/CompoundingInterestBenchmarks.cs b/MathEvaluation.Benchmarks/CompoundingInterestBenchmarks.cs index 2677827..088d152 100644 --- a/MathEvaluation.Benchmarks/CompoundingInterestBenchmarks.cs +++ b/MathEvaluation.Benchmarks/CompoundingInterestBenchmarks.cs @@ -56,7 +56,7 @@ public double NCalc_Evaluate() [Benchmark(Description = "MathEvaluator compilation")] public Func MathEvaluator_Compile() { - return "P * (1 + r/n)^d".Compile(new CompoundInterestFormulaParams(), _mathContext); + return "P * (1 + r/n)^d".Compile(_mathContext, null, new CompoundInterestFormulaParams()); } [Benchmark(Description = "NCalc compilation")] diff --git a/MathEvaluation.Tests/Compilation/DecimalDotNetStandartMathContextTests.cs b/MathEvaluation.Tests/Compilation/DecimalDotNetStandartMathContextTests.cs index b512425..30790ad 100644 --- a/MathEvaluation.Tests/Compilation/DecimalDotNetStandartMathContextTests.cs +++ b/MathEvaluation.Tests/Compilation/DecimalDotNetStandartMathContextTests.cs @@ -51,7 +51,7 @@ public void MathExpression_CompileDecimalThenInvoke_HasPostfixIncrement_Expected { testOutputHelper.WriteLine($"{expression} = {expectedValue}"); - var fn = expression.CompileDecimal(new { a = 0 }, _context); + var fn = expression.CompileDecimal(_context, null, new { a = 0 }); var value = fn(new { a = 5 }); testOutputHelper.WriteLine($"result: {value}"); @@ -69,7 +69,7 @@ public void MathExpression_CompileDecimalThenInvoke_HasPostfixDecrement_Expected { testOutputHelper.WriteLine($"{expression} = {expectedValue}"); - var fn = expression.CompileDecimal(new { a = 0 }, _context); + var fn = expression.CompileDecimal(_context, null, new { a = 0 }); var value = fn(new { a = 5 }); testOutputHelper.WriteLine($"result: {value}"); @@ -417,7 +417,7 @@ public void MathExpression_CompileDecimalThenInvoke_HasVariable_ExpectedValue(st testOutputHelper.WriteLine($"{expression} = {expectedValue}"); testOutputHelper.WriteLine($"variable value = {varValue}"); - var fn = expression.CompileDecimal(new { x = 0.0m, PI = 0.0m }, _context); + var fn = expression.CompileDecimal(_context, null, new { x = 0.0m, PI = 0.0m }); var value = fn(new { x = (decimal)varValue, PI = (decimal)varValue }); testOutputHelper.WriteLine($"result: {value}"); diff --git a/MathEvaluation.Tests/Compilation/DotNetStandartMathContextTests.cs b/MathEvaluation.Tests/Compilation/DotNetStandartMathContextTests.cs index 4b3f2bb..23f93cc 100644 --- a/MathEvaluation.Tests/Compilation/DotNetStandartMathContextTests.cs +++ b/MathEvaluation.Tests/Compilation/DotNetStandartMathContextTests.cs @@ -71,7 +71,7 @@ public void MathExpression_CompileThenInvoke_HasPostfixIncrement_ExpectedValue(s { testOutputHelper.WriteLine($"{expression} = {expectedValue}"); - var fn = expression.Compile(new { a = 0 }, _context); + var fn = expression.Compile(_context, null, new { a = 0 }); var value = fn(new { a = 5 }); testOutputHelper.WriteLine($"result: {value}"); @@ -89,7 +89,7 @@ public void MathExpression_CompileThenInvoke_HasPostfixDecrement_ExpectedValue(s { testOutputHelper.WriteLine($"{expression} = {expectedValue}"); - var fn = expression.Compile(new { a = 0 }, _context); + var fn = expression.Compile(_context, null, new { a = 0 }); var value = fn(new { a = 5 }); testOutputHelper.WriteLine($"result: {value}"); @@ -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(new { x = 0.0, PI = 0.0 }, _context); + var fn = expression.Compile(_context, null, new { x = 0.0, PI = 0.0 }); var value = fn(new { x = varValue, PI = varValue }); testOutputHelper.WriteLine($"result: {value}"); @@ -517,7 +517,7 @@ public void MathExpression_CompileComplexThenInvoke_HasVariable_ExpectedValue(st testOutputHelper.WriteLine($"{expression} = {expectedValue}"); testOutputHelper.WriteLine($"variable value = {varValue}"); - var fn = expression.CompileComplex(new { x = 0.0, PI = 0.0 }, _context); + var fn = expression.CompileComplex(_context, null, new { x = 0.0, PI = 0.0 }); var value = fn(new { x = varValue, PI = varValue }); testOutputHelper.WriteLine($"result: {value}"); diff --git a/MathEvaluation.Tests/Compilation/MathExpressionTests.Compile.cs b/MathEvaluation.Tests/Compilation/MathExpressionTests.Compile.cs index 64d9355..2bb8e96 100644 --- a/MathEvaluation.Tests/Compilation/MathExpressionTests.Compile.cs +++ b/MathEvaluation.Tests/Compilation/MathExpressionTests.Compile.cs @@ -482,7 +482,7 @@ public void MathExpression_CompileThenInvoke_HasVariable_ExpectedValue(string ma var parameters = new { x = varValue, PI = varValue }; - var fn = mathString.Compile(parameters, _scientificContext); + var fn = mathString.Compile(_scientificContext, null, parameters); var value = fn(parameters); testOutputHelper.WriteLine($"result: {value}"); @@ -506,7 +506,7 @@ public void MathExpression_CompileThenInvoke_HasGetValueFns_ExpectedValue(string var getX1 = () => x1; var getX2 = () => x2; - var fn = mathString.Compile(new { getX1, getX2 }, _scientificContext); + var fn = mathString.Compile(_scientificContext, null, new { getX1, getX2 }); var value = fn(new { getX1, getX2 }); testOutputHelper.WriteLine($"result: {value}"); @@ -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(parameters, context); + var fn = mathString.Compile(context, null, parameters); var value = fn(parameters); testOutputHelper.WriteLine($"result: {value}"); @@ -555,7 +555,7 @@ public void MathExpression_CompileThenInvoke_HasCustomMinFn_ExpectedValue(string return minValue; }; - var fn = mathString.Compile(new { min }); + var fn = mathString.Compile(null, null, new { min }); var value = fn(new { min }); testOutputHelper.WriteLine($"result: {value}"); diff --git a/MathEvaluation.Tests/Compilation/MathExpressionTests.CompileBoolean.cs b/MathEvaluation.Tests/Compilation/MathExpressionTests.CompileBoolean.cs index eff2e1e..aa046ae 100644 --- a/MathEvaluation.Tests/Compilation/MathExpressionTests.CompileBoolean.cs +++ b/MathEvaluation.Tests/Compilation/MathExpressionTests.CompileBoolean.cs @@ -143,7 +143,7 @@ public void MathExpression_CompileBooleanThenInvoke_HasVariables_ExpectedValue(s testOutputHelper.WriteLine($"{expression} = {expectedValue}"); var getC = () => c; - var fn = expression.CompileBoolean(new { A = a, B = b, C = getC }, _programmingContext); + var fn = expression.CompileBoolean(_programmingContext, null, new { A = a, B = b, C = getC }); var value = fn(new { A = a, B = b, C = getC }); testOutputHelper.WriteLine($"result: {value}"); @@ -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(new { a }, context); + var fn = expression.CompileBoolean(context, null, new { a }); var value = fn(new { a }); testOutputHelper.WriteLine($"result: {value}"); diff --git a/MathEvaluation.Tests/Compilation/MathExpressionTests_Complex.Compile.cs b/MathEvaluation.Tests/Compilation/MathExpressionTests_Complex.Compile.cs index 6ecb7d2..6da2894 100644 --- a/MathEvaluation.Tests/Compilation/MathExpressionTests_Complex.Compile.cs +++ b/MathEvaluation.Tests/Compilation/MathExpressionTests_Complex.Compile.cs @@ -499,7 +499,7 @@ public void MathExpression_CompileComplexThenInvoke_HasVariable_ExpectedValue(st var parameters = new { x = varValue, PI = varValue }; - var fn = mathString.CompileComplex(parameters, _scientificContext); + var fn = mathString.CompileComplex(_scientificContext, null, parameters); var value = fn(parameters); testOutputHelper.WriteLine($"result: {value}"); @@ -523,7 +523,7 @@ public void MathExpression_CompileComplexThenInvoke_HasGetValueFns_ExpectedValue var getX1 = () => x1; var getX2 = () => x2; - var fn = mathString.CompileComplex(new { getX1, getX2 }, _scientificContext); + var fn = mathString.CompileComplex(_scientificContext, null, new { getX1, getX2 }); var value = fn(new { getX1, getX2 }); testOutputHelper.WriteLine($"result: {value}"); @@ -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(parameters, context); + var fn = mathString.CompileComplex(context, null, parameters); var value = fn(parameters); testOutputHelper.WriteLine($"result: {value}"); @@ -572,7 +572,7 @@ public void MathExpression_CompileComplexThenInvoke_HasCustomMinFn_ExpectedValue return minValue; }; - var fn = mathString.CompileComplex(new { min }); + var fn = mathString.CompileComplex(null, null, new { min }); var value = fn(new { min }); testOutputHelper.WriteLine($"result: {value}"); diff --git a/MathEvaluation.Tests/Compilation/MathExpressionTests_Complex.CompileBoolean.cs b/MathEvaluation.Tests/Compilation/MathExpressionTests_Complex.CompileBoolean.cs index 1434eb5..ed38740 100644 --- a/MathEvaluation.Tests/Compilation/MathExpressionTests_Complex.CompileBoolean.cs +++ b/MathEvaluation.Tests/Compilation/MathExpressionTests_Complex.CompileBoolean.cs @@ -144,7 +144,7 @@ public void MathExpression_CompileComplexThenInvoke_HasVariables_ExpectedValue(s testOutputHelper.WriteLine($"{expression} = {expectedValue}"); var getC = () => c; - var fn = expression.CompileComplex(new { A = a, B = b, C = getC }, _programmingContext); + var fn = expression.CompileComplex(_programmingContext, null, new { A = a, B = b, C = getC }); var value = fn(new { A = a, B = b, C = getC }); testOutputHelper.WriteLine($"result: {value}"); @@ -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(new { a }, context); + var fn = expression.CompileComplex(context, null, new { a }); var value = fn(new { a }); testOutputHelper.WriteLine($"result: {value}"); diff --git a/MathEvaluation.Tests/Compilation/MathExpressionTests_Decimal.Compile.cs b/MathEvaluation.Tests/Compilation/MathExpressionTests_Decimal.Compile.cs index c7c5113..647c332 100644 --- a/MathEvaluation.Tests/Compilation/MathExpressionTests_Decimal.Compile.cs +++ b/MathEvaluation.Tests/Compilation/MathExpressionTests_Decimal.Compile.cs @@ -436,7 +436,7 @@ public void MathExpression_CompileDecimalThenInvoke_HasVariable_ExpectedValue(st testOutputHelper.WriteLine($"{mathString} = {expectedValue}"); testOutputHelper.WriteLine($"variable value = {varValue}"); - var fn = mathString.CompileDecimal(new { x = varValue, PI = varValue }, _scientificContext); + var fn = mathString.CompileDecimal(_scientificContext, null, new { x = varValue, PI = varValue }); var value = fn(new { x = varValue, PI = varValue }); testOutputHelper.WriteLine($"result: {value}"); @@ -457,7 +457,7 @@ public void MathExpression_CompileDecimalThenInvoke_HasGetVariableFns_ExpectedVa var getX1 = () => x1; var getX2 = () => x2; - var fn = mathString.CompileDecimal(new { getX1, getX2 }, _scientificContext); + var fn = mathString.CompileDecimal(_scientificContext, null, new { getX1, getX2 }); var value = fn(new { getX1, getX2 }); testOutputHelper.WriteLine($"result: {value}"); @@ -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(parameters, context); + var fn = mathString.CompileDecimal(context, null, parameters); var value = fn(parameters); testOutputHelper.WriteLine($"result: {value}"); @@ -506,7 +506,7 @@ public void MathExpression_CompileDecimalThenInvoke_HasCustomMinFn_ExpectedValue return minValue; }; - var fn = mathString.CompileDecimal(new { min }); + var fn = mathString.CompileDecimal(null, null, new { min }); var value = fn(new { min }); testOutputHelper.WriteLine($"result: {value}"); diff --git a/MathEvaluation.Tests/Compilation/MathExpressionTests_Decimal.CompileBoolean.cs b/MathEvaluation.Tests/Compilation/MathExpressionTests_Decimal.CompileBoolean.cs index b77d44b..9a0c9eb 100644 --- a/MathEvaluation.Tests/Compilation/MathExpressionTests_Decimal.CompileBoolean.cs +++ b/MathEvaluation.Tests/Compilation/MathExpressionTests_Decimal.CompileBoolean.cs @@ -143,7 +143,7 @@ public void MathExpression_CompileDecimalThenInvoke_HasVariables_ExpectedValue(s testOutputHelper.WriteLine($"{expression} = {expectedValue}"); var getC = () => c; - var fn = expression.CompileDecimal(new { A = a, B = b, C = getC }, _programmingContext); + var fn = expression.CompileDecimal(_programmingContext, null, new { A = a, B = b, C = getC }); var value = fn(new { A = a, B = b, C = getC }); testOutputHelper.WriteLine($"result: {value}"); @@ -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(new { a }, context); + var fn = expression.CompileDecimal(context, null, new { a }); var value = fn(new { a }); testOutputHelper.WriteLine($"result: {value}"); diff --git a/MathEvaluation/Extensions/StringExtensions.cs b/MathEvaluation/Extensions/StringExtensions.cs index 550c130..ae5073c 100644 --- a/MathEvaluation/Extensions/StringExtensions.cs +++ b/MathEvaluation/Extensions/StringExtensions.cs @@ -66,52 +66,36 @@ public static Complex EvaluateComplex(this string mathString, IMathParameters? parameters, IMathContext? context = null, IFormatProvider? provider = null) => new MathExpression(mathString, context, provider).EvaluateComplex(parameters); - /// - public static Func Compile(this string mathString, IMathContext context) - => new MathExpression(mathString, context).Compile(); - /// public static Func Compile(this string mathString, IMathContext? context = null, IFormatProvider? provider = null) => new MathExpression(mathString, context, provider).Compile(); /// - public static Func Compile(this string mathString, T parameters, IMathContext? context = null, IFormatProvider? provider = null) + public static Func Compile(this string mathString, IMathContext? context, IFormatProvider? provider, T parameters) => new MathExpression(mathString, context, provider).Compile(parameters); - /// - public static Func CompileDecimal(this string mathString, IMathContext context) - => new MathExpression(mathString, context).CompileDecimal(); - /// public static Func CompileDecimal(this string mathString, IMathContext? context = null, IFormatProvider? provider = null) => new MathExpression(mathString, context, provider).CompileDecimal(); /// - public static Func CompileDecimal(this string mathString, T parameters, IMathContext? context = null, IFormatProvider? provider = null) + public static Func CompileDecimal(this string mathString, IMathContext? context, IFormatProvider? provider, T parameters) => new MathExpression(mathString, context, provider).CompileDecimal(parameters); - /// - public static Func CompileBoolean(this string mathString, IMathContext context) - => new MathExpression(mathString, context).CompileBoolean(); - /// public static Func CompileBoolean(this string mathString, IMathContext? context = null, IFormatProvider? provider = null) => new MathExpression(mathString, context, provider).CompileBoolean(); /// - public static Func CompileBoolean(this string mathString, T parameters, IMathContext? context = null, IFormatProvider? provider = null) + public static Func CompileBoolean(this string mathString, IMathContext? context, IFormatProvider? provider, T parameters) => new MathExpression(mathString, context, provider).CompileBoolean(parameters); - /// - public static Func CompileComplex(this string mathString, IMathContext context) - => new MathExpression(mathString, context).CompileComplex(); - /// public static Func CompileComplex(this string mathString, IMathContext? context = null, IFormatProvider? provider = null) => new MathExpression(mathString, context, provider).CompileComplex(); /// - public static Func CompileComplex(this string mathString, T parameters, IMathContext? context = null, IFormatProvider? provider = null) + public static Func CompileComplex(this string mathString, IMathContext? context, IFormatProvider? provider, T parameters) => new MathExpression(mathString, context, provider).CompileComplex(parameters); #region internal static Methods diff --git a/MathEvaluation/MathEvaluation.csproj b/MathEvaluation/MathEvaluation.csproj index 896458c..62a08fd 100644 --- a/MathEvaluation/MathEvaluation.csproj +++ b/MathEvaluation/MathEvaluation.csproj @@ -23,7 +23,7 @@ Provides variable support within math expressions. Extensible with custom functions and operators. LICENSE.md - 2.2.0 + 2.2.1 MathEvaluator MathEvaluator