Inputting and printing mathematical expressions

Next topic: Tensors and Indices


Inputting expressions

The notation used for inputting mathematical expressions in Redberry is almost the same as in many other CASs. For inputting tensors, Redberry uses $\LaTeX$ notation with minor syntax modifications.

The following example of different input styles gives an idea of valid Redberry syntax for tensors:

def t
//here braces are necessary since indices separated by spaces
t = 'a * F^{A}_{B \\mu \\nu}'.t
//same, but without braces
t = 'a * F^A_B\\mu\\nu'.t
//here braces are necessary since indices contain subscripts
t = 'F^A_{B_{21}C\\mu\\nu}'.t
//tensor name in the LaTeX notation
t = '\\Gamma^A_{\\alpha\\beta}'.t
//complete LaTeX style
t = '\\Gamma{}^A{}_{\\alpha\\beta}'.t
As one can see, in many cases curly braces can be omitted when inputting the indices of tensor. Braces are necessary when one needs to separate indices by spaces, or input a subscripted index.

The notation for functions (both user defined and built-in scalar functions: sin, cos etc.) is the same as in Wolfram Mathematica:


//user defined tensor field
t = 'F^a_bcd[G^a_bc, p^a]'.t
//some Redberry built-in functions
t = 'Sin[m**2 - p_m*p^m] - Log[x/2]'.t
t = 'Power[a, b]'.t //same as 'a**b'.t

Printing expressions and exporting to other CASs

All Redberry objects have a default string representations. In order to output some expression to the console one can simply use println keyword in Groovy scripts. In addition to the default Redberry output format there are several other which allow to paste Rebderry output directly to $\TeX$ or other CASs.

By default, any Redberry output can be copied and pasted again into Redberry code:

def expr = 'a * F^{A}_{A m n} + g_mn'.t
println expr
  > a * F^{A}_{A m n} + g_{m n}

One can specify other output format in order to export expression to other systems:

import cc.redberry.groovy.Redberry
import cc.redberry.core.context.OutputFormat

use(Redberry){
    def expr = 'a * F^{A}_{A m n} + g_mn'.t
    println expr.toString(OutputFormat.Maple)
}
  > a * F[m, n, ~A, A] + g_[m, n]
All available output formats are listed in OutputFormat. There are output formats for LaTeX, Mathematica, Maple, Cadabra etc. The following example illustrates their usage:
import cc.redberry.groovy.Redberry
import static cc.redberry.core.context.OutputFormat.*

use(Redberry) {
    def t = 'F_mn^{\\alpha\\beta}/(a+b)'.t
    //LaTeX format:
    // \frac{1}{(a+b)} F_{mn}{}^{\alpha\beta}
    println t.toString(LaTeX)
    //Cadabra format:
    // (b+a)**(-1)*F_{m n \alpha \beta}
    println t.toString(Cadabra)
    //UTF8 format will print greek characters:
    // (a+b)**(-1)*F_{mn}^{αβ}
    println t.toString(UTF8)
    //WolframMathematica format:
    // Power[b+a, -1]*F[-m,-n,\[Alpha],\[Beta]]
    println t.toString(WolframMathematica)
    //Maple format:
    // (b+a)**(-1)*F[m,n,~alpha,~beta]
    println t.toString(Maple)
}

One can change default output format in the following way:

//set Cadabra output by default
CC.defaultOutputFormat = OutputFormat.Cadabra

If not changed Redberry uses its own output format (SimpleRedberry) as default. If default format is changed native format still can be used as follows:

println t.toString(SimpleRedberry)

See also