w3resource

Python Library for Polynomial Arithmetic

Write a Python program to create a Python library for working with polynomial arithmetic.

The task involves creating a Python library to handle polynomial arithmetic. This library should support the creation and manipulation of polynomials, including operations such as addition, subtraction, multiplication, and differentiation. Additionally, it should provide methods for evaluating polynomial expressions at given values. The goal is to enable users to perform these polynomial operations easily and efficiently through a well-defined API.

Sample Solution:

Python Code :

# Define a class for Polynomial arithmetic
class Polynomial:
    # Initialize the polynomial with a list of coefficients
    def __init__(self, coefficients):
        # Coefficients are ordered from the constant term to the highest degree
        self.coefficients = coefficients

    # Return a string representation of the polynomial for debugging
    def __repr__(self):
        return "Polynomial(" + ", ".join(map(str, self.coefficients)) + ")"

    # Return a human-readable string representation of the polynomial
    def __str__(self):
        # Create a list to hold terms of the polynomial
        terms = []
        # Iterate over the coefficients and their indices
        for i, coef in enumerate(self.coefficients):
            # Skip zero coefficients
            if coef != 0:
                # Add appropriate string for the term based on its degree
                if i == 0:
                    terms.append(str(coef))
                elif i == 1:
                    terms.append(f"{coef}x")
                else:
                    terms.append(f"{coef}x^{i}")
        # Join the terms with ' + ' and return, or return "0" if no terms
        return " + ".join(terms) if terms else "0"

    # Add two polynomials
    def __add__(self, other):
        # Determine the maximum length of the two polynomials
        max_len = max(len(self.coefficients), len(other.coefficients))
        # Initialize the result with zeros
        result = [0] * max_len
        # Add coefficients of the same degree
        for i in range(max_len):
            if i < len(self.coefficients):
                result[i] += self.coefficients[i]
            if i < len(other.coefficients):
                result[i] += other.coefficients[i]
        # Return a new Polynomial object with the result
        return Polynomial(result)

    # Subtract one polynomial from another
    def __sub__(self, other):
        # Determine the maximum length of the two polynomials
        max_len = max(len(self.coefficients), len(other.coefficients))
        # Initialize the result with zeros
        result = [0] * max_len
        # Subtract coefficients of the same degree
        for i in range(max_len):
            if i < len(self.coefficients):
                result[i] += self.coefficients[i]
            if i < len(other.coefficients):
                result[i] -= other.coefficients[i]
        # Return a new Polynomial object with the result
        return Polynomial(result)

    # Multiply two polynomials
    def __mul__(self, other):
        # Initialize the result with zeros, size is sum of lengths minus 1
        result = [0] * (len(self.coefficients) + len(other.coefficients) - 1)
        # Perform polynomial multiplication
        for i, a in enumerate(self.coefficients):
            for j, b in enumerate(other.coefficients):
                result[i + j] += a * b
        # Return a new Polynomial object with the result
        return Polynomial(result)

    # Check if two polynomials are equal
    def __eq__(self, other):
        # Compare coefficients of both polynomials
        return self.coefficients == other.coefficients

    # Differentiate the polynomial
    def differentiate(self):
        # If the polynomial is a constant, return zero polynomial
        if len(self.coefficients) == 1:
            return Polynomial([0])
        # Compute the derivative coefficients
        result = [i * coef for i, coef in enumerate(self.coefficients)][1:]
        # Return a new Polynomial object with the result
        return Polynomial(result)

    # Evaluate the polynomial at a given value of x
    def evaluate(self, x):
        # Compute the polynomial value by summing up the terms
        return sum(coef * (x ** i) for i, coef in enumerate(self.coefficients))


# Example usage:
if __name__ == "__main__":
    # Create polynomial p1: 1 + 2x + 3x^2
    p1 = Polynomial([1, 2, 3])
    # Create polynomial p2: 3 + 4x
    p2 = Polynomial([3, 4])

    # Print polynomial p1
    print("P1:", p1)
    # Print polynomial p2
    print("P2:", p2)

    # Print the result of adding p1 and p2
    print("P1 + P2:", p1 + p2)
    # Print the result of subtracting p2 from p1
    print("P1 - P2:", p1 - p2)
    # Print the result of multiplying p1 and p2
    print("P1 * P2:", p1 * p2)
    # Check and print if p1 and p2 are equal
    print("P1 == P2:", p1 == p2)

    # Print the derivative of p1
    print("P1 differentiated:", p1.differentiate())
    # Print the value of p2 evaluated at x=2
    print("P2 evaluated at x=2:", p2.evaluate(2))

Output:

P1: 1 + 2x + 3x^2
P2: 3 + 4x
P1 + P2: 4 + 6x + 3x^2
P1 - P2: -2 + -2x + 3x^2
P1 * P2: 3 + 10x + 17x^2 + 12x^3
P1 == P2: False
P1 differentiated: 2 + 6x
P2 evaluated at x=2: 11

Explanation:

  • Class Initialization (__init__):
  • coefficients: List of coefficients starting from the constant term.
  • String Representations (__repr__ and str):
  • repr: Provides an unambiguous string representation.
  • str: Provides a human-readable string representation of the polynomial.
  • Addition (__add__):
  • Adds two polynomials by summing their coefficients.
  • Subtraction (__sub__):
  • Subtracts one polynomial from another by subtracting their coefficients.
  • Multiplication (__mul__):
  • Multiplies two polynomials using nested loops.
  • Equality Check (__eq__):
  • Checks if two polynomials are equal by comparing their coefficients.
  • Differentiation (differentiate):
  • Differentiates the polynomial.
  • Evaluation (evaluate):
  • Evaluates the polynomial at a given value of 'x'.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Real-Time Data Visualization Dashboard with Plotly and Dash.
Next: Real-Time Data Visualization Dashboard with Plotly and Dash

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/advanced/create-a-python-library-for-polynomial-arithmetic.php