This shows you the differences between two versions of the page.
|
documentation:guide:applying_and_manipulating_transformations [2015/11/21 12:33] |
documentation:guide:applying_and_manipulating_transformations [2015/11/21 12:33] (current) |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Applying and manipulating transformations ====== | ||
| + | <html> | ||
| + | <div class="text-right" style="font-size: 15px; "> | ||
| + | </html> | ||
| + | Next topic: [[documentation:guide:substitutions]] | ||
| + | <html> | ||
| + | <span class="glyphicon glyphicon-arrow-right"></span> | ||
| + | </div> | ||
| + | </html> | ||
| + | |||
| + | ---- | ||
| + | |||
| + | All transformations in Redberry are first-class objects which means that they can be assigned to variables and share a common way for their applying and manipulation. Consider the syntax used for applying [[documentation:ref:transformation]] to mathematical expression: | ||
| + | <sxh groovy; gutter: false> | ||
| + | def tr = Expand | ||
| + | def t = '(A_k + B_k)*c'.t | ||
| + | def r = tr >> t, | ||
| + | l = t << tr | ||
| + | assert r == l | ||
| + | println r | ||
| + | </sxh> | ||
| + | <sxh plain; gutter: false> | ||
| + | > c*A_k+c*B_k | ||
| + | </sxh> | ||
| + | As it seen from the example, transformations are applied using left shift | ||
| + | ''%%<<%%'' or right shift ''%%>>%%'' operator. It should be noted | ||
| + | that both operators are left-associative so in order to apply several | ||
| + | transformations subsequently, it is better to use a special ''&'' | ||
| + | operator, which allows to join a set of transformations into a single one: | ||
| + | <sxh groovy; gutter: true> | ||
| + | def t = '(a+b)*c'.t | ||
| + | //first expand then substitute | ||
| + | def expandAndSubs = Expand & 'c = a + b'.t | ||
| + | println expandAndSubs >> t | ||
| + | </sxh> | ||
| + | <sxh plain; gutter: false> | ||
| + | > a*(a+b)+b*(a+b) | ||
| + | </sxh> | ||
| + | <sxh groovy; gutter: true;first-line: 5> | ||
| + | //first substitute then expand | ||
| + | def subsAndExpand= 'c = a + b'.t & Expand | ||
| + | println subsAndExpand >> t | ||
| + | </sxh> | ||
| + | <sxh plain; gutter: false> | ||
| + | > a**2+2*a*b+b**2 | ||
| + | </sxh> | ||
| + | |||
| + | Transformations (like tensors) are immutable in Redberry. Some transformations | ||
| + | may take required or optional arguments using square brackets: | ||
| + | <sxh groovy; gutter: true> | ||
| + | def eliminateWhileExpand = Expand[EliminateMetrics] | ||
| + | def x = '(g_mn + d_m^a*g_na)*f^mn'.t | ||
| + | println eliminateWhileExpand >> x | ||
| + | </sxh> | ||
| + | <sxh plain; gutter: false> | ||
| + | > 2*f_m^m | ||
| + | </sxh> | ||
| + | <sxh groovy; gutter: true;first-line: 4> | ||
| + | def diff = Differentiate['x_m'] | ||
| + | def y = 'x_m*x^m'.t | ||
| + | println diff >> y | ||
| + | </sxh> | ||
| + | <sxh plain; gutter: false> | ||
| + | > 2*x^m | ||
| + | </sxh> | ||
| + | In this example, the [[documentation:ref:Expand]] takes an optional parameter, which is a transformation to be applied on each level of expand procedure. In contrast, the argument of [[documentation:ref:Differentiate]] is required. In both cases a new object will be created and assigned to a corresponding variable. The meaning of arguments is specific for each particular [[documentation:ref:Transformation]]. | ||
| + | ====See also==== | ||
| + | * Related guides: [[documentation:guide:list_of_transformations]], [[documentation:guide:programming_with_redberry]] | ||
| + | * Related reference material: [[documentation:ref:Transformation]], [[documentation:ref:Tensor]] | ||