# Differentiate

### Description

• Differentiate[var1, var2] differentiates expression successively with respect to var1, var2
• Differentiate can differentiate tensorial expressions with respect to tensorial variables. Differentiate takes care about dummies relabelling and symmetries of tensors. The following convention is adopted:$\frac{\partial T_{m_1 \dots m_k}}{\partial T_{n_1 \dots n_k}} = \delta^{n_1}_{m_1} \times \dots \times \delta^{n_k}_{m_k} \,+\, permutations$such that r.h.s. has exactly same symmetries as l.h.s.

### Examples

Derivative with respect to x:

println Differentiate['x'] >> 'x**n'.t

   > n*x**(n-1)


Derivative with respect to f_mn: $\frac{\partial }{\partial f_{mn}} \, \left(\, f_{ab} f_{cd} \, \right) = \delta_a^m \, \delta_b^n \, f_{cd} \,+\, \delta_d^n \, \delta_c^m \, f_{ab}$

Redberry code:

println Differentiate['f_mn'] >> 'f_ab*f_cd'.t

   > d_a^m*d_b^n * f_cd + d_d^n*d_c^m*f_ab


Derivative involving a symbolic function f_mn[t_ab]: $\frac{\partial }{\partial t_{mn}} \, \left(\, t_{ab} f^{ab}(t_{pq}) \, \right) = f^{mn}(t_{dc}) \,+\, t_{ab} \frac{\partial }{\partial t_{mn}} \left( f^{ab}(t_{dc})\right)$

Redberry code:

println Differentiate['t_mn'] >> 't_ab*f^ab[t_pq]'.t

   > f^{mn}[t_{dc}] + t_{ab}*f~(1)^{abmn}[t_{dc}]


Derivative with respect to x_m and y_m: $\frac{\partial^4 }{\partial x_{m} \partial x^m \partial y_n \partial y^n} \, \left(\, (x_a y^a)^5 \, \right) = 240\, (y^b x_b)^3+40\, (y^b x_b)^3 \delta^a_a + 120\, (y_m y^m) (y^b x_b) (x_a x^a)$

Redberry code:

def diff = Differentiate['x_m', 'x^m', 'y_a', 'y^a'] & CollectScalars
println diff >> '(x_a*y^a)**5'.t

   > 240*(y^b*x_b)**3+40*(y^b*x_b)**3*d^a_a+120*y_m*y^m*y^b*x_b*x_a*x^a


Derivative with respect to antisymmetric tensor:

setAntiSymmetric 'R_pq'
println Differentiate['R_mn'] >> 'R_ab'.t

   > d_{a}^{m}*d_{b}^{n} - d_{a}^{n}*d_{b}^{m}


### Options

Differentiate[var1, var2, …, transformations] allows to pass additional transformations which will be applied after each step of differentiation (for performance reasons):

//setting up symmetries of Riemann tensor
addSymmetry 'R_abcd', -[1, 0, 2, 3].p
addSymmetry 'R_abcd', [2, 3, 0, 1].p

def tensor = 'R^acbd*Sin[R_abcd*R^abcd]'.t
def var1 = 'R^ma_m^b'.t,
var2 = 'R^mc_m^d'.t

def diff1, diff2
timing {
//take second derivative and then simplify
diff1 = (Differentiate[var2, var1] & ExpandAndEliminate) >> tensor
}

   > Time: 1338 ms.

timing {
//take second derivative and simplify permanently
diff2 = Differentiate[var2, var1, ExpandAndEliminate] >> tensor
}

   > Time: 14 ms.

assert diff1 == diff2