Ask Your Question
0

Why does Sage provide no explicit solution for solve() but Wolfram Alpha does ? [closed]

asked 2024-04-04 21:04:26 +0100

Wuddl gravatar image

updated 2024-04-05 07:29:50 +0100

Hello everyone, I'm working sporadically with Sage and now I have a problem which I can't solve with my limited rutine and rudimentary knowledge of math:

With the following input i do not ge a explicit solution form Sage:

forget(assumptions()) 
var('SagittalH r Da Di')

eqSagittalH = SagittalH == sqrt(r^2-(Di/2))-sqrt(r^2-(Da/2))

forget(assumptions()) 
#Assumptions
assume(Da,'real')
assume(Di,'real')
assume(Di<Da)
assume(r,'real')
assume(r>0)
assume(2*r>Da)
assume(SagittalH,'real')

eqDa = solve((eqSagittalH)^2,Da)
print(eqDa)
eqDaExplcit = solve((eqSagittalH)^2,Da,explicit_solutions=True)
print(eqDaExplcit,"If List is Empty: no Explicit solution!!")

Return of teh Explicit computaion is a empty list -> no Explicit solution Same result witout the assumptions...

the Prompt

eqDa = solve((eqSagittalH)^2,Da)

returns

[
Da == -2*SagittalH^2 + 4*r^2 - 4*sqrt(r^2 - 1/2*Da)*sqrt(r^2 - 1/2*Di) - Di
]

If I ask Wolfram Alpha the same Problem via the Promt

solve y = sqrt(r^2 - 1/2*a) - sqrt(r^2 - 1/2*b)  for b

i get the expected result

b==2*sqrt(2)*y*sqrt(2*r^2 - a) + a - 2*y^2

Can anyone tell me what I am doing wrong as I am not getting any results from Sage?

P.S: my Sage version is a freshly compiled SageMath 10.4 beta 1

PPS: my TI 89 is also able to solve the equation as expected.

PPPS: type(eqSagittalH) returns

<class 'sage.symbolic.expression.Expression'>

solve((eqSagittalH),Da) #without the ^2 returnsthis Error Message:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
File ~/sage/sage/src/sage/interfaces/maxima_lib.py:632, in MaximaLib._create(self, value, name)
    631     else:
--> 632         self.set(name, value)
    633 except RuntimeError as error:

File ~/sage/sage/src/sage/interfaces/maxima_lib.py:540, in MaximaLib.set(self, var, value)
    539 cmd = '%s : %s$' % (var, value.rstrip(';'))
--> 540 self.eval(cmd)

File ~/sage/sage/src/sage/interfaces/maxima_lib.py:486, in MaximaLib._eval_line(self, line, locals, reformat, **kwds)
    485         if statement:
--> 486             maxima_eval("#$%s$" % statement)
    487 if not reformat:

File ~/sage/sage/src/sage/libs/ecl.pyx:838, in sage.libs.ecl.EclObject.__call__()
    837 lispargs = EclObject(list(args))
--> 838 return ecl_wrap(ecl_safe_apply(self.obj, (<EclObject>lispargs).obj))
    839 

File ~/sage/sage/src/sage/libs/ecl.pyx:358, in sage.libs.ecl.ecl_safe_apply()
    357     else:
--> 358         raise RuntimeError("ECL says: {}".format(message))
    359 else:

RuntimeError: ECL says: Maxima asks: Is _SAGE_VAR_SagittalH-sqrt(-(_SAGE_VAR_Di-2*_SAGE_VAR_r^2)/2)
    positive, negative or zero?

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
File ~/sage/sage/src/sage/interfaces/interface.py:749, in InterfaceElement.__init__(self, parent, value, is_name, name)
    748 try:
--> 749     self._name = parent._create(value, name=name)
    750 except (TypeError, RuntimeError, ValueError) as x:

File ~/sage/sage/src/sage/interfaces/maxima_lib.py:636, in MaximaLib._create(self, value, name)
    635 if "Is" in s:  # Maxima asked for a condition
--> 636     self._missing_assumption(s)
    637 else:

File ~/sage/sage/src/sage/interfaces/maxima_lib.py:1074, in MaximaLib._missing_assumption(self, errstr)
   1073 outstr = outstr.replace('_SAGE_VAR_', '')
-> 1074 raise ValueError(outstr)

ValueError: Computation failed since Maxima requested additional constraints; using the 'assume' command before evaluation *may* help (example of legal syntax is 'assume(SagittalH-sqrt(-(Di-2*r^2)/2)
>0)', see `assume?` for more details)
Is SagittalH-sqrt(-(Di-2*r^2)/2)
    positive, negative or zero?

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
Cell In[12], line 1
----> 1 solve(SagittalH == sqrt(r**Integer(2)-(Di/Integer(2)))-sqrt(r**Integer(2)-(Da/Integer(2))),Da)
      2 #eqDa = solve(eqSagittalH,Da)
      3 #print(eqDa)

File ~/sage/sage/src/sage/symbolic/relation.py:1068, in solve(f, *args, **kwds)
   1063         raise TypeError("The first argument to solve() should be a "
   1064                         "symbolic expression or a list of symbolic "
   1065                         "expressions.")
   1067 if isinstance(f, Expression):  # f is a single expression
-> 1068     return _solve_expression(f, x, explicit_solutions, multiplicities, to_poly_solve, solution_dict, algorithm, domain)
   1070 if not isinstance(f, (list, tuple)):
   1071     raise TypeError("The first argument must be a symbolic expression or a list of symbolic expressions.")

