📜  微分给定多项式的程序

📅  最后修改于: 2021-09-22 10:47:19             🧑  作者: Mango

给定多项式字符串str ,任务是对给定的字符串进行微分,并在微分后打印该字符串。
注意:输入格式是这样的,在术语和“+”、“-”符号之间有一个空格
例子:

方法:思想是观察当给定的方程由多个多项式组成时

p(x) = p1(x) + p2(x)

, 给定多项式的微分

p'(x) = p1'(x) + p2'(x)

.并且,已知的导数

p(x) = AX^N

p'(x) = A*N*X^{N - 1}

因此,我们拆分给定的字符串并区分其中的每个术语。
下面是上述方法的实现:

C++
// C++ program to differentiate the
// given polynomial
 
#include "bits/stdc++.h"
#define MOD (1e9 + 7);
using ll = int64_t;
using ull = uint64_t;
#define ll long long
using namespace std;
 
// Function to differentiate the
// given term
string diffTerm(string pTerm)
{
    // Get the coefficient
    string coeffStr = "", S = "";
    int i;
 
    // Loop to get the coefficient
    for (i = 0; pTerm[i] != 'x'; i++)
        coeffStr.push_back(pTerm[i]);
 
    long long coeff
        = atol(coeffStr.c_str());
 
    // Loop to get the power of each term
    string powStr = "";
    for (i = i + 2; i != pTerm.size(); i++)
        powStr.push_back(pTerm[i]);
 
    long long power
        = atol(powStr.c_str());
    string a, b;
 
    // Converting the value
    // to the string
    ostringstream str1, str2;
 
    // For ax^n, we find (n)*a*x^(n-1)
    coeff = coeff * power;
    str1 << coeff;
    a = str1.str();
    power--;
    str2 << power;
    b = str2.str();
    S += a + "X^" + b;
 
    return S;
}
 
// Function to differentiate the
// given polynomial
string diffstr(string& poly)
{
 
    // We use istringstream to get
    // the input in tokens
    istringstream is(poly);
 
    string pTerm, S = "";
 
    // For every token, compute the
    // differentiation
    while (is >> pTerm) {
 
        // If the token is equal to
        // '+', '-' then
        // continue with the string
        if (pTerm == "+") {
            S += " + ";
            continue;
        }
 
        if (pTerm == "-") {
            S += " - ";
            continue;
        }
 
        // Otherwise find the differentiation
        // of that particular term
        else
            S += diffTerm(pTerm);
    }
    return S;
}
 
// Driver code
int main()
{
    string str = "5x^4 + 6x^2 + 5x^2";
    cout << diffstr(str);
    return 0;
}


Python3
# Python3 program to differentiate
# the given polynomial
MOD = (1e9 + 7)
 
# Function to differentiate
# the given term
def diffTerm(pTerm):
 
    # Get the coefficient
    coeffStr = ""
    S = ""
 
    # Loop to get the
    # coefficient
    i = 0
    while (i < len(pTerm) and
           pTerm[i] != 'x'):
        coeffStr += (pTerm[i])
        i += 1
 
    coeff = int(coeffStr)
 
    # Loop to get the power
    # of each term
    powStr = ""
    j = i + 2
    while j < len(pTerm):
        powStr += (pTerm[j])
        j += 1
 
    power = int(powStr)
 
    # For ax^n, we find
    # (n)*a*x^(n-1)
    coeff = coeff * power
    a = str(coeff)
    power -= 1
    b = str(power)
    S += a + "X^" + b
 
    return S
 
# Function to differentiate
# the given polynomial
def diffstr(poly):
 
    pTerm = poly.split(" ")
    S = ""
     
    for i in range(len(pTerm)):
 
        # If the token is equal to
        # '+', '-' then
        # continue with the string
        if (pTerm[i] == "+"):
            S += " + "
            continue
 
        if (pTerm[i] == "-"):
            S += " - "
            continue
 
        # Otherwise find the differentiation
        # of that particular term
        else:
            S += diffTerm(pTerm[i])
 
    return S
 
# Driver code
if __name__ == "__main__":
 
    st = "5x^4 + 6x^2 + 5x^2"
    print(diffstr(st))
 
# This code is contributed by Chitranayal


输出:
20X^3 + 12X^1 + 10X^1