Skip to content

Commit

Permalink
Trac #23835: Replace Maxima with Pynac/Singular in Expression.factor()
Browse files Browse the repository at this point in the history
The default for symbolic factorization should be changed. Maxima as
default should be replaced with a call to Pynac's `factor()`
implementation, which itself uses Singular at the moment. Maxima should
be made available via `ex.factor(algorithm=...)`.

As an example of performance gain here one of the Fateman benchmarks:
{{{
sage: var('a b c k s y z')
(a, b, c, k, s, y, z)
sage: f = (1+x+y+z)^20+1
sage: g = (f*(f+1)).expand()
sage: %time _=g.factor()
}}}
This takes 11 seconds on 8.1.beta5 and 3.3 seconds with Pynac factor
(identical time using polynomial ring).

URL: https://trac.sagemath.org/23835
Reported by: rws
Ticket author(s): Ralf Stephan
Reviewer(s): Travis Scrimshaw
  • Loading branch information
Release Manager authored and vbraun committed Aug 25, 2018
2 parents 23deed3 + 19cff3a commit 7d1d5fe
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/sage/coding/code_bounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ def entropy(x, q=2):
sage: codes.bounds.entropy(0, 2)
0
sage: codes.bounds.entropy(1/5,4).factor()
1/10*(log(5) + log(3) - 4*log(4/5))/log(2)
1/10*(log(3) - 4*log(4/5) - log(1/5))/log(2)
sage: codes.bounds.entropy(1, 3)
log(2)/log(3)
Expand Down
22 changes: 12 additions & 10 deletions src/sage/symbolic/expression.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4116,7 +4116,7 @@ cdef class Expression(CommutativeRingElement):
sage: g = derivative(f, x); g # this is a complex expression
-1/2*((x^2 + 1)*x/(x^2 - 1)^2 - x/(x^2 - 1))/((x^2 + 1)/(x^2 - 1))^(3/4)
sage: g.factor()
-x/((x + 1)^2*(x - 1)^2*((x^2 + 1)/(x^2 - 1))^(3/4))
-x/((x + 1)^2*(x - 1)^2*((x^2 + 1)/((x + 1)*(x - 1)))^(3/4))
::
Expand Down Expand Up @@ -11141,22 +11141,24 @@ cdef class Expression(CommutativeRingElement):
sage: (f(x).diff(x)^2-1).factor()
(diff(f(x), x) + 1)*(diff(f(x), x) - 1)
"""
from sage.calculus.calculus import symbolic_expression_from_maxima_string, symbolic_expression_from_string
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:
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())
return self

def factor_list(self, dontfactor=[]):
"""
Expand Down

0 comments on commit 7d1d5fe

Please sign in to comment.