]> www.ginac.de Git - ginac.git/blob - doc/examples/archive1.cpp
Build example programs to avoid breaking them on API changes
[ginac.git] / doc / examples / archive1.cpp
1 /*
2  *  Archiving code example from the tutorial
3  */
4
5 #include <fstream>
6
7 #ifdef IN_GINAC
8 #include "ginac.h"
9 #else
10 #include <ginac/ginac.h>
11 #endif
12 using namespace std;
13 using namespace GiNaC;
14
15 int main()
16 {
17         symbol x("x"), y("y"), z("z");
18
19         // do some math
20         
21         ex foo = sin(x + 2*y) + 3*z + 41;
22         ex bar = foo + 1;
23
24         // write the archive
25         
26         archive a;
27         a.archive_ex(foo, "foo");
28         a.archive_ex(bar, "the second one");
29
30         ofstream out("foobar.gar");
31         out << a;
32         out.close();
33
34         // read in the archive
35         
36         archive a2;
37         ifstream in("foobar.gar");
38         in >> a2;
39
40         lst syms = {x, y};
41
42         ex ex1 = a2.unarchive_ex(syms, "foo");
43         ex ex2 = a2.unarchive_ex(syms, "the second one");
44
45         // do some math again
46         
47         cout << ex1 << endl;              // prints "41+sin(x+2*y)+3*z"
48         cout << ex2 << endl;              // prints "42+sin(x+2*y)+3*z"
49         cout << ex1.subs(x == 2) << endl; // prints "41+sin(2+2*y)+3*z"
50 }
51