File: derivative.cpp

package info (click to toggle)
ginac 1.5.8-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 5,628 kB
  • ctags: 4,936
  • sloc: cpp: 44,703; sh: 11,126; perl: 1,157; yacc: 763; makefile: 414; lex: 200; sed: 32
file content (30 lines) | stat: -rw-r--r-- 718 bytes parent folder | download | duplicates (5)
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
// 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;
	}
}