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