Subtraction of Single variable Polynomial : Algorithm and Program
Subtracting single-variable polynomials means looking at terms with the same power and subtracting their coefficients. For example, if one polynomial has 5x² and the other has 3x², the result will have 2x². If a term exists in only one polynomial, it stays the same in the result. By doing this for all terms, we get a new polynomial that shows the difference between the two polynomials.
Steps in Subtracting Single-Variable Polynomials
- Identify Polynomials: Start by clearly identifying the two polynomials you intend to subtract. Let’s consider Polynomial A and Polynomial B as examples.
- Arrange Polynomials: Ensure both polynomials are arranged in descending order of their degrees. For example, a polynomial like 7x2 + 3 should be written as 7x2 + 0x + 3.
- Align Terms by Degree: Line up terms of the same degree from both polynomials. This might involve adding terms with a coefficient of zero for missing degrees in either polynomial.
- Subtract Corresponding Terms: Subtract the coefficients of corresponding terms (same degree) of Polynomial B from Polynomial A.For example:
- If Polynomial A has a term 7x3 and Polynomial B has 3x3, then the resulting term after subtraction is (7 – 3)x3 = 4x3.
- If a term appears in Polynomial A but not in B, subtract 0 from the coefficient of A.
- Compile the Result: Assemble the resulting terms to form the new polynomial. This is the result of the subtraction.
- Simplify the Result: Combine like terms, if any, and simplify the polynomial.
- Final Presentation: Present the final polynomial in a standard format, preferably in descending order of degrees.
Polynomial Subtraction Example:
Let’s illustrate this with an example:
Polynomial A: 7x3 + 5x2 + 4x + 9
Polynomial B: 3x3 + 2x + 5
Step by Step Subtraction:
- Arrange both polynomials in descending order, adding missing degrees with a coefficient of zero:
- Polynomial A: 7x3 + 5x2 + 4x + 9
- Polynomial B: 3x3 + 0x2 + 2x + 5
- Subtract corresponding terms:
- From x3 terms: 7x3 – 3x3 = 4x3
- From x2 terms: 5x2 – 0x2 = 5x2
- From x terms: 4x – 2x = 2x
- Constant terms: 9 – 5 = 4
- Compile the result:
- The resulting polynomial is 4x3 + 5x2 + 2x + 4
Polynomial Subtraction Algorithm
- Initialize Two Arrays for Polynomials: Represent the two polynomials as arrays, with each element being the coefficient of the corresponding power of the variable (usually
x). - Find Maximum Degree: Determine the higher degree between the two polynomials.
- Subtract Corresponding Terms: For each term (from degree 0 to the maximum degree), subtract the coefficients of the corresponding terms from each polynomial.
- Store the Result: Store the result of the subtraction in a new array.
- Handle Missing Terms: If one polynomial has a higher degree, copy the remaining terms as they are to the result array.
C Program to Subtract Two Polynomial
#include <stdio.h>
#define MAX_DEGREE 100
// Polynomials Substraction
void polynomialSubtraction(int polynomial1[], int polynomial2[], int degree1, int degree2) {
int maxDeg = degree1 > degree2 ? degree1 : degree2;
int result[MAX_DEGREE] = {0};
for (int i = 0; i <= maxDeg; i++) {
int val1 = (i <= degree1) ? polynomial1[i] : 0;
int val2 = (i <= degree2) ? polynomial2[i] : 0;
result[i] = val1 - val2;
}
printf("Resultant Polynomial= ");
for (int i = maxDeg; i >= 0; i--) {
if (result[i] != 0) {
printf("%dx^%d ", result[i], i);
if (i != 0) printf("+ ");
}
}
printf("\n");
}
int main() {
// Polynomial1: 2x^3 + 5x^2 + 4x + 3
// Polynomial2: 3x^3 + 2x + 5
int polynomial1[] = {3, 4, 5, 2}; // degree 3
int polynomial2[] = {5, 2, 0, 3}; // degree 3
polynomialSubtraction(polynomial1, polynomial2, 3, 3);
return 0;
}
Output
Resultant Polynomial= -1x3 + 5x2 + 2x1 + (-2x0)
Program Explanation
Header and Macro Definition:
#include <stdio.h>
#define MAX_DEGREE 100
#include <stdio.h>: Includes the Standard Input Output header file for functions like printf.
#define MAX_DEGREE 100: Defines a macro for the maximum degree of the polynomial, set to 100.
Polynomial Subtraction Function:
// Polynomials Substraction
void polynomialSubtraction(int polynomial1[], int polynomial2[], int degree1, int degree2) {
int maxDeg = degree1 > degree2 ? degree1 : degree2;
int result[MAX_DEGREE] = {0};
for (int i = 0; i <= maxDeg; i++) {
int val1 = (i <= degree1) ? polynomial1[i] : 0;
int val2 = (i <= degree2) ? polynomial2[i] : 0;
result[i] = val1 - val2;
}
printf("Resultant Polynomial= ");
for (int i = maxDeg; i >= 0; i--) {
if (result[i] != 0) {
printf("%dx^%d ", result[i], i);
if (i != 0) printf("+ ");
}
}
printf("\n");
}
void polynomialSubtraction(int polynomial1[], int polynomial2[], int degree1, int degree2): This function takes two integer arrays representing the coefficients of two polynomials and their respective degrees.
int maxDeg = degree1 > degree2 ? degree1 : degree2: Determines the maximum degree between the two polynomials.
int result[MAX_DEGREE] = {0}: Initializes an array to store the result of the polynomial subtraction with all elements set to zero.
The for loop iterates up to the maximum degree:
val1 and val2 hold the coefficients of the current term from each polynomial or 0 if the term does not exist in the polynomial.
The coefficients of the corresponding terms are subtracted and stored in result[i].
The second for loop iterates through the result array in reverse order to print the resultant polynomial. Zero coefficients are skipped.
Main Function:
int main() {
// Polynomial1: 2x^3 + 5x^2 + 4x + 3
// Polynomial2: 3x^3 + 2x + 5
int polynomial1[] = {3, 4, 5, 2}; // degree 3
int polynomial2[] = {5, 2, 0, 3}; // degree 3
polynomialSubtraction(polynomial1, polynomial2, 3, 3);
return 0;
}
Two arrays polynomial1 and polynomial2 represent the coefficients of two polynomials. The arrays are defined in reverse order of their degrees (constant term first). The polynomialSubtraction function is called with these arrays and their degrees. The program prints the result of the subtraction.
Example Execution
Given the input polynomials:
- Polynomial 1: 2x3 + 5x2 + 4x + 3 (represented as {3, 4, 5, 2})
- Polynomial 2: 3x3 + 2x + 5 (represented as {5, 2, 0, 3})
The program will perform the subtraction for each degree (starting from x0 to x3) and print the resultant polynomial.
Time & Space Complexity
| Operation | Time Complexity |
|---|---|
polynomialSubtraction() | O(max(degree1, degree2)) |
main() (overall) | O(max(degree1, degree2)) |
| Operation | Space Complexity |
|---|---|
polynomialSubtraction() | O(max(degree1, degree2) + 1) |
main() (overall) | O(max(degree1, degree2) + 1) |