Open In App

PHP Program to Add Two Polynomials

Last Updated : 01 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

  • Define two polynomials $A and $B as arrays of their coefficients.
  • Calculate the sizes $m and $n of the polynomials.
  • Print the two polynomials using the printPoly() function.
  • Define add() function to add the two polynomials and stores the result in $sum.
  • Print the sum of the two polynomials using the printPoly() function.

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

PHP
<?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.

  • Define a function that takes two associative arrays $poly1 and $poly2 as input, representing the two polynomials to be added.
  • The function iterates over the first polynomial and adds each term to the result array.
  • The function then iterates over the second polynomial. For each term, it checks if the degree already exists in the result array. If it does, the function adds the coefficient to the existing value. If not, it adds a new entry to the result array.
  • The function sorts the resultant polynomial by degree in descending order using krsort().
  • The function returns the resultant polynomial as an associative array.

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

PHP
<?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
)


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads