# Differentiating with respect to vectors of unspecified length

Given a function defined by

f[x_,y_] := Sum[ x[[i]] (g[y[[i]]] - g[Total[y]] - Log[x[[i]]]), {i, 1, Length[x]}]


I can easily calculate the derivative for a fixed n by

D[f[{x1, x2, x3}, {y1, y2, y3}], {{x1, x2, x3}}]


which yields

{-g(y1 + y2 + y3) + g(y1) - log(x1) - 1,
-g(y1 + y2 + y3) + g(y2) - log(x2) - 1,
-g(y1 + y2 + y3) + g(y3) - log(x3) - 1}


I want to differentiate w.r.t. x in the general case, by something like this:

D[f[x, y], x]


yielding something like this:

{-g(Total[y]) + g(y[[i]]) - Log[x[[i]]], {i, 1, Length[x]}}


Is this possible?

Ideally it chains with Solve as well so that I can write something like:

Solve[D[f[x, y], x] == Table[0, {Length[x]}], x]


and get something like

{{x[[i]] -> E^(-g(Total[y]) + g(y[[i]]) - 1), {i, 1, Length[x]}}

-

f[x_,y_] := Sum[ x[[i]] (g[y[[i]]] - g[Total[y]] - Log[x[[i]]]), {i, 1, Length[x]}]


you can specify x and y as (say) 5-vectors using the construct Array[x,5] and Array[y,5] where you can replace 5 by any number. Then your function is explicitly:

f[ Array[x,5], Array[y,5] ]


and you can take the desired derivative with respect to the vector Array[x,5] using:

D[f[Array[x, 5], Array[y, 5]], {Array[x, 5]}]


You can then gather the terms together as you wish. So, it's not quite a general n but it works easily and simply for any fixed n.

-