Opened 2 months ago

Last modified 13 days ago

#27304 new defect

Bug in factorization of simple symbolic expressions

Reported by: egourgoulhon Owned by:
Priority: blocker Milestone: sage-8.8
Component: symbolics Keywords: factor, exponential, pynac
Cc: rws, bpage Merged in:
Authors: Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Description (last modified by egourgoulhon)

As reported in this ask.sagemath question, we have currently (Sage 8.7.beta3):

sage: factor(2*exp(x) + exp(-x))
3*e^x

as well as

sage: factor(x*exp(-x) + exp(-x))
(x + 1)*e^x

This bug is there since at least Sage 8.4.beta4. It is not in Sage 8.3, hence it must have been introduced between 8.4.beta0 and 8.4.beta4. It seems to have been introduced in #23835 (see comment:5).

Change History (10)

comment:1 Changed 2 months ago by egourgoulhon

  • Description modified (diff)

comment:2 Changed 2 months ago by charpent

  • Priority changed from major to critical

in the same vein :

sage: e^(x+2*I*pi)
cosh(-x) - sinh(-x)

which is true, but largely not what we seek...

sage: e^(x+2*I*pi)==e^x
cosh(-x) - sinh(-x) == e^x
sage: bool(e^(x+2*I*pi)==e^x)
False

which is false. But :

sage: e^(x+2*I*pi).maxima_methods().exponentialize()
cosh(-x) - sinh(-x)
sage: (e^(x+2*I*pi)).maxima_methods().exponentialize()
e^x

Deserves to be critical, if not blocker.

comment:3 follow-up: Changed 2 months ago by egourgoulhon

A difference between Sage 8.3 (where the factorization works) and Sage 8.4.beta4 is that in Sage 8.3 the factorization is performed by Maxima: cf. these lines of src/sage/symbolic/expression.pyx

    def factor(self, dontfactor=[]):
        from sage.calculus.calculus import symbolic_expression_from_maxima_string, symbolic_expression_from_string
        if len(dontfactor) > 0:
            m = self._maxima_()
            name = m.name()
            varstr = ','.join(['_SAGE_VAR_'+str(v) for v in dontfactor])
            cmd = 'block([dontfactor:[%s]],factor(%s))'%(varstr, name)
            return symbolic_expression_from_maxima_string(cmd)
        else:
            try:
                from sage.rings.all import QQ
                f = self.polynomial(QQ)
                w = repr(f.factor())
                return symbolic_expression_from_string(w)
            except (TypeError, NotImplementedError):
                pass
            return self.parent()(self._maxima_().factor())

while in Sage 8.4.beta4 and later, it is performed by pynac (via the function g_factor):

     def factor(self, dontfactor=[]):
        from sage.calculus.calculus import symbolic_expression_from_maxima_string
        cdef GEx x
        cdef bint b
        if dontfactor:
            m = self._maxima_()
            name = m.name()
            varstr = ','.join(['_SAGE_VAR_' + str(v) for v in dontfactor])
            cmd = 'block([dontfactor:[%s]],factor(%s))' % (varstr, name)
            return symbolic_expression_from_maxima_string(cmd)
        sig_on()
        try:
            b = g_factor(self._gobj, x)
        finally:
            sig_off()
        if b:
            return new_Expression_from_GEx(self._parent, x)
        else:
            return self

Maxima is correct there: in Sage 8.7.beta3, we have

./sage -maxima
(%i1) factor(2*exp(x) + exp(-x));     
                                - x      2 x
(%o1)                         %e    (2 %e    + 1)

So I would say it is a pynac bug.

comment:4 Changed 2 months ago by egourgoulhon

  • Keywords pynac added

comment:5 in reply to: ↑ 3 Changed 2 months ago by egourgoulhon

The change from Maxima factorization to Pynac one, which triggered the bug, was introduced in #23835, which has been merged in Sage 8.4.beta3.

comment:6 Changed 2 months ago by egourgoulhon

  • Description modified (diff)

comment:7 Changed 2 months ago by charpent

This sage-support post might be related...

comment:8 Changed 4 weeks ago by embray

  • Milestone changed from sage-8.7 to sage-8.8

Moving all blocker/critical issues from 8.7 to 8.8.

comment:9 Changed 2 weeks ago by slelievre

  • Priority changed from critical to blocker

comment:10 Changed 13 days ago by bpage

  • Cc bpage added
Note: See TracTickets for help on using tickets.