Opened 5 years ago

Last modified 5 years ago

#16592 new defect

bug in evaluation of maxima function with product/quotient/power argument

Reported by: rws Owned by:
Priority: major Milestone: sage-6.4
Component: interfaces Keywords:
Cc: Merged in:
Authors: Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Description

From the report in http://ask.sagemath.org/question/10961/problems-with-maximafunction-of-one-variable/

sage: var('f,k,x')
(f, k, x)
sage: F = maxima.function('x','f')
sage: k=F(0)
sage: F(k*x)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-46-eb3fed206854> in <module>()
----> 1 F(k*x)

/home/ralf/sage/local/lib/python2.7/site-packages/sage/interfaces/maxima_abstract.pyc in __call__(self, *args)
   2012         if len(args) == 1:
   2013             args = '(%s)'%args
-> 2014         return P('%s%s'%(self.name(), args))
   2015 
   2016     def __repr__(self):

/home/ralf/sage/local/lib/python2.7/site-packages/sage/interfaces/interface.pyc in __call__(self, x, name)
    197 
    198         if isinstance(x, basestring):
--> 199             return cls(self, x, name=name)
    200         try:
    201             return self._coerce_from_special_method(x)

/home/ralf/sage/local/lib/python2.7/site-packages/sage/interfaces/maxima.pyc in __init__(self, parent, value, is_name, name)
   1127             True
   1128         """
-> 1129         ExpectElement.__init__(self, parent, value, is_name=False, name=None)                                                                         
   1130 
   1131     def display2d(self, onscreen=True):

/home/ralf/sage/local/lib/python2.7/site-packages/sage/interfaces/expect.pyc in __init__(self, parent, value, is_name, name)
   1304         else:
   1305             try:
-> 1306                 self._name = parent._create(value, name=name)
   1307             # Convert ValueError and RuntimeError to TypeError for
   1308             # coercion to work properly.

/home/ralf/sage/local/lib/python2.7/site-packages/sage/interfaces/interface.pyc in _create(self, value, name)
    387     def _create(self, value, name=None):
    388         name = self._next_var_name() if name is None else name
--> 389         self.set(name, value)
    390         return name
    391 

/home/ralf/sage/local/lib/python2.7/site-packages/sage/interfaces/maxima.pyc in set(self, var, value)
    972             self._batch(cmd, batchload=True)
    973         else:
--> 974             self._eval_line(cmd)
    975             #self._sendline(cmd)
    976             #self._expect_expr()

/home/ralf/sage/local/lib/python2.7/site-packages/sage/interfaces/maxima.pyc in _eval_line(self, line, allow_use_file, wait_for_prompt, reformat, error_check, restart_if_needed)
    764         out = self._before()        # input echo + output prompt + output
    765         if error_check:
--> 766             self._error_check(line, out)
    767         if not reformat:
    768             return out

/home/ralf/sage/local/lib/python2.7/site-packages/sage/interfaces/maxima.pyc in _error_check(self, cmd, out)
    904         m = r.search(out)
    905         if not m is None:
--> 906             self._error_msg(cmd, out)
    907 
    908     def _error_msg(self, cmd, out):

/home/ralf/sage/local/lib/python2.7/site-packages/sage/interfaces/maxima.pyc in _error_msg(self, cmd, out)
    921                 Principal Value
    922         """
--> 923         raise TypeError("Error executing code in Maxima\nCODE:\n\t%s\nMaxima ERROR:\n\t%s"%(cmd, out.replace('-- an error.  To debug this try debugmode(true);','')))
    924 
    925     ###########################################

TypeError: Error executing code in Maxima
CODE:
        sage38 : sage33(                                      f x)$
Maxima ERROR:
        incorrect syntax: X is not an infix operator
                f x)
                  ^

On the other hand F(k+k), F(k*k), F(k/x), and F(k^x) produces:

...
/home/ralf/sage/local/lib/python2.7/site-packages/sage/interfaces/maxima.pyc in _eval_line(self, line, allow_use_file, wait_for_prompt, reformat, error_check, restart_if_needed)
    759             return
    760         # line_echo sometimes has randomly inserted terminal echo in front #15811
--> 761         assert line_echo.strip().endswith(line.strip()), 'mismatch:\n' + line_echo + line
    762 
    763         self._expect_expr(self._display_prompt)

AssertionError: mismatch:
sage41 : sage33(                                       f
sage41 : sage33(                                       f
                                       -
                                       x)$

Change History (4)

comment:1 Changed 5 years ago by kcrisman

This really was intended to have actual functions on the inside, as all the documentation examples have.

        cmd = '%s(%s) := %s'%(name, args, defn)
        self._eval_line(cmd)

That's basically the code, where name is just the next sage0, sage1 or whatever is available. In Maxima:

(%i1) sage0(x) := f;
(%o1)                            sage0(x) := f
(%i3) sage0(x);
(%o3)                                  f
(%i5) sage0(0);
(%o5)                                  f
(%i7) sage0(f x);

stdin:28571:incorrect syntax: X is not an infix operator
sage0(f x)
        ^

The other errors seem to be something where the line passed by Sage isn't what is sent to Maxima, somehow.

So I guess what would have to happen is for that * to be sent in. Not sure why it isn't. Note that F(f*x) in your example returns f as expected.


sage: F.__call__??
<snip>
        if len(args) == 1:
            args = '(%s)'%args
        return P('%s%s'%(self.name(), args))

Got it. This is part of the problem that symbolic expressions have length.

sage: len(k*x)
2

Here is why it is different.

sage: def g(*args):
....:     return '%s'%args
....: 
sage: g(f*x)
'f*x'
sage: g(k*x)
'                                      f x'

Yikes!

sage: print k*x
                                      f x
sage: print f*x
f*x
sage: type(f*x)
<type 'sage.symbolic.expression.Expression'>
sage: type(k*x)
<class 'sage.interfaces.maxima.MaximaElement'>

I'm not sure what the best way to fix this is; presumably we print Maxima things this way because Maxima itself does, so changing that would be unwise:

(%i1) f*x;
(%o1)                                 f x

But I don't know that not just using the string rep of the expression is any better...

comment:2 Changed 5 years ago by nbruin

str is more for human consumption, so if you have to choose to handle something via a string, repr is the better one to go for:

sage: str(k*x)
'                                      f x'
sage: repr(k*x)
'f*x'

or with format strings:

sage: "%r"%(k*x)
'f*x'
sage: "%s"%(k*x)
'                                      f x'

The len doesn't come i, by the way:

sage: def tt(*args): return len(args)
sage: tt(k*x)
1

I'd say %r is the better one to go for.

comment:3 Changed 5 years ago by nbruin

Changing this example to:

F = maxima_calculus.function('x','f')

leads to a similar error, by the way (same cause, I'm sure). Of course, we shouldn't be doing things via strings in that case anyway, i.e., the whole "function" thing could be rewritten on that interface if that were a useful thing to have.

comment:4 Changed 5 years ago by vbraun_spam

  • Milestone changed from sage-6.3 to sage-6.4
Note: See TracTickets for help on using tickets.