]> www.ginac.de Git - ginac.git/blob - doc/examples/archive1.cpp
Documented compile_ex, link_ex, unlink_ex.
[ginac.git] / doc / examples / archive1.cpp
1 /*
2  *  Archiving code example from the tutorial
3  */
4
5 #include <fstream>
6 using namespace std;
7 #include <ginac/ginac.h>
8 using namespace GiNaC;
9
10 int main()
11 {
12         symbol x("x"), y("y"), z("z");
13
14         // do some math
15         
16         ex foo = sin(x + 2*y) + 3*z + 41;
17         ex bar = foo + 1;
18
19         // write the archive
20         
21         archive a;
22         a.archive_ex(foo, "foo");
23         a.archive_ex(bar, "the second one");
24
25         ofstream out("foobar.gar");
26         out << a;
27         out.close();
28
29         // read in the archive
30         
31         archive a2;
32         ifstream in("foobar.gar");
33         in >> a2;
34
35         lst syms;
36         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