ASKSAGE: Sage Q&A Forum - Latest question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 11 Apr 2019 12:41:03 -0500Quicker expansion of multivariate polynomialshttp://ask.sagemath.org/question/46098/quicker-expansion-of-multivariate-polynomials/ Hi everyone,
I'm working on a project that involves first generating polynomials, then checking to see if certain terms exist within them. Essentially, I have a function `build_polynomial` that builds polynomials in un-expanded form according to a certain set of specifications. The polynomials produced by this function usually look something like this:
(x1 - x2 - x3 + y1 - y2 + y3 - y4)*(x1 - x2)*(x1 - x3)*(x1 + y1 - y2)*(x2 + x3 - y1 + y2 - y3 + y4)*(x2 + x3 + y2 - y3)*(x2 + x3 - y3 + y4)*(x2 + x3)*(x2 - x3)*(x2 + y2 - y3)*(y1 - y2)*(y1 - y3)*(y1 - y4)*(y2 - y3)*(y2 - y4)*(y3 - y4)
This is a particularly small example, the polynomials expand quite rapidly as the problem grows in size, but this example should give an idea of generally what they look like.
Next, my program must check too see if specific terms exist in the expanded form of the polynomial. Currently, I do this by first expanding the polynomial with the Sage `expand` function, then using the `coefficient` function with the specific terms as arguments.
My issue is that as the polynomial grows, the completion time of the `expand` function also grows quite rapidly. For example, at medium-to-large complexity, the polynomial can take days to be expanded.
Is there any way around this? Possibly a more efficient function that anybody knows of? I know that Sage is not really the best option for large computations like this, but I'm looking for a way to at least slightly improve performance while I research a longer-term solution.
Thanks!trenzafeedsThu, 11 Apr 2019 12:41:03 -0500http://ask.sagemath.org/question/46098/Why can Sage not compute this efficiently?http://ask.sagemath.org/question/28784/why-can-sage-not-compute-this-efficiently/Consider this function:
def A261042_list(len):
f = lambda k: x*2*(k+1)*(k+2)
g = [0]*len
g[len-1] = 1
for k in range(len-2,-1,-1):
g[k] = 1 - f(k)/(f(k)-1/g[k+1])
return taylor(g[0], x, 0, len-1).list()
A261042_list(6)
But taking larger arguments, for example A261042_list(16), it takes longer than I have patience to wait.
In contrast Maple and Mathematica return *immediately* with small arguments like this.
Provided I have not made a mistake I do not ask for a workaround, I ask if this behavior is acknowledged as a bug.
**Edit:** By the comments of vdelecroix we can write
def A261042_list(len):
f = lambda k: x*2*(k+1)*(k+2)
g = 1
for k in range(len-2,-1,-1):
g = (1-f(k)/(f(k)-1/g)).simplify_rational()
return taylor(g, x, 0, len-1).list()
On the other hand I am still not satisfied that we are forced to use 'simplify_rational' here.Peter LuschnySat, 08 Aug 2015 15:19:01 -0500http://ask.sagemath.org/question/28784/