Ask Your Question
1

Error with solve: unable to make sense of Maxima expression

asked 2023-06-17 12:04:16 +0100

Periodic_1_6 gravatar image

updated 2023-06-18 12:17:33 +0100

slelievre gravatar image

Why does sagemath 9.2 give me an error ?

TypeError: unable to make sense of Maxima expression

import time
Start_Time = time.time()
var('N p x y c M H W V')

eq0 = N-187 == 0

eq1 = (sqrt((16*c^4*N+24*c^4+c^2*M+c^2-2*c^2*sqrt(24*c^2+M))/(c^4))-8)/16 - x == 0
eq2 = (sqrt((16*c^4*N+24*c^4+c^2*H+c^2+2*c^2*sqrt(24*c^2+H))/(c^4))-8)/16 - x ==0
eq3 = (4*x+2)^2-(2*y-1)^2 - N == 0
eq4 = M*(8*y^2-8*y-1) - (8*W^2-8*W-1) == 0
eq5 = H*(8*y^2-8*y-1) - (8*V^2-8*V-1) == 0
eq6 = (W-V) - (2*y-1) ==0
eq7 = 4*x+1-2*(y-1)-p == 0

eq8 = 1 - c == 0

solutions = solve([eq0,eq1,eq2,eq3,eq4,eq5,eq6,eq7,eq8],N,p,x,y,c,M,H,W,V)
sol = solutions 
Execution_Time = time.time() - Start_Time
print (Execution_Time)
print(sol)
edit retag flag offensive close merge delete

Comments

Version 9.2 is way outdated. Install the latest version 10.0.

FrédéricC gravatar imageFrédéricC ( 2023-06-17 13:08:03 +0100 )edit

Could you please post the solutions?

Periodic_1_6 gravatar imagePeriodic_1_6 ( 2023-06-17 13:11:03 +0100 )edit

Same

TypeError: unable to make sense of Maxima expression '[if((-pi/2 < parg(c58080)) and (-pi/2 < parg(c58080+2)) and (-pi/2 < parg(-sqrt(c58080^2+2*c58080+2993))) and (parg(c58080) <== pi/2) and (parg(c58080+2) <== pi/2) and (parg(-sqrt(c58080^2+2*c58080+2993)) <== pi/2),[_SAGE_VAR_H == c58080^2-24,_SAGE_VAR_M == c58080^2+4*c58080-20,_SAGE_VAR_N == 187,_SAGE_VAR_V == (c58080^2+c58080-20)/8,_SAGE_VAR_W == (c58080^2+3*c58080-18)/8,_SAGE_VAR_c == 1,_SAGE_VAR_p == ((-sqrt(c58080^2+2*c58080+2993))-c58080-1)/4,_SAGE_VAR_x == ((-sqrt(c58080^2+2*c58080+2993))-8)/16,_SAGE_VAR_y == (c58080+5)/8],union()),if((-pi/2 < parg(c58081)) and (-pi/2 < parg(c58081+2)) and (-pi/2 < parg(c58081^2+2*c58081+2993)/2) and (parg(c58081) <== pi/2) and (parg(c58081+2) <== pi/2) and (parg(c58081^2+2*c58081+2993)
FrédéricC gravatar imageFrédéricC ( 2023-06-17 13:17:44 +0100 )edit

But Is version 10.0 ?

