Ask Your Question

Olof Bergvall's profile - activity

2021-05-06 19:35:24 +0200 received badge  Nice Question (source)
2021-02-03 15:31:58 +0200 received badge  Student (source)
2016-08-26 22:40:53 +0200 received badge  Popular Question (source)
2016-08-26 22:40:53 +0200 received badge  Notable Question (source)
2016-08-26 22:40:53 +0200 received badge  Famous Question (source)
2015-09-15 11:09:49 +0200 asked a question High memory usage when substituting variables

I need to make a lot of variable substitutions in multivariate polynomials. However, I only need to store one polynomial at a time and I only need to make one substitution at a time. Nonetheless sage uses a lot of memory. This memory is not freed until sage is killed.

Here is a small example:

var("x y z")

# A polynomial in three variables (The Trott quartic):
poly=12^2*(x^4+y^4)-15^2*(x^2+y^2)*z^2+350*x^2*y^2+9^2*z^4
poly=poly.polynomial(ZZ)

pnt=[1,2,3]

# Makes a certain variable subsitution, defined by pt, n times.
def test_subs(f,pt,n):
    for i in xrange(n):
        temp=f.substitute(x=x+pt[0]*z,y=y+pt[1]*z,z=pt[2]*z)
        #temp=f(x=x+pt[0]*z,y=y+pt[1]*z,z=pt[2]*z) #This also uses a lot of memory.

When i run test_subs(poly,pnt,100000) I can see the memory usage ticking up from around 1% up to nearly 2%. if I then run the function again the ticking starts at 2%, and so on. In the real problem I have my program eventually fills up all memory and then crashes.

Can you see why this is happening and do you know how to prevent it from happening?