Hostname: page-component-6766d58669-bp2c4 Total loading time: 0 Render date: 2026-05-20T21:43:37.685Z Has data issue: false hasContentIssue false

Domain-specific tensor languages

Published online by Cambridge University Press:  12 March 2025

JEAN-PHILIPPE BERNARDY
Affiliation:
Chalmers University of Technology and University of Gothenburg, Gothenburg, Sweden (e-mail: jean-philippe.bernardy@gu.se)
PATRIK JANSSON
Affiliation:
Chalmers University of Technology and University of Gothenburg, Gothenburg, Sweden (e-mail: patrikj@chalmers.se)
Rights & Permissions [Opens in a new window]

Abstract

The tensor notation used in several areas of mathematics is a useful one, but it is not widely available to the functional programming community. In a practical sense, the (embedded) domain-specific languages (dsls) that are currently in use for tensor algebra are either 1. array-oriented languages that do not enforce or take advantage of tensor properties and algebraic structure or 2. follow the categorical structure of tensors but require the programmer to manipulate tensors in an unwieldy point-free notation. A deeper issue is that for tensor calculus, the dominant pedagogical paradigm assumes an audience which is either comfortable with notational liberties which programmers cannot afford, or focus on the applied mathematics of tensors, largely leaving their linguistic aspects (behaviour of variable binding, syntax and semantics, etc.) for the reader to figure out by themselves. This state of affairs is hardly surprising, because, as we highlight, several properties of standard tensor notation are somewhat exotic from the perspective of lambda calculi. We bridge the gap by defining a dsl, embedded in Haskell, whose syntax closely captures the index notation for tensors in wide use in the literature. The semantics of this edsl is defined in terms of the algebraic structures which define tensors in their full generality. This way, we believe that our edsl can be used both as a tool for scientific computing, but also as a vehicle to express and present the theory and applications of tensors.

Information

Type
Research Article
Creative Commons
Creative Common License - CCCreative Common License - BY
This is an Open Access article, distributed under the terms of the Creative Commons Attribution licence (https://creativecommons.org/licenses/by/4.0/), which permits unrestricted re-use, distribution and reproduction, provided the original article is properly cited.
Copyright
© The Author(s), 2025. Published by Cambridge University Press
Figure 0

Fig. 1: Tensor notations, edsls and relationships between them. Even though the index notation, the morphism notation and the string diagram notation are all equivalent mathematically, in our implementation Roger is coded as a (set of) type-classes, and the index and diagram notations are instances of it.

Figure 1

Fig. 2: Diagram, categorical and index notations for identity and composition.

Figure 2

Fig. 3: Diagram, categorical, and Einstein notations for morphisms of symmetric monoidal categories. They are in general polymorphic, but we display them here as acting on an atomic vector space T, or the simplest allowable combination thereof (see the last row in the figure for the monomorphic type of the respective morphisms). The morphisms $\bar{\alpha}$ and $\bar{\rho}$ are not shown, but are drawn symmetrically to $\alpha$ and $\rho$, respectively.

Figure 3

Fig. 4: Illustration of compact closed categories in various notations. Note that Einstein notation does not change when bending connections using $\eta$ or $\epsilon$, though in the third example, the new connection is notated by repeated use of the index.

Figure 4

Fig. 9: Syntax of the index language of Albert, as types of combinators.

Figure 5

Fig. 10: Syntax of the expression sub-language of Albert, as types of combinators. We repeat christoffel symbol and metric here even though they can be defined by the user as the embedding of the corresponding Roger primitives.

Figure 6

Fig. 5: Possible fields of bases for a local space field covering the Euclidean plane. In both examples, basis vectors are tangent to coordinate lines; either Cartesian or polar coordinates. In the second instance, the basis vectors are undefined at the origin.

Figure 7

Fig. 6: Tensor field primitives in various notations, and their types.

Figure 8

Fig. 7: Affinities $\mathsf{aff}\mskip 3.0mu\mathsf{a}$ at various types $\mathsf{a}$ (shown in the third row).

Figure 9

Fig. 8: Inheritance relationships between tensor structures. In the praxis, every instance of a category with a metric $\mathsf{Additive}$ is also additive. However, diagrams don’t support a good representation for addition, so the dotted line is implemented as a subclass relationship in our library.

Figure 10

Fig. 11: Final implementation

Submit a response

Discussions

No Discussions have been published for this article.