a field-theory motivated approach to computer algebra

Using Cadabra directly from C++

It is possible to use the functionality of Cadabra directly from C++ code, without dealing with the Python layer on top of it, and without using the notebook interface. The library is called cadabra2++ and building and installation instructions are provided in the project's README. Here we describe how to use this library.

Simple example

Basic use of the Cadabra C++ library consists of five parts: creating a kernel, inserting object properties into the kernel, defining expressions, acting with algorithms on those expressions, and displaying the result. A minimal example is as follows:
#include "cadabra2++.hh" #include using namespace cadabra; using namespace cadabra::cpplib; int main() { Kernel k(true); inject_property(k, "{A,B}"); auto ex = "A B - B A"_ex(k); sort_product sp(k, *ex); sp.apply_generic(); collect_terms(k, *ex); sp.apply_generic(); std::cout << pprint(k, ex) << std::endl; }
The output of this program is 2 A B. Most of the above should be fairly easy to understand for anyone who has worked with the Python interface to Cadabra before. Note how properties are attached to objects using the inject_property function call. This takes the kernel as argument, as well as a textual expression of what you would use in the Python interface. Algorithms are C++ objects, which you need to instantiate, and then run explicitly by calling the apply_generic function. As in the Python version, algorithms act in-place (mostly), and the ex expression above thus changes as the code progresses. Finally, the expression is printed by using the `pprint` function. This is necessary because printing requires information stored in the Cadabra kernel.
Copyright © 2001-2022 Kasper Peeters