Hello,
Yestday I found an memory issue in my experiment program.
I used sympy to generate legendre polynomials in my program and finally I found this part of code cause the problem that the memory which used by sympy cache cannot be released.
I'm new to sympy. Is it a good manner to call clear_cache() manually?Or is there any problem in my code?
Thanks.
def generate_legendre_polynomials(max_degree):
"""
Create a list of legendre polynomials from degree of zero to degree of max_degree.
"""
x, n = symbols('x n')
legendre_polynomial_list = [1, x]
def nth_order_legendre(nth):
"""
Create nth legendre polynomials iteratively
according to Bonnet’s recursion formula.
"""
if nth <= 1:
return legendre_polynomial_list[nth]
for i in range(2, nth + 1):
g = legendre_polynomial_list[i - 1]
h = legendre_polynomial_list[i - 2]
p = 1/n * ((2 * n - 1) * x * g - (n - 1) * h)
legendre_polynomial_list.append(expand(p.subs(n, i - 1)))
nth_order_legendre(max_degree)
return legendre_polynomial_list
legendre_polynomial_list = generate_legendre_polynomials(100) # legendre polynomial table
def generate_target_function(degree, legendre_polynomial_list=None):
"""
Generate a target function randomly
according to the target function formula.
"""
x = symbols('x')
if legendre_polynomial_list is None or len(legendre_polynomial_list) - 1 < degree:
legendre_polynomials = generate_legendre_polynomials(degree)
else:
legendre_polynomials = legendre_polynomial_list[:degree + 1]
alphas = [np.random.standard_normal() for i in range(degree + 1)]
f = np.dot(alphas, legendre_polynomials) # sum of a_i * L_i
Z = integrate(f**2, (x, -1, 1)) / 2 # integrate f**2 from -1 to 1
f = f / sqrt(Z) # normalized f
return f
# memory usege goes up after each iteration
for i in range(1000):
generate_target_function(20, legendre_polynomial_list)