]> www.ginac.de Git - ginac.git/blob - doc/tutorial/ginac.texi
e6d2d1a140b9f59cf44ad63b015820763b97adb3
[ginac.git] / doc / tutorial / ginac.texi
1 \input texinfo  @c -*-texinfo-*-
2 @c %**start of header
3 @setfilename ginac.info
4 @settitle GiNaC, an open framework for symbolic computation within the C++ programming language
5 @setchapternewpage on
6 @afourpaper
7 @c For `info' only.
8 @paragraphindent 0
9 @c For TeX only.
10 @iftex
11 @c I hate putting "@noindent" in front of every paragraph.
12 @parindent=0pt
13 @end iftex
14 @c %**end of header
15
16 @include version.texi
17
18 @direntry
19 * ginac: (ginac).                   C++ library for symbolic computation.
20 @end direntry
21
22 @ifinfo
23 This is a tutorial that documents GiNaC @value{VERSION}, an open
24 framework for symbolic computation within the C++ programming language.
25
26 Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
27
28 Permission is granted to make and distribute verbatim copies of
29 this manual provided the copyright notice and this permission notice
30 are preserved on all copies.
31
32 @ignore
33 Permission is granted to process this file through TeX and print the
34 results, provided the printed document carries copying permission
35 notice identical to this one except for the removal of this paragraph
36
37 @end ignore
38 Permission is granted to copy and distribute modified versions of this
39 manual under the conditions for verbatim copying, provided that the entire
40 resulting derived work is distributed under the terms of a permission
41 notice identical to this one.
42 @end ifinfo
43
44 @finalout
45 @c finalout prevents ugly black rectangles on overfull hbox lines
46 @titlepage
47 @title GiNaC @value{VERSION}
48 @subtitle An open framework for symbolic computation within the C++ programming language
49 @subtitle @value{UPDATED}
50 @author The GiNaC Group:
51 @author Christian Bauer, Alexander Frink, Richard Kreckel
52
53 @page
54 @vskip 0pt plus 1filll
55 Copyright @copyright{} 1999 Johannes Gutenberg University Mainz, Germany
56 @sp 2
57 Permission is granted to make and distribute verbatim copies of
58 this manual provided the copyright notice and this permission notice
59 are preserved on all copies.
60
61 Permission is granted to copy and distribute modified versions of this
62 manual under the conditions for verbatim copying, provided that the entire
63 resulting derived work is distributed under the terms of a permission
64 notice identical to this one.
65 @end titlepage
66
67 @page
68 @contents
69
70 @page
71
72
73 @node Top, Introduction, (dir), (dir)
74 @c    node-name, next, previous, up
75 @top GiNaC
76
77 This is a tutorial that documents GiNaC @value{VERSION}, an open
78 framework for symbolic computation within the C++ programming language.
79
80 @menu
81 * Introduction::                 GiNaC's purpose.
82 * A Tour of GiNaC::              A quick tour of the library.
83 * Installation::                 How to install the package.
84 * Basic Concepts::               Description of fundamental classes.
85 * Important Algorithms::         Algorithms for symbolic manipulations.
86 * Extending GiNaC::              How to extend the library.
87 * A Comparison With Other CAS::  Compares GiNaC to traditional CAS.
88 * Internal Structures::          Description of some internal structures.
89 * Bibliography::
90 * Concept Index::
91 @end menu
92
93
94 @node Introduction, A Tour of GiNaC, Top, Top
95 @c    node-name, next, previous, up
96 @chapter Introduction
97 @cindex history of GiNaC
98
99 The motivation behind GiNaC derives from the observation that most
100 present day computer algebra systems (CAS) are linguistically and
101 semantically impoverished.  Although they are quite powerful tools for
102 learning math and solving particular problems they lack modern
103 linguistical structures that allow for the creation of large-scale
104 projects.  GiNaC is an attempt to overcome this situation by extending a
105 well established and standardized computer language (C++) by some
106 fundamental symbolic capabilities, thus allowing for integrated systems
107 that embed symbolic manipulations together with more established areas
108 of computer science (like computation-intense numeric applications,
109 graphical interfaces, etc.) under one roof.
110
111 The particular problem that led to the writing of the GiNaC framework is
112 still a very active field of research, namely the calculation of higher
113 order corrections to elementary particle interactions.  There,
114 theoretical physicists are interested in matching present day theories
115 against experiments taking place at particle accelerators.  The
116 computations involved are so complex they call for a combined symbolical
117 and numerical approach.  This turned out to be quite difficult to
118 accomplish with the present day CAS we have worked with so far and so we
119 tried to fill the gap by writing GiNaC.  But of course its applications
120 are in no way restricted to theoretical physics.
121
122 This tutorial is intended for the novice user who is new to GiNaC but
123 already has some background in C++ programming.  However, since a
124 hand-made documentation like this one is difficult to keep in sync with
125 the development, the actual documentation is inside the sources in the
126 form of comments.  That documentation may be parsed by one of the many
127 Javadoc-like documentation systems.  If you fail at generating it you
128 may access it from @uref{http://www.ginac.de/reference/, the GiNaC home
129 page}.  It is an invaluable resource not only for the advanced user who
130 wishes to extend the system (or chase bugs) but for everybody who wants
131 to comprehend the inner workings of GiNaC.  This little tutorial on the
132 other hand only covers the basic things that are unlikely to change in
133 the near future.
134
135 @section License
136 The GiNaC framework for symbolic computation within the C++ programming
137 language is Copyright @copyright{} 1999 Johannes Gutenberg University Mainz,
138 Germany.
139
140 This program is free software; you can redistribute it and/or
141 modify it under the terms of the GNU General Public License as
142 published by the Free Software Foundation; either version 2 of the
143 License, or (at your option) any later version.
144
145 This program is distributed in the hope that it will be useful, but
146 WITHOUT ANY WARRANTY; without even the implied warranty of
147 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
148 General Public License for more details.
149
150 You should have received a copy of the GNU General Public License
151 along with this program; see the file COPYING.  If not, write to the
152 Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
153 MA 02111-1307, USA.
154
155
156 @node A Tour of GiNaC, How to use it from within C++, Introduction, Top
157 @c    node-name, next, previous, up
158 @chapter A Tour of GiNaC
159
160 This quick tour of GiNaC wants to arise your interest in the
161 subsequent chapters by showing off a bit.  Please excuse us if it
162 leaves many open questions.
163
164 @menu
165 * How to use it from within C++::  Two simple examples.
166 * What it can do for you::         A Tour of GiNaC's features.
167 @end menu
168
169
170 @node How to use it from within C++, What it can do for you, A Tour of GiNaC, A Tour of GiNaC
171 @c    node-name, next, previous, up
172 @section How to use it from within C++
173
174 The GiNaC open framework for symbolic computation within the C++ programming
175 language does not try to define a language of it's own as conventional
176 CAS do.  Instead, it extends the capabilities of C++ by symbolic
177 manipulations.  Here is how to generate and print a simple (and rather
178 pointless) bivariate polynomial with some large coefficients:
179
180 @example
181 #include <ginac/ginac.h>
182 using namespace GiNaC;
183
184 int main()
185 @{
186     symbol x("x"), y("y");
187     ex poly;
188
189     for (int i=0; i<3; ++i)
190         poly += factorial(i+16)*pow(x,i)*pow(y,2-i);
191
192     cout << poly << endl;
193     return 0;
194 @}
195 @end example
196
197 Assuming the file is called @file{hello.cc}, on our system we can compile
198 and run it like this:
199
200 @example
201 $ c++ hello.cc -o hello -lcln -lginac
202 $ ./hello
203 355687428096000*x*y+20922789888000*y^2+6402373705728000*x^2
204 @end example
205
206 @cindex Hermite polynomial
207 Next, there is a more meaningful C++ program that calls a function which
208 generates Hermite polynomials in a specified free variable.
209
210 @example
211 #include <ginac/ginac.h>
212 using namespace GiNaC;
213
214 ex HermitePoly(symbol x, int deg)
215 @{
216     ex HKer=exp(-pow(x,2));
217     // uses the identity H_n(x) == (-1)^n exp(x^2) (d/dx)^n exp(-x^2) 
218     return normal(pow(-1,deg) * diff(HKer, x, deg) / HKer);
219 @}
220
221 int main()
222 @{
223     symbol z("z");
224
225     for (int i=0; i<6; ++i)
226         cout << "H_" << i << "(z) == " << HermitePoly(z,i) << endl;
227
228     return 0;
229 @}
230 @end example
231
232 When run, this will type out
233
234 @example
235 H_0(z) == 1
236 H_1(z) == 2*z
237 H_2(z) == 4*z^2-2
238 H_3(z) == -12*z+8*z^3
239 H_4(z) == -48*z^2+16*z^4+12
240 H_5(z) == 120*z-160*z^3+32*z^5
241 @end example
242
243 This method of generating the coefficients is of course far from optimal
244 for production purposes.
245
246 In order to show some more examples of what GiNaC can do we will now use
247 the @command{ginsh}, a simple GiNaC interactive shell that provides a
248 convenient window into GiNaC's capabilities.
249
250
251 @node What it can do for you, Installation, How to use it from within C++, A Tour of GiNaC
252 @c    node-name, next, previous, up
253 @section What it can do for you
254
255 @cindex @code{ginsh}
256 After invoking @command{ginsh} one can test and experiment with GiNaC's
257 features much like in other Computer Algebra Systems except that it does
258 not provide programming constructs like loops or conditionals.  For a
259 concise description of the @command{ginsh} syntax we refer to its
260 accompanied man page. Suffice to say that assignments and comparisons in
261 @command{ginsh} are written as they are in C, i.e. @code{=} assigns and
262 @code{==} compares.
263
264 It can manipulate arbitrary precision integers in a very fast way.
265 Rational numbers are automatically converted to fractions of coprime
266 integers:
267
268 @example
269 > x=3^150;
270 369988485035126972924700782451696644186473100389722973815184405301748249
271 > y=3^149;
272 123329495011708990974900260817232214728824366796574324605061468433916083
273 > x/y;
274 3
275 > y/x;
276 1/3
277 @end example
278
279 All numbers occuring in GiNaC's expressions can be converted into floating
280 point numbers with the @code{evalf} method, to arbitrary accuracy:
281
282 @example
283 > evalf(1/7);
284 0.14285714285714285714
285 > Digits=150;
286 150
287 > evalf(1/7);
288 0.1428571428571428571428571428571428571428571428571428571428571428571428
289 5714285714285714285714285714285714285
290 @end example
291
292 Exact numbers other than rationals that can be manipulated in GiNaC
293 include predefined constants like Archimedes' @code{Pi}.  They can both
294 be used in symbolic manipulations (as an exact number) as well as in
295 numeric expressions (as an inexact number):
296
297 @example
298 > a=Pi^2+x;
299 x+Pi^2
300 > evalf(a);
301 x+9.869604401089358619L0
302 > x=2;
303 2
304 > evalf(a);
305 11.869604401089358619L0
306 @end example
307
308 Built-in functions evaluate immediately to exact numbers if
309 this is possible.  Conversions that can be safely performed are done
310 immediately; conversions that are not generally valid are not done:
311
312 @example
313 > cos(42*Pi);
314 1
315 > cos(acos(x));
316 x
317 > acos(cos(x));
318 acos(cos(x))
319 @end example
320
321 (Note that converting the last input to @code{x} would allow one to
322 conclude that @code{42*Pi} is equal to @code{0}.)
323
324 Linear equation systems can be solved along with basic linear
325 algebra manipulations over symbolic expressions.  In C++ GiNaC offers
326 a matrix class for this purpose but we can see what it can do using
327 @command{ginsh}'s notation of double brackets to type them in:
328
329 @example
330 > lsolve(a+x*y==z,x);
331 y^(-1)*(z-a);
332 lsolve([3*x+5*y == 7, -2*x+10*y == -5], [x, y]);
333 [x==19/8,y==-1/40]
334 > M = [[ [[1, 3]], [[-3, 2]] ]];
335 [[ [[1,3]], [[-3,2]] ]]
336 > determinant(M);
337 11
338 > charpoly(M,lambda);
339 lambda^2-3*lambda+11
340 @end example
341
342 Multivariate polynomials and rational functions may be expanded,
343 collected and normalized (i.e. converted to a ratio of two coprime 
344 polynomials):
345
346 @example
347 > a = x^4 + 2*x^2*y^2 + 4*x^3*y + 12*x*y^3 - 3*y^4;
348 -3*y^4+x^4+12*x*y^3+2*x^2*y^2+4*x^3*y
349 > b = x^2 + 4*x*y - y^2;
350 -y^2+x^2+4*x*y
351 > expand(a*b);
352 3*y^6+x^6-24*x*y^5+43*x^2*y^4+16*x^3*y^3+17*x^4*y^2+8*x^5*y
353 > collect(a*b,x);
354 3*y^6+48*x*y^4+2*x^2*y^2+x^4*(-y^2+x^2+4*x*y)+4*x^3*y*(-y^2+x^2+4*x*y)
355 > normal(a/b);
356 3*y^2+x^2
357 @end example
358
359 You can differentiate functions and expand them as Taylor or
360 Laurent series (the third argument of series is the evaluation point,
361 the fourth defines the order):
362
363 @example
364 > diff(tan(x),x);
365 tan(x)^2+1
366 > series(sin(x),x,0,4);
367 x-1/6*x^3+Order(x^4)
368 > series(1/tan(x),x,0,4);
369 x^(-1)-1/3*x+Order(x^2)
370 @end example
371
372 If you ever wanted to convert units in C or C++ and found this
373 is cumbersome, here is the solution.  Symbolic types can always be
374 used as tags for different types of objects.  Converting from wrong
375 units to the metric system is therefore easy:
376
377 @example
378 > in=.0254*m;
379 0.0254*m
380 > lb=.45359237*kg;
381 0.45359237*kg
382 > 200*lb/in^2;
383 140613.91592783185568*kg*m^(-2)
384 @end example
385
386
387 @node Installation, Prerequisites, What it can do for you, Top
388 @c    node-name, next, previous, up
389 @chapter Installation
390
391 @cindex CLN
392 GiNaC's installation follows the spirit of most GNU software. It is
393 easily installed on your system by three steps: configuration, build,
394 installation.
395
396 @menu
397 * Prerequisites::                Packages upon which GiNaC depends.
398 * Configuration::                How to configure GiNaC.
399 * Building GiNaC::               How to compile GiNaC.
400 * Installing GiNaC::             How to install GiNaC on your system.
401 @end menu
402
403
404 @node Prerequisites, Configuration, Installation, Installation
405 @c    node-name, next, previous, up
406 @section Prerequisites
407
408 In order to install GiNaC on your system, some prerequisites need
409 to be met.  First of all, you need to have a C++-compiler adhering to
410 the ANSI-standard @cite{ISO/IEC 14882:1998(E)}.  We used @acronym{GCC} for
411 development so if you have a different compiler you are on your own.
412 For the configuration to succeed you need a Posix compliant shell
413 installed in @file{/bin/sh}, GNU @command{bash} is fine.  Perl is needed
414 by the built process as well, since some of the source files are automatically
415 generated by Perl scripts.  Last but not least, Bruno Haible's library
416 @acronym{CLN} is extensively used and needs to be installed on your system.
417 Please get it from @uref{ftp://ftp.santafe.edu/pub/gnu/} or from
418 @uref{ftp://ftp.ilog.fr/pub/Users/haible/gnu/, Bruno Haible's FTP site}
419 (it is covered by GPL) and install it prior to trying to install GiNaC.
420 The configure script checks if it can find it and if it cannot
421 it will refuse to continue.
422
423
424 @node Configuration, Building GiNaC, Prerequisites, Installation
425 @c    node-name, next, previous, up
426 @section Configuration
427 @cindex configuration
428 @cindex Autoconf
429
430 To configure GiNaC means to prepare the source distribution for
431 building.  It is done via a shell script called @command{configure} that
432 is shipped with the sources and was originally generated by GNU
433 Autoconf.  Since a configure script generated by GNU Autoconf never
434 prompts, all customization must be done either via command line
435 parameters or environment variables.  It accepts a list of parameters,
436 the complete set of which can be listed by calling it with the
437 @option{--help} option.  The most important ones will be shortly
438 described in what follows:
439
440 @itemize @bullet
441
442 @item
443 @option{--disable-shared}: When given, this option switches off the
444 build of a shared library, i.e. a @file{.so} file.  This may be convenient
445 when developing because it considerably speeds up compilation.
446
447 @item
448 @option{--prefix=@var{PREFIX}}: The directory where the compiled library
449 and headers are installed. It defaults to @file{/usr/local} which means
450 that the library is installed in the directory @file{/usr/local/lib},
451 the header files in @file{/usr/local/include/GiNaC} and the documentation
452 (like this one) into @file{/usr/local/share/doc/GiNaC}.
453
454 @item
455 @option{--libdir=@var{LIBDIR}}: Use this option in case you want to have
456 the library installed in some other directory than
457 @file{@var{PREFIX}/lib/}.
458
459 @item
460 @option{--includedir=@var{INCLUDEDIR}}: Use this option in case you want
461 to have the header files installed in some other directory than
462 @file{@var{PREFIX}/include/ginac/}. For instance, if you specify
463 @option{--includedir=/usr/include} you will end up with the header files
464 sitting in the directory @file{/usr/include/ginac/}. Note that the
465 subdirectory @file{GiNaC} is enforced by this process in order to
466 keep the header files separated from others.  This avoids some
467 clashes and allows for an easier deinstallation of GiNaC. This ought
468 to be considered A Good Thing (tm).
469
470 @item
471 @option{--datadir=@var{DATADIR}}: This option may be given in case you
472 want to have the documentation installed in some other directory than
473 @file{@var{PREFIX}/share/doc/GiNaC/}.
474
475 @end itemize
476
477 In addition, you may specify some environment variables.
478 @env{CXX} holds the path and the name of the C++ compiler
479 in case you want to override the default in your path.  (The
480 @command{configure} script searches your path for @command{c++},
481 @command{g++}, @command{gcc}, @command{CC}, @command{cxx}
482 and @command{cc++} in that order.)  It may be very useful to
483 define some compiler flags with the @env{CXXFLAGS} environment
484 variable, like optimization, debugging information and warning
485 levels.  If omitted, it defaults to @option{-g -O2}.
486
487 The whole process is illustrated in the following two
488 examples. (Substitute @command{setenv @var{VARIABLE} @var{value}} for
489 @command{export @var{VARIABLE}=@var{value}} if the Berkeley C shell is
490 your login shell.)
491
492 Here is a simple configuration for a site-wide GiNaC library assuming
493 everything is in default paths:
494
495 @example
496 $ export CXXFLAGS="-Wall -O2"
497 $ ./configure
498 @end example
499
500 And here is a configuration for a private static GiNaC library with
501 several components sitting in custom places (site-wide @acronym{GCC} and
502 private @acronym{CLN}).  The compiler is pursuaded to be picky and full
503 assertions and debugging are switched on:
504
505 @example
506 $ export CXX=/usr/local/gnu/bin/c++
507 $ export CPPFLAGS="$(CPPFLAGS) -I$(HOME)/include"
508 $ export CXXFLAGS="$(CXXFLAGS) -DDO_GINAC_ASSERT -ggdb -Wall -ansi -pedantic"
509 $ export LDFLAGS="$(LDFLAGS) -L$(HOME)/lib"
510 $ ./configure --disable-shared --prefix=$(HOME)
511 @end example
512
513
514 @node Building GiNaC, Installing GiNaC, Configuration, Installation
515 @c    node-name, next, previous, up
516 @section Building GiNaC
517 @cindex building GiNaC
518
519 After proper configuration you should just build the whole
520 library by typing
521 @example
522 $ make
523 @end example
524 at the command prompt and go for a cup of coffee.  The exact time it
525 takes to compile GiNaC depends not only on the speed of your machines
526 but also on other parameters, for instance what value for @env{CXXFLAGS}
527 you entered.  Optimization may be very time-consuming.
528
529 Just to make sure GiNaC works properly you may run a simple test
530 suite by typing
531
532 @example
533 $ make check
534 @end example
535
536 This will compile some sample programs, run them and compare the output
537 to reference output. Each of the checks should return a message @samp{passed}
538 together with the CPU time used for that particular test.  If it does
539 not, something went wrong.  This is mostly intended to be a QA-check
540 if something was broken during the development, not a sanity check
541 of your system.  Another intent is to allow people to fiddle around
542 with optimization.  If @acronym{CLN} was installed all right
543 this step is unlikely to return any errors.
544
545 Generally, the top-level Makefile runs recursively to the
546 subdirectories.  It is therfore safe to go into any subdirectory
547 (@code{doc/}, @code{ginsh/}, ...) and simply type @code{make}
548 @var{target} there in case something went wrong.
549
550
551 @node Installing GiNaC, Basic Concepts, Building GiNaC, Installation
552 @c    node-name, next, previous, up
553 @section Installing GiNaC
554 @cindex installation
555
556 To install GiNaC on your system, simply type
557
558 @example
559 $ make install
560 @end example
561
562 As described in the section about configuration the files will be
563 installed in the following directories (the directories will be created
564 if they don't already exist):
565
566 @itemize @bullet
567
568 @item
569 @file{libginac.a} will go into @file{@var{PREFIX}/lib/} (or
570 @file{@var{LIBDIR}}) which defaults to @file{/usr/local/lib/}.
571 So will @file{libginac.so} unless the configure script was
572 given the option @option{--disable-shared}.  The proper symlinks
573 will be established as well.
574
575 @item
576 All the header files will be installed into @file{@var{PREFIX}/include/ginac/}
577 (or @file{@var{INCLUDEDIR}/ginac/}, if specified).
578
579 @item
580 All documentation (HTML and Postscript) will be stuffed into
581 @file{@var{PREFIX}/share/doc/GiNaC/} (or
582 @file{@var{DATADIR}/doc/GiNaC/}, if @var{DATADIR} was specified).
583
584 @end itemize
585
586 For the sake of completeness we will list some other useful make
587 targets: @command{make clean} deletes all files generated by
588 @command{make}, i.e. all the object files.  In addition @command{make
589 distclean} removes all files generated by the configuration and
590 @command{make maintainer-clean} goes one step further and deletes files
591 that may require special tools to rebuild (like the @command{libtool}
592 for instance).  Finally @command{make uninstall} removes the installed
593 library, header files and documentation@footnote{Uninstallation does not
594 work after you have called @command{make distclean} since the
595 @file{Makefile} is itself generated by the configuration from
596 @file{Makefile.in} and hence deleted by @command{make distclean}.  There
597 are two obvious ways out of this dilemma.  First, you can run the
598 configuration again with the same @var{PREFIX} thus creating a
599 @file{Makefile} with a working @samp{uninstall} target.  Second, you can
600 do it by hand since you now know where all the files went during
601 installation.}.
602
603 @node Basic Concepts, Expressions, Installing GiNaC, Top
604 @c    node-name, next, previous, up
605 @chapter Basic Concepts
606
607 This chapter will describe the different fundamental objects that can be
608 handled by GiNaC.  But before doing so, it is worthwhile introducing you
609 to the more commonly used class of expressions, representing a flexible
610 meta-class for storing all mathematical objects.
611
612 @menu
613 * Expressions::                  The fundamental GiNaC class.
614 * The Class Hierarchy::          Overview of GiNaC's classes.
615 * Symbols::                      Symbolic objects.
616 * Numbers::                      Numerical objects.
617 * Constants::                    Pre-defined constants.
618 * Fundamental operations::       The power, add and mul classes.
619 * Built-in functions::           Mathematical functions.
620 @end menu
621
622
623 @node Expressions, The Class Hierarchy, Basic Concepts, Basic Concepts
624 @c    node-name, next, previous, up
625 @section Expressions
626 @cindex expression (class @code{ex})
627 @cindex @code{has()}
628
629 The most common class of objects a user deals with is the expression
630 @code{ex}, representing a mathematical object like a variable, number,
631 function, sum, product, etc...  Expressions may be put together to form
632 new expressions, passed as arguments to functions, and so on.  Here is a
633 little collection of valid expressions:
634
635 @example
636 ex MyEx1 = 5;                       // simple number
637 ex MyEx2 = x + 2*y;                 // polynomial in x and y
638 ex MyEx3 = (x + 1)/(x - 1);         // rational expression
639 ex MyEx4 = sin(x + 2*y) + 3*z + 41; // containing a function
640 ex MyEx5 = MyEx4 + 1;               // similar to above
641 @end example
642
643 Expressions are handles to other more fundamental objects, that many
644 times contain other expressions thus creating a tree of expressions
645 (@xref{Internal Structures} for particular examples).  Most methods on
646 @code{ex} therefore run top-down through such an expression tree.  For
647 example, the method @code{has()} scans recursively for occurrences of
648 something inside an expression.  Thus, if you have declared @code{MyEx4}
649 as in the example above @code{MyEx4.has(y)} will find @code{y} inside
650 the argument of @code{sin} and hence return @code{true}.
651
652 The next sections will outline the general picture of GiNaC's class
653 hierarchy and describe the classes of objects that are handled by
654 @code{ex}.
655
656
657 @node The Class Hierarchy, Symbols, Expressions, Basic Concepts
658 @c    node-name, next, previous, up
659 @section The Class Hierarchy
660
661 GiNaC's class hierarchy consists of several classes representing
662 mathematical objects, all of which (except for @code{ex} and some
663 helpers) are internally derived from one abstract base class called
664 @code{basic}.  You do not have to deal with objects of class
665 @code{basic}, instead you'll be dealing with symbols and functions of
666 symbols.  You'll soon learn in this chapter how many of the functions on
667 symbols are really classes.  This is because simple symbolic arithmetic
668 is not supported by languages like C++ so in a certain way GiNaC has to
669 implement its own arithmetic.
670
671 To give an idea about what kinds of symbolic composits may be built we
672 have a look at the most important classes in the class hierarchy.  The
673 dashed line symbolizes a "points to" or "handles" relationship while the
674 solid lines stand for "inherits from" relationship in the class
675 hierarchy:
676
677 @image{classhierarchy}
678
679 Some of the classes shown here (the ones sitting in white boxes) are
680 abstract base classes that are of no interest at all for the user.  They
681 are used internally in order to avoid code duplication if two or more
682 classes derived from them share certain features.  An example would be
683 @code{expairseq}, which is a container for a sequence of pairs each
684 consisting of one expression and a number (@code{numeric}).  What
685 @emph{is} visible to the user are the derived classes @code{add} and
686 @code{mul}, representing sums of terms and products, respectively.
687 We'll come back later to some more details about these two classes and
688 motivate the use of pairs in sums and products here.
689
690
691 @node Symbols, Numbers, The Class Hierarchy, Basic Concepts
692 @c    node-name, next, previous, up
693 @section Symbols
694 @cindex Symbols (class @code{symbol})
695
696 Symbols are for symbolic manipulation what atoms are for chemistry.  You
697 can declare objects of class @code{symbol} as any other object simply by
698 saying @code{symbol x,y;}.  There is, however, a catch in here having to
699 do with the fact that C++ is a compiled language.  The information about
700 the symbol's name is thrown away by the compiler but at a later stage
701 you may want to print expressions holding your symbols.  In order to
702 avoid confusion GiNaC's symbols are able to know their own name.  This
703 is accomplished by declaring its name for output at construction time in
704 the fashion @code{symbol x("x");}.  If you declare a symbol using the
705 default constructor (i.e. without string argument) the system will deal
706 out a unique name.  That name may not be suitable for printing but for
707 internal routines when no output is desired it is often enough.  We'll
708 come across examples of such symbols later in this tutorial.
709
710 This implies that the strings passed to symbols at construction time may
711 not be used for comparing two of them.  It is perfectly legitimate to
712 write @code{symbol x("x"),y("x");} but it is likely to lead into
713 trouble.  Here, @code{x} and @code{y} are different symbols and
714 statements like @code{x-y} will not be simplified to zero although the
715 output @code{x-x} looks funny.  Such output may also occur when there
716 are two different symbols in two scopes, for instance when you call a
717 function that declares a symbol with a name already existent in a symbol
718 in the calling function.  Again, comparing them (using @code{operator==}
719 for instance) will always reveal their difference.  Watch out, please.
720
721 Although symbols can be assigned expressions for internal reasons, you
722 should not do it (and we are not going to tell you how it is done).  If
723 you want to replace a symbol with something else in an expression, you
724 can use the expression's @code{.subs()} method.
725
726
727 @node Numbers, Constants, Symbols, Basic Concepts
728 @c    node-name, next, previous, up
729 @section Numbers
730 @cindex numbers (class @code{numeric})
731
732 @cindex CLN
733 For storing numerical things, GiNaC uses Bruno Haible's library
734 @acronym{CLN}.  The classes therein serve as foundation classes for
735 GiNaC.  @acronym{CLN} stands for Class Library for Numbers or
736 alternatively for Common Lisp Numbers.  In order to find out more about
737 @acronym{CLN}'s internals the reader is refered to the documentation of
738 that library.  @inforef{Introduction, , cln}, for more
739 information. Suffice to say that it is by itself build on top of another
740 library, the GNU Multiple Precision library @acronym{GMP}, which is an
741 extremely fast library for arbitrary long integers and rationals as well
742 as arbitrary precision floating point numbers.  It is very commonly used
743 by several popular cryptographic applications.  @acronym{CLN} extends
744 @acronym{GMP} by several useful things: First, it introduces the complex
745 number field over either reals (i.e. floating point numbers with
746 arbitrary precision) or rationals.  Second, it automatically converts
747 rationals to integers if the denominator is unity and complex numbers to
748 real numbers if the imaginary part vanishes and also correctly treats
749 algebraic functions.  Third it provides good implementations of
750 state-of-the-art algorithms for all trigonometric and hyperbolic
751 functions as well as for calculation of some useful constants.
752
753 The user can construct an object of class @code{numeric} in several
754 ways.  The following example shows the four most important constructors.
755 It uses construction from C-integer, construction of fractions from two
756 integers, construction from C-float and construction from a string:
757
758 @example
759 #include <ginac/ginac.h>
760 using namespace GiNaC;
761
762 int main()
763 @{
764     numeric two(2);                     // exact integer 2
765     numeric r(2,3);                     // exact fraction 2/3
766     numeric e(2.71828);                 // floating point number
767     numeric p("3.1415926535897932385"); // floating point number
768
769     cout << two*p << endl;  // floating point 6.283...
770     // ...
771 @}
772 @end example
773
774 Note that all those constructors are @emph{explicit} which means you are
775 not allowed to write @code{numeric two=2;}.  This is because the basic
776 objects to be handled by GiNaC are the expressions @code{ex} and we want
777 to keep things simple and wish objects like @code{pow(x,2)} to be
778 handled the same way as @code{pow(x,a)}, which means that we need to
779 allow a general @code{ex} as base and exponent.  Therefore there is an
780 implicit constructor from C-integers directly to expressions handling
781 numerics at work in most of our examples.  This design really becomes
782 convenient when one declares own functions having more than one
783 parameter but it forbids using implicit constructors because that would
784 lead to ambiguities.
785
786 It may be tempting to construct numbers writing @code{numeric r(3/2)}.
787 This would, however, call C's built-in operator @code{/} for integers
788 first and result in a numeric holding a plain integer 1.  @strong{Never
789 use @code{/} on integers!} Use the constructor from two integers
790 instead, as shown in the example above.  Writing @code{numeric(1)/2} may
791 look funny but works also.
792
793 @cindex @code{Digits}
794 @cindex accuracy
795 We have seen now the distinction between exact numbers and floating
796 point numbers.  Clearly, the user should never have to worry about
797 dynamically created exact numbers, since their "exactness" always
798 determines how they ought to be handled, i.e. how "long" they are.  The
799 situation is different for floating point numbers.  Their accuracy is
800 controlled by one @emph{global} variable, called @code{Digits}.  (For
801 those readers who know about Maple: it behaves very much like Maple's
802 @code{Digits}).  All objects of class numeric that are constructed from
803 then on will be stored with a precision matching that number of decimal
804 digits:
805
806 @example
807 #include <ginac/ginac.h>
808 using namespace GiNaC;
809
810 void foo()
811 @{
812     numeric three(3.0), one(1.0);
813     numeric x = one/three;
814
815     cout << "in " << Digits << " digits:" << endl;
816     cout << x << endl;
817     cout << Pi.evalf() << endl;
818 @}
819
820 int main()
821 @{
822     foo();
823     Digits = 60;
824     foo();
825     return 0;
826 @}
827 @end example
828
829 The above example prints the following output to screen:
830
831 @example
832 in 17 digits:
833 0.333333333333333333
834 3.14159265358979324
835 in 60 digits:
836 0.333333333333333333333333333333333333333333333333333333333333333333
837 3.14159265358979323846264338327950288419716939937510582097494459231
838 @end example
839
840 It should be clear that objects of class @code{numeric} should be used
841 for constructing numbers or for doing arithmetic with them.  The objects
842 one deals with most of the time are the polymorphic expressions @code{ex}.
843
844 @subsection Tests on numbers
845
846 Once you have declared some numbers, assigned them to expressions and
847 done some arithmetic with them it is frequently desired to retrieve some
848 kind of information from them like asking whether that number is
849 integer, rational, real or complex.  For those cases GiNaC provides
850 several useful methods.  (Internally, they fall back to invocations of
851 certain CLN functions.)
852
853 As an example, let's construct some rational number, multiply it with
854 some multiple of its denominator and test what comes out:
855
856 @example
857 #include <ginac/ginac.h>
858 using namespace GiNaC;
859
860 // some very important constants:
861 const numeric twentyone(21);
862 const numeric ten(10);
863 const numeric five(5);
864
865 int main()
866 @{
867     numeric answer = twentyone;
868
869     answer /= five;
870     cout << answer.is_integer() << endl;  // false, it's 21/5
871     answer *= ten;
872     cout << answer.is_integer() << endl;  // true, it's 42 now!
873     // ...
874 @}
875 @end example
876
877 Note that the variable @code{answer} is constructed here as an integer
878 by @code{numeric}'s copy constructor but in an intermediate step it
879 holds a rational number represented as integer numerator and integer
880 denominator.  When multiplied by 10, the denominator becomes unity and
881 the result is automatically converted to a pure integer again.
882 Internally, the underlying @acronym{CLN} is responsible for this
883 behaviour and we refer the reader to @acronym{CLN}'s documentation.
884 Suffice to say that the same behaviour applies to complex numbers as
885 well as return values of certain functions.  Complex numbers are
886 automatically converted to real numbers if the imaginary part becomes
887 zero.  The full set of tests that can be applied is listed in the
888 following table.
889
890 @cartouche
891 @multitable @columnfractions .33 .67
892 @item Method @tab Returns true if@dots{}
893 @item @code{.is_zero()}
894 @tab object is equal to zero
895 @item @code{.is_positive()}
896 @tab object is not complex and greater than 0
897 @item @code{.is_integer()}
898 @tab object is a (non-complex) integer
899 @item @code{.is_pos_integer()}
900 @tab object is an integer and greater than 0
901 @item @code{.is_nonneg_integer()}
902 @tab object is an integer and greater equal 0
903 @item @code{.is_even()}
904 @tab object is an even integer
905 @item @code{.is_odd()}
906 @tab object is an odd integer
907 @item @code{.is_prime()}
908 @tab object is a prime integer (probabilistic primality test)
909 @item @code{.is_rational()}
910 @tab object is an exact rational number (integers are rational, too, as are complex extensions like @math{2/3+7/2*I})
911 @item @code{.is_real()}
912 @tab object is a real integer, rational or float (i.e. is not complex)
913 @end multitable
914 @end cartouche
915
916
917 @node Constants, Fundamental operations, Numbers, Basic Concepts
918 @c    node-name, next, previous, up
919 @section Constants
920 @cindex constants (class @code{constant})
921 @cindex @code{evalf()}
922
923 Constants behave pretty much like symbols except that they return some
924 specific number when the method @code{.evalf()} is called.
925
926 The predefined known constants are:
927
928 @cartouche
929 @multitable @columnfractions .14 .29 .57
930 @item Name @tab Common Name @tab Numerical Value (35 digits)
931 @item @code{Pi}
932 @tab Archimedes' constant
933 @tab 3.14159265358979323846264338327950288
934 @item @code{Catalan}
935 @tab Catalan's constant
936 @tab 0.91596559417721901505460351493238411
937 @item @code{EulerGamma}
938 @tab Euler's (or Euler-Mascheroni) constant
939 @tab 0.57721566490153286060651209008240243
940 @end multitable
941 @end cartouche
942
943
944 @node Fundamental operations, Built-in functions, Constants, Basic Concepts
945 @c    node-name, next, previous, up
946 @section Fundamental operations: the @code{power}, @code{add} and @code{mul} classes
947 @cindex polynomials
948 @cindex @code{add}
949 @cindex @code{mul}
950 @cindex @code{power}
951
952 Simple polynomial expressions are written down in GiNaC pretty much like
953 in other CAS or like expressions involving numerical variables in C.
954 The necessary operators @code{+}, @code{-}, @code{*} and @code{/} have
955 been overloaded to achieve this goal.  When you run the following
956 program, the constructor for an object of type @code{mul} is
957 automatically called to hold the product of @code{a} and @code{b} and
958 then the constructor for an object of type @code{add} is called to hold
959 the sum of that @code{mul} object and the number one:
960
961 @example
962 #include <ginac/ginac.h>
963 using namespace GiNaC;
964
965 int main()
966 @{
967     symbol a("a"), b("b");
968     ex MyTerm = 1+a*b;
969     // ...
970 @}
971 @end example
972
973 For exponentiation, you have already seen the somewhat clumsy (though C-ish)
974 statement @code{pow(x,2);} to represent @code{x} squared.  This direct
975 construction is necessary since we cannot safely overload the constructor
976 @code{^} in C++ to construct a @code{power} object.  If we did, it would
977 have several counterintuitive effects:
978
979 @itemize @bullet
980 @item
981 Due to C's operator precedence, @code{2*x^2} would be parsed as @code{(2*x)^2}.
982 @item
983 Due to the binding of the operator @code{^}, @code{x^a^b} would result in
984 @code{(x^a)^b}. This would be confusing since most (though not all) other CAS
985 interpret this as @code{x^(a^b)}.
986 @item
987 Also, expressions involving integer exponents are very frequently used,
988 which makes it even more dangerous to overload @code{^} since it is then
989 hard to distinguish between the semantics as exponentiation and the one
990 for exclusive or.  (It would be embarassing to return @code{1} where one
991 has requested @code{2^3}.)
992 @end itemize
993
994 @cindex @code{ginsh}
995 All effects are contrary to mathematical notation and differ from the
996 way most other CAS handle exponentiation, therefore overloading @code{^}
997 is ruled out for GiNaC's C++ part.  The situation is different in
998 @command{ginsh}, there the exponentiation-@code{^} exists.  (Also note
999 that the other frequently used exponentiation operator @code{**} does
1000 not exist at all in C++).
1001
1002 To be somewhat more precise, objects of the three classes described
1003 here, are all containers for other expressions.  An object of class
1004 @code{power} is best viewed as a container with two slots, one for the
1005 basis, one for the exponent.  All valid GiNaC expressions can be
1006 inserted.  However, basic transformations like simplifying
1007 @code{pow(pow(x,2),3)} to @code{x^6} automatically are only performed
1008 when this is mathematically possible.  If we replace the outer exponent
1009 three in the example by some symbols @code{a}, the simplification is not
1010 safe and will not be performed, since @code{a} might be @code{1/2} and
1011 @code{x} negative.
1012
1013 Objects of type @code{add} and @code{mul} are containers with an
1014 arbitrary number of slots for expressions to be inserted.  Again, simple
1015 and safe simplifications are carried out like transforming
1016 @code{3*x+4-x} to @code{2*x+4}.
1017
1018 The general rule is that when you construct such objects, GiNaC
1019 automatically creates them in canonical form, which might differ from
1020 the form you typed in your program.  This allows for rapid comparison of
1021 expressions, since after all @code{a-a} is simply zero.  Note, that the
1022 canonical form is not necessarily lexicographical ordering or in any way
1023 easily guessable.  It is only guaranteed that constructing the same
1024 expression twice, either implicitly or explicitly, results in the same
1025 canonical form.
1026
1027
1028 @node Built-in functions, Important Algorithms, Fundamental operations, Basic Concepts
1029 @c    node-name, next, previous, up
1030 @section Built-in functions
1031
1032 There are quite a number of useful functions built into GiNaC.  They are
1033 all objects of class @code{function}.  They accept one or more
1034 expressions as arguments and return one expression.  If the arguments
1035 are not numerical, the evaluation of the function may be halted, as it
1036 does in the next example:
1037
1038 @example
1039 #include <ginac/ginac.h>
1040 using namespace GiNaC;
1041
1042 int main()
1043 @{
1044     symbol x("x"), y("y");
1045     
1046     ex foo = x+y/2;
1047     cout << "gamma(" << foo << ") -> " << gamma(foo) << endl;
1048     ex bar = foo.subs(y==1);
1049     cout << "gamma(" << bar << ") -> " << gamma(bar) << endl;
1050     ex foobar = bar.subs(x==7);
1051     cout << "gamma(" << foobar << ") -> " << gamma(foobar) << endl;
1052     // ...
1053 @}
1054 @end example
1055
1056 This program shows how the function returns itself twice and finally an
1057 expression that may be really useful:
1058
1059 @example
1060 gamma(x+(1/2)*y) -> gamma(x+(1/2)*y)
1061 gamma(x+1/2) -> gamma(x+1/2)
1062 gamma(15/2) -> (135135/128)*Pi^(1/2)
1063 @end example
1064
1065 Most of these functions can be differentiated, series expanded and so
1066 on.  Read the next chapter in order to learn more about this.
1067
1068
1069 @node Important Algorithms, Polynomial Expansion, Built-in functions, Top
1070 @c    node-name, next, previous, up
1071 @chapter Important Algorithms
1072 @cindex polynomials
1073
1074 In this chapter the most important algorithms provided by GiNaC will be
1075 described.  Some of them are implemented as functions on expressions,
1076 others are implemented as methods provided by expression objects.  If
1077 they are methods, there exists a wrapper function around it, so you can
1078 alternatively call it in a functional way as shown in the simple
1079 example:
1080
1081 @example
1082 #include <ginac/ginac.h>
1083 using namespace GiNaC;
1084
1085 int main()
1086 @{
1087     ex x = numeric(1.0);
1088     
1089     cout << "As method:   " << sin(x).evalf() << endl;
1090     cout << "As function: " << evalf(sin(x)) << endl;
1091     // ...
1092 @}
1093 @end example
1094
1095 The general rule is that wherever methods accept one or more parameters
1096 (@var{arg1}, @var{arg2}, @dots{}) the order of arguments the function
1097 wrapper accepts is the same but preceded by the object to act on
1098 (@var{object}, @var{arg1}, @var{arg2}, @dots{}).  This approach is the
1099 most natural one in an OO model but it may lead to confusion for MapleV
1100 users because where they would type @code{A:=x+1; subs(x=2,A);} GiNaC
1101 would require @code{A=x+1; subs(A,x==2);} (after proper declaration of
1102 @code{A} and @code{x}).  On the other hand, since MapleV returns 3 on
1103 @code{A:=x^2+3; coeff(A,x,0);} (GiNaC: @code{A=pow(x,2)+3;
1104 coeff(A,x,0);}) it is clear that MapleV is not trying to be consistent
1105 here.  Also, users of MuPAD will in most cases feel more comfortable
1106 with GiNaC's convention.  All function wrappers are always implemented
1107 as simple inline functions which just call the corresponding method and
1108 are only provided for users uncomfortable with OO who are dead set to
1109 avoid method invocations.  Generally, a chain of function wrappers is
1110 much harder to read than a chain of methods and should therefore be
1111 avoided if possible.  On the other hand, not everything in GiNaC is a
1112 method on class @code{ex} and sometimes calling a function cannot be
1113 avoided.
1114
1115 @menu
1116 * Polynomial Expansion::
1117 * Collecting expressions::
1118 * Polynomial Arithmetic::
1119 * Symbolic Differentiation::
1120 * Series Expansion::
1121 @end menu
1122
1123
1124 @node Polynomial Expansion, Collecting expressions, Important Algorithms, Important Algorithms
1125 @c    node-name, next, previous, up
1126 @section Polynomial Expansion
1127 @cindex @code{expand()}
1128
1129 A polynomial in one or more variables has many equivalent
1130 representations.  Some useful ones serve a specific purpose.  Consider
1131 for example the trivariate polynomial @math{4*x*y + x*z + 20*y^2 +
1132 21*y*z + 4*z^2} (written down here in output-style).  It is equivalent
1133 to the factorized polynomial @math{(x + 5*y + 4*z)*(4*y + z)}.  Other
1134 representations are the recursive ones where one collects for exponents
1135 in one of the three variable.  Since the factors are themselves
1136 polynomials in the remaining two variables the procedure can be
1137 repeated.  In our expample, two possibilities would be @math{(4*y + z)*x
1138 + 20*y^2 + 21*y*z + 4*z^2} and @math{20*y^2 + (21*z + 4*x)*y + 4*z^2 +
1139 x*z}.
1140
1141 To bring an expression into expanded form, its method @code{.expand()}
1142 may be called.  In our example above, this corresponds to @math{4*x*y +
1143 x*z + 20*y^2 + 21*y*z + 4*z^2}.  Again, since the canonical form in
1144 GiNaC is not easily guessable you should be prepared to see different
1145 orderings of terms in such sums!
1146
1147
1148 @node Collecting expressions, Polynomial Arithmetic, Polynomial Expansion, Important Algorithms
1149 @c    node-name, next, previous, up
1150 @section Collecting expressions
1151 @cindex @code{collect()}
1152 @cindex @code{coeff()}
1153
1154 Another useful representation of multivariate polynomials is as a
1155 univariate polynomial in one of the variables with the coefficients
1156 being polynomials in the remaining variables.  The method
1157 @code{collect()} accomplishes this task.  Here is its declaration:
1158
1159 @example
1160 ex ex::collect(symbol const & s);
1161 @end example
1162
1163 Note that the original polynomial needs to be in expanded form in order
1164 to be able to find the coefficients properly.  The range of occuring
1165 coefficients can be checked using the two methods
1166
1167 @example
1168 int ex::degree(symbol const & s);
1169 int ex::ldegree(symbol const & s);
1170 @end example
1171
1172 where @code{degree()} returns the highest coefficient and
1173 @code{ldegree()} the lowest one.  (These two methods work also reliably
1174 on non-expanded input polynomials).  An application is illustrated in
1175 the next example, where a multivariate polynomial is analysed:
1176
1177 @example
1178 #include <ginac/ginac.h>
1179 using namespace GiNaC;
1180
1181 int main()
1182 @{
1183     symbol x("x"), y("y");
1184     ex PolyInp = 4*pow(x,3)*y + 5*x*pow(y,2) + 3*y
1185                  - pow(x+y,2) + 2*pow(y+2,2) - 8;
1186     ex Poly = PolyInp.expand();
1187     
1188     for (int i=Poly.ldegree(x); i<=Poly.degree(x); ++i) @{
1189         cout << "The x^" << i << "-coefficient is "
1190              << Poly.coeff(x,i) << endl;
1191     @}
1192     cout << "As polynomial in y: " 
1193          << Poly.collect(y) << endl;
1194     // ...
1195 @}
1196 @end example
1197
1198 When run, it returns an output in the following fashion:
1199
1200 @example
1201 The x^0-coefficient is y^2+11*y
1202 The x^1-coefficient is 5*y^2-2*y
1203 The x^2-coefficient is -1
1204 The x^3-coefficient is 4*y
1205 As polynomial in y: -x^2+(5*x+1)*y^2+(-2*x+4*x^3+11)*y
1206 @end example
1207
1208 As always, the exact output may vary between different versions of GiNaC
1209 or even from run to run since the internal canonical ordering is not
1210 within the user's sphere of influence.
1211
1212
1213 @node Polynomial Arithmetic, Symbolic Differentiation, Collecting expressions, Important Algorithms
1214 @c    node-name, next, previous, up
1215 @section Polynomial Arithmetic
1216
1217 @subsection GCD and LCM
1218 @cindex GCD
1219 @cindex LCM
1220
1221 The functions for polynomial greatest common divisor and least common
1222 multiple have the synopsis:
1223
1224 @example
1225 ex gcd(const ex & a, const ex & b);
1226 ex lcm(const ex & a, const ex & b);
1227 @end example
1228
1229 The functions @code{gcd()} and @code{lcm()} accept two expressions
1230 @code{a} and @code{b} as arguments and return a new expression, their
1231 greatest common divisor or least common multiple, respectively.  If the
1232 polynomials @code{a} and @code{b} are coprime @code{gcd(a,b)} returns 1
1233 and @code{lcm(a,b)} returns the product of @code{a} and @code{b}.
1234
1235 @example
1236 #include <ginac/ginac.h>
1237 using namespace GiNaC;
1238
1239 int main()
1240 @{
1241     symbol x("x"), y("y"), z("z");
1242     ex P_a = 4*x*y + x*z + 20*pow(y, 2) + 21*y*z + 4*pow(z, 2);
1243     ex P_b = x*y + 3*x*z + 5*pow(y, 2) + 19*y*z + 12*pow(z, 2);
1244
1245     ex P_gcd = gcd(P_a, P_b);
1246     // x + 5*y + 4*z
1247     ex P_lcm = lcm(P_a, P_b);
1248     // 4*x*y^2 + 13*y*x*z + 20*y^3 + 81*y^2*z + 67*y*z^2 + 3*x*z^2 + 12*z^3
1249     // ...
1250 @}
1251 @end example
1252
1253 @subsection The @code{normal} method
1254 @cindex @code{normal()}
1255 @cindex temporary replacement
1256
1257 While in common symbolic code @code{gcd()} and @code{lcm()} are not too
1258 heavily used, simplification is called for frequently.  Therefore
1259 @code{.normal()}, which provides some basic form of simplification, has
1260 become a method of class @code{ex}, just like @code{.expand()}.  It
1261 converts a rational function into an equivalent rational function where
1262 numerator and denominator are coprime.  This means, it finds the GCD of
1263 numerator and denominator and cancels it.  If it encounters some object
1264 which does not belong to the domain of rationals (a function for
1265 instance), that object is replaced by a temporary symbol.  This means
1266 that both expressions @code{t1} and @code{t2} are indeed simplified in
1267 this little program:
1268
1269 @example
1270 #include <ginac/ginac.h>
1271 using namespace GiNaC;
1272
1273 int main()
1274 @{
1275     symbol x("x");
1276     ex t1 = (pow(x,2) + 2*x + 1)/(x + 1);
1277     ex t2 = (pow(sin(x),2) + 2*sin(x) + 1)/(sin(x) + 1);
1278     cout << "t1 is " << t1.normal() << endl;
1279     cout << "t2 is " << t2.normal() << endl;
1280     // ...
1281 @}
1282 @end example
1283
1284 Of course this works for multivariate polynomials too, so the ratio of
1285 the sample-polynomials from the section about GCD and LCM above would be
1286 normalized to @code{P_a/P_b} = @code{(4*y+z)/(y+3*z)}.
1287
1288
1289 @node Symbolic Differentiation, Series Expansion, Polynomial Arithmetic, Important Algorithms
1290 @c    node-name, next, previous, up
1291 @section Symbolic Differentiation
1292 @cindex differentiation
1293 @cindex chain rule
1294 @cindex product rule
1295
1296 GiNaC's objects know how to differentiate themselves.  Thus, a
1297 polynomial (class @code{add}) knows that its derivative is the sum of
1298 the derivatives of all the monomials:
1299
1300 @example
1301 #include <ginac/ginac.h>
1302 using namespace GiNaC;
1303
1304 int main()
1305 @{
1306     symbol x("x"), y("y"), z("z");
1307     ex P = pow(x, 5) + pow(x, 2) + y;
1308
1309     cout << P.diff(x,2) << endl;  // 20*x^3 + 2
1310     cout << P.diff(y) << endl;    // 1
1311     cout << P.diff(z) << endl;    // 0
1312     // ...
1313 @}
1314 @end example
1315
1316 If a second integer parameter @var{n} is given, the @code{diff} method
1317 returns the @var{n}th derivative.
1318
1319 If @emph{every} object and every function is told what its derivative
1320 is, all derivatives of composed objects can be calculated using the
1321 chain rule and the product rule.  Consider, for instance the expression
1322 @code{1/cosh(x)}.  Since the derivative of @code{cosh(x)} is
1323 @code{sinh(x)} and the derivative of @code{pow(x,-1)} is
1324 @code{-pow(x,-2)}, GiNaC can readily compute the composition.  It turns
1325 out that the composition is the generating function for Euler Numbers,
1326 i.e. the so called @var{n}th Euler number is the coefficient of
1327 @code{x^n/n!} in the expansion of @code{1/cosh(x)}.  We may use this
1328 identity to code a function that generates Euler numbers in just three
1329 lines:
1330
1331 @cindex Euler numbers
1332 @example
1333 #include <ginac/ginac.h>
1334 using namespace GiNaC;
1335
1336 ex EulerNumber(unsigned n)
1337 @{
1338     symbol x;
1339     ex generator = pow(cosh(x),-1);
1340     return generator.diff(x,n).subs(x==0);
1341 @}
1342
1343 int main()
1344 @{
1345     for (unsigned i=0; i<11; i+=2)
1346         cout << EulerNumber(i) << endl;
1347     return 0;
1348 @}
1349 @end example
1350
1351 When you run it, it produces the sequence @code{1}, @code{-1}, @code{5},
1352 @code{-61}, @code{1385}, @code{-50521}.  We increment the loop variable
1353 @code{i} by two since all odd Euler numbers vanish anyways.
1354
1355
1356 @node Series Expansion, Extending GiNaC, Symbolic Differentiation, Important Algorithms
1357 @c    node-name, next, previous, up
1358 @section Series Expansion
1359 @cindex series expansion
1360 @cindex Taylor expansion
1361 @cindex Laurent expansion
1362
1363 Expressions know how to expand themselves as a Taylor series or (more
1364 generally) a Laurent series.  Similar to most conventional Computer
1365 Algebra Systems, no distinction is made between those two.  There is a
1366 class of its own for storing such series as well as a class for storing
1367 the order of the series.  A sample program could read:
1368
1369 @example
1370 #include <ginac/ginac.h>
1371 using namespace GiNaC;
1372
1373 int main()
1374 @{
1375     symbol x("x");
1376     numeric point(0);
1377     ex MyExpr1 = sin(x);
1378     ex MyExpr2 = 1/(x - pow(x, 2) - pow(x, 3));
1379     ex MyTailor, MySeries;
1380     
1381     MyTailor = MyExpr1.series(x, point, 5);
1382     cout << MyExpr1 << " == " << MyTailor
1383          << " for small " << x << endl;
1384     MySeries = MyExpr2.series(x, point, 7);
1385     cout << MyExpr2 << " == " << MySeries
1386          << " for small " << x << endl;
1387     // ...
1388 @}
1389 @end example
1390
1391 @cindex M@'echain's formula
1392 As an instructive application, let us calculate the numerical value of
1393 Archimedes' constant
1394 @tex
1395 $\pi$
1396 @end tex
1397 (for which there already exists the built-in constant @code{Pi}) 
1398 using M@'echain's amazing formula
1399 @tex
1400 $\pi=16$~atan~$\!\left(1 \over 5 \right)-4$~atan~$\!\left(1 \over 239 \right)$.
1401 @end tex
1402 @ifnottex
1403 @math{Pi==16*atan(1/5)-4*atan(1/239)}.
1404 @end ifnottex
1405 We may expand the arcus tangent around @code{0} and insert the fractions
1406 @code{1/5} and @code{1/239}.  But, as we have seen, a series in GiNaC
1407 carries an order term with it and the question arises what the system is
1408 supposed to do when the fractions are plugged into that order term.  The
1409 solution is to use the function @code{series_to_poly()} to simply strip
1410 the order term off:
1411
1412 @example
1413 #include <ginac/ginac.h>
1414 using namespace GiNaC;
1415
1416 ex mechain_pi(int degr)
1417 @{
1418     symbol x;
1419     ex pi_expansion = series_to_poly(atan(x).series(x,0,degr));
1420     ex pi_approx = 16*pi_expansion.subs(x==numeric(1,5))
1421                    -4*pi_expansion.subs(x==numeric(1,239));
1422     return pi_approx;
1423 @}
1424
1425 int main()
1426 @{
1427     ex pi_frac;
1428     for (int i=2; i<12; i+=2) @{
1429         pi_frac = mechain_pi(i);
1430         cout << i << ":\t" << pi_frac << endl
1431              << "\t" << pi_frac.evalf() << endl;
1432     @}
1433     return 0;
1434 @}
1435 @end example
1436
1437 When you run this program, it will type out:
1438
1439 @example
1440 2:      3804/1195
1441         3.1832635983263598326
1442 4:      5359397032/1706489875
1443         3.1405970293260603143
1444 6:      38279241713339684/12184551018734375
1445         3.141621029325034425
1446 8:      76528487109180192540976/24359780855939418203125
1447         3.141591772182177295
1448 10:     327853873402258685803048818236/104359128170408663038552734375
1449         3.1415926824043995174
1450 @end example
1451
1452
1453 @node Extending GiNaC, What does not belong into GiNaC, Series Expansion, Top
1454 @c    node-name, next, previous, up
1455 @chapter Extending GiNaC
1456
1457 By reading so far you should have gotten a fairly good understanding of
1458 GiNaC's design-patterns.  From here on you should start reading the
1459 sources.  All we can do now is issue some recommendations how to tackle
1460 GiNaC's many loose ends in order to fulfill everybody's dreams.  If you
1461 develop some useful extension please don't hesitate to contact the GiNaC
1462 authors---they will happily incorporate them into future versions.
1463
1464 @menu
1465 * What does not belong into GiNaC::  What to avoid.
1466 * Symbolic functions::               Implementing symbolic functions.
1467 @end menu
1468
1469
1470 @node What does not belong into GiNaC, Symbolic functions, Extending GiNaC, Extending GiNaC
1471 @c    node-name, next, previous, up
1472 @section What doesn't belong into GiNaC
1473
1474 @cindex @code{ginsh}
1475 First of all, GiNaC's name must be read literally.  It is designed to be
1476 a library for use within C++.  The tiny @command{ginsh} accompanying
1477 GiNaC makes this even more clear: it doesn't even attempt to provide a
1478 language.  There are no loops or conditional expressions in
1479 @command{ginsh}, it is merely a window into the library for the
1480 programmer to test stuff (or to show off).  Still, the design of a
1481 complete CAS with a language of its own, graphical capabilites and all
1482 this on top of GiNaC is possible and is without doubt a nice project for
1483 the future.
1484
1485 There are many built-in functions in GiNaC that do not know how to
1486 evaluate themselves numerically to a precision declared at runtime
1487 (using @code{Digits}).  Some may be evaluated at certain points, but not
1488 generally.  This ought to be fixed.  However, doing numerical
1489 computations with GiNaC's quite abstract classes is doomed to be
1490 inefficient.  For this purpose, the underlying bignum-package
1491 @acronym{CLN} is much better suited.
1492
1493
1494 @node Symbolic functions, A Comparison With Other CAS, What does not belong into GiNaC, Extending GiNaC
1495 @c    node-name, next, previous, up
1496 @section Symbolic functions
1497
1498 The easiest and most instructive way to start with is probably to
1499 implement your own function.  Objects of class @code{function} are
1500 inserted into the system via a kind of "registry".  They get a serial
1501 number that is used internally to identify them but you usually need not
1502 worry about this.  What you have to care for are functions that are
1503 called when the user invokes certain methods.  These are usual
1504 C++-functions accepting a number of @code{ex} as arguments and returning
1505 one @code{ex}.  As an example, if we have a look at a simplified
1506 implementation of the cosine trigonometric function, we first need a
1507 function that is called when one wishes to @code{eval} it.  It could
1508 look something like this:
1509
1510 @example
1511 static ex cos_eval_method(ex const & x)
1512 @{
1513     // if x%2*Pi return 1
1514     // if x%Pi return -1
1515     // if x%Pi/2 return 0
1516     // care for other cases...
1517     return cos(x).hold();
1518 @}
1519 @end example
1520
1521 The last line returns @code{cos(x)} if we don't know what else to do and
1522 stops a potential recursive evaluation by saying @code{.hold()}.  We
1523 should also implement a method for numerical evaluation and since we are
1524 lazy we sweep the problem under the rug by calling someone else's
1525 function that does so, in this case the one in class @code{numeric}:
1526
1527 @example
1528 static ex cos_evalf_method(ex const & x)
1529 @{
1530     return sin(ex_to_numeric(x));
1531 @}
1532 @end example
1533
1534 Differentiation will surely turn up and so we need to tell
1535 @code{sin} how to differentiate itself:
1536
1537 @example
1538 static ex cos_diff_method(ex const & x, unsigned diff_param)
1539 @{
1540     return cos(x);
1541 @}
1542 @end example
1543
1544 @cindex product rule
1545 The second parameter is obligatory but uninteresting at this point.  It
1546 specifies which parameter to differentiate in a partial derivative in
1547 case the function has more than one parameter and its main application
1548 is for correct handling of the chain rule.  For Taylor expansion, it is
1549 enough to know how to differentiate.  But if the function you want to
1550 implement does have a pole somewhere in the complex plane, you need to
1551 write another method for Laurent expansion around that point.
1552
1553 Now that all the ingrediences for @code{cos} have been set up, we need
1554 to tell the system about it.  This is done by a macro and we are not
1555 going to descibe how it expands, please consult your preprocessor if you
1556 are curious:
1557
1558 @example
1559 REGISTER_FUNCTION(cos, cos_eval_method, cos_evalf_method, cos_diff, NULL);
1560 @end example
1561
1562 The first argument is the function's name, the second, third and fourth
1563 bind the corresponding methods to this objects and the fifth is a slot
1564 for inserting a method for series expansion.  Also, the new function
1565 needs to be declared somewhere.  This may also be done by a convenient
1566 preprocessor macro:
1567
1568 @example
1569 DECLARE_FUNCTION_1P(cos)
1570 @end example
1571
1572 The suffix @code{_1P} stands for @emph{one parameter}.  Of course, this
1573 implementation of @code{cos} is very incomplete and lacks several safety
1574 mechanisms.  Please, have a look at the real implementation in GiNaC.
1575 (By the way: in case you are worrying about all the macros above we can
1576 assure you that functions are GiNaC's most macro-intense classes.  We
1577 have done our best to avoid them where we can.)
1578
1579 That's it. May the source be with you!
1580
1581
1582 @node A Comparison With Other CAS, Internal Structures, Symbolic functions, Top
1583 @c    node-name, next, previous, up
1584 @chapter A Comparison With Other CAS
1585 @cindex advocacy
1586
1587 This chapter will give you some information on how GiNaC compares to
1588 other, traditional Computer Algebra Systems, like @emph{Maple},
1589 @emph{Mathematica} or @emph{Reduce}, where it has advantages and
1590 disadvantages over these systems.
1591
1592
1593 @heading Advantages
1594
1595 GiNaC has several advantages over traditional Computer
1596 Algebra Systems, like 
1597
1598 @itemize @bullet
1599
1600 @item
1601 familiar language: all common CAS implement their own proprietary
1602 grammar which you have to learn first (and maybe learn again when your
1603 vendor chooses to "enhance" it).  With GiNaC you can write your program
1604 in common C++, which is standardized.
1605
1606 @item
1607 structured data types: you can build up structured data types using
1608 @code{struct}s or @code{class}es together with STL features instead of
1609 using unnamed lists of lists of lists.
1610
1611 @item
1612 strongly typed: in CAS, you usually have only one kind of variables
1613 which can hold contents of an arbitrary type.  This 4GL like feature is
1614 nice for novice programmers, but dangerous.
1615     
1616 @item
1617 development tools: powerful development tools exist for C++, like fancy
1618 editors (e.g. with automatic indentation and syntax highlighting),
1619 debuggers, visualization tools, documentation tools...
1620
1621 @item
1622 modularization: C++ programs can easily be split into modules by
1623 separating interface and implementation.
1624
1625 @item
1626 price: GiNaC is distributed under the GNU Public License which means
1627 that it is free and available with source code.  And there are excellent
1628 C++-compilers for free, too.
1629     
1630 @item
1631 extendable: you can add your own classes to GiNaC, thus extending it on
1632 a very low level.  Compare this to a traditional CAS that you can
1633 usually only extend on a high level by writing in the language defined
1634 by the parser.  In particular, it turns out to be almost impossible to
1635 fix bugs in a traditional system.
1636
1637 @item
1638 seemless integration: it is somewhere between difficult and impossible
1639 to call CAS functions from within a program written in C++ or any other
1640 programming language and vice versa.  With GiNaC, your symbolic routines
1641 are part of your program.  You can easily call third party libraries,
1642 e.g. for numerical evaluation or graphical interaction.  All other
1643 approaches are much more cumbersome: they range from simply ignoring the
1644 problem (i.e. @emph{Maple}) to providing a method for "embedding" the
1645 system (i.e. @emph{Yacas}).
1646
1647 @item
1648 efficiency: often large parts of a program do not need symbolic
1649 calculations at all.  Why use large integers for loop variables or
1650 arbitrary precision arithmetics where double accuracy is sufficient?
1651 For pure symbolic applications, GiNaC is comparable in speed with other
1652 CAS.
1653
1654 @end itemize
1655
1656
1657 @heading Disadvantages
1658
1659 Of course it also has some disadvantages:
1660
1661 @itemize @bullet
1662
1663 @item
1664 not interactive: GiNaC programs have to be written in an editor,
1665 compiled and executed. You cannot play with expressions interactively.
1666 However, such an extension is not inherently forbidden by design.  In
1667 fact, two interactive interfaces are possible: First, a simple shell
1668 that exposes GiNaC's types to a command line can readily be written (and
1669 has been written) and second, as a more consistent approach we plan an
1670 integration with the @acronym{CINT} C++ interpreter.
1671
1672 @item
1673 advanced features: GiNaC cannot compete with a program like
1674 @emph{Reduce} which exists for more than 30 years now or @emph{Maple}
1675 which grows since 1981 by the work of dozens of programmers, with
1676 respect to mathematical features. Integration, factorization,
1677 non-trivial simplifications, limits etc. are missing in GiNaC (and are
1678 not planned for the near future).
1679
1680 @item
1681 portability: While the GiNaC library itself is designed to avoid any
1682 platform dependent features (it should compile on any ANSI compliant C++
1683 compiler), the currently used version of the CLN library (fast large
1684 integer and arbitrary precision arithmetics) can be compiled only on
1685 systems with a recently new C++ compiler from the GNU Compiler
1686 Collection (@acronym{GCC}).  GiNaC uses recent language features like
1687 explicit constructors, mutable members, RTTI, @code{dynamic_cast}s and
1688 STL, so ANSI compliance is meant literally.  Recent @acronym{GCC}
1689 versions starting at 2.95, although itself not yet ANSI compliant,
1690 support all needed features.
1691     
1692 @end itemize
1693
1694
1695 @heading Why C++?
1696
1697 Why did we choose to implement GiNaC in C++ instead of Java or any other
1698 language? C++ is not perfect: type checking is not strict (casting is
1699 possible), separation between interface and implementation is not
1700 complete, object oriented design is not enforced.  The main reason is
1701 the often scolded feature of operator overloading in C++. While it may
1702 be true that operating on classes with a @code{+} operator is rarely
1703 meaningful, it is perfectly suited for algebraic expressions. Writing
1704 @math{3x+5y} as @code{3*x+5*y} instead of
1705 @code{x.times(3).plus(y.times(5))} looks much more natural. Furthermore,
1706 the main developers are more familiar with C++ than with any other
1707 programming language.
1708
1709
1710 @node Internal Structures, Expressions are reference counted, A Comparison With Other CAS, Top
1711 @c    node-name, next, previous, up
1712 @appendix Internal Structures
1713
1714 @menu
1715 * Expressions are reference counted::
1716 * Internal representation of products and sums::
1717 @end menu
1718
1719 @node Expressions are reference counted, Internal representation of products and sums, Internal Structures, Internal Structures
1720 @c    node-name, next, previous, up
1721 @appendixsection Expressions are reference counted
1722
1723 @cindex reference counting
1724 @cindex copy-on-write
1725 @cindex garbage collection
1726 An expression is extremely light-weight since internally it works like a
1727 handle to the actual representation and really holds nothing more than a
1728 pointer to some other object. What this means in practice is that
1729 whenever you create two @code{ex} and set the second equal to the first
1730 no copying process is involved. Instead, the copying takes place as soon
1731 as you try to change the second.  Consider the simple sequence of code:
1732
1733 @example
1734 #include <ginac/ginac.h>
1735 using namespace GiNaC;
1736
1737 int main()
1738 @{
1739     symbol x("x"), y("y"), z("z");
1740     ex e1, e2;
1741
1742     e1 = sin(x + 2*y) + 3*z + 41;
1743     e2 = e1;                // e2 points to same object as e1
1744     cout << e2 << endl;     // prints sin(x+2*y)+3*z+41
1745     e2 += 1;                // e2 is copied into a new object
1746     cout << e2 << endl;     // prints sin(x+2*y)+3*z+42
1747     // ...
1748 @}
1749 @end example
1750
1751 The line @code{e2 = e1;} creates a second expression pointing to the
1752 object held already by @code{e1}.  The time involved for this operation
1753 is therefore constant, no matter how large @code{e1} was.  Actual
1754 copying, however, must take place in the line @code{e2 += 1;} because
1755 @code{e1} and @code{e2} are not handles for the same object any more.
1756 This concept is called @dfn{copy-on-write semantics}.  It increases
1757 performance considerably whenever one object occurs multiple times and
1758 represents a simple garbage collection scheme because when an @code{ex}
1759 runs out of scope its destructor checks whether other expressions handle
1760 the object it points to too and deletes the object from memory if that
1761 turns out not to be the case.  A slightly less trivial example of
1762 differentiation using the chain-rule should make clear how powerful this
1763 can be:
1764
1765 @example
1766 #include <ginac/ginac.h>
1767 using namespace GiNaC;
1768
1769 int main()
1770 @{
1771     symbol x("x"), y("y");
1772
1773     ex e1 = x + 3*y;
1774     ex e2 = pow(e1, 3);
1775     ex e3 = diff(sin(e2), x);   // first derivative of sin(e2) by x
1776     cout << e1 << endl          // prints x+3*y
1777          << e2 << endl          // prints (x+3*y)^3
1778          << e3 << endl;         // prints 3*(x+3*y)^2*cos((x+3*y)^3)
1779     // ...
1780 @}
1781 @end example
1782
1783 Here, @code{e1} will actually be referenced three times while @code{e2}
1784 will be referenced two times.  When the power of an expression is built,
1785 that expression needs not be copied.  Likewise, since the derivative of
1786 a power of an expression can be easily expressed in terms of that
1787 expression, no copying of @code{e1} is involved when @code{e3} is
1788 constructed.  So, when @code{e3} is constructed it will print as
1789 @code{3*(x+3*y)^2*cos((x+3*y)^3)} but the argument of @code{cos()} only
1790 holds a reference to @code{e2} and the factor in front is just
1791 @code{3*e1^2}.
1792
1793 As a user of GiNaC, you cannot see this mechanism of copy-on-write
1794 semantics.  When you insert an expression into a second expression, the
1795 result behaves exactly as if the contents of the first expression were
1796 inserted.  But it may be useful to remember that this is not what
1797 happens.  Knowing this will enable you to write much more efficient
1798 code.  If you still have an uncertain feeling with copy-on-write
1799 semantics, we recommend you have a look at the
1800 @uref{http://www.cerfnet.com/~mpcline/c++-faq-lite/, C++-FAQ lite} by
1801 Marshall Cline.  Chapter 16 covers this issue and presents an
1802 implementation which is pretty close to the one in GiNaC.
1803
1804
1805 @node Internal representation of products and sums, Bibliography, Expressions are reference counted, Internal Structures
1806 @c    node-name, next, previous, up
1807 @appendixsection Internal representation of products and sums
1808
1809 @cindex representation
1810 @cindex @code{add}
1811 @cindex @code{mul}
1812 @cindex @code{power}
1813 Although it should be completely transparent for the user of
1814 GiNaC a short discussion of this topic helps to understand the sources
1815 and also explain performance to a large degree.  Consider the 
1816 unexpanded symbolic expression 
1817 @tex
1818 $2d^3 \left( 4a + 5b - 3 \right)$
1819 @end tex
1820 @ifnottex
1821 @math{2*d^3*(4*a+5*b-3)}
1822 @end ifnottex
1823 which could naively be represented by a tree of linear containers for
1824 addition and multiplication, one container for exponentiation with base
1825 and exponent and some atomic leaves of symbols and numbers in this
1826 fashion:
1827
1828 @image{repnaive}
1829
1830 @cindex pair-wise representation
1831 However, doing so results in a rather deeply nested tree which will
1832 quickly become inefficient to manipulate.  We can improve on this by
1833 representing the sum instead as a sequence of terms, each one being a
1834 pair of a purely numeric multiplicative coefficient and its rest.  In
1835 the same spirit we can store the multiplication as a sequence of terms,
1836 each having a numeric exponent and a possibly complicated base, the tree
1837 becomes much more flat:
1838
1839 @image{reppair}
1840
1841 The number @code{3} above the symbol @code{d} shows that @code{mul}
1842 objects are treated similarly where the coefficients are interpreted as
1843 @emph{exponents} now.  Addition of sums of terms or multiplication of
1844 products with numerical exponents can be coded to be very efficient with
1845 such a pair-wise representation.  Internally, this handling is performed
1846 by most CAS in this way.  It typically speeds up manipulations by an
1847 order of magnitude.  The overall multiplicative factor @code{2} and the
1848 additive term @code{-3} look somewhat out of place in this
1849 representation, however, since they are still carrying a trivial
1850 exponent and multiplicative factor @code{1} respectively.  Within GiNaC,
1851 this is avoided by adding a field that carries an overall numeric
1852 coefficient.  This results in the realistic picture of internal
1853 representation for
1854 @tex
1855 $2d^3 \left( 4a + 5b - 3 \right)$
1856 @end tex
1857 @ifnottex
1858 @math{2*d^3*(4*a+5*b-3)}
1859 @end ifnottex
1860 :
1861
1862 @image{repreal}
1863
1864 This also allows for a better handling of numeric radicals, since
1865 @code{sqrt(2)} can now be carried along calculations.  Now it should be
1866 clear, why both classes @code{add} and @code{mul} are derived from the
1867 same abstract class: the data representation is the same, only the
1868 semantics differs.  In the class hierarchy, methods for polynomial
1869 expansion and the like are reimplemented for @code{add} and @code{mul},
1870 but the data structure is inherited from @code{expairseq}.
1871
1872
1873
1874 @node Bibliography, Concept Index, Internal representation of products and sums, Top
1875 @c    node-name, next, previous, up
1876 @appendix Bibliography
1877
1878 @itemize @minus{}
1879
1880 @item
1881 @cite{ISO/IEC 14882:1998: Programming Languages: C++}
1882
1883 @item
1884 @cite{CLN: A Class Library for Numbers}, @email{haible@@ilog.fr, Bruno Haible}
1885
1886 @item
1887 @cite{The C++ Programming Language}, Bjarne Stroustrup, 3rd Edition, ISBN 0-201-88954-4, Addison Wesley
1888
1889 @item
1890 @cite{C++ FAQs}, Marshall Cline, ISBN 0-201-58958-3, 1995, Addison Wesley
1891
1892 @item
1893 @cite{Algorithms for Computer Algebra}, Keith O. Geddes, Stephen R. Czapor,
1894 and George Labahn, ISBN 0-7923-9259-0, 1992, Kluwer Academic Publishers, Norwell, Massachusetts
1895
1896 @item
1897 @cite{Computer Algebra: Systems and Algorithms for Algebraic Computation},
1898 J.H. Davenport, Y. Siret, and E. Tournier, ISBN 0-12-204230-1, 1988, 
1899 Academic Press, London
1900
1901 @end itemize
1902
1903
1904 @node Concept Index, , Bibliography, Top
1905 @c    node-name, next, previous, up
1906 @unnumbered Concept Index
1907
1908 @printindex cp
1909
1910 @bye
1911