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
What did you think?
Similar Reads
-
Subtraction of Two-Variable Polynomial in C
Polynomials with two variables are common in engineering, graphics, and scientific computation. While addition is frequently discussed, subtraction is equally… -
Explain the Addition of Two variable polynomial
Polynomials are fundamental in mathematics, and their use extends into computer science, engineering, physics, and more. While single-variable polynomials are… -
Reverses the Doubly Circular Linked List
Algorithm to Reverse a Doubly Circular Linked List Check if the List is Empty or Has Only One Node: If… -
Algorithm and Program in C to traverse Doubly Circular Linked List
A doubly circular linked list is a special type of linked list in which every node is connected to both… -
Polynomial representation Using Array
Polynomials are fundamental mathematical expressions used extensively in various fields. Representing them efficiently in programming is crucial for calculations and… -
Multiplication of Single Variable Polynomial : Algorithm and Program
Multiplication of single-variable polynomials is a fundamental operation in polynomial algebra. The process involves multiplying each term of one polynomial… -
Subtraction of Single variable Polynomial : Algorithm and Program
Subtracting single-variable polynomials involves reducing each corresponding term of the polynomials from each other. This process is similar to addition,… -
Addition of Single variable Polynomial : Program and Algorithm
The addition of single-variable polynomials is a fundamental concept in algebra, often encountered in mathematics and computer science. Let's break… -
Polynomial Representation Using Linked List
Polynomial representation using linked lists is a critical concept in computer science and mathematics. In this guide, we explore how… -
Delete a Node at End of Doubly Circular Linked List
In this tutorial we will learn writing Algorithm and Program to delete a node from beginning on Doubly Circular Linked…