Automate The Differnetiation of Polynomial
Automate The Differnetiation of Polynomial
Abstract—This paper introduces us to the algorithm to com- We initialize a Boolean variable prevConst which is initially
pute the differentiation of a polynomial which is taken as input in false. It is been used so that when the previous term is a
the form of a string. The algorithm uses simple string operations constant so we can replace the operator before that term by
and has a complexity of O(n).
the next operator.
I. I NTRODUCTION Below is the implementation of above approach :
This problem requires us to find the differentiation of
a polynomial . We take the input as a string in the first Algorithm 2 FindDerivative
step , next we parse each term of the string in a function 1: Input:A string s
where we check if the term is a operator , a function of x or 2: stringstream ss(s)
a constant , according to which each of these terms is evaluated. 3: prevConst=false;
4: while(ss >> term)
if term is of form p(x) = axn then p0 (x) = (an) ∗ xn−1 5: if term == ” + ” || term == ” − ” then
else if term is constant p(x) = K then p’(x) = 0. 6: if prevc onstant == f alse then
Also, if p(x) = p1(x) + p2(x) (Here p1 and p2 are polynomials 7: ans = ans + ”” + term
too) then p’(x) = p1’(x) + p2’(x). 8: else
9: ans.erase(ans.length() − 1);
10: ans = ans + term;
This report further contains - 11: end if
II. Algorithm Design 12: else if isConstant(term) == true then
III. Algorithm Analysis 13: prevc onstant = true;
V. Experimental Study 14: continue;
VI. Conclusion 15: else
16: ans = ans + ”” + Di f f erentiate(term);
17: prevc onstant = f alse;
II. A LGORITHM D ESIGN
18: end if
A. Main 19: int n=ans.length();
. We take a string as input and pass it to the derivative 20: if ans[n − 1] ==0 +0 ||ans[n − 1] ==0 −0 then
function. Below is the implementation of above approach : 21: ans = ans.substr(0, n − 1);
22: end if
23: return ans;
Algorithm 1 Main
1: String s;
2: getline(cin,s);
C. IsConstant
3: string ans=Derivative(s);
4: Print(ans); This function helps us to check if the term is constant or
not because derivative of constant and a term that is a function
of x are calculated differently.
B. FindDerivative Below is the implementation of above approach:
. In this function , we break the string into different terms
using stringstream and then these terms are classified into either
a constant , an operator or a function of x by using if-else and
respectively find the derivative of each of these terms.
Algorithm 3 IsConstant We take a string variable which stores the ans and is
1: Input:String s updated as we add each term and we are also using a boolean
2: for i ← 0 to N do constant so the space complexity is as follows.
3: if isal pha(s[i]) then
4: return f alse; Space Complexity : O(2).
5: end if
6: end for 2) IsConstant: In this algorithm , We run a for loop
7: returntrue; through the size of the string passed to check if there exists a
alphabet in the string or not using isalpha() function.If n is
the size of the string passed to the function then complexity
D. Differentiation can be written as follows:
This function finds the derivative of the term which is a Time complexity : O(n).
function of x . From the passed input string we find the
coefficient of the term and the power by slicing the string There is no space occupied in this function.
. We will get the coefficients and power in the form of strings Space Complexity : -
and we need to convert it into integers using the function
atol. Once we get these the derivative is simply (power* xˆ
coefficient-1). 3) Differentiation: In this algorithm , We run two for
Below is the implementation of above approach: loops , first to calculate the coefficient of the term and the
second to calculate the power of the term.
III. A LGORITHM A NALYSIS Best Case : : When the polynomial length is equal to 1.
Here is the time and space complexities of algorithm which complexity = O(1).
we found after doing theoretical analysis of algorithms. Worst Case : : There will be no worst case as the the time
goes on increasing with n.
A. Time Complexity
1) FindDerivative: In this algorithm , We run a while loop n Total Space Complexity
times where n is the no of terms in the given polynomial.If we Space complexity will be constant
assume that n = n1 + n2 +n3 , where n1 is the no of operators Complexity = O(1)
, n2 is the no of terms as a function of x and n3 is the no of
constant terms . So we can write the time complexity as follows
IV. E XPERIMENTAL A NALYSIS
Average Time complexity : O(n1) + O(n2 *Len(coeffStr) + The integrated development environment of C++ is used for
Len(powerStr)) +O(n3 * Len(const term) . processing the algorithm and graphical analysis is done using
GNU Plot. GNU Plot of above algorithm w.r.t time looks
something like this:
V. C ONCLUSION
Through this paper we proposed the algorithm to compute
the differentiation of a given polynomial . The time complexity
of our algorithm is directly proportional to n and the space
complexity is constant.
R EFERENCES
[1] https://math.stackexchange.com/
[2] https://www.geeksforgeeks.org/
[3] http://gnuplot.sourceforge.net/docs4.2/node249.html
[4] https://en.wikipedia.org/wiki/Sieveo fE ratosthenes