Define polynomial inductively and symbolic calculation

101 Views Asked by At

I need to define polynomials like $n\sum_{i=1}^aX^i +m \sum_{i=1}^bX^i$, where $a,b,n$ and $m$ are unknown integers, and then sum polynomials like this up. How should I do this in Sage?

Should I define the polynomial inductively by for loop? And how to make $a,b,n$ and $m$ to be variables? I am really new to programming, sorry for the basic questions!

2

There are 2 best solutions below

0
On BEST ANSWER

Those polynomials in X are just standard geometric series so use the formula for that.

0
On

Probably off-topic here and more adapted to Ask Sage (ask.sagemath.org) or sage-support (groups.google.com/g/sage-support).

Here is a way.

Define a polynomial ring and its variable:

sage: R = PolynomialRing(QQ, 'X')
sage: X = R.gen()

Alternatively, use the shortcut syntax:

sage: R.<X> = QQ[]

Define a function to produce $n \sum_{i = 1}^{a} X^i$:

sage: def poly(a, n):
....:     return R([0] + [n]*a)

Explanation: the polynomial ring can transform a list of coefficients into a polynomial. The polynomial we want has a zero constant term, followed by a monomials for the following a powers of X, with coefficients all equal to n.

Now use that function:

sage: poly(3, 2))
2*X^3 + 2*X^2 + 2*X

sage: poly(2, 3))
3*X^2 + 3*X

sage: poly(3, 2) + poly(2, 3))
2*X^3 + 5*X^2 + 5*X