]> www.ginac.de Git - ginac.git/blobdiff - doc/tutorial/ginac.texi
[BUGFIX] Fix crash in parser.
[ginac.git] / doc / tutorial / ginac.texi
index 331c21254fff79e0ab1df7f23a375d0420c910e4..52c5828e95a178e8d2de18d3e66d3fb3c7fb2adb 100644 (file)
@@ -24,7 +24,7 @@
 This is a tutorial that documents GiNaC @value{VERSION}, an open
 framework for symbolic computation within the C++ programming language.
 
-Copyright (C) 1999-2019 Johannes Gutenberg University Mainz, Germany
+Copyright (C) 1999-2024 Johannes Gutenberg University Mainz, Germany
 
 Permission is granted to make and distribute verbatim copies of
 this manual provided the copyright notice and this permission notice
@@ -48,11 +48,11 @@ notice identical to this one.
 @title GiNaC @value{VERSION}
 @subtitle An open framework for symbolic computation within the C++ programming language
 @subtitle @value{UPDATED}
-@author @uref{http://www.ginac.de}
+@author @uref{https://www.ginac.de}
 
 @page
 @vskip 0pt plus 1filll
-Copyright @copyright{} 1999-2019 Johannes Gutenberg University Mainz, Germany
+Copyright @copyright{} 1999-2024 Johannes Gutenberg University Mainz, Germany
 @sp 2
 Permission is granted to make and distribute verbatim copies of
 this manual provided the copyright notice and this permission notice
@@ -126,7 +126,7 @@ hand-made documentation like this one is difficult to keep in sync with
 the development, the actual documentation is inside the sources in the
 form of comments.  That documentation may be parsed by one of the many
 Javadoc-like documentation systems.  If you fail at generating it you
-may access it from @uref{http://www.ginac.de/reference/, the GiNaC home
+may access it from @uref{https://www.ginac.de/reference/, the GiNaC home
 page}.  It is an invaluable resource not only for the advanced user who
 wishes to extend the system (or chase bugs) but for everybody who wants
 to comprehend the inner workings of GiNaC.  This little tutorial on the
@@ -135,7 +135,7 @@ the near future.
 
 @section License
 The GiNaC framework for symbolic computation within the C++ programming
-language is Copyright @copyright{} 1999-2019 Johannes Gutenberg
+language is Copyright @copyright{} 1999-2024 Johannes Gutenberg
 University Mainz, Germany.
 
 This program is free software; you can redistribute it and/or
@@ -372,8 +372,8 @@ lambda^2-3*lambda+11
 @end example
 
 Multivariate polynomials and rational functions may be expanded,
-collected and normalized (i.e. converted to a ratio of two coprime 
-polynomials):
+collected, factorized, and normalized (i.e. converted to a ratio of
+two coprime polynomials):
 
 @example
 > a = x^4 + 2*x^2*y^2 + 4*x^3*y + 12*x*y^3 - 3*y^4;
@@ -382,6 +382,8 @@ polynomials):
 4*x*y-y^2+x^2
 > expand(a*b);
 8*x^5*y+17*x^4*y^2+43*x^2*y^4-24*x*y^5+16*x^3*y^3+3*y^6+x^6
+> factor(%);
+(4*x*y+x^2-y^2)^2*(x^2+3*y^2)
 > collect(a+b,x);
 4*x^3*y-y^2-3*y^4+(12*y^3+4*y)*x+x^4+x^2*(1+2*y^2)
 > collect(a+b,y);
@@ -390,6 +392,9 @@ polynomials):
 3*y^2+x^2
 @end example
 
+Here we have made use of the @command{ginsh}-command @code{%} to pop the
+previously evaluated element from @command{ginsh}'s internal stack.
+
 You can differentiate functions and expand them as Taylor or Laurent
 series in a very natural syntax (the second argument of @code{series} is
 a relation defining the evaluation point, the third specifies the
@@ -414,9 +419,6 @@ x^(-1)-0.5772156649015328606+(0.9890559953279725555)*x
 -Euler-1/12+Order((x-1/2*Pi)^3)
 @end example
 
-Here we have made use of the @command{ginsh}-command @code{%} to pop the
-previously evaluated element from @command{ginsh}'s internal stack.
-
 Often, functions don't have roots in closed form.  Nevertheless, it's
 quite easy to compute a solution numerically, to arbitrary precision:
 
@@ -484,7 +486,7 @@ required for the configuration, it can be downloaded from
 @uref{http://pkg-config.freedesktop.org}.
 Last but not least, the CLN library
 is used extensively and needs to be installed on your system.
-Please get it from @uref{http://www.ginac.de/CLN/} (it is licensed under
+Please get it from @uref{https://www.ginac.de/CLN/} (it is licensed under
 the GPL) and install it prior to trying to install GiNaC.  The configure
 script checks if it can find it and if it cannot, it will refuse to
 continue.
@@ -717,7 +719,6 @@ meta-class for storing all mathematical objects.
 * Matrices::                     Matrices.
 * Indexed objects::              Handling indexed quantities.
 * Non-commutative objects::      Algebras with non-commutative products.
-* Hash maps::                    A faster alternative to std::map<>.
 @end menu
 
 
@@ -1193,7 +1194,7 @@ For storing numerical things, GiNaC uses Bruno Haible's library CLN.
 The classes therein serve as foundation classes for GiNaC.  CLN stands
 for Class Library for Numbers or alternatively for Common Lisp Numbers.
 In order to find out more about CLN's internals, the reader is referred to
-the documentation of that library.  @inforef{Introduction, , cln}, for
+the documentation of that library.  @xref{Top,,, cln, The CLN Manual}, for
 more information. Suffice to say that it is by itself build on top of
 another library, the GNU Multiple Precision library GMP, which is an
 extremely fast library for arbitrary long integers and rationals as well
@@ -1533,6 +1534,15 @@ rational number will return a floating-point approximation. Both
 @code{to_int()/to_long()} and @code{to_double()} discard the imaginary
 part of complex numbers.
 
+Note the signature of the above methods, you may need to apply a type
+conversion and call @code{evalf()} as shown in the following example:
+@example
+    ...
+    ex e1 = 1, e2 = sin(Pi/5);
+    cout << ex_to<numeric>(e1).to_int() << endl
+         << ex_to<numeric>(e2.evalf()).to_double() << endl;
+    ...
+@end example
 
 @node Constants, Fundamental containers, Numbers, Basic concepts
 @c    node-name, next, previous, up
@@ -1856,13 +1866,31 @@ substitutions.  They are also used as arguments to the @code{ex::series}
 method, where the left hand side of the relation specifies the variable
 to expand in and the right hand side the expansion point.  They can also
 be used for creating systems of equations that are to be solved for
-unknown variables.  But the most common usage of objects of this class
+unknown variables.
+
+But the most common usage of objects of this class
 is rather inconspicuous in statements of the form @code{if
 (expand(pow(a+b,2))==a*a+2*a*b+b*b) @{...@}}.  Here, an implicit
 conversion from @code{relational} to @code{bool} takes place.  Note,
 however, that @code{==} here does not perform any simplifications, hence
 @code{expand()} must be called explicitly.
 
+Simplifications of
+relationals may be more efficient if preceded by a call to
+@example
+ex relational::canonical() const
+@end example
+which returns an equivalent relation with the zero
+right-hand side. For example:
+@example
+possymbol p("p");
+relational rel = (p >= (p*p-1)/p);
+if (ex_to<relational>(rel.canonical().normal()))
+       cout << "correct inequality" << endl;
+@end example
+However, a user shall not expect that any inequality can be fully
+resolved by GiNaC.
+
 @node Integrals, Matrices, Relations, Basic concepts
 @c    node-name, next, previous, up
 @section Integrals
@@ -2978,7 +3006,7 @@ one form for @samp{F} and explicitly multiply it with a matrix representation
 of the metric tensor.
 
 
-@node Non-commutative objects, Hash maps, Indexed objects, Basic concepts
+@node Non-commutative objects, Methods and functions, Indexed objects, Basic concepts
 @c    node-name, next, previous, up
 @section Non-commutative objects
 
@@ -3214,7 +3242,8 @@ element, which defaults to 4.
 The @code{dirac_trace()} function is a linear functional that is equal to the
 ordinary matrix trace only in @math{D = 4} dimensions. In particular, the
 functional is not cyclic in
-@tex $D \ne 4$
+@tex
+$D \ne 4$
 @end tex
 @ifnottex
 @math{D != 4}
@@ -3225,7 +3254,8 @@ expressions containing @samp{gamma5}, so it's not a proper trace. This
 @cite{The Role of gamma5 in Dimensional Regularization} (@ref{Bibliography}).
 
 The value of the trace itself is also usually different in 4 and in
-@tex $D \ne 4$
+@tex
+$D \ne 4$
 @end tex
 @ifnottex
 @math{D != 4}
@@ -3298,15 +3328,16 @@ You can use this to compare two expressions or for further simplifications:
 @subsubsection A generic Clifford algebra
 
 A generic Clifford algebra, i.e. a
-@tex $2^n$
+@tex
+$2^n$
 @end tex
 @ifnottex
 2^n
 @end ifnottex
-dimensional algebra with
-generators 
-@tex $e_k$
-@end tex 
+dimensional algebra with generators
+@tex
+$e_k$
+@end tex
 @ifnottex
 e_k
 @end ifnottex
@@ -3762,43 +3793,7 @@ example:
 @end example
 
 
-@node Hash maps, Methods and functions, Non-commutative objects, Basic concepts
-@c    node-name, next, previous, up
-@section Hash Maps
-@cindex hash maps
-@cindex @code{exhashmap} (class)
-
-For your convenience, GiNaC offers the container template @code{exhashmap<T>}
-that can be used as a drop-in replacement for the STL
-@code{std::map<ex, T, ex_is_less>}, using hash tables to provide faster,
-typically constant-time, element look-up than @code{map<>}.
-
-@code{exhashmap<>} supports all @code{map<>} members and operations, with the
-following differences:
-
-@itemize @bullet
-@item
-no @code{lower_bound()} and @code{upper_bound()} methods
-@item
-no reverse iterators, no @code{rbegin()}/@code{rend()}
-@item 
-no @code{operator<(exhashmap, exhashmap)}
-@item
-the comparison function object @code{key_compare} is hardcoded to
-@code{ex_is_less}
-@item
-the constructor @code{exhashmap(size_t n)} allows specifying the minimum
-initial hash table size (the actual table size after construction may be
-larger than the specified value)
-@item
-the method @code{size_t bucket_count()} returns the current size of the hash
-table
-@item 
-@code{insert()} and @code{erase()} operations invalidate all iterators
-@end itemize
-
-
-@node Methods and functions, Information about expressions, Hash maps, Top
+@node Methods and functions, Information about expressions, Non-commutative objects, Top
 @c    node-name, next, previous, up
 @chapter Methods and functions
 @cindex polynomial
@@ -3852,6 +3847,7 @@ avoided.
 * Symmetrization::
 * Built-in functions::              List of predefined mathematical functions.
 * Multiple polylogarithms::
+* Iterated integrals::
 * Complex expressions::
 * Solving linear systems of equations::
 * Input/output::                    Input and output of expressions.
@@ -4880,7 +4876,7 @@ presented this would be impractical.
 One solution to this dilemma is the @dfn{Visitor} design pattern,
 which is implemented in GiNaC (actually, Robert Martin's Acyclic Visitor
 variation, described in detail in
-@uref{http://objectmentor.com/publications/acv.pdf}). Instead of adding
+@uref{https://condor.depaul.edu/dmumaugh/OOT/Design-Principles/acv.pdf}). Instead of adding
 virtual functions to the class hierarchy to implement operations, GiNaC
 provides a single "bouncing" method @code{accept()} that takes an instance
 of a special @code{visitor} class and redirects execution to the one
@@ -5386,6 +5382,27 @@ some care with subsequent processing of the result:
 Note also, how factors with the same exponents are not fully factorized
 with this method.
 
+@subsection Square-free partial fraction decomposition
+@cindex square-free partial fraction decomposition
+@cindex partial fraction decomposition
+@cindex @code{sqrfree_parfrac()}
+
+GiNaC also supports square-free partial fraction decomposition of
+rational functions:
+@example
+ex sqrfree_parfrac(const ex & a, const symbol & x);
+@end example
+It is called square-free because it assumes a square-free
+factorization of the input's denominator:
+@example
+    ...
+    symbol x("x");
+
+    ex rat = (x-4)/(pow(x,2)*(x+2));
+    cout << sqrfree_parfrac(rat, x) << endl;
+     // -> -2*x^(-2)+3/2*x^(-1)-3/2*(2+x)^(-1)
+@end example
+
 @subsection Polynomial factorization
 @cindex factorization
 @cindex polynomial factorization
@@ -5707,7 +5724,7 @@ using namespace GiNaC;
 ex machin_pi(int degr)
 @{
     symbol x;
-    ex pi_expansion = series_to_poly(atan(x).series(x,degr));
+    ex pi_expansion = series_to_poly(atan(x).series(x==0,degr));
     ex pi_approx = 16*pi_expansion.subs(x==numeric(1,5))
                    -4*pi_expansion.subs(x==numeric(1,239));
     return pi_approx;
@@ -5899,6 +5916,12 @@ GiNaC contains the following predefined mathematical functions:
 @cindex @code{zeta()}
 @item @code{zetaderiv(n, x)}
 @tab derivatives of Riemann's zeta function
+@item @code{iterated_integral(a, y)}
+@tab iterated integral
+@cindex @code{iterated_integral()}
+@item @code{iterated_integral(a, y, N)}
+@tab iterated integral with explicit truncation parameter
+@cindex @code{iterated_integral()}
 @item @code{tgamma(x)}
 @tab gamma function
 @cindex @code{tgamma()}
@@ -5914,6 +5937,12 @@ GiNaC contains the following predefined mathematical functions:
 @cindex @code{psi()}
 @item @code{psi(n, x)}
 @tab derivatives of psi function (polygamma functions)
+@item @code{EllipticK(x)}
+@tab complete elliptic integral of the first kind
+@cindex @code{EllipticK()}
+@item @code{EllipticE(x)}
+@tab complete elliptic integral of the second kind
+@cindex @code{EllipticE()}
 @item @code{factorial(n)}
 @tab factorial function @math{n!}
 @cindex @code{factorial()}
@@ -5994,7 +6023,7 @@ GiNaC uses the opposite order: firstly expands the function and then its
 argument. Of course, a user can fine-tune this behavior by sequential
 calls of several @code{expand()} methods with desired flags.
 
-@node Multiple polylogarithms, Complex expressions, Built-in functions, Methods and functions
+@node Multiple polylogarithms, Iterated integrals, Built-in functions, Methods and functions
 @c    node-name, next, previous, up
 @subsection Multiple polylogarithms
 
@@ -6008,7 +6037,7 @@ calls of several @code{expand()} methods with desired flags.
 The multiple polylogarithm is the most generic member of a family of functions,
 to which others like the harmonic polylogarithm, Nielsen's generalized
 polylogarithm and the multiple zeta value belong.
-Everyone of these functions can also be written as a multiple polylogarithm with specific
+Each of these functions can also be written as a multiple polylogarithm with specific
 parameters. This whole family of functions is therefore often referred to simply as
 multiple polylogarithms, containing @code{Li}, @code{G}, @code{H}, @code{S} and @code{zeta}.
 The multiple polylogarithm itself comes in two variants: @code{Li} and @code{G}. While
@@ -6177,7 +6206,71 @@ J.Borwein, D.Bradley, D.Broadhurst, P.Lisonek, Trans.Amer.Math.Soc. 353/3 (2001)
 @cite{Numerical Evaluation of Multiple Polylogarithms}, 
 J.Vollinga, S.Weinzierl, hep-ph/0410259
 
-@node Complex expressions, Solving linear systems of equations, Multiple polylogarithms, Methods and functions
+@node Iterated integrals, Complex expressions, Multiple polylogarithms, Methods and functions
+@c    node-name, next, previous, up
+@subsection Iterated integrals
+
+Multiple polylogarithms are a particular example of iterated integrals.
+An iterated integral is defined by the function @code{iterated_integral(a,y)}.
+The variable @code{y} gives the upper integration limit for the outermost integration, by convention the lower integration limit is always set to zero.
+The variable @code{a} must be a GiNaC @code{lst} containing sub-classes of @code{integration_kernel} as elements.
+The depth of the iterated integral corresponds to the number of elements of @code{a}.
+The available integrands for iterated integrals are
+(for a more detailed description the user is referred to the publications listed at the end of this section)
+@cartouche
+@multitable @columnfractions .40 .60
+@item @strong{Class} @tab @strong{Description}
+@item @code{integration_kernel()}
+@tab Base class, represents the one-form @math{dy}
+@cindex @code{integration_kernel()}
+@item @code{basic_log_kernel()}
+@tab Logarithmic one-form @math{dy/y}
+@cindex @code{basic_log_kernel()}
+@item @code{multiple_polylog_kernel(z_j)}
+@tab The one-form @math{dy/(y-z_j)}
+@cindex @code{multiple_polylog_kernel()}
+@item @code{ELi_kernel(n, m, x, y)}
+@tab The one form @math{ELi_{n;m}(x;y;q) dq/q}
+@cindex @code{ELi_kernel()}
+@item @code{Ebar_kernel(n, m, x, y)}
+@tab The one form @math{\overline{E}_{n;m}(x;y;q) dq/q}
+@cindex @code{Ebar_kernel()}
+@item @code{Kronecker_dtau_kernel(k, z_j, K, C_k)}
+@tab The one form @math{C_k K (k-1)/(2 \pi i)^k g^{(k)}(z_j,K \tau) dq/q}
+@cindex @code{Kronecker_dtau_kernel()}
+@item @code{Kronecker_dz_kernel(k, z_j, tau, K, C_k)}
+@tab The one form @math{C_k (2 \pi i)^{2-k} g^{(k-1)}(z-z_j,K \tau) dz}
+@cindex @code{Kronecker_dz_kernel()}
+@item @code{Eisenstein_kernel(k, N, a, b, K, C_k)}
+@tab The one form @math{C_k E_{k,N,a,b,K}(\tau) dq/q}
+@cindex @code{Eisenstein_kernel()}
+@item @code{Eisenstein_h_kernel(k, N, r, s, C_k)}
+@tab The one form @math{C_k h_{k,N,r,s}(\tau) dq/q}
+@cindex @code{Eisenstein_h_kernel()}
+@item @code{modular_form_kernel(k, P, C_k)}
+@tab The one form @math{C_k P dq/q}
+@cindex @code{modular_form_kernel()}
+@item @code{user_defined_kernel(f, y)}
+@tab The one form @math{f(y) dy}
+@cindex @code{user_defined_kernel()}
+@end multitable
+@end cartouche
+All parameters are assumed to be such that all integration kernels have a convergent Laurent expansion
+around zero with at most a simple pole at zero.
+The iterated integral may also be called with an optional third parameter
+@code{iterated_integral(a,y,N_trunc)}, in which case the numerical evaluation will truncate the series
+expansion at order @code{N_trunc}.
+
+The classes @code{Eisenstein_kernel()}, @code{Eisenstein_h_kernel()} and @code{modular_form_kernel()}
+provide a method @code{q_expansion_modular_form(q, order)}, which can used to obtain the q-expansion
+of @math{E_{k,N,a,b,K}(\tau)}, @math{h_{k,N,r,s}(\tau)} or @math{P} to the specified order.
+
+Useful publications:
+
+@cite{Numerical evaluation of iterated integrals related to elliptic Feynman integrals}, 
+M.Walden, S.Weinzierl, arXiv:2010.05271
+
+@node Complex expressions, Solving linear systems of equations, Iterated integrals, Methods and functions
 @c    node-name, next, previous, up
 @section Complex expressions
 @c
@@ -6722,8 +6815,8 @@ expression a unique name:
 
 @example
 #include <fstream>
-using namespace std;
 #include <ginac/ginac.h>
+using namespace std;
 using namespace GiNaC;
 
 int main()
@@ -6743,14 +6836,16 @@ The archive can then be written to a file:
 
 @example
     // ...
-    ofstream out("foobar.gar");
+    ofstream out("foobar.gar", ios::binary);
     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}.
+reconstruct the expressions @code{foo} and @code{bar}. The flag
+@code{ios::binary} prevents locales setting of your OS tampers the
+archive file structure.
 
 @cindex @command{viewgar}
 The tool @command{viewgar} that comes with GiNaC can be used to view
@@ -6768,7 +6863,7 @@ read in again:
 @example
     // ...
     archive a2;
-    ifstream in("foobar.gar");
+    ifstream in("foobar.gar", ios::binary);
     in >> a2;
     // ...
 @end example
@@ -7637,9 +7732,8 @@ product in a C++ @code{struct}:
 
 @example
 #include <iostream>
-using namespace std;
-
 #include <ginac/ginac.h>
+using namespace std;
 using namespace GiNaC;
 
 struct sprod_s @{
@@ -8031,9 +8125,8 @@ as follows:
 #include <iostream>
 #include <string>   
 #include <stdexcept>
-using namespace std;
-
 #include <ginac/ginac.h>
+using namespace std;
 using namespace GiNaC;
 @end example
 
@@ -8612,9 +8705,9 @@ inserted.  But it may be useful to remember that this is not what
 happens.  Knowing this will enable you to write much more efficient
 code.  If you still have an uncertain feeling with copy-on-write
 semantics, we recommend you have a look at the
-@uref{http://www.parashift.com/c++-faq-lite/, C++-FAQ lite} by
-Marshall Cline.  Chapter 16 covers this issue and presents an
-implementation which is pretty close to the one in GiNaC.
+@uref{https://isocpp.org/faq, C++-FAQ's} chapter on memory management.
+It covers this issue and presents an implementation which is pretty
+close to the one in GiNaC.
 
 
 @node Internal representation of products and sums, Package tools, Expressions are reference counted, Internal structures
@@ -8713,12 +8806,12 @@ program use @footnote{If GiNaC is installed into some non-standard
 directory @var{prefix} one should set the @var{PKG_CONFIG_PATH}
 environment variable to @var{prefix}/lib/pkgconfig for this to work.}
 @example
-g++ -o simple `pkg-config --cflags --libs ginac` simple.cpp
+g++ -o simple simple.cpp `pkg-config --cflags --libs ginac`
 @end example
 
 This command line might expand to (for example):
 @example
-g++ -o simple -lginac -lcln simple.cpp
+g++ -o simple simple.cpp -lginac -lcln
 @end example
 
 Not only is the form using @command{pkg-config} easier to type, it will