Polynomial multiplication is a common operation in algebra, used in areas such as scientific computing, engineering simulations, and symbolic algebra systems. When working with two-variable polynomials, the process involves combining like terms and multiplying each term of one polynomial with every term of another.
What Is a Two-Variable Polynomial?
A two-variable polynomial takes the form:
P(x, y) = a₁x^i y^j + a₂x^k y^l + ...
Each term consists of:
- A coefficient
- An exponent for variable
x - An exponent for variable
y
How Does Polynomial Multiplication Work?
Let’s take two example polynomials:
P1(x, y) = 2x + 3y
P2(x, y) = x + y
Multiplication (P1 × P2):
Multiply each term of P1 by each term of P2:
= (2x)(x) + (2x)(y) + (3y)(x) + (3y)(y)
= 2x² + 2xy + 3xy + 3y²
= 2x² + 5xy + 3y²
The final result combines like terms (in this case, 2xy + 3xy = 5xy).
C Structure to Represent Terms
We’ll define a Term structure to hold each term’s data:
typedef struct {
int coeff;
int expX;
int expY;
} Term;
We’ll use arrays of terms to represent each polynomial.
C Program to Multiply Two-Variable Polynomials
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
typedef struct {
int coeff;
int expX;
int expY;
} Term;
// Input a polynomial
int inputPolynomial(Term poly[]) {
int n;
printf("Enter number of terms: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("Enter coefficient, exponent of x and exponent of y for term %d: ", i + 1);
scanf("%d %d %d", &poly[i].coeff, &poly[i].expX, &poly[i].expY);
}
return n;
}
// Display polynomial
void displayPolynomial(Term poly[], int n) {
for (int i = 0; i < n; i++) {
if (poly[i].coeff != 0) {
printf("%d*x^%d*y^%d", poly[i].coeff, poly[i].expX, poly[i].expY);
if (i < n - 1)
printf(" + ");
}
}
printf("\n");
}
// Combine like terms
int combineTerms(Term result[], int n) {
for (int i = 0; i < n; i++) {
if (result[i].coeff == 0)
continue;
for (int j = i + 1; j < n; j++) {
if (result[i].expX == result[j].expX && result[i].expY == result[j].expY) {
result[i].coeff += result[j].coeff;
result[j].coeff = 0; // Mark as combined
}
}
}
// Remove terms with 0 coefficient
Term temp[MAX];
int k = 0;
for (int i = 0; i < n; i++) {
if (result[i].coeff != 0) {
temp[k++] = result[i];
}
}
for (int i = 0; i < k; i++) {
result[i] = temp[i];
}
return k;
}
// Multiply two polynomials
int multiplyPolynomials(Term p1[], int n1, Term p2[], int n2, Term result[]) {
int k = 0;
for (int i = 0; i < n1; i++) {
for (int j = 0; j < n2; j++) {
result[k].coeff = p1[i].coeff * p2[j].coeff;
result[k].expX = p1[i].expX + p2[j].expX;
result[k].expY = p1[i].expY + p2[j].expY;
k++;
}
}
return combineTerms(result, k);
}
// Main function
int main() {
Term poly1[MAX], poly2[MAX], result[MAX];
int n1, n2, nResult;
printf("Enter first polynomial\n");
n1 = inputPolynomial(poly1);
printf("Enter second polynomial\n");
n2 = inputPolynomial(poly2);
printf("\nFirst Polynomial: ");
displayPolynomial(poly1, n1);
printf("Second Polynomial: ");
displayPolynomial(poly2, n2);
nResult = multiplyPolynomials(poly1, n1, poly2, n2, result);
printf("\nResult (P1 * P2): ");
displayPolynomial(result, nResult);
return 0;
}
Output
Enter first polynomial
Enter number of terms: 2
Enter coefficient, exponent of x and exponent of y for term 1: 4 2 1
Enter coefficient, exponent of x and exponent of y for term 2: 5 3 1
Enter second polynomial
Enter number of terms: 2
Enter coefficient, exponent of x and exponent of y for term 1: 6 3 1
Enter coefficient, exponent of x and exponent of y for term 2: 4 2 1
First Polynomial: 4*x^2*y^1 + 5*x^3*y^1
Second Polynomial: 6*x^3*y^1 + 4*x^2*y^1
Result (P1 * P2): 44*x^5*y^2 + 16*x^4*y^2 + 30*x^6*y^2
Explanation of Code
Header Files and Definitions
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
The above code includes stdio.h for input/output functions like printf and scanf, and stdlib.h for functions related to memory allocation and other utilities. The line #define MAX 100 sets a constant value of 100, which can be used to limit the maximum number of terms in a polynomial or for other array size restrictions.
Polynomial Term Structure
typedef struct {
int coeff;
int expX;
int expY;
} Term;
This above code defines a new data type called Term using typedef struct. Each Term represents a single term of a polynomial with three fields: coeff for the coefficient, expX for the exponent of the variable x, and expY for the exponent of the variable y. This structure makes it easy to store and manipulate polynomial terms in the program.
Input a Polynomial
int inputPolynomial(Term poly[]) {
int n;
printf("Enter number of terms: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("Enter coefficient, exponent of x and exponent of y for term %d: ", i + 1);
scanf("%d %d %d", &poly[i].coeff, &poly[i].expX, &poly[i].expY);
}
return n;
}
The above function inputPolynomial is used to read a polynomial from the user and store it in an array of Term structures. It first asks the user to enter the number of terms in the polynomial. Then, for each term, it prompts the user to input the coefficient, exponent of x, and exponent of y, and stores these values in the corresponding fields of the array. Finally, it returns the total number of terms entered so that the program knows how many terms the polynomial contains.
Display Polynomial
void displayPolynomial(Term poly[], int n) {
for (int i = 0; i < n; i++) {
if (poly[i].coeff != 0) {
printf("%d*x^%d*y^%d", poly[i].coeff, poly[i].expX, poly[i].expY);
if (i < n - 1)
printf(" + ");
}
}
printf("\n");
}
The above function displayPolynomial is used to print a polynomial stored in an array of Term structures. It loops through each term and, if the coefficient is not zero, prints it in the format coeff*x^expX*y^expY. After each term (except the last one), it prints a + sign to separate the terms. Finally, it prints a newline to complete the display of the polynomial
Combine Like Terms
int combineTerms(Term result[], int n) {
for (int i = 0; i < n; i++) {
if (result[i].coeff == 0) continue;
for (int j = i + 1; j < n; j++) {
if (result[i].expX == result[j].expX && result[i].expY == result[j].expY) {
result[i].coeff += result[j].coeff;
result[j].coeff = 0; // mark as combined
}
}
}
// Remove terms with 0 coefficient
Term temp[MAX];
int k = 0;
for (int i = 0; i < n; i++) {
if (result[i].coeff != 0) temp[k++] = result[i];
}
for (int i = 0; i < k; i++) result[i] = temp[i];
return k;
}
The above function combineTerms is used to merge like terms in a polynomial stored in the result array. It goes through each term and checks if any other term has the same exponents for x and y. If such a term is found, their coefficients are added together, and the duplicate term is marked with a coefficient of 0. After combining, all terms with a coefficient of 0 are removed, and the remaining terms are shifted to form a clean polynomial. The function then returns the new number of terms in the polynomial.
Multiply Two Polynomials
int multiplyPolynomials(Term p1[], int n1, Term p2[], int n2, Term result[]) {
int k = 0;
for (int i = 0; i < n1; i++) {
for (int j = 0; j < n2; j++) {
result[k].coeff = p1[i].coeff * p2[j].coeff;
result[k].expX = p1[i].expX + p2[j].expX;
result[k].expY = p1[i].expY + p2[j].expY;
k++;
}
}
return combineTerms(result, k);
}
The above function multiplyPolynomials is used to multiply two polynomials stored in the arrays p1 and p2. It loops through each term of the first polynomial and multiplies it with every term of the second polynomial. For each multiplication, it calculates the new coefficient by multiplying the coefficients and calculates the new exponents by adding the exponents of x and y. Each resulting term is stored in the result array. After all terms are multiplied, it calls combineTerms to merge like terms and returns the final number of terms in the resulting polynomial.
Main Function
int main() {
Term poly1[MAX], poly2[MAX], result[MAX];
int n1, n2, nResult;
printf("Enter first polynomial\n");
n1 = inputPolynomial(poly1);
printf("Enter second polynomial\n");
n2 = inputPolynomial(poly2);
printf("\nFirst Polynomial: ");
displayPolynomial(poly1, n1);
printf("Second Polynomial: ");
displayPolynomial(poly2, n2);
nResult = multiplyPolynomials(poly1, n1, poly2, n2, result);
printf("\nResult (P1 * P2): ");
displayPolynomial(result, nResult);
return 0;
}
The main function demonstrates polynomial multiplication using the previously defined functions. It first declares arrays poly1, poly2, and result to store the terms of the two input polynomials and their product. It then reads the first and second polynomials from the user using inputPolynomial and displays them using displayPolynomial. Next, it multiplies the two polynomials with multiplyPolynomials, which also combines like terms, and stores the result in the result array. Finally, it displays the resulting polynomial and ends the program.
Time & Space Complexity
| Operation | Time Complexity |
|---|---|
inputPolynomial() | O(n) |
displayPolynomial() | O(n) |
combineTerms() | O(n²) |
multiplyPolynomials() | O(n1 * n2 + k²) |
main() (overall) | O(n1 * n2 + k²) |
| Operation | Space Complexity |
|---|---|
inputPolynomial() | O(n) |
displayPolynomial() | O(1) |
combineTerms() | O(n) |
multiplyPolynomials() | O(n1 * n2) |
main() (overall) | O(n1 + n2 + n1 * n2) |