Opened 10 years ago

# Translate maxima's if() function to Sage's cases()

Reported by: Owned by: Volker Braun Burcin Erocal major sage-8.2 symbolics Marcelo Forets N/A

### Description (last modified by Ralf Stephan)

```sage: var('r theta psi x y z')
(r, theta, psi, x, y, z)
sage: (r,theta,psi,x,y,z)
(r, theta, psi, x, y, z)
sage: e1 = r == +sqrt(x^2+y^2+z^2)
sage: e2 = theta == arccos(z/sqrt(x^2+y^2+z^2))
sage: e3 = psi == arctan(y/x)
sage: solve([e1,e2,e3],x,y,z)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/home/vbraun/opt/sage-5.5.rc0/devel/sage-main/<ipython console> in <module>()

/home/vbraun/opt/sage-5.5.rc0/local/lib/python2.7/site-packages/sage/symbolic/relation.pyc in solve(f, *args, **kwds)
751             s = []
752
--> 753     sol_list = string_to_list_of_solutions(repr(s))
754
755     # Relaxed form suggested by Mike Hansen (#8553):

/home/vbraun/opt/sage-5.5.rc0/local/lib/python2.7/site-packages/sage/symbolic/relation.pyc in string_to_list_of_solutions(s)
455     from sage.structure.sequence import Sequence
456     from sage.calculus.calculus import symbolic_expression_from_maxima_string
--> 457     v = symbolic_expression_from_maxima_string(s, equals_sub=True)
458     return Sequence(v, universe=Objects(), cr_str=True)
459

/home/vbraun/opt/sage-5.5.rc0/local/lib/python2.7/site-packages/sage/calculus/calculus.pyc in symbolic_expression_from_maxima_string(x, equals_sub, maxima)
1789         return symbolic_expression_from_string(s, syms, accept_sequence=True)
1790     except SyntaxError:
-> 1791         raise TypeError, "unable to make sense of Maxima expression '%s' in Sage"%s
1792     finally:
1793         is_simplified = False

TypeError: unable to make sense of Maxima expression '[If(and(-pi/2<parg(-r),-pi/2<parg(r),parg(-r)<==pi/2,parg(r)<==pi/2,-r*sqrt(1/(tan(psi)^2+1)-cos(theta)^2/(tan(psi)^2+1))!=0,sqrt(r^2*(1/(tan(psi)^2+1)-cos(theta)^2/(tan(psi)^2+1))+r^2*cos(theta)^2+tan(psi)^2*r^2*(1-cos(theta))*(cos(theta)+1)/(tan(psi)^2+1))!=0),[x==-r*sqrt(1/(tan(psi)^2+1)-cos(theta)^2/(tan(psi)^2+1)),y==-tan(psi)*r*sqrt(1-cos(theta))*sqrt(cos(theta)+1)/sqrt(tan(psi)^2+1),z==-r*cos(theta)],union()),If(and(-pi/2<parg(-r),-pi/2<parg(r),parg(-r)<==pi/2,parg(r)<==pi/2,r*sqrt(1/(tan(psi)^2+1)-cos(theta)^2/(tan(psi)^2+1))!=0,sqrt(r^2*(1/(tan(psi)^2+1)-cos(theta)^2/(tan(psi)^2+1))+r^2*cos(theta)^2+tan(psi)^2*r^2*(1-cos(theta))*(cos(theta)+1)/(tan(psi)^2+1))!=0),[x==r*sqrt(1/(tan(psi)^2+1)-cos(theta)^2/(tan(psi)^2+1)),y==tan(psi)*r*sqrt(1-cos(theta))*sqrt(cos(theta)+1)/sqrt(tan(psi)^2+1),z==-r*cos(theta)],union()),If(and(-pi/2<parg(r),parg(r)<==pi/2,-r*sqrt(1/(tan(psi)^2+1)-cos(theta)^2/(tan(psi)^2+1))!=0,sqrt(r^2*(1/(tan(psi)^2+1)-cos(theta)^2/(tan(psi)^2+1))+r^2*cos(theta)^2+tan(psi)^2*r^2*(1-cos(theta))*(cos(theta)+1)/(tan(psi)^2+1))!=0),[x==-r*sqrt(1/(tan(psi)^2+1)-cos(theta)^2/(tan(psi)^2+1)),y==-tan(psi)*r*sqrt(1-cos(theta))*sqrt(cos(theta)+1)/sqrt(tan(psi)^2+1),z==r*cos(theta)],union()),If(and(-pi/2<parg(r),parg(r)<==pi/2,r*sqrt(1/(tan(psi)^2+1)-cos(theta)^2/(tan(psi)^2+1))!=0,sqrt(r^2*(1/(tan(psi)^2+1)-cos(theta)^2/(tan(psi)^2+1))+r^2*cos(theta)^2+tan(psi)^2*r^2*(1-cos(theta))*(cos(theta)+1)/(tan(psi)^2+1))!=0),[x==r*sqrt(1/(tan(psi)^2+1)-cos(theta)^2/(tan(psi)^2+1)),y==tan(psi)*r*sqrt(1-cos(theta))*sqrt(cos(theta)+1)/sqrt(tan(psi)^2+1),z==r*cos(theta)],union())]' in Sage
```

### comment:1 Changed 10 years ago by Karl-Dieter Crisman

Just for the record, it's objecting to the "if". The "in Sage" is just what we append. Also, the "union" in the spot they're in will not get properly parsed, though we do handle that in certain easy circumstances. I feel like we may already have a ticket for this, even, but it's not going to be easy to fix unless we bring in our own "if", and I don't know that we want to do that.

### comment:2 Changed 9 years ago by Jeroen Demeyer

Milestone: sage-5.11 → sage-5.12

### comment:3 Changed 9 years ago by For batch modifications

Milestone: sage-6.1 → sage-6.2

### comment:4 Changed 8 years ago by For batch modifications

Milestone: sage-6.2 → sage-6.3

### comment:5 Changed 8 years ago by For batch modifications

Milestone: sage-6.3 → sage-6.4

### comment:6 Changed 7 years ago by Ralf Stephan

Related to #16653, i.e. maybe output a list of pairs (condition, result).

### comment:7 Changed 5 years ago by Ralf Stephan

Description: modified (diff) sage-6.4 → sage-8.2 maxima solve output parser insufficient → Translate maxima's if() function to Sage's cases()

### comment:8 Changed 5 years ago by Marcelo Forets

Cc: Marcelo Forets added

### comment:9 Changed 5 years ago by Karl-Dieter Crisman

Another example at #24800:

```var('r2 si co r12 r22 r32 d32')
eq1 = r12==r2*d32*(1-si*(co+sqrt(3*(1-co*co)))/2)/2
eq2 = r22==r2*d32*(1-si*(co-sqrt(3*(1-co*co)))/2)/2
eq3 = r32==r2*d32*(1+si*co)/2
solve([eq1,eq2,eq3],r2,si,co)
```

HT to rws for noticing that was a dup.

Note: See TracTickets for help on using tickets.