I recommend using Indexed
in this case, as it allows to work with both variables and explicit List
s, and it works flawlessly with D
/Derivative
and other functions. Example:
(* utility to create a list of indexed variables *)
indexed[x_, to_Integer] := Table[Indexed[x, i], {i, to}]
indexed[x_, from_Integer, to_Integer] := Table[Indexed[x, i], {i, from, to}]
(* a couple functions defined with this convention *)
f[x_] := With[{xi = indexed[x, 5]}, xi.xi]
g[x_] := With[{x1 = Indexed[x, 1], x2 = Indexed[x, 2]}, x1 + x2]
(* all the following examples work as expected *)
D[f[x], Indexed[x, 3]]
Derivative[{0, 0, 1, 0, 0}][f][indexed[x, 5]]
Reduce[f[x] < 1]
Reduce[f[{x1, 0, x3, 0, x5}] < 1]
f[{1, 2, 3, 4, 5}]
Minimize[{g[x], f[x] < 1}, x \[Element] Ball[{1, 0, 0, 0, 0}]]
The code
Derivative[{0, 0, 1, 0, 0}][f][x]
requires special treatment, because Derivative
insists in using Part
instead of Indexed
. A possible workaround is
(Derivative[{0, 0, 1, 0, 0}][f] /. Part -> Indexed)[x]
The only thing I regret is that Indexed
cannot be inserted in a notebook with two-dimensional layout the same way as Subscript
can (using Ctrl + -
or Ctrl + _
). Unfortunately one has to type Indexed
explicitly. Other than that, it's perfect.
Derivative
work. For a while I would define two paradigms:f[v_List] := f @@ v; f[x_, y_, z_] := {x y, x^2 + z^2, 2 - y z}
or whatever. This is not exactly what you're asking about, but I would advise not to definef
as a function on a vector/List
if you want to differentiate it. $\endgroup$f = Norm[#] < 1 &
is nearly equivalent (is if all numbers are always real). $\endgroup$DTranspose = {3, 1, 4, 2}
. Then I can doA = Array[a, {2, 2}]; X = Array[x, {2, 2}]; B = Array[b, {2, 2}]; Transpose[D[A.X + X.B, {X}], DTranspose] // MatrixForm
. $\endgroup$