# Minimise a function taking a vector as an input directly

f1[x_] := x*2 // Abs;
f2[x_] := (x // Transpose).x;
Minimize[f1[x], {x}] (* fine, returns 0 *)
Minimize[f2[x], {x}] (* not fine, returns "Minimize[Transpose[x].x, x]" *)


A workaround for this would be to do:

Minimize[f2[{{a}, {b}}], {a, b}]
{0, {a -> 0, b -> 0}}


However, that isn't very nice when X is a long vector.

Is there a way to minimise a function taking a vector directly without resorting to workarounds?

If I have to use a workaround, is there a way to automate what I did in that example maybe using "macros"?

• When I run the code Minimize[f1[x], {x}] returns 0 – Jagra Jun 6 '14 at 13:35
• @Jagra there was a mistake I switched f1 with f2 – SpaceMonkey Jun 6 '14 at 13:59

Would that work?

Minimize[f2@#, #] & /@ {a, b, c}

• thanks, that's a clever way of making it shorter ! – SpaceMonkey Jun 6 '14 at 13:45
• It should be "@" not "/@" though – SpaceMonkey Jun 6 '14 at 13:46
• @Spacemonkey - Reproducing your question, I found that "f1" doesn't work whereas f2 works - it's just the opposite. – eldo Jun 6 '14 at 13:58
• I made a mistake and switched f1 with f2 in my question, fixed now. – SpaceMonkey Jun 6 '14 at 13:59

Based on edlo's answer, I have come up with the following shortcut:

CreateVarsList[n_] := Table[Unique[], {n}]; (* get n list of vars *)
CreateVarsList[x_, n_] :=
Array[ToExpression[x <> ToString[#]] &, n]; (* get n list of vars named xi *)


Now I can do:

mini = Minimize[J[{#} // Transpose], #] & @ CreateVarsList


Where J(theta) takes a vector of length 28. Previously I would have to type a list of 28 variables twice.

• you could define f2 as {x}.{x} which is the same as x^2. Transposing is not possible here. – eldo Jun 6 '14 at 14:17
• The Array/ToExpression/ToString stuff is arguably unnecessary. Creation of sequentially numbered symbols is the job of Unique. – Oleksandr R. Jun 6 '14 at 15:41
• @OleksandrR. I have added another definition based on what you suggested. Thanks ! – SpaceMonkey Jun 6 '14 at 16:32
• @eldo I don't get what you mean. I'm multiplying a vector (column) by each row in a matrix to produce another vector (column) so that would be XTranspose(theta) = y where X(mn) , theta(n*1) and y(m*1). – SpaceMonkey Jun 6 '14 at 17:04