Skip to content
Related Articles

Related Articles

Improve Article
Partial derivative of a polynomial using Doubly Linked List
  • Difficulty Level : Expert
  • Last Updated : 01 Jun, 2021

Given a 2-variable polynomial represented by a doubly linked list, the task is to find the partial derivative of a polynomial stored in the doubly-linked list.

Examples:

Input: P(x, y) = 2(x^3 y^4) + 3(x^5 y^7) + 1(x^2 y^6)
Output:
Partial derivatives w.r.t. x: 6(x^2 y^4) + 15(x^4 y^7) + 2(x^1 y^6)
Partial derivatives w.r.t. y: 24(x^2 y^3) + 105(x^4 y^6) + 12(x^1 y^5)
Partial derivatives w.r.t. x and y: 144(x^1 y^2) + 2520(x^3 y^5) + 60(x^0 y^4)

Input: P(x, y) = 3(x^2 y^1) + 4(x^2 y^3) + 2(x^4 y^7)
Output:
Partial derivatives w.r.t. x: 6(x^1 y^1) + 8(x^1 y^3) + 8(x^3 y^7)
Partial derivatives w.r.t. y: 6(x^1 y^0) + 24(x^1 y^2) + 56(x^3 y^6)
Partial derivatives w.r.t. x and y: 48(x^0 y^1) + 1008(x^2 y^5)

Approach: Follow the steps belo to solve this problem:



  • Declare a class or structure to store the contents of a node, i.e. data representing the coefficient, power1 representing the power to which x is raised, power2 representing the power to which y is raised, and the pointers to its next and previous node.
  • Declare functions to calculate derivatives with respect to x, derivative with respect to y, and derivative with respect to x and y.
  • Calculate and print the derivaties obtained.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Structure of a node
struct node {
    node* link1 = NULL;
    node* link2 = NULL;
    int data = 0;
    int pow1 = 0;
    int pow2 = 0;
};
 
// Function to generate Doubly Linked
// List from given parameters
void input_equation(node*& head, int d,
                    int p1, int p2)
{
    node* temp = head;
 
    // If list is empty
    if (head == NULL) {
 
        // Create new node
        node* ptr = new node();
        ptr->data = d;
        ptr->pow1 = p1;
        ptr->pow2 = p2;
 
        // Set it as the head
        // of the linked list
        head = ptr;
    }
 
    // If list is not empty
    else {
 
        // Temporarily store
        // address of the head node
        temp = head;
 
        // Traverse the linked list
        while (temp->link2 != NULL) {
 
            // Link to next node
            temp = temp->link2;
        }
 
        // Create new node
        node* ptr = new node();
        ptr->data = d;
        ptr->pow1 = p1;
        ptr->pow2 = p2;
 
        // Connect the nodes
        ptr->link1 = temp;
        temp->link2 = ptr;
    }
}
 
// Function to calculate partial
// derivative w.r.t. X
void derivation_with_x(node*& head)
{
    cout << "Partial derivatives"
         << " w.r.t. x: ";
 
    node* temp = head;
 
    // Traverse the Linked List
    while (temp != NULL) {
 
        if (temp->pow1 != 0) {
            temp->data = (temp->data)
                         * (temp->pow1);
            temp->pow1 = temp->pow1 - 1;
        }
        else {
            temp->data = 0;
            temp->pow1 = 0;
            temp->pow2 = 0;
        }
 
        temp = temp->link2;
    }
 
    temp = head;
 
    cout << " " << temp->data
         << "(x^" << temp->pow1
         << " y^" << temp->pow2
         << ")";
    temp = temp->link2;
 
    while (temp != NULL) {
        cout << " + "
             << temp->data << "(x^"
             << temp->pow1 << " y^"
             << temp->pow2 << ")";
        temp = temp->link2;
    }
 
    cout << "\n";
}
 
// Function to calculate partial
// derivative w.r.t. Y
void derivation_with_y(node*& head)
{
    cout << "Partial derivatives"
         << " w.r.t. y: ";
 
    node* temp = head;
 
    // Traverse the Linked List
    while (temp != NULL) {
 
        if (temp->pow2 != 0) {
            temp->data = (temp->data)
                         * (temp->pow2);
            temp->pow2 = temp->pow2 - 1;
        }
        else {
            temp->data = 0;
            temp->pow1 = 0;
            temp->pow2 = 0;
        }
 
        temp = temp->link2;
    }
 
    temp = head;
    cout << " "
         << temp->data
         << "(x^" << temp->pow1
         << " y^"
         << temp->pow2 << ")";
    temp = temp->link2;
 
    while (temp != NULL) {
        cout << " + "
             << temp->data << "(x^"
             << temp->pow1 << " y^"
             << temp->pow2 << ")";
        temp = temp->link2;
    }
    cout << "\n";
}
 
// Function to calculate partial
// derivative w.r.t. XY
void derivation_with_x_y(node*& head)
{
    cout << "Partial derivatives"
         << " w.r.t. x and y: ";
 
    node* temp = head;
 
    // Derivative with respect to
    // the first variable
    while (temp != NULL) {
        if (temp->pow1 != 0) {
 
            temp->data = (temp->data)
                         * (temp->pow1);
            temp->pow1 = temp->pow1 - 1;
        }
 
        else {
            temp->data = 0;
            temp->pow1 = 0;
            temp->pow2 = 0;
        }
 
        temp = temp->link2;
    }
    temp = head;
 
    // Derivative with respect to
    // the second variable
    while (temp != NULL) {
 
        if (temp->pow2 != 0) {
            temp->data = (temp->data)
                         * (temp->pow2);
            temp->pow2 = temp->pow2 - 1;
        }
 
        else {
            temp->data = 0;
            temp->pow1 = 0;
            temp->pow2 = 0;
        }
 
        temp = temp->link2;
    }
 
    temp = head;
    cout << " "
         << temp->data << "(x^"
         << temp->pow1 << " y^"
         << temp->pow2 << ")";
 
    temp = temp->link2;
 
    // Print the list after the
    // calculating the derivative
    while (temp != NULL) {
 
        cout << " + "
             << temp->data << "(x^"
             << temp->pow1 << " y^"
             << temp->pow2 << ")";
        temp = temp->link2;
    }
    cout << "\n";
}
 
// Driver Code
int main()
{
    node* head1 = NULL;
 
    // Creating doubly-linked list
    input_equation(head1, 2, 3, 4);
    input_equation(head1, 3, 5, 7);
    input_equation(head1, 1, 2, 6);
 
    // Function Call
    derivation_with_x(head1);
    derivation_with_y(head1);
    derivation_with_x_y(head1);
 
    return 0;
}
Output: 
Partial derivatives w.r.t. x:  6(x^2 y^4) + 15(x^4 y^7) + 2(x^1 y^6)
Partial derivatives w.r.t. y:  24(x^2 y^3) + 105(x^4 y^6) + 12(x^1 y^5)
Partial derivatives w.r.t. x and y:  144(x^1 y^2) + 2520(x^3 y^5) + 60(x^0 y^4)

 

Time Complexity: O(N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live 




My Personal Notes arrow_drop_up
Recommended Articles
Page :