File: derivative.cpp

package info (click to toggle)
ginac 1.8.9-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 6,640 kB
  • sloc: cpp: 49,195; sh: 5,402; makefile: 448; python: 193
file content (34 lines) | stat: -rw-r--r-- 766 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// 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>
#ifdef IN_GINAC
#include "ginac.h"
#else
#include <ginac/ginac.h>
#endif
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;
	}
}