How to make “cases” or “if, then” depending on the comparison operator used in Sage

I have some function that returns inequalities like

s= solve(x^(2)<4,x) 
s
[[x>-2, 2<x]]

I would like to be able to convert this to the string "] -2,2 [" for export to LaTeX.

How can I check which comparison operator is used, and then create a script with multiple scripts based on this? for example

 if s[1].operatorused== "<" 
  then do stuff
 if s[1].operatorused== "<=" 
  then do stuff

etc.

+4
source share
1 answer

You can access the operator through .operator()and perform your comparisons this way:

sage: s[0][0].operator()
<function operator.gt>
sage: s[0][1].operator()
<function operator.lt>
sage: s[0][0].operator() == operator.gt
True
sage: s[0][0].operator() == operator.lt
False

, , ( , > >=.)

+2

All Articles