Open In App

PHP Program to Add Two Polynomials

Given two polynomials, the task is to add two polynomials in PHP. Polynomials are mathematical expressions consisting of variables and coefficients. Adding two polynomials involves combining like terms (terms with the same variable raised to the same power).

Example:

Input: 
    A(x) = 5 + 10x^2 + 6x^3
    B(x) = 1 + 2x + 4x^2

Output: Sum(x) = 6 + 2x + 14x^2 + 6x^3

Add Two Polynomials using Arrays

One way to represent and add polynomials in PHP is by using arrays, where the elements represent the values of coefficients.

Example: This example shows the use of the above-explained approach.

<?php

// X[] represents coefficients of first polynomial
// Y[] represents coefficients of second polynomial
// m and n are sizes of X[] and Y[] respectively
function addition($X, $Y, $m, $n) {
    $size = max($m, $n);
    $sum = array_fill(0, $size, 0);
    
    // Initialize the product polynomial
    for ($i = 0; $i < $m; $i++)
        $sum[$i] = $X[$i];
    
    // Take every term of the second polynomial
    for ($i = 0; $i < $n; $i++)
        $sum[$i] += $Y[$i];
    
    return $sum;
}

// A utility function to print a polynomial
function printPolynomial($poly, $n) {
    for ($i = 0; $i < $n; $i++) {
        if ($poly[$i] != 0) {
            if ($i != 0 && $poly[$i] > 0)
                echo " + ";
            elseif ($poly[$i] < 0)
                echo " - ";
            // Print absolute value of coefficient
            echo abs($poly[$i]);
            if ($i != 0)
                echo "x^" . $i;
        }
    }
}

// Polynomial 5 + 0x^1 + 10x^2 + -6x^3
$X = array(5, 0, 10, -6);
// Polynomial 1 + 2x^1 + 4x^2
$Y = array(1, 2, 4);

$m = count($X);
$n = count($Y);

echo "First polynomial: ";
printPolynomial($X, $m);

echo "\nSecond polynomial: ";
printPolynomial($Y, $n);

$sum = addition($X, $Y, $m, $n);
$size = max($m, $n);

echo "\nPolynomial Sum: ";
printPolynomial($sum, $size);

?>

Output
First polynomial: 5 + 10x^2 - 6x^3
Second polynomial: 1 + 2x^1 + 4x^2
Polynomial Sum: 6 + 2x^1 + 14x^2 - 6x^3

Add Two Polynomials using Associative Arrays

First, we create an associative array of each polynomial, where the key is the degree of the term and the value is the coefficient of the term. The addition of two polynomials involves adding the coefficients of the terms with the same degree.

Example: This example shows the use of the above-explained approach.

<?php

function addPolynomials($poly1, $poly2) {
    $result = [];

    // Add terms of the first polynomial to the result
    foreach ($poly1 as $degree => $coefficient) {
        $result[$degree] = $coefficient;
    }

    // Add terms of the second polynomial to the result
    foreach ($poly2 as $degree => $coefficient) {
        if (isset($result[$degree])) {
            $result[$degree] += $coefficient;
        } else {
            $result[$degree] = $coefficient;
        }
    }

    // Sort the result by degree
    // in descending order
    krsort($result);

    return $result;
}

// Driver code
$poly1 = [2 => 3, 1 => 4, 0 => 2]; // 3x^2 + 4x + 2
$poly2 = [3 => 1, 1 => 2, 0 => 3]; // x^3 + 2x + 3

$result = addPolynomials($poly1, $poly2);
print_r($result);

?>

Output
Array
(
    [3] => 1
    [2] => 3
    [1] => 6
    [0] => 5
)
Article Tags :