# Program to calculate the sum of polynomials

• MHB
• mathmari
In summary: P1, p2) : pol = {} key = 0 x = True while x == True : if key in P1 and key in P2: sum_pol = P1[key]+P2[key] pol['key'] = sum_pol elif key in P1: sum_pol = P1[key]
mathmari
Gold Member
MHB
Hey!

A polynomial can be represented by a dictionary by setting the powers as keys and the coefficients as values. For example $x^12+4x^5-7x^2-1$ can be represented by the dictionary as $\{0 : -1, 2 : -7, 5 : 4, 12 : 1\}$. Write a function in Python that has as arguments two polynomials in dictionary representation and returns the dictionary that represent the sum of the polynomials.

I wrote the following program :

Code:
def add_poly(P1, p2) :
p3 = {}
for key in p1:
if key in p2:
sum_pol = p1[key]+p2[key]
p3[key] = sum_pol
return p3

p1 = {0:-1, 2:-7, 5:4, 12:1}
p2 = {0:2, 2:5, 5:2, 12:3}
print(add_poly(p1,p2))

But this works only if the given polynomials have the same powers, right?
What can we change to consider the general case?

I thought to do something like :

Code:
def add_poly(p1, p2) :
pol = {}
key = 0
x = True
while x == True :
if key in p1 and key in p2:
sum_pol = p1[key]+p2[key]
pol['key'] = sum_pol
elif key in p1 :
sum_pol = p1[key]
pol['key'] = sum_pol
elif key in p2 :
sum_pol = p2[key]
pol['key'] = sum_pol
elif key > len(p1) and key > len(p2) :
x = False
else :
pass
key += 1
return pol

p1 = {0:-1, 2:-7, 5:4, 12:1}
p2 = {0:2, 2:5, 3:2, 12:3}
print(add_poly(p1,p2))

But this doesn't work correctly.

:unsure:

Hey mathmari!

I can do:
Code:
python
>>> len({0:-1, 2:-7, 5:4, 12:1})
4

But 4 is not the highest power is it? (Worried)

Klaas van Aarsen said:
I can do:
Code:
python
>>> len({0:-1, 2:-7, 5:4, 12:1})
4

But 4 is not the highest power is it? (Worried)

So is the part

Code:
elif key > len(p1) and key > len(p2) :
x = False

wrong? :unsure:

mathmari said:
So is the part
elif key > len(p1) and key > len(p2) : x = False
wrong?
Indeed. Instead we need to check if the key is greater than the greatest key in both p1 and p2.

We can use max(p1) to find the highest power in p1.

But basically we want to iterate over the set that both polynomials have in common.
We can construct it with set().union(p1.keys(), p2.keys()).
Then we can do for power in set().union(p1.keys(), p2.keys()): ....

Additionally, we can do p1.get(key, 0) to get the power if there is one, or 0 otherwise.
Then we don't have to use if statements, but we can just do p1.get(key, 0) + p2.get(key, 0).

## 1. How does the program calculate the sum of polynomials?

The program uses the concept of polynomial addition, where the coefficients of the same degree terms are added together to obtain the sum. For example, adding 3x^2 + 4x + 1 and 2x^2 + 5x + 2 would result in 5x^2 + 9x + 3.

## 2. Can the program handle polynomials of different degrees?

Yes, the program is designed to handle polynomials of any degree. It will automatically add terms with the same degree and leave the terms with different degrees as they are.

## 3. What happens if there are no terms with the same degree in the polynomials?

If there are no terms with the same degree, the program will simply combine all the terms and present the final result. For example, adding 3x^2 + 4x + 1 and 5x + 2 would result in 3x^2 + 9x + 3.

## 4. Is there a limit to the number of polynomials that can be added?

No, there is no limit to the number of polynomials that can be added. The program allows the user to input as many polynomials as needed and will calculate the sum accordingly.

## 5. Are there any special cases that the program can't handle?

As long as the input is in the correct format (i.e. with correct coefficients and exponents), the program can handle any polynomial. However, if the input is not in the correct format, the program will prompt the user to re-enter the polynomials in the correct format.

Replies
1
Views
796
Replies
5
Views
2K
Replies
3
Views
1K
Replies
1
Views
1K
Replies
5
Views
2K
Replies
6
Views
1K
Replies
1
Views
1K
Replies
2
Views
2K
Replies
7
Views
1K
Replies
13
Views
1K