ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 03 Sep 2021 13:56:00 +0200How to simplify expressionhttps://ask.sagemath.org/question/58806/how-to-simplify-expression/What are some ways to simplify the following expression?
(A <= B && B <= C) || (A <= D && D <= C) || (B <= A && A <= D && D <= C) || (D >= C && A <= B && B <= C ) || (B <= A && D >= C)
Thu, 02 Sep 2021 18:12:33 +0200https://ask.sagemath.org/question/58806/how-to-simplify-expression/Comment by Emmanuel Charpentier for <p>What are some ways to simplify the following expression?</p>
<pre><code>(A <= B && B <= C) || (A <= D && D <= C) || (B <= A && A <= D && D <= C) || (D >= C && A <= B && B <= C ) || (B <= A && D >= C)
</code></pre>
https://ask.sagemath.org/question/58806/how-to-simplify-expression/?comment=58822#post-id-58822From the [manual](https://doc.sagemath.org/html/en/reference/logic/sage/logic/propcalc.html) :
"Formulas consist of the following operators:
- & – and
- | – or
- ~ – not
- ^ – xor
- -> – if-then
- <-> – if and only if
Operators can be applied to variables that consist of a leading letter and trailing underscores and alphanumerics. Parentheses may be used to explicitly show order of operation."
Your code uses symbols not in the above list ; no wonder that it can't compile...
More important : "A>=B" is a predicative assertion, not a proposition. First-order predicate logic is a horse of another color...Fri, 03 Sep 2021 13:56:00 +0200https://ask.sagemath.org/question/58806/how-to-simplify-expression/?comment=58822#post-id-58822Comment by ortollj for <p>What are some ways to simplify the following expression?</p>
<pre><code>(A <= B && B <= C) || (A <= D && D <= C) || (B <= A && A <= D && D <= C) || (D >= C && A <= B && B <= C ) || (B <= A && D >= C)
</code></pre>
https://ask.sagemath.org/question/58806/how-to-simplify-expression/?comment=58817#post-id-58817to try to answer, I tried the code below (which must surely not be correct) but the kernel never stops !
log = SymbolicLogic()
s = log.statement("((A <= B) | (B <= C)) | ((A <= D) & (D <= C)) | ((B <= A) & ((A <= D) & (D <= C)) | ((D >= C) & (A <= B)) & ((B <= C ) | (B <= A) & (D >= C))")Fri, 03 Sep 2021 08:23:44 +0200https://ask.sagemath.org/question/58806/how-to-simplify-expression/?comment=58817#post-id-58817Answer by Emmanuel Charpentier for <p>What are some ways to simplify the following expression?</p>
<pre><code>(A <= B && B <= C) || (A <= D && D <= C) || (B <= A && A <= D && D <= C) || (D >= C && A <= B && B <= C ) || (B <= A && D >= C)
</code></pre>
https://ask.sagemath.org/question/58806/how-to-simplify-expression/?answer=58816#post-id-58816A couple of answer elements :
- Sage *per se* has not (yet) symbolic logical operators (but see [Trac#31911](https://trac.sagemath.org/ticket/31911)).
- Your (Mathematica ?) syntax is not Python : there is no such operators as `&&` or `||`...
With these limitations in mind, you can do :
sage: var("A, B, C, D")
(A, B, C, D)
sage: import sympy
sage: sympy.sympify('((A <= B) & (B <= C)) | ((A <= D) & (D <= C)) | ((B <= A) & (A <= D) & (D <= C)) | ((D >= C) & (A <= B) & (B <= C) ) | ((B <= A) & (D >= C))').simplify()
((A >= B) & (C <= D)) | ((C >= D) & (A <= D)) | ((A <= B) & (B <= C))
but this result cannot (yet) be translated in Sage.
There is some support for [propositional logic](https://doc.sagemath.org/html/en/reference/logic/index.html) in Sage, but these tools use string representation of the formulæ ; the translation to Sage symbolic expressions would require the addition of some support for symbolic logical functions (see the Trac cicket...).
FWIW :
sage: mathematica("FullSimplify[(A <= B && B <= C) || (A <= D && D <= C) || (B <= A && A <= D && D <= C) || (D >= C && A <= B && B <= C ) || (B <= A && D >= C)]")
(D >= C && B <= A) || (A <= B && B <= C) || (A <= D && D <= C)
sage: maxima_console()
;;; Loading #P"/usr/local/sage-9/local/lib/ecl-21.2.1/sb-bsd-sockets.fas"
;;; Loading #P"/usr/local/sage-9/local/lib/ecl-21.2.1/sockets.fas"
Maxima 5.45.0 https://maxima.sourceforge.io
using Lisp ECL 21.2.1
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
(%i1) simplify((A <= B and B <= C) or (A <= D and D <= C) or (B <= A and A <= D and D <= C) or (D >= C and A <= B and B <= C ) or (B <= A and D >= C));
(%o1) simplify(((A <= B) and (B <= C)) or ((A <= D) and (D <= C))
or ((B <= A) and (A <= D) and (D <= C))
or ((D >= C) and (A <= B) and (B <= C)) or ((B <= A) and (D >= C)))
HTH,
Fri, 03 Sep 2021 08:13:38 +0200https://ask.sagemath.org/question/58806/how-to-simplify-expression/?answer=58816#post-id-58816Answer by philipp7 for <p>What are some ways to simplify the following expression?</p>
<pre><code>(A <= B && B <= C) || (A <= D && D <= C) || (B <= A && A <= D && D <= C) || (D >= C && A <= B && B <= C ) || (B <= A && D >= C)
</code></pre>
https://ask.sagemath.org/question/58806/how-to-simplify-expression/?answer=58819#post-id-58819If we assume that all $A,B,C,D$ are real, we can use cylindrical algebraic decomposition (CAD) to simplify such an expression (in general, CAD can handle logical formulas involving polynomial (in)equalities). Sage has an interface to QEPCAD (QEPCAD itself, however, I believe is not shipped with sage, so has to be installed) which is one of the most popular implementations of CAD. Here is how QEPCAD simplifies your expression:
sage: from sage.interfaces.qepcad import qepcad_formula, qepcad, qformula
sage: var("A,B,C,D")
sage: qf = qepcad_formula
sage: expr = qf.or_([qf.and_([A<=B, B<=C]), qf.and_([A<=D, D<=C]), qf.and_([B<=A, A<=D, D<=C]), qf.and_([D>=C, A<=B, B<=C]), qf.and_([B<=A, D>=C])])
sage: qepcad(expr)
[ B - A >= 0 /\ C - B >= 0 ] \/ [ D - A >= 0 /\ D - C <= 0 ] \/ [ B - A <= 0 /\ D - C >= 0 ]Fri, 03 Sep 2021 09:27:47 +0200https://ask.sagemath.org/question/58806/how-to-simplify-expression/?answer=58819#post-id-58819