Differences

This shows you the differences between two versions of the page.

Link to this comparison view

documentation:guide:inputting_and_printing_mathematical_expressions [2015/11/21 12:33]
documentation:guide:inputting_and_printing_mathematical_expressions [2015/11/21 12:33] (current)
Line 1: Line 1:
 +====== Inputting and printing mathematical expressions ======
 +<​html>​
 +<div class="​text-right"​ style="​font-size:​ 15px; ">
 +</​html>​
 +Next topic: ​
 +[[documentation:​guide:​tensors_and_indices]]
 +<​html>​
 +<span class="​glyphicon glyphicon-arrow-right"></​span>​
 +</​div>​
 +</​html>​
 +
 +----
 +====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:
 +<sxh groovy; gutter: false>
 +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
 +</​sxh>​
 +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: ​
 +<sxh groovy; gutter: false>
 +
 +//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
 +</​sxh>​
 +
 +
 +====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:
 +<sxh groovy; gutter: false>
 +def expr = 'a * F^{A}_{A m n} + g_mn'​.t
 +println expr
 +</​sxh>​
 +<sxh plain; gutter: false>
 +  > a * F^{A}_{A m n} + g_{m n}
 +</​sxh>​
 +
 +One can specify other output format in order to export expression to other systems:
 +<sxh groovy; gutter: false>
 +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)
 +}
 +</​sxh>​
 +<sxh plain; gutter: false>
 +  > a * F[m, n, ~A, A] + g_[m, n]
 +</​sxh>​
 +All available output formats are listed in [[documentation:​ref:​OutputFormat]]. There are output formats for LaTeX, Mathematica,​ Maple, Cadabra etc. The following example illustrates their usage:
 +<sxh groovy; gutter: false>
 +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)
 +}
 +</​sxh>​
 +
 +One can change default output format in the following way:
 +<sxh groovy; gutter: false>
 +//set Cadabra output by default
 +CC.defaultOutputFormat = OutputFormat.Cadabra
 +</​sxh>​
 +
 +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:
 +<sxh groovy; gutter: false>
 +println t.toString(SimpleRedberry)
 +</​sxh>​
 +====See also====
 +  * Related guides: [[documentation:​guide:​einstein_notation]], ​ [[documentation:​guide:​setting_up_matrix_objects]]
 +  * Related reference: [[documentation:​ref:​OutputFormat]]