# Applying and manipulating transformations

Next topic: Substitutions

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 Transformation to mathematical expression:

def tr = Expand
def t = '(A_k + B_k)*c'.t
def r = tr >> t,
l = t << tr
assert r == l
println r

   > c*A_k+c*B_k

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:
def t = '(a+b)*c'.t
//first expand then substitute
def expandAndSubs = Expand & 'c = a + b'.t
println expandAndSubs >> t

   > a*(a+b)+b*(a+b)

//first substitute then expand
def subsAndExpand= 'c = a + b'.t & Expand
println subsAndExpand >> t

   > a**2+2*a*b+b**2


Transformations (like tensors) are immutable in Redberry. Some transformations may take required or optional arguments using square brackets:

def eliminateWhileExpand = Expand[EliminateMetrics]
def x = '(g_mn + d_m^a*g_na)*f^mn'.t
println eliminateWhileExpand >> x

   > 2*f_m^m

def diff = Differentiate['x_m']
def y = 'x_m*x^m'.t
println diff >> y

   > 2*x^m

In this example, the Expand takes an optional parameter, which is a transformation to be applied on each level of expand procedure. In contrast, the argument of 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 Transformation.