New Features in the Second Version of the Cadabra Computer Algebra System
D. S. Kulyabov, A. V. Korolkova, and L. A. Sevastianov

TL;DR
This paper discusses the major updates in the second version of Cadabra, a computer algebra system tailored for tensor computations, highlighting new features and architectural changes that enhance its capabilities.
Contribution
It introduces the revolutionary implementation of component computations and a new software mechanism in Cadabra v2, improving tensor operation support.
Findings
Implementation of component computations
Change in software architecture
Enhanced tensor operation capabilities
Abstract
In certain scientific domains, there is a need for tensor operations. To facilitate tensor computations,computer algebra systems are employed. In our research, we have been using Cadabra as the main computer algebra system for several years. Recently, an operable second version of this software was released. In this version, a number of improvements were made that can be regarded as revolutionary ones. The most significant improvements are the implementation of component computations and the change in the ideology of the Cadabra's software mechanism as compared to the first version. This paper provides a brief overview of the key improvements in the Cadabra system.
Peer Reviews
No public reviews on file for this paper yet. If you reviewed it on a platform where reviews are public (OpenReview, ICLR, NeurIPS, ICML), you can paste yours below so the community can read it here.
Videos
No videos yet. Explain this paper in a talk, walkthrough, or lecture? Add one.
New Features in the Second Version of the Cadabra Computer Algebra System
D. S. Kulyabov
Department of Applied Probability and Informatics,
Peoples’ Friendship University of Russia (RUDN University),
6 Miklukho-Maklaya St, Moscow, 117198, Russian Federation
Laboratory of Information Technologies
Joint Institute for Nuclear Research
6 Joliot-Curie, Dubna, Moscow region, 141980, Russia
A. V. Korolkova
Department of Applied Probability and Informatics,
Peoples’ Friendship University of Russia (RUDN University),
6 Miklukho-Maklaya St, Moscow, 117198, Russian Federation
L. A. Sevastianov
Department of Applied Probability and Informatics,
Peoples’ Friendship University of Russia (RUDN University),
6 Miklukho-Maklaya St, Moscow, 117198, Russian Federation
Bogoliubov Laboratory of Theoretical Physics
Joint Institute for Nuclear Research
6 Joliot-Curie, Dubna, Moscow region, 141980, Russia
Abstract
In certain scientific domains, there is a need for tensor operations. To facilitate tensor computations, computer algebra systems are employed. In our research, we have been using Cadabra as the main computer algebra system for several years. Recently, an operable second version of this software was released. In this version, a number of improvements were made that can be regarded as revolutionary ones. The most significant improvements are the implementation of component computations and the change in the ideology of the Cadabra’s software mechanism as compared to the first version. This paper provides a brief overview of the key improvements in the Cadabra system.
computer algebra systems, tensor calculations, component calculations, python, sympy
I Introduction
Computer algebra systems that support tensor calculus can be conventionally divided into three groups maccallum:2018:computer-algebra-gravity . The first group includes universal tensor calculus systems designed mainly for use in the general theory of relativity. In these systems, focus is placed on computing characteristic quantities of Riemannian geometry (Christoffel symbols, Riemann tensor) ilyin:1996:atensor ; gomez-lobo:2012:spinors-xact ; maccallum:2002:ca-gr . The second group includes computer algebra systems for tensor computations in quantum field theory bolotin:2013:redberry ; poslavsky:2015:redberry ; fliegner:1999:form ; heck:2000:formbook ; tung:2005:form . They focus on computations with Dirac spinors and simple symmetries. The third group of computer algebra systems for tensor calculus is designed mainly as a framework for arbitrary tensor computations. These systems have fewer default templates but provide more expressive means for designing new objects. It is to this group that Cadabra belongs. This paper is devoted to the specific features of this computer algebra system.
Cadabra is currently available in two versions. The first version (hereinafter, Cadabra 1.x) significantly differs from the other computer algebra systems for tensor calculus, particularly, in
- •
use of the TEX notation;
- •
convenient definition of tensors of arbitrary types;
- •
use of Young tableau to describe the symmetry properties of tensors.
However, Cadabra 1.x has significant disadvantages. In particular, this version does not support component computations. Moreover, the monolithic and rigid software structure of the system held no promise for the implementation of component computations in the near future. However, a revolutionary step taken in the second version of the system (hereinafter, Cadabra 2.x), namely, the use of the Python ecosystem, made it possible to solve these problems. In our opinion, it is the combination of component computations and Python ecosystem that constitutes a revolutionary improvement of the Cadabra system.
Unfortunately, the documentation on Cadabra leaves much to be desired. The website of the system (https://cadabra.science) provides only a few examples. More specific information can be found in research papers that may contain possible applications of Cadabra peeters:2007:cadabra ; peeters:2007:cpc:cadabra ; brewin:2010:cpc:tensor-cadabra ; kulyabov:2009:nucleilett:cadabra ; kulyabov:2013:springer:cadabra ; kulyabov:2016:pcs .
This paper provides a brief overview of the new features in Cadabra 2.x. The paper is organized as follows. Section II discusses the implementation of Cadabra 1.x and 2.x. Section III describes the process of action variation for a source-free electromagnetic field to illustrate the syntax of Cadabra 2.x. To compare the 1.x and 2.x syntaxes, see kulyabov:2009:nucleilett:cadabra ; kulyabov:2013:springer:cadabra ; kulyabov:2016:pcs . Section IV considers one of the most important features in Cadabra 2.x: its transparent interaction with the universal scalar computer algebra system SymPy. Section V describes the key (in our opinion) innovation in Cadabra 2.x—component computations—by the example of finding basic quantities for the general theory of relativity, namely, Christoffel connection and different curvatures. Finally, Section VI illustrates work with graphics in Cadabra 2.x.
When implementing the examples, we used code fragment from the Cadabra 2.x documentation (https://cadabra.science/tutorials.html).
II Implementation Features of Cadabra 1.x and 2.x
Each computer algebra system has its own implementation features. There are several levels of implementation:
- •
notation;
- •
manipulation language;
- •
implementation language;
- •
extension language.
Not every computer algebra system has all these levels. For instance, in most systems, the notation is based on the manipulation language.
In Cadabra 2.x (as in 1.x), the notation is based on the TeX notation (more precisely, TeX-like notation). In this case, a certain subset of TeX symbols (letters of different alphabets, symbols for integral and derivatives, etc.) is used. Above all, indices are denoted by the symbols _ and ^, as in the TeX system.
The manipulation language is used to work in the system. The syntax of Cadabra 1.x is quite simple and more oriented to code parsing rather than to userfriendliness. This approach to language design was common in the early years of computing technology development (e.g., Shell and Perl languages). Cadabra 2.x takes a qualitative step forward by switching to Python. As a result, all operations in Cadabra 2.x are written in a Python-like syntax.
Cadabra 1.x is implemented in C++ with the use of the LiE computer algebra system leeuwen:1992:lie (currently, the compilation of the LiE system causes certain difficulties). Its main purpose is processing Lie groups, on which operations with tensor symmetries are based. It should be noted that the entire system was actually implemented by one person. This required great efforts. However, the presence of only one author and the monolithic software structure of the system caused some concern.
In Cadabra 2.x, the author fundamentally changed his approach to the structure of the system by integrating it with the Python ecosystem. The 2.x system is still written in C++, but Python is used as a glue language (and also as a manipulation language). In addition, Python can be employed for writing extensions.
The Python infrastructure opens access to a large number of scientific libraries, including the SciPy project oliphant:2007:scipy . This allows the SciPy libraries to be used seamlessly, transparently to the user. In our opinion, this allowed Cadabra 2.x to take a revolutionary step forward.
III Elements of the 2.x Syntax
Let us recall some elements of the 1.x and 2.x syntaxes. As an example, we obtain the source-free Maxwell’s equation ll:2::en :
[TABLE]
by varying the action
[TABLE]
In this case, the Maxwell tensor is expressed in terms of the vector potential:
[TABLE]
Again, both versions of Cadabra use the TeX notation. In Cadabra 2.x, it becomes possible to use functionality of Python, in particular, define functions directly in the text of a program written in Cadabra 2.x. Note that, just like Cadabra 1.x, the 2.x version does not process resulting expressions by default (except for collecting like terms); this processing is left to the user. If it is required to apply a certain set of rules to all expressions used, then the function post_process can be called, which is executed after each operation (in fact, post_process is simply a Python function):
sort_product(ex)
canonicalise(ex)
collect_terms(ex)
If necessary, this function can be made empty, thus disabling any processing of expressions. The interface of Cadabra 2.x exploits the ideology of a notepad, i.e., in addition to writing program code, the user can also add comments. However, in contrast to iPython perez:2007:ipython , text is written in the LaTeX syntax rather than in the Markdown syntax rfc:7763 .
The object in Cadabra 2.x can be assigned a property, which, in turn, has a set of its own settings. Since we are dealing with tensors, the most useful property is Indices. The option position=free allows the system to raise and lower indices:
x::Coordinate.
\partial{#}::Derivative.
Here, # is a wildcard. The dot at the end of the expression suppresses the output (as is common in computer algebra systems).
To work with abstract indices, it is necessary to take into account the symmetry properties of tensors. In addition, when differentiating and integrating, the coordinate dependence of objects needs to be taken into account:
F_{\mu\nu}::Depends(x).
A_{\mu}::Depends(x,\partial{#}).
*\delta{#}::*Accent;
In this case, the variation sign is regarded as a modifier (rather than as an object with the Derivative property) and does not introduce any additional computational semantics. In our example, is a Maxwell tensor. We express it in terms of the vector potential (3):
Here, the sign defines a row label (in our case, it is ). The label denominates the expression for convenience of referring to it.
Next, the action for the electromagnetic field (2) is defined:
By substitution, the action is expressed in terms of the vector potential :
Then, the action has the following form:
The value of the action in this expression differs from the initial one. It takes the form that it received after the last computation. This is a bit unusual. The point is that most computer algebra systems are implemented using functional languages, or they follow a functional paradigm in which variables have the property of immutability. In this case, the label acts as a variable in imperative languages (Python is an imperative language). This makes work in Cadabra 2.x necessary linear: the user cannot randomly navigate through the notebook and perform computations at arbitrary points.
Let us vary the action:
Here, the expression itself, rather than its label, is used. For this purpose, the expression is put between two dollar symbols ($), as in the standard TeX.
Next, we expand the products and collect the like terms:
Then, integration by parts is carried out:
In this case, integration by parts is quite a formal action that uses only the Derivative property of the object.
Once again, we perform the substitution, expand the products, and collect the like terms:
Here, the label _ denotes the previous expression.
As a result, we obtain the desired Maxwell equation (1):
[TABLE]
This example demonstrates that the syntax of the manipulation language in Cadabra 2.x is based on the Python syntax, which is more customary than the syntax of the 1.x language.
IV Interaction between Cadabra and SymPy
Computer algebra systems for tensor calculus support quite a small number of operations. They are sufficient for basic manipulations with tensors in the formalism of abstract indices, as well as the index-free formalism. However, in many cases (e.g., full-fledged implementation of component computations), the support of scalar operations is required. If a computer algebra system for tensor calculus is implemented in the framework of a universal computer algebra system, then no problems arise. However, Cadabra is an independent system. Cadabra 1.x implements a mechanism (though inconvenient) for communication with the Maxima universal computer algebra system; however, it seems that this mechanism was implemented only as a proof of concept.
In Cadabra 2.x, communication with the universal computer algebra system is implemented via SymPy lamy:sympy_starter . Moreover, this communication is seamless: the work of the mechanism is invisible for the user, which is owing to implementation of Cadabra 2.x in Python.
Let us illustrate the use of SymPy in Cadabra 2.x by computing the integral
[TABLE]
The main function for the explicit call of SymPy is map_sympy(). This function has a side effect: it changes the value of the argument. However, as noted above, the absence of immutability is a feature of Cadabra 2.x. Let us consider the simplest call of this function:
To confirm the presence of this side effect, we check the current value of the expression ex:
It can be seen that the value of ex has changed.
We can transfer the value of the expression to SymPy and, moreover, call a particular function to process it. For instance, in this case, we can call the SymPy’s function integrate:
The second argument of the function map_sympy() is SymPy function name:
Again, this confirms the side effect of map_sympy().
In Python, the same action can be performed in several ways. Hence, this can be done in Cadabra 2.x. For illustration purposes, let us consider the following variants.
The class method sympy() can be used as follows:
While regarding the label ex as an object, we call the method sympy():
Check the state of the environment:
It is seen that the state of the environment has not changed, i.e., the method sympy() has no side effect.
In addition, we can use the function sympy with a method corresponding to a callee function of the SymPy environment:
Let us call the function sympy with the method integrate:
Note that this function always requires specifying a particular method, which is why it cannot be used in the previous case.
Again, check the state of the environment:
It can be seen that the function sympy does not have the side effect. Based on the examples considered above, we can conclude that the interaction with SymPy in Cadabra 2.x is implemented in quite an elegant way. In our opinion, the main advantage of this operation is its deep integration with the system, e.g., for implementation of component computations (see Section V).
V Component Computations in Cadabra 2.x
To illustrate component operations, we find curvature on a sphere of radius :
[TABLE]
For this purpose, we evaluate Christoffel symbols , Riemann tensor , and Ricci tensor mtw:1::en; ll:2::en . Let us define coordinates and labels for the indices while specifying what values these labels can take:
{\alpha, \beta, \gamma, \delta, \rho, \sigma, \mu, \nu,
lambda}::Indices(values={\varphi, \theta}, position=fixed);
\partial{#}::PartialDerivative;
Next, we define a tensor with the Metric property and, similarly, define the inverse metric:
g^{\alpha\beta}::InverseMetric.
In this case, it is sufficient to specify the components for the original metric. The components for the inverse metric are computed using the function complete:
complete(g, );
Let us define the Christoffel symbols :
We expand the Christoffel symbols by using the metric tensor. In this case, expansion is applied only to the right-hand side of the definition (after the equal sign). This order of the expansion is determined by the option rhsonly=True. The function evaluate() implicitly calls SymPy for operations on components (this is another advantage of using the Python infrastructure). To expand trigonometric relationships, SymPy should be called explicitly:
map_sympy(Gamma, "expand_trig");
Similarly, for the Riemann tensor , we write its definition and evaluate its components:
substitute(R4, Gamma)
evaluate(R4, g, rhsonly=True);
The Ricci tensor is computed from the Riemann tensor:
substitute(R2, R4)
evaluate(R2, g, rhsonly=True);
Finally, we compute the scalar curvature :
substitute(R, R2)
evaluate(R, g, rhsonly=True);
Thus, from the user’s perspective, component computations in Cadabra 2.x are represented by the additional property Coordinate and several functions with the main one being evaluate() (it is this function that computes the components).
At the system level, component computations are implemented in Cadabra 2.x through interaction with a scalar computer algebra system, namely, with SymPy.
VI Use of Graphics in Cadabra 2.x
The need for graphics in tensor-oriented computer algebra systems is questionable. In our opinion, it is simply not needed. However, in this case, graphics capabilities is nothing more than an additional (side) effect of implementing Cadabra 2.x in the framework of the Python ecosystem. That is why graph plotting in Cadabra 2.x is the same as in Python.
First, we need to choose a plotting library. The following example uses the popular Matplotlib library tosi:2009:matplotlib ; vaingast:2009:matplotlib ; muller:2016:python-ml . For numerical computations, the NumPy idris:numpy_cookbook ; oliphant:guide_numpy library is used.
Then, we import the modules for matplotlib and numpy:
import numpy as np
Let us construct a vector field; i.e., to each point in a space (in our case, on a plane), we assign a vector that originates from this point. Suppose that the vector has the form
[TABLE]
Let us define a square grid on which the vector field is evaluated. Since grid pitch along the axes is the same, we use only the values for :
u = np.sin(x)*np.cos(x)
v = np.cos(x)
uu, vv = np.meshgrid(u,v)
The function meshgrid from the numpy package generates a rectangular grid based on two arrays (in our case, and ).
Now, we construct the vector field by using the function streamplot from the matplotlib package:
plt.streamplot(x, x, uu, vv, color=’black’)
plt.title(’Vector field’)
plt.xlabel(’x’)
plt.ylabel(’y’)
The resulting plot can be both saved and displayed:
The image of the vector field is shown in Fig. 1.
We hope that, in the future, Cadabra will include more useful applications than the one illustrated by this simple example.
VII Conclusion
We can make the following conclusions. From the user’s perspective, the main breakthrough of Cadabra 2.x is the implementation of component computations, which allows the system to cover the whole range of necessary tensor operations. From the developer’s perspective, the main innovation is rewriting the system by using the Python language and its entire ecosystem. We hope that this will increase interest in Cadabra 2.x when solving problems that involve tensor operations.
Acknowledgements.
The publication has been prepared with the support of the ‘‘RUDN University Program 5-100’’ and funded by Russian Foundation for Basic Research (RFBR) according to the research project No 16-07-00556, 18-07-00567, 18-51-18005.
The reference list from the paper itself. Each links out to its DOI / PubMed record.
- 1(1) M. A. H. Mac Callum, Computer algebra in gravity research, Living Reviews in Relativity 21 (1) (2018) 1–93. doi:10.1007/s 41114-018-0015-6 . · doi ↗
- 2(2) V. Ilyin, A. Kryukov, ATENSOR — REDUCE program for tensor simplification, Computer Physics Communications 96 (1) (1996) 36–52. doi:10.1016/0010-4655(96)00060-4 . · doi ↗
- 3(3) A. G. P. Gómez-Lobo, J. M. Martín-García, Spinors: A Mathematica package for doing spinor calculus in General Relativity, Computer Physics Communications 183 (10) (2012) 2214–2225. ar Xiv:1110.2662 , doi:10.1016/j.cpc.2012.04.024 . · doi ↗
- 4(4) M. Mac Callum, Computer Algebra in General Relativity, International Journal of Modern Physics A 17 (20) (2002) 2707–2710. doi:10.1142/S 0217751 X 02011643 . · doi ↗
- 5(5) D. A. Bolotin, S. V. Poslavsky, Introduction to Redberry: the Computer Algebra System Designed for Tensor Manipulation (2015) 1–27 ar Xiv:1302.1219 .
- 6(6) S. Poslavsky, D. Bolotin, Redberry: a computer algebra system designed for tensor manipulation, Journal of Physics: Conference Series 608 (1) (2015) 012060. ar Xiv:1302.1219 , doi:10.1088/1742-6596/608/1/012060 . · doi ↗
- 7(7) D. Fliegner, A. Retey, J. a. M. Vermaseren, Parallelizing the Symbolic Manipulation Program FORM Part I: Workstation Clusters and Message Passing (2000). ar Xiv:0007221 .
- 8(8) A. Heck, FORM for Pedestrians (2000).
