<<

GiNaC is Not a System

Chris Dams∗

September 1, 2006

[email protected] A bit of history

Around 1998 the XLOOPs project had problems with their use the CAS.

Maple was not very suitable to support large projects.

Major rewrites were necessary every new Maple version. Use a standardized language

Use C++ and implement algebraic capabilities within it. • No need to invent your own language. • A mainstream language has good tools available. • There is a standard library with good data structures. • Support for modern programming techniques. • Use it seamlessly with any other C/C++-library. • Operator overloading. GiNaCs classes

ex basic symbol expairseq constant exprseq pseries power numeric wildcard structure relational lst idx function ncmul add indexed mul abstract class class template tensor

varidx container class atomic class

fderivative A basic clifford tensdelta color is derived wraps ...... from B ex Example code: integrating delta functions

The Dirac-delta function is a “function” that has the properties • δ(x) is zero for x 6= 0; • δ(x − x ) dx = 1; Z 0 • think of it as the mass density of a point particle.

For an integral containing a delta function we have

f(x)δ(x − x ) dx = f(x ). Z 0 0 More generally f(x ) f(x)δ(g(x)) dx = i . ′ Z X |g (xi)| g(xi)=0 Main program int main() { symbol x("x"); ex e = integ(x,1)*delta(2*x-1)*exp(x); cout << "The expression " << e << endl; cout << "can be simplified into " << trydeltaintegrals(e) << endl; return 0; }

The output is: The expression delta(-1+2*x)*exp(x)*integ(x,1) can be simplified into 1/2*exp(1/2) The function that makes it possible

DECLARE_FUNCTION_2P(integ); REGISTER_FUNCTION(integ, dummy());

DECLARE_FUNCTION_1P(delta); REGISTER_FUNCTION(delta, dummy()); ex trydeltaintegrals(const ex &x) { if (is_a(x)) ... code for handling multiplications ... if (is_a(x)) return x.map(trydeltaintegrals); return x; } The code for handling multiplications

for (int i=0; i

ex delta_arg = x.op(j).op(0); if (!delta_arg.is_polynomial(integ_var)) continue; if (delta_arg.degree(integ_var) != 1) continue; ex a = delta_arg.coeff(integ_var, 1); ex b = delta_arg.coeff(integ_var, 0); ex result = x/x.op(i)/x.op(j); result = result.subs( integ_var == -b/a ); result = result * power(abs(a), -x.op(i).op(1)); result = trydeltaintegrals(result); return result; Features • Arbitrary precision arithmetic for integers, fractions and reals; • data structures: lst, exvector, exmap, exhashmap; • linear algebra: Linear systems, matrix operations; • algebraic substitution:

apbqarbs → ap+rbq+s by using the method .subs( power(wild(0),wild(1)) * power(wild(0),wild(2)) == power(wild(0), wild(1)+wild(2)), subs_options::algebraic ); • polynomial arithmetic: expanding, collecting, division, GCD, LCM, resultant, square-free decomposition; • analysis: Symbolic differentiation/Series expansion; • HEF: Indexed objects, clifford algebra, lie algebras of SU(2) and SU(3); • automatic dummy index renaming: 2 f .subs(f == x_i*y_i) → xiyixjyj; • generic clifford algebras; • taking real and imaginary parts of expressions; • numeric integration; • series expansion of symbolic integrals. Fundamental concepts • Reference counting for memory management; • most copies are shallow; • automatic evaluation: – x + x → 2x; – ex::ex(const basic &other) for conversion; – calls virtual basic::eval() methods; – only operations that are of complexity N log(N)p; ptr ex::construct_from_basic(const basic & other) { if (!(other.flags & status_flags::evaluated)) { const ex & tmpex = other.eval(1); if ((other.get_refcount() == 0) && (other.flags & status_flags::dynallocated)) delete &other; return tmpex.bp; } else { if (other.flags & status_flags::dynallocated) { return ptr(const_cast(other)); } else { basic *bp = other.duplicate(); bp->setflag(status_flags::dynallocated); return bp; } } } Fundamental Concept (continued) • built-in methods use virtual functions (extendable!); • map functions & type switch; • hashing (Fibonacci hash) for fast comparison. Projects using GiNaC • Feelfem (finite element method (FEM) code generator); • gTybalt (combines GiNaC with TeXmacs and Root into an interactive ); • Nested sums (supports for some kind of transcendental functions); • PURRS (C++ library for solving recurrence relations); • PyGiNaC (Python interface to GiNaC); • Swiginac (Python interface to GiNaC); • Antipodes (computes divergent terms arising in HEF-computations); • Ecco (simulation of (electro)chemical reaction kinetics); • ORSA (Orbit Reconstruction, Simulation and Analysis); • SyFi (finite element method). Get it from http://www.ginac.de

Also included in most Linux distributions.

Authors/contributors • Founding fathers: Christian Bauer, Alexander Frink, Richard Kreckel; • contributers: Roberto Bagnara, Jonathan Brandmeyer, Matti Peltom¨aki, Do Hoang Son, Bernard Parisse, Pearu Peterson, Ben Sapp, Stefan Weinzierl, Oliver Welzel • active developers: Christian Bauer, Chris Dams, Vladimir Kisil, Richard Kreckel, Alexei Sheplyakov, Jens Vollinga.