First time here? Check out the FAQ!

Ask Your Question
0

Issue with Processing Symbolic Polynomial Function

asked 0 years ago

Fractal Rot gravatar image

updated 0 years ago

Max Alekseyev gravatar image

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
Preview: (hide)

1 Answer

Sort by » oldest newest most voted
0

answered 0 years ago

Max Alekseyev gravatar image

updated 0 years ago

You have two different x in your code - one is a symbolic variable defined by x = var('x') and another is a polynomial variable (generator of R). For example, in (8.5*pi^4 + 0.166*pi^2*x^2)*x, the x inside the parentheses is symbolic, while the one outside is polynomial. When you convert a symbolic expression (even if it involves symbolic x) into R, Sage interprets it as a scalar and turns it into a polynomial of degree 0. Here is a simple example:

sage: x = var('x')
sage: R = PolynomialRing(SR, x)
sage: R(x+pi*x^2)
pi*x^2 + x
sage: _.dict()
{0: pi*x^2 + x}

If your goal is to recognize x inside a symbolic expression and turn it into a polynomial variable, then you can do so via converting the expression into a string first:

sage: R(str(x+pi*x^2))
pi*x^2 + x
sage: _.dict()
{1: 1, 2: pi}
Preview: (hide)
link

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 0 years ago

Seen: 148 times

Last updated: Feb 20