# Recursive application of Grad in scalars

I am looking for an efficient way to apply Grad recursively to a scalar function. The code I have so far is in the structure below. I define the scalar function outside the loop, take its derivatives and apply it to a list of points, then I take another Grad apply it again to the list of points and so on.

In this case, the function has 7 variables in total, but I am trying to write the structure so that it works for more or less variables as well(and derivatives are taken with respect to a subset only). The problem is that the function is very slow to calculate (complicated expression depending on special functions) and very heavy on memory.

However, I don't need to store all the components of tensors with rank>= 2 or calculate all the components of the tensors in the list of points, because the successive acting of grad will return symmetric tensors.

How could I tell Mathematica this: When taking the grad, take only of a subset of components, and when applying the function to the list of data it only has to calculate some components of the tensor?

Edit1:Simple function, just to illustrate:

F = x1^2 +...+ x^7
list = Reap@Do[
F = Function[{x1,...,x7},
];
Sow[
Table[ F@@Obspoints[[i]], { i,1, Length@Obspoints}]
],
{i,1,n}]//Last//Last

• "apply Grad recursively" - whenever you want to apply a function recursively, look at Nest (or NestList if you need the intermediate results as well). Additionally, please give us an example of a simple function and the output your would like to obtain. It would be easier than trying to reverse-engineer your code. Jan 18 at 14:28
• Both seem bad in terms of memory, they will just apply the gradient normally again and again in a straightforward manner. This is what I am trying to avoid, every time I take a gradient the size of the resulting expression increases by a factor of 10, roughly. I simplified the code in the question. Jan 18 at 14:56
• Felipe, once again, please include a complete example of your function, your list of points, etc, and the desired output. Jan 18 at 17:11
• You can use F = Function[{x1,x2,x3,x4,x5,x6,x7}, Exp[- ( x1^2 + x2^2 + x3^2 + x4^2 +x5^2 + x6^2 + x7^2)] ] and ObsPoints = RandomReal[{0,1}, {100, 7}]. Jan 18 at 17:33

In case someone else finds the same problem, there is at least one solution. If you define a gradient which has the following property(latex notation): $\partial_i T_{jkp} = V_{jkpi}$ and $V_{jkpi} = 0$ if i < p, then it is easy to see that $\partial_{i_n}...\partial_{i_1} V_{j} = T_{j i_1 ... i_n}$, where the only non-zero components of T are those for which $j <= i_1 <= i_2 ... <= i_n$. These can always be taken to be the independent components of a totally symmetric tensor. About how to implement this modified Grad I am not sure the way I did is the most efficient one. Assuming F[x1,...,x7]:
dummy = { {x1,...,x7}, {s1,x2,...,x7}, {s1,s2,x3,...,x7}, ..., {s1,...,s6,x7}}
`