I’m trying to use the `ad`

package to produce the gradient of an optimisation function, but the parameters of the function are dynamic (known at runtime) and featured throughout the function.

So far as I can tell, the function from `Numeric.AD`

that I should use is `grad`

. It allows me to do things like `grad (\[x,y,z]-> x*y*z)`

or even `grad (xs -> product xs)`

.

However, the function I’m interested in looks like this:

`(a_1*b_1 - v_1)^2 + (a2*b_1 - v2)^2 + (a_1*b2-v3)^2 + ...`

Note that the a/b variables are re-used throughout, and how the a’s and b’s are re-used is decided at runtime.

If I have to pass in the a’s and b’s into the `grad`

function as a list, it seems to me that I’d have to do a lot of O(N) indexing with `!!`

to produce the calculation I need.

Is there a more efficient way to approach this?

For example, if there was a way to create symbolic variables, build a function which uses them, and then calculate the gradient vector of those variables, that would work. In Python, what I typically do is use a matrix with dummy coding to indicate which variables are used in any given row – that would also work.