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, with the only difference being the subtraction operation.

Steps in Subtracting Single-Variable Polynomials

  1. Identify Polynomials: Start by clearly identifying the two polynomials you intend to subtract. Let’s consider Polynomial A and Polynomial B as examples.
  2. Arrange Polynomials: Ensure both polynomials are arranged in descending order of their degrees. For example, a polynomial like 7x^2 + 3 should be written as 7x^2 + 0x + 3.
  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.
  4. 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 7x^3 and Polynomial B has 3x^3, then the resulting term after subtraction is (7 – 3)x^3 = 4x^3.
    • If a term appears in Polynomial A but not in B, subtract 0 from the coefficient of A.
  5. Compile the Result: Assemble the resulting terms to form the new polynomial. This is the result of the subtraction.
  6. Simplify the Result: Combine like terms, if any, and simplify the polynomial.
  7. 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: 7x^3 + 5x^2 + 4x + 9
Polynomial B: 3x^3 + 2x + 5

Step by Step Subtraction:

  • Arrange both polynomials in descending order, adding missing degrees with a coefficient of zero:
    • Polynomial A: 7x^3 + 5x^2 + 4x + 9
    • Polynomial B: 3x^3 + 0x^2 + 2x + 5
  • Subtract corresponding terms:
    • From x^3 terms: 7x^3 – 3x^3 = 4x^3
    • From x^2 terms: 5x^2 – 0x^2 = 5x^2
    • From x terms: 4x – 2x = 2x
    • Constant terms: 9 – 5 = 4
  • Compile the result:
    • The resulting polynomial is 4x^3 + 5x^2 + 2x + 4

Polynomial Subtraction Algorithm

  1. 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).
  2. Find Maximum Degree: Determine the higher degree between the two polynomials.
  3. Subtract Corresponding Terms: For each term (from degree 0 to the maximum degree), subtract the coefficients of the corresponding terms from each polynomial.
  4. Store the Result: Store the result of the subtraction in a new array.
  5. 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= -1x^3 + 5x^2 + 2x^1 + -2x^0 

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: 2x^3 + 5x^2 + 4x + 3 (represented as {3, 4, 5, 2})
  • Polynomial 2: 3x^3 + 2x + 5 (represented as {5, 2, 0, 3})

The program will perform the subtraction for each degree (starting from x^0 to x^3) and print the resultant polynomial.

Hope this tutorial helped you to understand how to perform subtraction between two polynomial.