File ~/sage/sage/src/sage/symbolic/relation.py:1338, in _solve_expression(f, x, explicit_solutions, multiplicities, to_poly_solve, solution_dict, algorithm, domain)
   1336 try:
   1337     if to_poly_solve != 'force':
-> 1338         s = m.solve(x).str()
   1339     else:  # omit Maxima's solve command
   1340         s = str([])

File ~/sage/sage/src/sage/interfaces/interface.py:697, in InterfaceFunctionElement.__call__(self, *args, **kwds)
    696 def __call__(self, *args, **kwds):
--> 697     return self._obj.parent().function_call(self._name, [self._obj] + list(args), kwds)

File ~/sage/sage/src/sage/interfaces/interface.py:617, in Interface.function_call(self, function, args, kwds)
    613 self._check_valid_function_name(function)
    614 s = self._function_call_string(function,
    615                                [s.name() for s in args],
    616                                ['%s=%s' % (key, value.name()) for key, value in kwds.items()])
--> 617 return self.new(s)

File ~/sage/sage/src/sage/interfaces/interface.py:386, in Interface.new(self, code)
    385 def new(self, code):
--> 386     return self(code)

File ~/sage/sage/src/sage/interfaces/interface.py:299, in Interface.__call__(self, x, name)
    296         pass
    298 if isinstance(x, str):
--> 299     return cls(self, x, name=name)
    300 try:
    301     # Special methods do not and should not have an option to
    302     # set the name directly, as the identifier assigned by the
    303     # interface should stay consistent. An identifier with a
    304     # user-assigned name might change its value, so we return a
    305     # new element.
    306     result = self._coerce_from_special_method(x)

File ~/sage/sage/src/sage/interfaces/interface.py:751, in InterfaceElement.__init__(self, parent, value, is_name, name)
    749     self._name = parent._create(value, name=name)
    750 except (TypeError, RuntimeError, ValueError) as x:
--> 751     raise TypeError(x)

TypeError: Computation failed since Maxima requested additional constraints; using the 'assume' command before evaluation *may* help (example of legal syntax is 'assume(SagittalH-sqrt(-(Di-2*r^2)/2)
>0)', see `assume?` for more details)
Is SagittalH-sqrt(-(Di-2*r^2)/2)
    positive, negative or zero?

I'm not able to get the problem solved with further assumptions.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Wuddl
close date 2024-04-05 17:46:28.997727

1 Answer

Sort by ยป oldest newest most voted
1

answered 2024-04-05 10:06:14 +0100

Emmanuel Charpentier gravatar image

updated 2024-04-06 15:33:40 +0100

You have to eliminate the radicals by squaring the equation (thus possibly introducing non-solutions) and separating the terms containing and not containing a radical; since there are more than one of them, you have to repeat... In your case :

sage: (((eqSagittalH^2).expand()*2-4*r^2+Da+Di)^2).solve(Da)
[Da == -2*SagittalH^2 - 2*sqrt(4*r^2 - 2*Di)*SagittalH + Di, Da == -2*SagittalH^2 + 2*sqrt(4*r^2 - 2*Di)*SagittalH + Di]

FWIW, ISTR that Sympy has an adequate function for this operation, but, my head on the executioner's bill, I can't remember its name at the moment. Aging is descending...

HTH,

EDIT : It's sympy.solvers.solvers.unrad, documeted here :

sage: from sympy.solvers.solvers import unrad
sage: unrad(eqSagittalH._sympy_())[0]._sage_().solve(Da)
[Da == -2*SagittalH^2 - 2*sqrt(4*r^2 - 2*Di)*SagittalH + Di, Da == -2*SagittalH^2 + 2*sqrt(4*r^2 - 2*Di)*SagittalH + Di]

EDIT2 : Alternate solutions :

sage: var('SagittalH r Da Di')
(SagittalH, r, Da, Di)
sage: eqSagittalH = SagittalH == sqrt(r^2-(Di/2))-sqrt(r^2-(Da/2))

Sympy :

sage: import sympy
sage: [{Da:s._sage_()} for s in sympy.solve(*map(sympy.sympify, (eqSagittalH, Da)), dict=False)]
[{Da: -(sqrt(2)*SagittalH - sqrt(2*r^2 - Di))^2 + 2*r^2}]

Giac :

sage: [{Da:s.sage()} for s in giac.solve(*map(giac, (eqSagittalH.rhs()-eqSagittalH.lhs(), Da)))]
[{Da: -1/2*(sqrt(2)*sqrt(2*r^2 - Di) - 2*SagittalH)^2 + 2*r^2}]

Fricas :

sage: [{Da:s.rhs().sage()} for s in fricas.solve(*map(fricas, (eqSagittalH, Da)))]
[{Da: 2*sqrt(2)*sqrt(2*r^2 - Di)*SagittalH - 2*SagittalH^2 + Di}]

The problem is therefore a weakness of Sage's default solver (i. e. Maxima's).

edit flag offensive delete link more

Comments

Thank you for your time!

your solution is working.

Again Sage is not verry userfriendly...

Wuddl gravatar imageWuddl ( 2024-04-05 17:45:52 +0100 )edit

Question Tools

1 follower

Stats

Asked: 2024-04-04 21:04:26 +0100

Seen: 202 times

Last updated: Apr 06