# C++ differentiation library

I have a string with some expression and I want get string with derivative of this expression. For example:

``````std::string s = "x^2 - 6*x + 5";
std::string s2 = differentiate(s);
std::cout << s2;
``````

output will be :

``````2 * x - 6
``````

is there some C++ library which can deal with this problem?

• I haven't found answer for this question but I have found better way to deal with my problem: instead of differentiate functions and evaluate them, I will use numerical differentiation. – Тимофей Черников Feb 9 '17 at 11:43

## 1 Answer

You want to do symbolic math in C++; differentiation is just an example of that.

While I don't have experience doing that myself, I can at least help you with a few suggestion:

• ``````Symbolic p("p"), q("q");
cout << df(cos(p[q]^2) - (q^2)*sin(q),q) << endl; // - 2*q*sin(q)-q^(2)*cos(q) -2*sin(p[q]^(2))*p[q]*df(p[q],q)
cout << integrate(ln(q), q) << endl;     // q*ln(q) - q
``````
• ``````// some kind of definition of x and y as symbolic functions
diff( x + y, x ); // returns 1
diff( (2.0 - x) * (3.0 + y), y); // returns 2.0 - x
``````
• ``````symbol x("x"), y("y"), z("z");
ex P = pow(x, 5) + pow(x, 2) + y;

cout << P.diff(x,2) << endl;  // 20*x^3 + 2
cout << P.diff(y) << endl;    // 1
cout << P.diff(z) << endl;    // 0
``````
• C or C++ bindings for Octave - which I understand exist, but I haven't really looked into them. Octave's symbolic math facilities obviously support differentiation.