First time here? Check out the FAQ!

Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Issue with Processing Symbolic Polynomial Function

I am trying to create a function in SageMath that processes a symbolic polynomial expression, multiplies it by a symbolic variable (say x) and then extracts the odd powers of x to send their degrees to another function, G. The function should work for multivariable polynomials and should handle cases where the coefficients involve irrational expressions.

I have found that the function works correctly for constants expression, it fails for all other cases. Has anyone encountered similiar issues? Is there a better way to treat this problem in general?

The function:

def integrate_one_free(expr, L1_val, Lj):
    R = PolynomialRing(SR, x)
    poly = R(expr)
    result = 0    
    new_poly = R.gen() * expr

for deg in range(1, new_poly.degree() + 1, 2):
    coeff = new_poly[deg]            
    result += coeff * (1/2) * (G_function(deg, L1_val + Lj) + G_function(deg, L1_val - Lj))

return result

Here is the relevant function call, where and are integers, and is a list of length declared as symbolic variables:

if n > 1:
        x = var('x')        
        for j in range(1, n):
            subvol = V(g, n-1, [x] + Ls[1:j] + Ls[j+1:])
            total += integrate_one_free(subvol, L1_val, Ls[j])

if I put a print statement in my "for deg" loop with an example expression, I get output:

polynomial is (8.5*pi^4 + 0.166*pi^2*x^2)*x
coeff is 8.5*pi^4 + 0.166*pi^2*x^2 and deg is 1
click to hide/show revision 2
None

updated 0 years ago

Max Alekseyev gravatar image

Issue with Processing Symbolic Polynomial Function

I am trying to create a function in SageMath that processes a symbolic polynomial expression, multiplies it by a symbolic variable (say x) and then extracts the odd powers of x to send their degrees to another function, G. The function should work for multivariable polynomials and should handle cases where the coefficients involve irrational expressions.

I have found that the function works correctly for constants expression, it fails for all other cases. Has anyone encountered similiar issues? Is there a better way to treat this problem in general?

The function:

def integrate_one_free(expr, L1_val, Lj):
    R = PolynomialRing(SR, x)
    poly = R(expr)
    result = 0    
    new_poly = R.gen() * expr

 for deg in range(1, new_poly.degree() + 1, 2):
     coeff = new_poly[deg]            
     result += coeff * (1/2) * (G_function(deg, L1_val + Lj) + G_function(deg, L1_val - Lj))

 return result

Here is the relevant function call, where and are integers, and is a list of length declared as symbolic variables:

if n > 1:
        x = var('x')        
        for j in range(1, n):
            subvol = V(g, n-1, [x] + Ls[1:j] + Ls[j+1:])
            total += integrate_one_free(subvol, L1_val, Ls[j])

if I put a print statement in my "for deg" loop with an example expression, I get output:

polynomial is (8.5*pi^4 + 0.166*pi^2*x^2)*x
coeff is 8.5*pi^4 + 0.166*pi^2*x^2 and deg is 1