From 4bcae65be6da226a6d2e7e3ce57be580cde146e9 Mon Sep 17 00:00:00 2001 From: axexlck Date: Sat, 20 Jul 2024 00:42:42 +0200 Subject: [PATCH] WIP #1009: Add missing IMatrch interface --- .../core/eval/interfaces/IMatch.java | 25 ++++++ .../core/system/MinMaxFunctionsTest.java | 89 +++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/eval/interfaces/IMatch.java create mode 100644 symja_android_library/matheclipse-core/src/test/java/org/matheclipse/core/system/MinMaxFunctionsTest.java diff --git a/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/eval/interfaces/IMatch.java b/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/eval/interfaces/IMatch.java new file mode 100644 index 000000000..9cb704687 --- /dev/null +++ b/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/eval/interfaces/IMatch.java @@ -0,0 +1,25 @@ +package org.matheclipse.core.eval.interfaces; + +import org.matheclipse.core.eval.EvalEngine; +import org.matheclipse.core.expression.F; +import org.matheclipse.core.interfaces.IAST; +import org.matheclipse.core.interfaces.IExpr; + +public interface IMatch { + + default IExpr match2(IAST ast, EvalEngine engine) { + return F.NIL; + } + + default IExpr match3(IAST ast, EvalEngine engine) { + return F.NIL; + } + + default IExpr match4(IAST ast, EvalEngine engine) { + return F.NIL; + } + + default IExpr match5(IAST ast, EvalEngine engine) { + return F.NIL; + } +} diff --git a/symja_android_library/matheclipse-core/src/test/java/org/matheclipse/core/system/MinMaxFunctionsTest.java b/symja_android_library/matheclipse-core/src/test/java/org/matheclipse/core/system/MinMaxFunctionsTest.java new file mode 100644 index 000000000..0ec44bf94 --- /dev/null +++ b/symja_android_library/matheclipse-core/src/test/java/org/matheclipse/core/system/MinMaxFunctionsTest.java @@ -0,0 +1,89 @@ +package org.matheclipse.core.system; + +import org.junit.Test; + +public class MinMaxFunctionsTest extends ExprEvaluatorTestCase { + + @Test + public void testNArgMax() { + checkNumeric("NArgMax(-2*x^2-3*x+5,x)", // + "{-0.7500000000000001}"); + checkNumeric("NArgMax(1-(x*y-3)^2, {x, y})", // + "{1.0,3.0}"); + } + + + @Test + public void testNArgMin() { + checkNumeric("NArgMin(2*x^2-3*x+5,x)", // + "{0.7499999999999998}"); + + checkNumeric("NArgMin((x*y-3)^2+1, {x,y})", // + "{1.0,3.0}"); + checkNumeric("NArgMin({x-2*y, x+y<= 1}, {x, y})", // + "{0.0,1.0}"); + } + + @Test + public void testNMaxValue() { + checkNumeric("NMaxValue(-2*x^2-3*x+5,x)", // + "6.125"); + checkNumeric("NMaxValue(1-(x*y-3)^2, {x, y})", // + "1.0"); + } + + @Test + public void testNMinValue() { + checkNumeric("NMinValue(2*x^2-3*x+5,x)", // + "3.875"); + + checkNumeric("NMinValue((x*y-3)^2+1, {x,y})", // + "1.0"); + checkNumeric("NMinValue({x-2*y, x+y<= 1}, {x, y})", // + "-2.0"); + } + + @Test + public void testNMaximize() { + checkNumeric("NMaximize(-x^4 - 3* x^2 + x, x)", // + "{0.08258881886826407,{x->0.16373996720676331}}"); + + check("NMaximize({-2*x+y-5, x+2*y<=6 && 3*x + 2*y <= 12 }, {x, y})", // + "{-2.0,{x->0.0,y->3.0}}"); + check("NMaximize({-x - y, 3*x + 2*y >= 7 && x + 2*y >= 6}, {x, y})", // + "{-3.25,{x->0.5,y->2.75}}"); + } + + @Test + public void testNMinimize() { + check("NMinimize({-5-2*x+y,x+2*y<=6&&3*x+2*y<=12},{x,y})", // + "{-13.0,{x->4.0,y->0.0}}"); + + check("NMinimize({-Sinc(x)-Sinc(y)}, {x, y})", // + "{-2.0,{x->0.0,y->0.0}}"); + + check("NMinimize(x^2 + y^2 + 2, {x,y})", // + "{2.0,{x->0.0,y->0.0}}"); + check("NMinimize({Sinc(x)+Sinc(y)}, {x, y})", // + "{-0.434467,{x->4.49341,y->4.49341}}"); + + checkNumeric("NMinimize({Sinc(x)+Sinc(y)}, {x, y})", // + "{-0.4344672564224433,{x->4.493409457896563,y->4.493409457738778}}"); + + checkNumeric("NMinimize(x^4 - 3*x^2 - x, x)", // + "{-3.513905038934788,{x->1.3008395656679863}}"); + + // TODO non-linear not supported + // check("NMinimize({x^2 - (y - 1)^2, x^2 + y^2 <= 4}, {x, y})", ""); + check("NMinimize({-2*y+x-5, x+2*y<=6 && 3*x + 2*y <= 12 }, {x, y})", // + "{-11.0,{x->0.0,y->3.0}}"); + check("NMinimize({-2*y+x-5, x+2*y<=6 && 3*x + 2*y <= 12 }, {x, y})", // + "{-11.0,{x->0.0,y->3.0}}"); + check("NMinimize({-2*x+y-5, x+2*y<=6 && 3*x + 2*y <= 12 }, {x, y})", // + "{-13.0,{x->4.0,y->0.0}}"); + check("NMinimize({x + 2*y, -5*x + y == 7 && x + y >= 26 && x >= 3 && y >= 4}, {x, y})", // + "{48.83333,{x->3.16667,y->22.83333}}"); + check("NMinimize({x + y, 3*x + 2*y >= 7 && x + 2*y >= 6 }, {x, y})", // + "{3.25,{x->0.5,y->2.75}}"); + } +}