From 3d177597bc8e42230f731862a061933d5cf6cb98 Mon Sep 17 00:00:00 2001 From: axexlck Date: Sun, 3 Dec 2023 16:26:48 +0100 Subject: [PATCH] Create simple `IntervalData` objects with new methods: - reals - the Reals domain - emptySet - the empty interval - close - an interval including each boundary - open - an interval including neither boundary - lOpen - an interval not including the left boundary - rOpen - an interval not including the right boundary --- .../core/builtin/MinMaxFunctions.java | 42 +++++-- .../org/matheclipse/core/expression/F.java | 2 +- .../core/expression/IntervalDataSym.java | 113 +++++++++++++++--- 3 files changed, 132 insertions(+), 25 deletions(-) diff --git a/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/builtin/MinMaxFunctions.java b/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/builtin/MinMaxFunctions.java index 7643ffcf0c..b876c3d5b8 100644 --- a/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/builtin/MinMaxFunctions.java +++ b/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/builtin/MinMaxFunctions.java @@ -271,6 +271,33 @@ public IExpr visit3(IExpr head, IExpr arg1, IExpr arg2) { } } + + // public IExpr evaluate(final IAST ast, EvalEngine engine) { + // IExpr function = ast.arg1(); + // IExpr xExpr = ast.arg2(); + // IExpr yExpr = ast.arg3(); + // IBuiltInSymbol domain = S.Reals; + // try { + // if (xExpr.isSymbol() && yExpr.isSymbol()) { + // IAST constrained_interval = IntervalDataSym.reals(); + // if (function.isAST()) { + // IAST f = (IAST) function; + // for (int i = 1; i < f.size(); i++) { + // IExpr arg = f.get(i); + // if (arg.isPower()) { + // + // } else if (arg.isLog()) { + // + // } + // } + // } + // } + // } catch (RuntimeException rex) { + // rex.printStackTrace(); + // LOGGER.debug("FunctionRange.evaluate() failed", rex); + // } + // return F.NIL; + // } @Override public IExpr evaluate(final IAST ast, EvalEngine engine) { IExpr function = ast.arg1(); @@ -557,20 +584,15 @@ private IAST relationToInterval(IExpr expr, IBuiltInSymbol relationalSymbol, IEx int headID = temp.headID(); switch (headID) { case ID.Greater: - return F.IntervalData(// - F.List(rhs, S.Less, S.Less, F.CInfinity)); + return IntervalDataSym.open(rhs, F.CInfinity); case ID.GreaterEqual: - return F.IntervalData(// - F.List(rhs, S.LessEqual, S.Less, F.CInfinity)); + return IntervalDataSym.rOpen(rhs, F.CInfinity); case ID.Less: - return F.IntervalData(// - F.List(F.CNInfinity, S.Less, S.Less, rhs)); + return IntervalDataSym.open(F.CNInfinity, rhs); case ID.LessEqual: - return F.IntervalData(// - F.List(F.CNInfinity, S.Less, S.LessEqual, rhs)); + return IntervalDataSym.lOpen(F.CNInfinity, rhs); case ID.Equal: - return F.IntervalData(// - F.List(rhs, S.LessEqual, S.LessEqual, rhs)); + return IntervalDataSym.close(rhs, rhs); case ID.Unequal: return F.IntervalData(// F.List(F.CNInfinity, S.Less, S.Less, rhs), // diff --git a/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/expression/F.java b/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/expression/F.java index 5fbb8ccdbf..12c1cfcd08 100644 --- a/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/expression/F.java +++ b/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/expression/F.java @@ -5351,7 +5351,7 @@ public static IAST IntervalData(final IAST list) { *

