Skip to content

Commit

Permalink
Create simple IntervalData objects with new methods:
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
axkr committed Dec 3, 2023
1 parent dbbb0a3 commit 3d17759
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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), //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5351,7 +5351,7 @@ public static IAST IntervalData(final IAST list) {
* <p>
* Intervals will be represented by objects with head {@link S#IntervalData} wrapped around a
* sequence of quadruples of the form, e.g., <code>{a,Less,LessEqual,b}</code> representing the
* half open interval <code>(a,b]</code>. The empty interval is represented by
* half open interval <code>(a,b]</code>. The empty interval set is represented by
* <code>Interval()</code>.
*
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* <p>
* Intervals will be represented by objects with head {@link S#IntervalData} wrapped around a
* sequence of quadruples of the form, e.g., <code>{a,Less,LessEqual,b}</code> representing the half
* open interval <code>(a,b]</code>. The empty interval is represented by
* open interval <code>(a,b]</code>. The empty interval set is represented by
* <code>IntervalData()</code>.
*
* <p>
Expand Down Expand Up @@ -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++) {
Expand Down Expand Up @@ -463,6 +471,88 @@ private static IAST normalizeArgument(final IExpr arg, final EvalEngine engine)
arg);
}

/**
* The {@link S#Reals} domain is represented by <code>IntervalData({-oo, Less, Less, oo})</code>.
*
* @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 <code>IntervalData()</code>.
*
* @return
*/
public static IAST emptySet() {
return F.IntervalData();
}

/**
* Return an interval including neither boundary.
*
* @param lhs left boundary
* @param rhs right boundary
* @return <code>IntervalData({lhs, Less, Less, rhs}))</code>
*/
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 <code>IntervalData({lhs, Less, LessEqual, rhs}))</code>
*/
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 <code>IntervalData({lhs, LessEqual, Less, rhs}))</code>
*/
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 <code>IntervalData({lhs, LessEqual, LessEqual, rhs}))</code>
*/
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, //
Expand Down Expand Up @@ -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");
}
Expand Down

0 comments on commit 3d17759

Please sign in to comment.