Can Sage solve inequalities?

644 views
Skip to first unread message

ma...@mendelu.cz

unread,
Oct 17, 2009, 1:21:45 PM10/17/09
to sage-devel
Hello all,

does Sage solve inequlitites? According to Maxima mail list (
http://thread.gmane.org/gmane.comp.mathematics.maxima.general/28497/focus=28530
) Maxima can do at elast someting in this area. I was not able to find
out, wheather Sage can somehow solve ineqaulities - via maxima or via
another way. Can it?

Robert Marik

ma...@mendelu.cz

unread,
Oct 17, 2009, 1:22:40 PM10/17/09
to sage-devel

William Stein

unread,
Oct 17, 2009, 2:43:38 PM10/17/09
to sage-...@googlegroups.com

No, I don't think so.

sage: eqn = x + 3 < 10
sage: solve(eqn,x)
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)

/Users/wstein/.sage/temp/flat.local/9055/_Users_wstein__sage_init_sage_0.py
in <module>()

/Users/wstein/sage/build/64bit/sage/local/lib/python2.6/site-packages/sage/symbolic/relation.pyc
in solve(f, *args, **kwds)
489 """
490 try:
--> 491 return f.solve(*args,**kwds)
492 except AttributeError:
493 from sage.symbolic.ring import is_SymbolicVariable

/Users/wstein/sage/build/64bit/sage/local/lib/python2.6/site-packages/sage/symbolic/expression.so
in sage.symbolic.expression.Expression.solve
(sage/symbolic/expression.cpp:22004)()

NotImplementedError: solving only implemented for equalities
sage: m = maxima(eqn)
sage: m.solve(x)
ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (2, 0))

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)

/Users/wstein/.sage/temp/flat.local/9055/_Users_wstein__sage_init_sage_0.py
in <module>()

/Users/wstein/sage/build/64bit/sage/local/lib/python2.6/site-packages/sage/interfaces/expect.pyc
in __call__(self, *args, **kwds)
1402
1403 def __call__(self, *args, **kwds):
-> 1404 return self._obj.parent().function_call(self._name,
[self._obj] + list(args), kwds)
1405
1406 def help(self):

/Users/wstein/sage/build/64bit/sage/local/lib/python2.6/site-packages/sage/interfaces/expect.pyc
in function_call(self, function, args, kwds)
1310 [s.name() for s in args],
1311
['%s=%s'%(key,value.name()) for key, value in kwds.items()])
-> 1312 return self.new(s)
1313
1314 def _function_call_string(self, function, args, kwds):

/Users/wstein/sage/build/64bit/sage/local/lib/python2.6/site-packages/sage/interfaces/expect.pyc
in new(self, code)
1094
1095 def new(self, code):
-> 1096 return self(code)
1097
1098 ###################################################################

/Users/wstein/sage/build/64bit/sage/local/lib/python2.6/site-packages/sage/interfaces/expect.pyc
in __call__(self, x, name)
1029
1030 if isinstance(x, basestring):
-> 1031 return cls(self, x, name=name)
1032 try:
1033 return self._coerce_from_special_method(x)

/Users/wstein/sage/build/64bit/sage/local/lib/python2.6/site-packages/sage/interfaces/expect.pyc
in __init__(self, parent, value, is_name, name)
1445 except (TypeError, KeyboardInterrupt,
RuntimeError, ValueError), x:
1446 self._session_number = -1
-> 1447 raise TypeError, x
1448 self._session_number = parent._session_number
1449

TypeError: Error executing code in Maxima
CODE:
sage3 : solve(sage1,sage2)$
Maxima ERROR:

Cannot solve inequalities. -`solve'

ma...@mendelu.cz

unread,
Oct 17, 2009, 2:58:07 PM10/17/09
to sage-devel
----------------------------------------------------------------------
| Sage Version 4.1.2, Release Date: 2009-10-13 |
| Type notebook() for the GUI, and license() for information. |
----------------------------------------------------------------------
sage: a= x^2+5*x+6 < 0
sage: b=a._maxima_()
sage: b.parent().eval("load(solve_rat_ineq)")
'"/opt/sage-4.1.2-linux-Debian_GNU_Linux_5.0.3_lenny-i686-Linux/local/
share/maxima/5.19.1/share/contrib/solve_rat_ineq.mac"'
sage: b.solve_rat_ineq
()
[[x>-3,x<-2]]
sage: a= x^2+5*x+6 <= 0
sage: b=a._maxima_()
sage: b.solve_rat_ineq()
[[x>=-3,x<=-2]]
sage:



Hm, probably this is the only available way how to solve inequalities
and at least the wrapper should be written.
I can try it, but not within these 14 days.

Robert


William Stein

unread,
Oct 17, 2009, 3:17:56 PM10/17/09
to sage-...@googlegroups.com

Wow, that's awesome. You should definitely try two weeks from now!!

William

David Joyner

unread,
Oct 17, 2009, 3:47:26 PM10/17/09
to sage-...@googlegroups.com
sage: x,y = var("x,y")
sage: region_plot([x^2+y^2<1, x<y], (x,-2,2), (y,-2,2))

is an example of plotting the solutions to some inequalities.

>
> Robert Marik
>
>
> >
>

Robert Dodier

unread,
Oct 17, 2009, 5:27:59 PM10/17/09
to sage-devel
Maybe the Maxima share package fourier_elim is useful to you.
There is no documentation, but there are some examples in
rtest_fourier_elim.mac. Also look in the mailing list archive for
some mention of it. I'm sure the author (Barton Willis) would be
happy to answer questions posted to the mailing list.

FWIW

Robert Dodier

ma...@mendelu.cz

unread,
Oct 17, 2009, 6:42:42 PM10/17/09
to sage-devel
On 17 říj, 23:27, Robert Dodier <robert.dod...@gmail.com> wrote:
> ma...@mendelu.cz wrote:
> > does Sage solve inequlitites? According to Maxima mail list (
> >http://thread.gmane.org/gmane.comp.mathematics.maxima.general/28497/f...
> > ) Maxima can do at elast someting in this area. I was not able to find
> > out, wheather Sage can somehow solve ineqaulities - via maxima or via
> > another way. Can it?
>
> Maybe the Maxima share package fourier_elim is useful to you.
> There is no documentation, but there are some examples in
> rtest_fourier_elim.mac. Also look in the mailing list archive for
> some mention of it. I'm sure the author (Barton Willis) would be
> happy to answer questions posted to the mailing list.
>

Many thanks for this information! Very interesting!

Robert

> FWIW
>
> Robert Dodier

mhampton

unread,
Oct 17, 2009, 7:18:07 PM10/17/09
to sage-devel
Sage includes a couple of different linear programming solvers, which
may be relevant - cvxopt, cddlib, and I think Nathan Cohen added GLPK
as an optional package. I'm not sure how well they could be
integrated into the symbolics, I suppose adding support for what
maxima can do might be the easiest route.

-Marshall

kcrisman

unread,
Oct 17, 2009, 8:48:17 PM10/17/09
to sage-devel


On Oct 17, 5:27 pm, Robert Dodier <robert.dod...@gmail.com> wrote:
> ma...@mendelu.cz wrote:
> > does Sage solve inequlitites? According to Maxima mail list (
> >http://thread.gmane.org/gmane.comp.mathematics.maxima.general/28497/f...
> > ) Maxima can do at elast someting in this area. I was not able to find
> > out, wheather Sage can somehow solve ineqaulities - via maxima or via
> > another way. Can it?
>
> Maybe the Maxima share package fourier_elim is useful to you.
> There is no documentation, but there are some examples in
> rtest_fourier_elim.mac. Also look in the mailing list archive for
> some mention of it. I'm sure the author (Barton Willis) would be
> happy to answer questions posted to the mailing list.

Yes, Barton's fourier_elim package and the solve_rat_ineq stuff should
definitely be wrapped. Incidentally, his to_poly_solver package is
now wrapping certain inequality solving as well, and we do already
have that as an option in our solve() command.

Do you think that solve() would automatically call this/some LP thing,
or would it be better to have a separate command?

- kcrisman

ma...@mendelu.cz

unread,
Oct 20, 2009, 10:22:17 AM10/20/09
to sage-devel


On 17 říj, 21:17, William Stein <wst...@gmail.com> wrote:
> > Hm, probably this is the only available way how to solve inequalities
> > and at least the wrapper should be written.
> > I can try it, but not within these 14 days.
>
> Wow, that's awesome.  You should definitely try two weeks from now!!

Despite my previous posts, I am working on this, hope to finish within
two weeks :)

* I foud a bug (and a solution how to fix) in solve_rat_ineq -- I will
report it in Maxima forum as soon as possible
* the maxima expression x#0 (x is not equal 0) has no meaning in
Sage. If the answer from Maxima contains x#0 and this answer is in
variable sol, then
sage: sol.sage()
fails. I have no idea how and where could fix it. Can somebody add
this feature to Sage? Thanks.

Robert

>
> William

Wilfried Huss

unread,
Oct 20, 2009, 11:04:24 AM10/20/09
to sage-...@googlegroups.com
ma...@mendelu.cz schrieb:
The actual parser for maxima expression is in the file: sage/misc/parser.pyx

But the string that is send to the parser is first preprocessed in the
function
symbolic_expression_from_maxima_string() in the file
sage/calculus/calculus.py

It is probably enough to put something like:

s = s.replace('#', '!=')

somewhere into this function. At least if '#' can not have an other
meaning in Maxima.

Wilfried

kcrisman

unread,
Oct 20, 2009, 11:08:55 AM10/20/09
to sage-devel


On Oct 20, 10:22 am, "ma...@mendelu.cz" <ma...@mendelu.cz> wrote:
> On 17 říj, 21:17, William Stein <wst...@gmail.com> wrote:
>
> > > Hm, probably this is the only available way how to solve inequalities
> > > and at least the wrapper should be written.
> > > I can try it, but not within these 14 days.
>
> > Wow, that's awesome.  You should definitely try two weeks from now!!
>
> Despite my previous posts, I am working on this, hope to finish within
> two weeks :)
>
> * I foud a bug (and a solution how to fix) in solve_rat_ineq -- I will
> report it in Maxima forum as soon as possible

Great! Don't forget to try fourier_elim as a "backup" case, or at
least as a keyword for those who want to try a little harder, similar
to the to_poly_solve keyword introduced on ticket # 6642.

> * the maxima expression x#0  (x is not equal 0) has no meaning in
> Sage. If the answer from Maxima contains x#0 and  this answer is in
> variable sol, then
> sage: sol.sage()
> fails. I have no idea how and where could fix it. Can somebody add
> this feature to Sage? Thanks.

This is already fixed on Trac # 1163, waiting for review - I hope this
does what you need. In fact, if you could review that, # 385, and #
3914, I think they are all related to things you are working on, so
that would be very helpful.

- kcrisman

ma...@mendelu.cz

unread,
Oct 20, 2009, 12:13:38 PM10/20/09
to sage-devel
>
> This is already fixed on Trac # 1163, waiting for review - I hope this
> does what you need.  In fact, if you could review that, # 385, and #
> 3914, I think they are all related to things you are working on, so
> that would be very helpful.
>

Thanks for your answer

The patches are nice and important, but have in this week very limited
access to Sage to test these patchs.

And another problem is, that I am completely new in Sage and Python. I
think that the tickets should be closed by some priviledges skilled
developers. Is this the case in Sage?

Robert

William Stein

unread,
Oct 20, 2009, 1:46:49 PM10/20/09
to sage-...@googlegroups.com
On Tue, Oct 20, 2009 at 7:22 AM, ma...@mendelu.cz <ma...@mendelu.cz> wrote:
>
>
>
> On 17 říj, 21:17, William Stein <wst...@gmail.com> wrote:
>> > Hm, probably this is the only available way how to solve inequalities
>> > and at least the wrapper should be written.
>> > I can try it, but not within these 14 days.
>>
>> Wow, that's awesome.  You should definitely try two weeks from now!!
>
> Despite my previous posts, I am working on this, hope to finish within
> two weeks :)

Working on Sage is addictive, isn't it!!

-- William

kcrisman

unread,
Oct 20, 2009, 2:03:51 PM10/20/09
to sage-devel
Not necessarily, though there has been discussion around this. If you
like, you should observe in the lists who is knowledgeable about your
topic (for instance, symbolics or calculus) and send a private email
to one or more of them to ask them to review something. If they don't
have time, they'll let you know, and you can ask someone else.
Unfortunately, I can't review them :)

- kcrisman

ma...@mendelu.cz

unread,
Oct 27, 2009, 5:53:12 PM10/27/09
to sage-devel


> > > > Hm, probably this is the only available way how to solveinequalities
> > > > and at least the wrapper should be written.
> > > > I can try it, but not within these 14 days.


The patch is here: http://trac.sagemath.org/sage_trac/ticket/7325


Robert

ma...@mendelu.cz

unread,
Nov 9, 2009, 6:07:41 PM11/9/09
to sage-devel
> > * the maxima expression x#0  (x is not equal 0) has no meaning in
> > Sage. If the answer from Maxima contains x#0 and  this answer is in
> > variable sol, then
> > sage: sol.sage()
> > fails. I have no idea how and where could fix it. Can somebody add
> > this feature to Sage? Thanks.
>
> This is already fixed on Trac # 1163, waiting for review - I hope this
> does what you need.  

I do not think so. I installed this patch and the command
sage: maxima("x#0").sage()
gives still error: TypeError: unable to make sense of Maxima
expression 'x#0' in Sage

Anyway, I uploaded new patch and hope that problem related to parsing
# will be solved in another patch.

Many thanks for info.
Robert

kcrisman

unread,
Nov 9, 2009, 8:57:32 PM11/9/09
to sage-devel

> I do not think so. I installed this patch and the command
> sage:  maxima("x#0").sage()
> gives still error: TypeError: unable to make sense of Maxima
> expression 'x#0' in Sage
>
> Anyway, I uploaded new patch and hope that problem related to parsing
> # will be solved in another patch.
>

Ah, thanks for observing this - #1163 currently makes it so

sage: maxima((x != 1))
x#1

but didn't parse the other way around, didn't occur to me that this
was not already implemented. I will try to update that one.

In the long run, all these Maxima parsing functions should be
refactored to make them easier to find, but that probably won't happen
for a while - also see #7377, which has a much more ambitious agenda.

Incidentally, if you were able to produce the current patch at #7325,
I am sure you are more than competent to review #1163 :-)

- kcrisman
Reply all
Reply to author
Forward
0 new messages