* Intervals will be represented by objects with head {@link S#IntervalData} wrapped around a * sequence of quadruples of the form, e.g., {a,Less,LessEqual,b} representing the - * half open interval (a,b]. The empty interval is represented by + * half open interval (a,b]. The empty interval set is represented by * Interval(). * *

diff --git a/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/expression/IntervalDataSym.java b/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/expression/IntervalDataSym.java index df451aa676..7957ebcc7d 100644 --- a/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/expression/IntervalDataSym.java +++ b/symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/expression/IntervalDataSym.java @@ -19,7 +19,7 @@ *

* Intervals will be represented by objects with head {@link S#IntervalData} wrapped around a * sequence of quadruples of the form, e.g., {a,Less,LessEqual,b} representing the half - * open interval (a,b]. The empty interval is represented by + * open interval (a,b]. The empty interval set is represented by * IntervalData(). * *

@@ -119,6 +119,14 @@ private static Apfloat[] interval(Apfloat x) { return new Apfloat[] {h.nextDown(x), h.nextUp(x)}; } + /** + * Rewrite an interval in terms of inequalities and logic operators. + * + * @param andCopy + * @param interval + * @param variable + * @return + */ public static IExpr intervalToOr(IAST andCopy, IAST interval, IExpr variable) { IASTAppendable orAST = F.ast(S.Or, interval.argSize()); for (int i = 1; i < interval.size(); i++) { @@ -463,6 +471,88 @@ private static IAST normalizeArgument(final IExpr arg, final EvalEngine engine) arg); } + /** + * The {@link S#Reals} domain is represented by IntervalData({-oo, Less, Less, oo}). + * + * @return + */ + public static IAST reals() { + return F.IntervalData(// + F.List(F.CNInfinity, // + S.Less, // + S.Less, // + F.CInfinity)); + } + + /** + * The empty interval set is represented by IntervalData(). + * + * @return + */ + public static IAST emptySet() { + return F.IntervalData(); + } + + /** + * Return an interval including neither boundary. + * + * @param lhs left boundary + * @param rhs right boundary + * @return IntervalData({lhs, Less, Less, rhs})) + */ + public static IAST open(final IExpr lhs, final IExpr rhs) { + return F.IntervalData(// + F.List(lhs, // + S.Less, // + S.Less, // + rhs)); + } + + /** + * Return an interval not including the left boundary. + * + * @param lhs left boundary + * @param rhs right boundary + * @return IntervalData({lhs, Less, LessEqual, rhs})) + */ + public static IAST lOpen(final IExpr lhs, final IExpr rhs) { + return F.IntervalData(// + F.List(lhs, // + S.Less, // + S.LessEqual, // + rhs)); + } + + /** + * Return an interval not including the right boundary. + * + * @param lhs left boundary + * @param rhs right boundary + * @return IntervalData({lhs, LessEqual, Less, rhs})) + */ + public static IAST rOpen(final IExpr lhs, final IExpr rhs) { + return F.IntervalData(// + F.List(lhs, // + S.LessEqual, // + S.Less, // + rhs)); + } + + /** + * Return an interval including each boundary. + * + * @param lhs left boundary + * @param rhs right boundary + * @return IntervalData({lhs, LessEqual, LessEqual, rhs})) + */ + public static IAST close(final IExpr lhs, final IExpr rhs) { + return F.IntervalData(// + F.List(lhs, // + S.LessEqual, // + S.LessEqual, // + rhs)); + } + public static IAST notInRange(final IExpr arg) { return F.IntervalData(// F.List(F.CNInfinity, // @@ -531,27 +621,22 @@ private static IBuiltInSymbol precedenceUnion(IBuiltInSymbol s1, IBuiltInSymbol return (s1 == S.LessEqual || s2 == S.LessEqual) ? S.LessEqual : S.Less; } - public static IAST relationToInterval(int headID, IExpr rhs) { + public static IAST relationToInterval(int headID, IExpr value) { switch (headID) { case ID.Greater: - return F.IntervalData(// - F.List(rhs, S.Less, S.Less, F.CInfinity)); + return open(value, F.CInfinity); case ID.GreaterEqual: - return F.IntervalData(// - F.List(rhs, S.LessEqual, S.Less, F.CInfinity)); + return rOpen(value, F.CInfinity); case ID.Less: - return F.IntervalData(// - F.List(F.CNInfinity, S.Less, S.Less, rhs)); + return open(F.CNInfinity, value); case ID.LessEqual: - return F.IntervalData(// - F.List(F.CNInfinity, S.Less, S.LessEqual, rhs)); + return lOpen(F.CNInfinity, value); case ID.Equal: - return F.IntervalData(// - F.List(rhs, S.LessEqual, S.LessEqual, rhs)); + return close(value, value); case ID.Unequal: return F.IntervalData(// - F.List(F.CNInfinity, S.Less, S.Less, rhs), // - F.List(rhs, S.Less, S.Less, F.CInfinity)); + F.List(F.CNInfinity, S.Less, S.Less, value), // + F.List(value, S.Less, S.Less, F.CInfinity)); } throw new ArgumentTypeStopException("Not implemented"); }