# Welcome to the Grim documentation

Grim is a symbolic language for representing mathematical formulas.

Source code: https://github.com/fredrik-johansson/fungrim

Grim is human-readable, computer-readable, and can be converted to LaTeX for rendering. It can be used both as a mathematical markup language and as a simple functional programming language for expressing mathematical objects. Grim is being developed as a means to represent mathematics in semantic form in Fungrim: the Mathematical Functions Grimoire. A second goal is to have a symbolic interface to Arb.

This project has the following components:

• A simple core language for symbolic expressions (similar to Lisp S-expressions and Wolfram language M-expressions).
• A vocabulary of hundreds of builtin symbols for mathematical objects and operations.
• A reference implementation in Python (Pygrim), which currently handles LaTeX conversion and symbolic or numerical evaluation of a subset of the functions.
• The 2600+ formulas added Fungrim so far which both serve as documentation and as a test suite.

Warning: Grim is currently alpha-level, and anything in this documentation may change. There are many known inconsistencies. Feedback is welcome on anything from mathematical foundations to syntax and naming.

## Quick formula examples

GrimGenerated LaTeXRendered formula
Implies(Element(n, ZZ), Equal(Sin(Mul(Pi, n)), 0)) \left(n \in \mathbb{Z}\right) \implies \left(\sin\!\left(\pi n\right) = 0\right) $$\left(n \in \mathbb{Z}\right) \implies \left(\sin\!\left(\pi n\right) = 0\right)$$
Equal(Exp(z), Sum(Div(Pow(z, n), Factorial(n)), For(n, 0, Infinity))) {e}^{z} = \sum_{n=0}^{\infty} \frac{{z}^{n}}{n !} $${e}^{z} = \sum_{n=0}^{\infty} \frac{{z}^{n}}{n !}$$
Equal(Det(Matrix(BellNumber(Add(i, j)), For(i, 0, n), For(j, 0, n))), Product(Factorial(k), For(k, 1, n)), BarnesG(Add(n, 2))) \operatorname{det}\displaystyle{\begin{pmatrix} B_{0 + 0} & B_{0 + 1} & \cdots & B_{0 + n} \\ B_{1 + 0} & B_{1 + 1} & \cdots & B_{1 + n} \\ \vdots & \vdots & \ddots & \vdots \\ B_{n + 0} & B_{n + 1} & \ldots & B_{n + n} \end{pmatrix}} = \prod_{k=1}^{n} k ! = G\!\left(n + 2\right) $$\operatorname{det}\displaystyle{\begin{pmatrix} B_{0 + 0} & B_{0 + 1} & \cdots & B_{0 + n} \\ B_{1 + 0} & B_{1 + 1} & \cdots & B_{1 + n} \\ \vdots & \vdots & \ddots & \vdots \\ B_{n + 0} & B_{n + 1} & \ldots & B_{n + n} \end{pmatrix}} = \prod_{k=1}^{n} k ! = G\!\left(n + 2\right)$$

In the table above, the Grim expressions are written in a rather verbose way using explicit function calls for arithmetic operators. Pygrim also supports infix Python syntax: x**2 + 3*y for Add(Pow(x, 2), Mul(3, y)).

## Symbolic evaluation examples

Input: Zeros(x**5 - x**4 - 4*x**3 + 4*x**2 + 2*x - 2, ForElement(x, CC), Greater(Re(x), 0))
$$\mathop{\operatorname{zeros}\,}\limits_{x \in \mathbb{C},\,\operatorname{Re}(x) > 0} \left[{x}^{5} - {x}^{4} - 4 {x}^{3} + 4 {x}^{2} + 2 x - 2\right]$$
Output: Set(Sqrt(Add(2, Sqrt(2))), 1, Sqrt(Sub(2, Sqrt(2))))   (evaluated by pygrim in 0.0304 s)
$$\left\{\sqrt{2 + \sqrt{2}}, 1, \sqrt{2 - \sqrt{2}}\right\}$$

By default, Grim expressions are inert (remain unevaluated). Calling the .eval() method in Pygrim evaluates an expression symbolically; that is, Pygrim produces an equivalent symbolic expression consisting of more explicit (and hopefully simpler) objects. For example, an implicit description of a finite set might be replaced by an explicit listing of the elements. This example demonstrates computing the set of roots of a polynomial which satisfy a given condition.

Input: Re(Gamma(Div(7,4)) * DedekindEta(5+4*ConstI))
$$\operatorname{Re}\!\left(\Gamma\!\left(\frac{7}{4}\right) \eta\!\left(5 + 4 i\right)\right)$$
Output: Div(Mul(3, Sub(Sqrt(3), 1), Pow(2, Div(3, 16)), Pow(Add(1, Sqrt(2)), Div(-1, 4)), Pow(Pi, Div(1, 4))), 32)   (evaluated by pygrim in 0.2204 s)
$$\frac{3 \left(\sqrt{3} - 1\right) {2}^{3 / 16} {\left(1 + \sqrt{2}\right)}^{-1 / 4} {\pi}^{1 / 4}}{32}$$

Pygrim can find symbolic closed form evaluations of various transcendental functions.

## Numerical evaluation examples

Input: RiemannZetaZero(10**6)
$$\rho_{1000000}$$
Output: Add(RealBall(Decimal("0.50000000000000000000"), 0), Mul(RealBall(Decimal("600269.67701244495552"), Decimal("1.24e-15")), ConstI))   (evaluated by pygrim in 0.0287 s)
$$\left[0.50000000000000000000 \pm 0\right] + \left[600269.67701244495552 \pm 1.24 \cdot 10^{-15}\right] i$$

Calling the .n() method on an expression in Pygrim produces an enclosure of the numerical value. This shows an example output.

Last updated: 2020-03-06 00:22:16