]> www.ginac.de Git - ginac.git/blobdiff - doc/examples/derivative.cpp
Document the new parser, provide an example.
[ginac.git] / doc / examples / derivative.cpp
diff --git a/doc/examples/derivative.cpp b/doc/examples/derivative.cpp
new file mode 100644 (file)
index 0000000..c1a09b9
--- /dev/null
@@ -0,0 +1,30 @@
+// Input expression containing variable 'x' and compute its derivative
+// with respect to 'x'.
+// Example from the tutorial (chapter Input/Output, section `Expression
+// input').
+#include <iostream>
+#include <string>
+#include <stdexcept>
+#include <ginac/ginac.h>
+using namespace std;
+using namespace GiNaC;
+
+int main()
+{
+       cout << "Enter an expression containing 'x': " << flush;
+       parser reader;
+
+       try {
+               ex e = reader(cin);
+               symtab table = reader.get_syms();
+
+               symbol x = table.find("x") != table.end() ? 
+                          ex_to<symbol>(table["x"]) : symbol("x");
+
+               cout << "The derivative of " << e << " with respect to x is ";
+               cout << e.diff(x) << "." << endl;
+       } catch (exception &p) {
+               cerr << p.what() << endl;
+       }
+}
+