给定一个由双向链表表示的 2 变量多项式,任务是找到存储在双向链表中的多项式的偏导数。
例子:
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)
方法:按照下面的步骤来解决这个问题:
- 声明一个类或结构来存储节点的内容,即表示系数的数据,power1表示x的幂, power2表示 y的幂,以及指向其下一个和上一个节点的指针。
- 声明函数来计算关于x 的导数、关于y 的导数以及关于x和y 的导数。
- 计算并打印得到的导数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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;
}
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)
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live