Periodic_1_6 gravatar imagePeriodic_1_6 ( 2023-06-17 13:25:44 +0100 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2023-06-17 21:59:26 +0100

Emmanuel Charpentier gravatar image

updated 2023-06-18 12:16:36 +0100

slelievre gravatar image

Maxima's solver (which Sage defaults to) solves this system, but Sage is currently unable to translate it.

After running:

Vars=var('N p x y c M H W V')
eq0 = N-187 == 0
eq1 = (sqrt((16*c^4*N+24*c^4+c^2*M+c^2-2*c^2*sqrt(24*c^2+M))/(c^4))-8)/16 - x == 0
eq2 = (sqrt((16*c^4*N+24*c^4+c^2*H+c^2+2*c^2*sqrt(24*c^2+H))/(c^4))-8)/16 - x ==0
eq3 = (4*x+2)^2-(2*y-1)^2 - N == 0
eq4 = M*(8*y^2-8*y-1) - (8*W^2-8*W-1) == 0
eq5 = H*(8*y^2-8*y-1) - (8*V^2-8*V-1) == 0
eq6 = (W-V) - (2*y-1) ==0
eq7 = 4*x+1-2*(y-1)-p == 0
eq8 = 1 - c == 0
Sys = [eq0,eq1,eq2,eq3,eq4,eq5,eq6,eq7,eq8]

one can solve the system in Maxima:

sage: %time foo=maxima_calculus("Sol:to_poly_solve(%s, %s);"%tuple(map(lambda u:repr(maxima(u)), (Sys, Vars))))
CPU times: user 2min 42s, sys: 1.05 s, total: 2min 43s
Wall time: 1min 58s

This answer is complex:

sage: maxima_calculus("part(Sol,0);")
%union

It is a set of many solutions. Examine the first:

sage: maxima_calculus("part(Sol,1);")
%if((-%pi/2 < parg(%c2423837)) %and (-%pi/2 < parg(%c2423837+2)) %and (-%pi/2 < parg(-sqrt(%c2423837^2+2*%c2423837+2993))) %and (parg(%c2423837) <= %pi/2) %and (parg(%c2423837+2) <= %pi/2) %and (parg(-sqrt(%c2423837^2+2*%c2423837+2993)) <= %pi/2),[_SAGE_VAR_H = %c2423837^2-24,_SAGE_VAR_M = %c2423837^2+4*%c2423837-20,_SAGE_VAR_N = 187,_SAGE_VAR_V = (%c2423837^2+%c2423837-20)/8,_SAGE_VAR_W = (%c2423837^2+3*%c2423837-18)/8,_SAGE_VAR_c = 1,_SAGE_VAR_p = -(sqrt(%c2423837^2+2*%c2423837+2993)+%c2423837+1)/4,_SAGE_VAR_x = -(sqrt(%c2423837^2+2*%c2423837+2993)+8)/16,_SAGE_VAR_y = (%c2423837+5)/8],%union())

Two parts: a condition and a solution.

The condition:

sage: maxima_calculus("part(Sol,1,1);")
(-%pi/2 < parg(%c2423837)) %and (-%pi/2 < parg(%c2423837+2)) %and (-%pi/2 < parg(-sqrt(%c2423837^2+2*%c2423837+2993))) %and (parg(%c2423837) <= %pi/2) %and (parg(%c2423837+2) <= %pi/2) %and (parg(-sqrt(%c2423837^2+2*%c2423837+2993)) <= %pi/2)

The solution:

sage: maxima_calculus("part(Sol,1,2);")
[_SAGE_VAR_H = %c2423837^2-24,_SAGE_VAR_M = %c2423837^2+4*%c2423837-20,_SAGE_VAR_N = 187,_SAGE_VAR_V = (%c2423837^2+%c2423837-20)/8,_SAGE_VAR_W = (%c2423837^2+3*%c2423837-18)/8,_SAGE_VAR_c = 1,_SAGE_VAR_p = -(sqrt(%c2423837^2+2*%c2423837+2993)+%c2423837+1)/4,_SAGE_VAR_x = -(sqrt(%c2423837^2+2*%c2423837+2993)+8)/16,_SAGE_VAR_y = (%c2423837+5)/8]

This solution can be translated by Sage:

sage: maxima_calculus("part(Sol,1,2);").sage()
[H == c2423837^2 - 24,
 M == c2423837^2 + 4*c2423837 - 20,
 N == 187,
 V == 1/8*c2423837^2 + 1/8*c2423837 - 5/2,
 W == 1/8*c2423837^2 + 3/8*c2423837 - 9/4,
 c == 1,
 p == -1/4*c2423837 - 1/4*sqrt(c2423837^2 + 2*c2423837 + 2993) - 1/4,
 x == -1/16*sqrt(c2423837^2 + 2*c2423837 + 2993) - 1/2,
 y == 1/8*c2423837 + 5/8]

The condition cannot:

sage: maxima_calculus("part(Sol,1,1);").sage()
---------------------------------------------------------------------------
SyntaxError                               Traceback (most recent call last)
File /usr/local/sage-10/src/sage/calculus/calculus.py:2403, in symbolic_expression_from_maxima_string(x, equals_sub, maxima)
   2402     SRM_parser._callable_constructor().set_names(function_syms)
-> 2403     return SRM_parser.parse_sequence(s)
   2404 except SyntaxError:

File /usr/local/sage-10/src/sage/misc/parser.pyx:571, in sage.misc.parser.Parser.parse_sequence()
    570 
--> 571     cpdef parse_sequence(self, s):
    572         """

File /usr/local/sage-10/src/sage/misc/parser.pyx:587, in sage.misc.parser.Parser.parse_sequence()
    586 cdef Tokenizer tokens = Tokenizer(s)
--> 587 all = self.p_sequence(tokens)
    588 if tokens.next() != EOS:

File /usr/local/sage-10/src/sage/misc/parser.pyx:653, in sage.misc.parser.Parser.p_sequence()
    652 elif token == c'(':
--> 653     obj = self.p_tuple(tokens)
    654 elif token == EOS:

File /usr/local/sage-10/src/sage/misc/parser.pyx:721, in sage.misc.parser.Parser.p_tuple()
    720 tokens.reset(start)
--> 721 return self.p_eqn(tokens)
    722 

File /usr/local/sage-10/src/sage/misc/parser.pyx:750, in sage.misc.parser.Parser.p_eqn()
    749 """
--> 750 lhs = self.p_expr(tokens)
    751 cdef int op = tokens.next()

File /usr/local/sage-10/src/sage/misc/parser.pyx:790, in sage.misc.parser.Parser.p_expr()
    789 cdef int op
--> 790 operand1 = self.p_term(tokens)
    791 op = tokens.next()

File /usr/local/sage-10/src/sage/misc/parser.pyx:824, in sage.misc.parser.Parser.p_term()
    823 cdef int op
--> 824 operand1 = self.p_factor(tokens)
    825 op = tokens.next()

File /usr/local/sage-10/src/sage/misc/parser.pyx:867, in sage.misc.parser.Parser.p_factor()
    866 tokens.backtrack()
--> 867 return self.p_power(tokens)
    868 

File /usr/local/sage-10/src/sage/misc/parser.pyx:895, in sage.misc.parser.Parser.p_power()
    894 """
--> 895 operand1 = self.p_atom(tokens)
    896 cdef int token = tokens.next()

File /usr/local/sage-10/src/sage/misc/parser.pyx:962, in sage.misc.parser.Parser.p_atom()
    961 if token != c')':
--> 962     self.parse_error(tokens, "Mismatched parentheses")
    963 return expr

File /usr/local/sage-10/src/sage/misc/parser.pyx:1040, in sage.misc.parser.Parser.parse_error()
   1039 cdef parse_error(self, Tokenizer tokens, msg="Malformed expression"):
-> 1040     raise SyntaxError(msg, tokens.s, tokens.pos)
   1041 

SyntaxError: Mismatched parentheses

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
Cell In [74], line 1
----> 1 maxima_calculus("part(Sol,1,1);").sage()

File /usr/local/sage-10/src/sage/interfaces/interface.py:1121, in InterfaceElement.sage(self, *args, **kwds)
   1102 def sage(self, *args, **kwds):
   1103     """
   1104     Attempt to return a Sage version of this object.
   1105 
   (...)
   1119         [0 0]
   1120     """
-> 1121     return self._sage_(*args, **kwds)

File /usr/local/sage-10/src/sage/interfaces/maxima_abstract.py:1254, in MaximaAbstractElement._sage_(self)
   1199 """
   1200 Attempt to make a native Sage object out of this Maxima object.
   1201 This is useful for automatic coercions in addition to other
   (...)
   1251     (True, False)
   1252 """
   1253 import sage.calculus.calculus as calculus
-> 1254 return calculus.symbolic_expression_from_maxima_string(self.name(),
   1255         maxima=self.parent())

File /usr/local/sage-10/src/sage/calculus/calculus.py:2405, in symbolic_expression_from_maxima_string(x, equals_sub, maxima)
   2403     return SRM_parser.parse_sequence(s)
   2404 except SyntaxError:
-> 2405     raise TypeError("unable to make sense of Maxima expression '%s' in Sage" % s)
   2406 finally:
   2407     is_simplified = False

TypeError: unable to make sense of Maxima expression '(-pi/2 < parg(c2423837)) and (-pi/2 < parg(c2423837+2)) and (-pi/2 < parg(-sqrt(c2423837^2+2*c2423837+2993))) and (parg(c2423837) <= pi/2) and (parg(c2423837+2) <= pi/2) and (parg(-sqrt(c2423837^2+2*c2423837+2993)) <= pi/2)' in Sage

Note that the elements of the condition can be translated:

sage: maxima_calculus("length(part(Sol,1,1));")
6
sage: [maxima_calculus("part(Sol,1,1,%d);"%u).sage() for u in (1..6)]
[-1/2*pi < parg(c2423837),
 -1/2*pi < parg(c2423837 + 2),
 -1/2*pi < parg(-sqrt(c2423837^2 + 2*c2423837 + 2993)),
 parg(c2423837) <= 1/2*pi,
 parg(c2423837 + 2) <= 1/2*pi,
 parg(-sqrt(c2423837^2 + 2*c2423837 + 2993)) <= 1/2*pi]

but that the logical expression using Maxima's "and" cannot:

sage: maxima_calculus("x>0 and y<1")
x > 0 and y < 1
sage: maxima_calculus("x>0 and y<1").sage()
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File /usr/local/sage-10/src/sage/misc/parser.pyx:1092, in sage.misc.parser.LookupNameMaker.__call__()
   1091 try:
-> 1092     return self.names[name]
   1093 except KeyError:

KeyError: 'and'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
File /usr/local/sage-10/src/sage/calculus/calculus.py:2494, in _find_var(name, interface)
   2493 try:
-> 2494     return SR(sage.all.__dict__[name])
   2495 except (KeyError, TypeError):

KeyError: 'and'

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
Cell In [95], line 1
----> 1 maxima_calculus("x>0 and y<1").sage()

File /usr/local/sage-10/src/sage/interfaces/interface.py:1121, in InterfaceElement.sage(self, *args, **kwds)
   1102 def sage(self, *args, **kwds):
   1103     """
   1104     Attempt to return a Sage version of this object.
   1105 
   (...)
   1119         [0 0]
   1120     """
-> 1121     return self._sage_(*args, **kwds)

File /usr/local/sage-10/src/sage/interfaces/maxima_abstract.py:1254, in MaximaAbstractElement._sage_(self)
   1199 """
   1200 Attempt to make a native Sage object out of this Maxima object.
   1201 This is useful for automatic coercions in addition to other
   (...)
   1251     (True, False)
   1252 """
   1253 import sage.calculus.calculus as calculus
-> 1254 return calculus.symbolic_expression_from_maxima_string(self.name(),
   1255         maxima=self.parent())

File /usr/local/sage-10/src/sage/calculus/calculus.py:2403, in symbolic_expression_from_maxima_string(x, equals_sub, maxima)
   2401     SRM_parser._variable_constructor().set_names(var_syms)
   2402     SRM_parser._callable_constructor().set_names(function_syms)
-> 2403     return SRM_parser.parse_sequence(s)
   2404 except SyntaxError:
   2405     raise TypeError("unable to make sense of Maxima expression '%s' in Sage" % s)

File /usr/local/sage-10/src/sage/misc/parser.pyx:571, in sage.misc.parser.Parser.parse_sequence()
    569     return expr
    570 
--> 571 cpdef parse_sequence(self, s):
    572     """
    573     Parse a (possibly nested) set of lists and tuples.

File /usr/local/sage-10/src/sage/misc/parser.pyx:587, in sage.misc.parser.Parser.parse_sequence()
    585 """
    586 cdef Tokenizer tokens = Tokenizer(s)
--> 587 all = self.p_sequence(tokens)
    588 if tokens.next() != EOS:
    589     self.parse_error(tokens)

File /usr/local/sage-10/src/sage/misc/parser.pyx:660, in sage.misc.parser.Parser.p_sequence()
    658     return all
    659 else:
--> 660     obj = self.p_eqn(tokens)
    661 PyList_Append(all, obj)
    662 token = tokens.next()

File /usr/local/sage-10/src/sage/misc/parser.pyx:761, in sage.misc.parser.Parser.p_eqn()
    759     return lhs <= self.p_expr(tokens)
    760 elif op == c'>':
--> 761     return lhs > self.p_expr(tokens)
    762 elif op == GREATER_EQ:
    763     return lhs >= self.p_expr(tokens)

File /usr/local/sage-10/src/sage/misc/parser.pyx:790, in sage.misc.parser.Parser.p_expr()
    788 # Note: this is left-recursive, so we can't just recurse
    789 cdef int op
--> 790 operand1 = self.p_term(tokens)
    791 op = tokens.next()
    792 while op == c'+' or op == c'-':

File /usr/local/sage-10/src/sage/misc/parser.pyx:830, in sage.misc.parser.Parser.p_term()
    828     tokens.backtrack()
    829 while op == c'*' or op == c'/':
--> 830     operand2 = self.p_factor(tokens)
    831     if op == c'*':
    832         operand1 = operand1 * operand2

File /usr/local/sage-10/src/sage/misc/parser.pyx:867, in sage.misc.parser.Parser.p_factor()
    865         else:
    866             tokens.backtrack()
--> 867             return self.p_power(tokens)
    868 
    869 # power ::=  (atom | atom!) ^ factor | atom | atom!

File /usr/local/sage-10/src/sage/misc/parser.pyx:895, in sage.misc.parser.Parser.p_power()
    893 
    894         """
--> 895         operand1 = self.p_atom(tokens)
    896         cdef int token = tokens.next()
    897         if token == c'^':

File /usr/local/sage-10/src/sage/misc/parser.pyx:957, in sage.misc.parser.Parser.p_atom()
    955     else:
    956         tokens.backtrack()
--> 957         return self.variable_constructor(name)
    958 elif token == c'(':
    959     expr = self.p_expr(tokens)

File /usr/local/sage-10/src/sage/misc/parser.pyx:1095, in sage.misc.parser.LookupNameMaker.__call__()
   1093 except KeyError:
   1094     if self.fallback is not None:
-> 1095         return self.fallback(name)
   1096     raise NameError(f"Unknown variable: '{name}'")

File /usr/local/sage-10/src/sage/calculus/calculus.py:2597, in <lambda>(x)
   2592     parser._callable_constructor().set_names({k[0]: v for k, v in syms.items()
   2593                                               if _is_function(v)})
   2594     return parse_func(s)
-> 2597 parser_make_Mvar = LookupNameMaker({}, fallback=lambda x: _find_var(x, interface='maxima'))
   2599 SRM_parser = Parser(make_int=lambda x: SR(Integer(x)),
   2600                     make_float=lambda x: SR(RealDoubleElement(x)),
   2601                     make_var=parser_make_Mvar,
   2602                     make_function=parser_make_function)
   2604 SR_parser_giac = Parser(make_int=lambda x: SR(Integer(x)),
   2605                         make_float=lambda x: SR(create_RealNumber(x)),
   2606                         make_var=LookupNameMaker({}, fallback=lambda x: _find_var(x, interface='giac')),
   2607                         make_function=parser_make_function)

File /usr/local/sage-10/src/sage/calculus/calculus.py:2496, in _find_var(name, interface)
   2494     return SR(sage.all.__dict__[name])
   2495 except (KeyError, TypeError):
-> 2496     return var(name)

File /usr/local/sage-10/src/sage/symbolic/ring.pyx:1358, in sage.symbolic.ring.var()
   1356     ValueError: The name "3" is not a valid Python identifier.
   1357 """
-> 1358 return SR.var(name, **kwds)
   1359 
   1360 

File /usr/local/sage-10/src/sage/symbolic/ring.pyx:889, in sage.symbolic.ring.SymbolicRing.var()
    887 for s in names_list:
    888     if not isidentifier(s):
--> 889         raise ValueError(f'The name "{s}" is not a valid Python identifier.')
    890     # warn on bad symbol names, but only once
    891     # symbol... names are temporary variables created with

ValueError: The name "and" is not a valid Python identifier.

This is not a new problem: Sage does not (yet) have logical functions: these are not difficult to implement, but interfacing such expressions with Maxima turned out more difficult than initially thought.

Contributions are welcome...

edit flag offensive delete link more

Comments

Periodic_1_6 gravatar imagePeriodic_1_6 ( 2023-06-17 22:41:20 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2023-06-17 12:04:16 +0100

Seen: 561 times

Last updated: Jun 18 '23