Differences

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

Link to this comparison view

documentation:guide:getting_started [2015/11/21 12:33]
documentation:guide:getting_started [2015/11/21 12:33] (current)
Line 1: Line 1:
 +====== Getting started ======
 +<​html>​
 +<div class="​text-right"​ style="​font-size:​ 15px; ">
 +</​html>​
 +Next topic: [[documentation:​guide:​inputting_and_printing_mathematical_expressions]]
 +<​html>​
 +<span class="​glyphicon glyphicon-arrow-right"></​span>​
 +</​div>​
 +</​html>​
 +
 +----
 +====Installing and running Redberry====
 +In order to install Redberry follow the instructions on the [[:​Installation|Installation page]]. The majority of code snippets presented on this site can be executed by wrapping them with the following Groovy code:
 +
 +<sxh groovy; gutter: false>
 +
 +import cc.redberry.core.context.*
 +import cc.redberry.core.indices.*
 +import cc.redberry.core.tensor.*
 +import cc.redberry.core.transformations.*
 +import cc.redberry.groovy.Redberry
 +
 +import static cc.redberry.core.tensor.Tensors.*
 +import static cc.redberry.core.indices.IndexType.*
 +import static cc.redberry.groovy.RedberryPhysics.*
 +import static cc.redberry.groovy.RedberryStatic.*
 + 
 +use(Redberry){
 +    //put your code here
 +}
 +</​sxh> ​
 +
 +However, some examples require additional ''​import''​ statements, in this case it will be specifically indicated in the description of the snippet.
 +
 +
 +If you are using Redberry through IntelliJ IDEA (as recommended on the [[:​Installation|Installation page]]), work with code will be sufficiently simplified by many useful features of the IDE like code completion, syntax highlighting and automatic importing. Just start typing some Redberry statement (or press additionally Ctrl + Space or Ctrl + Space + Space) and IntelliJ will automatically suggest a correct function name and insert ''​import''​ statement if necessary:
 +
 +<​html><​div align="​center"></​html>​
 +{{:​documentation:​guide:​screen_shot_2014-05-30_at_22.25.44.png?​700|}}
 +<​html></​div></​html>​
 +====Preliminaries====
 +When you are using Redberry, you are actually coding in a Groovy language, so the main language features are inherited from Groovy. ​ Besides, Redberry provides a lot of special syntax constructions that make a general Groovy syntax more convenient for the purposes of computer algebra.
 +
 +
 +Two basic things that one need to know before starting any calculations with Redberry are how to define expressions and how to apply transformations to them (substitutions,​ simplifications etc.). Let's start from a preliminary overview of how these things can be done in Redberry via Groovy syntax.
 +
 +===Expressions====
 +To input expression by hand and bind it to a variable one can do:
 +<sxh groovy; gutter: false> ​
 +def expr = 'a * F^{A}_{B \\mu \\nu}'​.t
 +println expr
 +</​sxh>​
 +<sxh plain; gutter: false>
 +  > a * F^{A}_{B \\mu \\nu}
 +</​sxh>​
 +Here '''​...'​.t''​ syntax is used to convert a string representation of expression into a computer object. ​
 +The [[documentation:​guide:​Inputting and printing mathematical expressions|input notation]] for tensors is nearly identical to $\LaTeX{}$ syntax for mathematical expressions with minor syntax modifications. Some additional constraints on expressions arise form [[documentation:​guide:​einstein_notation]].
 +
 +===Transformations===
 +Transformations can be applied by right shift ''​%%>>​%%''​ operator in the following way:
 +<sxh groovy; gutter: false> ​
 +def expr = '(x + y) * a'.t
 +println Expand >> expr
 +</​sxh>​
 +<sxh plain; gutter: false>
 +   > x * a + y * a
 +</​sxh>​
 +Here [[documentation:​ref:​Expand]] is a [[documentation:​ref:​transformation]] (for the list of all available transformations see [[documentation:​guide:​List of transformations]]). In order to apply several transformations sequentially one can use join operator ''&'':​
 +<sxh groovy; gutter: false> ​
 +def t = '(g_mn + h_mn) * (h^mn - g^nm)'​.t
 +t = (Expand & EliminateMetrics) >> t
 +println t
 +</​sxh>​
 +<sxh plain; gutter: false>
 +   > -d^{n}_{n} + h^{mn} * h_{mn}
 +</​sxh>​
 +
 +One can find more information about transformations on [[documentation:​guide:​applying_and_manipulating_transformations]] page.
 +
 +From the last example one can also see that Redberry uses a predefined notation for [[documentation:​ref:​metric tensor|metric tensor]] ''​g_{mn}''​ and for [[documentation:​ref:​Kronecker delta|Kronecker delta]] ''​d^m_n''​. Since in the majority of cases the dimension of the space affects only trace of Kronecker tensor (''​d^n_n''​),​ Redberry does not bind a particular dimension to each [[documentation:​ref:​IndexType|index type]] (thus one can use different dimensions for different index types).
 +
 +
 +===Substitutions===
 +One can define and apply [[documentation:​guide:​substitutions|substitution]] in Redberry in the following way:
 +<sxh groovy; gutter: false>
 +def subs = 'f_mn = t_mn + k_mn'​.t
 +def tensor = 'f_ab * (f^ab + f^ba)'​.t
 +println subs >> tensor
 +</​sxh>​
 +<sxh plain; gutter: false>
 +   > (t_ab + k_ab) * (t^ab + t^ba + k^ab + k^ba)
 +</​sxh>​
 +For further reading about substitutions in Redberry see [[documentation:​guide:​substitutions|Substitutions]] page.
 +====Introductory example====
 +
 +Let's consider a minimal real physical example in order to quickly dive into Redberry. The example demonstrates calculation of differential cross section of the Compton scattering in scalar massless Quantum Electrodynamics (QED) (the reader which is not familiar with this area can skip physical details and focus just on the algebraic operations):​
 +
 +<sxh groovy>
 +// photon-scalar-scalar vertex
 +def V1 = '​V_i[p_a,​ q_b] = -I*e*(p_i+q_i)'​.t
 +// photon-photon-scalar-scalar vertex
 +def V2 = '​V_{ij} = 2*I*e**2*g_{ij}'​.t
 +// scalar propagator for massless particle
 +def P = '​D[k_a] = -I/​(k^a*k_a)'​.t
 +// matrix element
 +def M = ('M^ij ='
 +        + '​V^i[k3_a,​k3_a+k1_a]*D[k3_a+k1_a]*V^j[-k4_a,​-k3_a-k1_a]'​
 +        + '​+V^j[k3_a,​k3_a-k2_a]*D[k3_a-k2_a]*V^i[-k3_a+k2_a,​-k4_a]'​
 +        + '​+V^ij'​).t
 +// substituting vertices and propagator in matrix element
 +M = (V1 & V2 & P) >> M
 +// squared matrix element
 +// minus here is due to complex conjugation
 +def M2 = M >> 'M2 = -M_ij*M^ij'​.t
 +// expand squared matrix element and
 +// eliminate metrics and Kronecker deltas
 +M2 = (ExpandAll & EliminateMetrics) >> M2
 +M2 = 'd_i^i = 4'.t >> M2 //​substitute dimension 4
 +
 +// massless particles
 +// k1_a*k1^a = 0, k2_a*k2^a = 0 etc.
 +for (def i in 1..4)
 +    M2 = "​k${i}_a*k${i}^a = 0".t >> M2
 +
 +// momentum conservation
 +M2 = ('​k1_a*k2^a = k3_a*k4^a'​.t
 +        & '​k1_a*k3^a = k2_a*k4^a'​.t
 +        & '​k1_a*k4^a = k2_a*k3^a'​.t) >> M2
 +
 +// factor terms
 +M2 = Factor >> M2
 +println M2
 +</​sxh>​
 +
 +This script will print a well-known expression for the squared matrix element of 
 +the Compton scattering in massless scalar QED:
 +\begin{equation*}
 + ​|\mathcal{M}|^2 ​ = -\frac{e^{4}}{2} \frac{-18 (k_2 k_3) (k_2 k_4)+(k_3 ​
 +k_4)^{2}+(k_2 k_3)^{2}+2( (k_2 k_3)- (k_2 k_4)) (k_3 k_4)+(k_2 k_4)^{2}}{(k_2 ​
 +k_3)(k_2 k_4)}, ​
 +\end{equation*}
 +where $(k\,p)$ denotes scalar product $k_i p^i$.
 +
 +
 +This example reproduces a typical basic steps needed to solve almost any problem: we define expressions,​ apply substitutions and other simplifications in order to obtain the result. The code above speaks for itself, so leaving aside the physical aspects of the problem, let's focus only on a few programming aspects. ​
 +
 +In the first few lines we defined [[documentation:​guide:​Substitutions|substitutions]] that will be applied to matrix element defined in line 8. Functions (tensorial functions that depend on a list of arguments) in Redberry can be defined using square brackets. So, line 2
 +<sxh groovy; gutter: false>
 +def V1 = '​V_i[p_a,​ q_b] = -I*e*(p_i+q_i)'​.t
 +</​sxh>​
 +defines a function of two tensorial arguments (built-in symbol ''​I''​ stands for the imaginary unit):
 +$$V_{i}(p_i,​ q_b) = -i e (p_i+q_i) $$ 
 +When applying such substitution to an expression, the arguments of function are matched automatically:​
 +<sxh groovy; gutter: false>
 +def V1 = '​V_i[p_a,​ q_b] = -I*e*(p_i+q_i)'​.t
 +println V1 >> '​V_a[k_i +p_i, p_i]*V^a[k_i-p_i,​k_i]'​.t
 +</​sxh>​
 +<sxh plain; gutter: false>
 +   > -e**2*(2*k^{a}-p^{a})*(k_{a}+2*p_{a})
 +</​sxh>​
 +
 +All transformations in Redberry (e.g. [[documentation:​guide:​Substitutions|substitutions]],​ [[documentation:​ref:​Expand]],​ [[documentation:​ref:​EliminateMetrics]] etc.) are first-class objects and can be assigned to a variable. They can be applied to mathematical expression using ''​%%>>​%%''​
 +operator. Detailed discussion on Redberry transformations usage and list of basic 
 +built-in transformations can be found on [[documentation:​guide:​Applying and manipulating transformations]] and [[documentation:​guide:​List of transformations]] pages.
 +
 +
 +The last point that should be discussed here is looping construct used in lines 24, 25.  Here [[documentation:​guide:​programming_with_redberry|standard Groovy syntax]] is used to apply several similar substitutions ($k_{1i} k_1{}^i = 0$, $k_{2i} k_2{}^i = 0$ and so on). So, the code 
 +<sxh groovy; gutter: false>
 +for (def i in 1..4)
 +    M2 = "​k${i}_a*k${i}^a = 0".t >> M2
 +</​sxh>​
 +is a shorthand for
 +<sxh groovy; gutter: false>
 +M2 = "​k1_a*k1^a = 0".t >> M2
 +M2 = "​k2_a*k2^a = 0".t >> M2
 +M2 = "​k3_a*k3^a = 0".t >> M2
 +M2 = "​k4_a*k4^a = 0".t >> M2
 +</​sxh>​
 +
 +====See also====
 +  * Related guides: [[documentation:​guide:​inputting_and_printing_mathematical_expressions]],​ [[documentation:​guide:​applying_and_manipulating_transformations]],​ [[documentation:​guide:​list_of_transformations]]