Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in factorization of simple symbolic expressions #27304

Closed
egourgoulhon opened this issue Feb 16, 2019 · 22 comments
Closed

Bug in factorization of simple symbolic expressions #27304

egourgoulhon opened this issue Feb 16, 2019 · 22 comments

Comments

@egourgoulhon
Copy link
Member

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

Another example of erroneous result exhibited in https://groups.google.com/d/msg/sage-devel/ytLqIb4soLw/c14ZKGqcAAAJ is

sage: var('t u')
(t, u)
sage: L = (u + t)/(u - t)
sage: factor(L.substitute(t=sqrt(u)))
(u + 1)/(u - 1)

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).

CC: @rwst @sagetrac-bpage

Component: symbolics

Keywords: factor, exponential, pynac

Author: Benjamin Hackl

Branch/Commit: 7c2432c

Reviewer: Emmanuel Charpentier

Issue created by migration from https://trac.sagemath.org/ticket/27304

@egourgoulhon

This comment has been minimized.

@EmmanuelCharpentier
Copy link
Mannequin

EmmanuelCharpentier mannequin commented Feb 16, 2019

comment:2

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.

@egourgoulhon
Copy link
Member Author

comment:3

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.

@egourgoulhon
Copy link
Member Author

Changed keywords from factor, exponential to factor, exponential, pynac

@egourgoulhon
Copy link
Member Author

comment:5

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.

@egourgoulhon

This comment has been minimized.

@EmmanuelCharpentier
Copy link
Mannequin

EmmanuelCharpentier mannequin commented Feb 17, 2019

comment:7

This sage-support post might be related...

@embray
Copy link
Contributor

embray commented Mar 25, 2019

comment:8

Moving all blocker/critical issues from 8.7 to 8.8.

@embray embray modified the milestones: sage-8.7, sage-8.8 Mar 25, 2019
@egourgoulhon

This comment has been minimized.

@behackl
Copy link
Member

behackl commented May 10, 2019

comment:12

Would it be an option to let pynac handle factorization only when called on a rational function and call maxima (just as it was before #23835) otherwise?

@EmmanuelCharpentier
Copy link
Mannequin

EmmanuelCharpentier mannequin commented May 13, 2019

comment:13

Replying to @behackl:

Would it be an option to let pynac handle factorization only when called on a rational function and call maxima (just as it was before #23835) otherwise?

Seconded :

If that means reverting the changes brought in by #23835, and if that does not break other changes intervening since, this would be a reasonable option (even if bad for performance), and would give people who know what they're talking about time to poner our next move...

@egourgoulhon
Copy link
Member Author

comment:14

Replying to @EmmanuelCharpentier:

Replying to @behackl:

Would it be an option to let pynac handle factorization only when called on a rational function and call maxima (just as it was before #23835) otherwise?

Seconded :

Idem, +1. This sounds a good strategy for the short term. It would indeed be a pity if Sage 8.8 is shipped with such a bug.
Benjamin, do you intend to take this in charge?

@behackl
Copy link
Member

behackl commented May 14, 2019

comment:15

Replying to @egourgoulhon:

Idem, +1. This sounds a good strategy for the short term. It would indeed be a pity if Sage 8.8 is shipped with such a bug.
Benjamin, do you intend to take this in charge?

I already thought a bit about it, so yes, I will prepare a branch with the (short-term) bugfix I proposed above.

@egourgoulhon
Copy link
Member Author

comment:16

Replying to @behackl:

I already thought a bit about it, so yes, I will prepare a branch with the (short-term) bugfix I proposed above.

Great!

@behackl
Copy link
Member

behackl commented May 14, 2019

Commit: 7c2432c

@behackl
Copy link
Member

behackl commented May 14, 2019

comment:17

This is what I would suggest. I'm entirely open for suggestions regarding the name as well as the code for is_rational_expression.

Locally, I only checked the doctests in expression.pyx, maybe a patchbot finds further occurrences of the symbolic factor method where the output has to be changed.


New commits:

0618fd3helper method: is_rational_expression
131cd84let maxima handle factorization of non-rational expressions
7c2432cfix doctest; add more tests

@behackl
Copy link
Member

behackl commented May 14, 2019

Author: Benjamin Hackl

@behackl
Copy link
Member

behackl commented May 14, 2019

@EmmanuelCharpentier
Copy link
Mannequin

EmmanuelCharpentier mannequin commented May 14, 2019

Reviewer: Emmanuel Charpentier

@EmmanuelCharpentier
Copy link
Mannequin

EmmanuelCharpentier mannequin commented May 14, 2019

comment:18

On top of 8.8.beta5+#27738, builds and passes ptestlong exept for a transient (and already reported twice):

----------------------------------------------------------------------
sage -t --long --warn-long 151.5 src/sage/tests/books/computational-mathematics-with-sagemath/graphique_doctest.py  # 1 doctest failed
----------------------------------------------------------------------

which passes when ran standalone.

==> positive_review.

@egourgoulhon
Copy link
Member Author

comment:19

Thank you Benjamin!

@vbraun
Copy link
Member

vbraun commented May 21, 2019

Changed branch from u/behackl/simple-factorization-rational to 7c2432c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants