]> www.ginac.de Git - ginac.git/blobdiff - doc/tutorial/ginac.texi
- changed all instances of "foo const &/*" to "const foo &/*"
[ginac.git] / doc / tutorial / ginac.texi
index 5a45924e767a3a90fa6ba7c89331df7e03ce42c2..abcb90c6fc7bc61582e64cc5231f1a7a5eca2ccd 100644 (file)
@@ -636,6 +636,7 @@ meta-class for storing all mathematical objects.
 * Fundamental containers::       The power, add and mul classes.
 * Built-in functions::           Mathematical functions.
 * Relations::                    Equality, Inequality and all that.
+* Archiving::                    Storing expression libraries in files.
 @end menu
 
 
@@ -1117,7 +1118,7 @@ expansion and so on.  Read the next chapter in order to learn more about
 this.
 
 
-@node Relations, Important Algorithms, Built-in functions, Basic Concepts
+@node Relations, Archiving, Built-in functions, Basic Concepts
 @c    node-name, next, previous, up
 @section Relations
 @cindex relations (class @code{relational})
@@ -1134,7 +1135,96 @@ the @code{.subs()} method show how objects of class relational are used
 as arguments.  There they provide an intuitive syntax for substitutions.
 
 
-@node Important Algorithms, Polynomial Expansion, Relations, Top
+@node Archiving, Important Algorithms, Relations, Basic Concepts
+@c    node-name, next, previous, up
+@section Archiving Expressions
+@cindex archives (class @code{archive})
+
+GiNaC allows creating @dfn{archives} of expressions which can be stored
+to or retrieved from files. To create an archive, you declare an object
+of class @code{archive} and archive expressions in it, giving each
+expressions a unique name:
+
+@example
+#include <ginac/ginac.h>
+#include <fstream>
+using namespace GiNaC;
+
+int main()
+@{
+    symbol x("x"), y("y"), z("z");
+
+    ex foo = sin(x + 2*y) + 3*z + 41;
+    ex bar = foo + 1;
+
+    archive a;
+    a.archive_ex(foo, "foo");
+    a.archive_ex(bar, "the second one");
+    // ...
+@end example
+
+The archive can then be written to a file:
+
+@example
+    // ...
+    ofstream out("foobar.gar");
+    out << a;
+    out.close();
+    // ...
+@end example
+
+The file @file{foobar.gar} contains all information that is needed to
+reconstruct the expressions @code{foo} and @code{bar}.
+
+The tool @command{viewgar} that comes with GiNaC can be used to view
+the contents of GiNaC archive files:
+
+@example
+$ viewgar foobar.gar
+foo = 41+sin(x+2*y)+3*z
+the second one = 42+sin(x+2*y)+3*z
+@end example
+
+The point of writing archive files is of course that they can later be
+read in again:
+
+@example
+    // ...
+    archive a2;
+    ifstream in("foobar.gar");
+    in >> a2;
+    // ...
+@end example
+
+And the stored expressions can be retrieved by their name:
+
+@example
+    // ...
+    lst syms;
+    syms.append(x); syms.append(y);
+
+    ex ex1 = a2.unarchive_ex(syms, "foo");
+    ex ex2 = a2.unarchive_ex(syms, "the second one");
+
+    cout << ex1 << endl;              // prints "41+sin(x+2*y)+3*z"
+    cout << ex2 << endl;              // prints "42+sin(x+2*y)+3*z"
+    cout << ex1.subs(x == 2) << endl; // prints "41+sin(2+2*y)+3*z"
+    // ...
+@}
+@end example
+
+Note that you have to supply a list of the symbols which are to be inserted
+in the expressions. Symbols in archives are stored by their name only and
+if you don't specify which symbols you have, unarchiving the expression will
+create new symbols with that name. E.g. if you hadn't included @code{x} in
+the @code{syms} list above, the @code{ex1.subs(x == 2)} statement would
+have had no effect because the @code{x} in @code{ex1} would have been a
+different symbol than the @code{x} which was defined at the beginning of
+the program, altough both would appear as @samp{x} when printed.
+
+
+
+@node Important Algorithms, Polynomial Expansion, Archiving, Top
 @c    node-name, next, previous, up
 @chapter Important Algorithms
 @cindex polynomial
@@ -1226,7 +1316,7 @@ being polynomials in the remaining variables.  The method
 @code{collect()} accomplishes this task.  Here is its declaration:
 
 @example
-ex ex::collect(symbol const & s);
+ex ex::collect(const symbol & s);
 @end example
 
 Note that the original polynomial needs to be in expanded form in order
@@ -1236,8 +1326,8 @@ coefficients can be checked using the two methods
 @cindex @code{degree()}
 @cindex @code{ldegree()}
 @example
-int ex::degree(symbol const & s);
-int ex::ldegree(symbol const & s);
+int ex::degree(const symbol & s);
+int ex::ldegree(const symbol & s);
 @end example
 
 where @code{degree()} returns the highest coefficient and
@@ -1587,7 +1677,7 @@ function that is called when one wishes to @code{eval} it.  It could
 look something like this:
 
 @example
-static ex cos_eval_method(ex const & x)
+static ex cos_eval_method(const ex & x)
 @{
     // if (!x%(2*Pi)) return 1
     // if (!x%Pi) return -1
@@ -1606,7 +1696,7 @@ lazy we sweep the problem under the rug by calling someone else's
 function that does so, in this case the one in class @code{numeric}:
 
 @example
-static ex cos_evalf_method(ex const & x)
+static ex cos_evalf_method(const ex & x)
 @{
     return sin(ex_to_numeric(x));
 @}
@@ -1616,7 +1706,7 @@ Differentiation will surely turn up and so we need to tell
 @code{sin} how to differentiate itself:
 
 @example
-static ex cos_diff_method(ex const & x, unsigned diff_param)
+static ex cos_diff_method(const ex & x, unsigned diff_param)
 @{
     return cos(x);
 @}