Dear Nils, Thank you for this hindsight full response. A couple remarks below :
Le jeudi 6 mai 2021 à 08:06:18 UTC+2, Nils Bruin a écrit : > "and" and "or" in python are not logical operators -- they are flow > control for expressions; much like the " ... if ... else ..." expression > is. Their behaviour cannot be overloaded. The bitwise operators "&" and "|" > would be candidates for overloading. > > Concerning the translation from maxima to sage, the string parser may be > difficult that way. I'm not so sure it can do smart things with infix. > it may do ; it already does for the arithmetic operators : sage: ME=maxima sage: c=ME("a^b") sage: c.parent() Maxima sage: c.sage() a^b sage: [maxima.part(c,u) for u in (0..len(c))] ["^", a, b] So maybe we can use whatever `maxima` does for "^" as a cookie-cutter for logical functions ? > The binary-based interface would be very easy to adapt, though: > > sage: M=maxima_calculus > sage: c=M('a and b') > sage: c.ecl() > <ECL: ((MAND SIMP) $A $B)> > > Adapting the s-expr based translator would be very straightforward, since > (as you can see) the and appears in exactly the same way other functions > would. Example: > > sage: c=M('sin(a) + b') > sage: c.ecl() > <ECL: ((MPLUS SIMP) ((%SIN SIMP) $A) $B)> > Agreed so far. > The challenge would be that sage calculus never fully completed the > transition to the s-expr translation. > That is a *different* problem. If I understand you correctly, you would like to complete the transition to the library interface and essentially kill the pexpect one, thus dispensing with updating it for logical operators ? This would leave in the dark all code previously written using pexpect-specific features of the Maxima interface. Not so nice... > It's much more efficient and robust, but the strings-based translation was > "good enough" for anyone to bother completing the transition. A few > operations (integrals and limits if I'm not mistaken) do use this layer, > but other don't (I think simplifications mostly don't). Perhaps this is > enough motivation to complete the transition, or at least to the degree > needed for boolean expressions? > I agree that such cleanup may be beneficial. But IMHO it won't be neither fast nor clean... A distinct task ? The relevant conversion functions are > > sage.interfaces.maxima_lib.sr_to_max > sage.interfaces.maxima_lib.max_to_sr > > They use some dictionaries that would need to be preseeded with > appropriate translation values for MAND and MOR. You'd furthermore have to > search for occurrences of them in the library to see where they are already > used. > Thank you for the information. Sincerely, > > > On Wednesday, May 5, 2021 at 2:45:50 PM UTC-7 emanuel.c...@gmail.com > wrote: > >> At least Sympy, Maxima and Mathematica can treat boolean expressions as >> other expressions. Maxima : : >> >> (%i1) display2d:false; >> >> (%o1) false >> (%i2) foo: a and b; >> >> (%o2) a and b >> (%i3) bar: subst([a=(%pi>3), b=(%pi<4)], foo); >> >> (%o3) %pi > 3 and %pi < 4 >> (%i4) is(bar); >> >> (%o4) unknown >> (%i5) bar, ev; >> >> (%o5) true >> >> Sympy: >> >> >>> a, b, x=symbols("a, b, x") >> >>> foo.subs({a:pi>3, b:x<4}) >> x < 4 >> >>> from sympy import * >> >>> a, b, x=symbols("a, b, x") >> >>> foo=And(a, b) ; foo >> a & b >> >>> foo.subs({a:pi>3, b:x<4}) >> x < 4 >> >>> foo.subs({a:pi>3, b:pi<4}) >> True >> >> Thanks to Sage’s and and or “lazy” operators and the interpretation on >> anything not zero as True, Sage’s expressions containing logical >> operators gives surprising results : >> >> sage: var("a, b") >> (a, b) >> sage: foo = a and b ; foo >> b >> sage: (x>3) and (x<4) >> x > 3 >> sage: (pi>3) and (x<4) >> x < 4 >> sage: (pi>3) and (pi<4) >> pi < 4 >> >> bool((pi>3) and (pi<4)) >> True >> >> As a consequence, we have no way to translate in sage the boolean >> expressions sometimes returned by Sympy or Maxima (e. g. Piecewise >> functions returned by Sympy for some integrations. It ois easy to slap >> _sage_ methods to Sympy’s logical functions in terms of Python >> expressions, but : >> >> - their interpretation won't be what us expected, and >> >> - these expressions won't translate to Maxima in a useful way : >> >> sage: "print(%s)"%(a and b) >> 'print(b)' >> sage: maxima("print(%s)"%(a and b)) >> b >> >> Another (better) solution is to wrap, for example, Sympy’s logical >> functions in symbolic function, and define suitanle conversion functions. >> This is easy for Sage<-> sympy translations ; Sage -> maxima translation is >> also manageable. >> >> Where I’m stuck is the Maxima->Sage translation (necessary given the >> importance of Maxima for a lot of our symbolics) : the current interface >> isn’”t useable : >> >> sage: maxima("a and b") >> aandb >> sage: maxima("a and b").sage() >> aandb >> sage: maxima("a and b").sage().parent() >> Symbolic Ring >> sage: maxima("a and b").sage().variables() >> (aandb,) >> sage: maxima("a and b").sage().operator() is None >> True >> sage: maxima("a and b").sage().operands() >> [] >> >> or worse : >> >> sage: maxima("subst([a=(%pi>3), b=(%pi<4)], a and b)") >> %pi>3and%pi<4 >> sage: maxima("subst([a=(%pi>3), b=(%pi<4)], a and b)").sage() >> --------------------------------------------------------------------------- >> SyntaxError Traceback (most recent call last) >> >> [ Snip... ] >> >> TypeError: unable to make sense of Maxima expression 'pi>3andpi<4' in Sage >> >> Maxima’s logical operators are not recognized as such and are “pasted” >> along their arguments. >> >> What should be done would be to get them parsed, and translated a Sages >> logical function calls. An alternative would be to create “symbolic logical >> operators” at least for the classes relevant asossible Maxima returns, but, >> as far as I know, this is not possible in Python. >> >> In both cases, this requires recognizing and treating specially Maxima >> logical operators. The code of the current Maxima interfaces stymied me : I >> have been unable to identify the relevant parts. Traceing test >> expressions failed, since the cricial parts probably written in Cython, >> aren’t accessible to pdb. >> >> Any hint or criticism well received. >> >> > -- You received this message because you are subscribed to the Google Groups "sage-devel" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sage-devel/aef3e942-1c3b-42f6-b5ca-b22893e107c5n%40googlegroups.com.