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"?

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

Would that work?

Minimize[f2@#, #] & /@ {a, b, c}
  • $\begingroup$ thanks, that's a clever way of making it shorter ! $\endgroup$ – SpaceMonkey Jun 6 '14 at 13:45
  • $\begingroup$ It should be "@" not "/@" though $\endgroup$ – SpaceMonkey Jun 6 '14 at 13:46
  • $\begingroup$ @Spacemonkey - Reproducing your question, I found that "f1" doesn't work whereas f2 works - it's just the opposite. $\endgroup$ – eldo Jun 6 '14 at 13:58
  • $\begingroup$ I made a mistake and switched f1 with f2 in my question, fixed now. $\endgroup$ – 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[28]

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

  • $\begingroup$ you could define f2 as {x}.{x} which is the same as x^2. Transposing is not possible here. $\endgroup$ – eldo Jun 6 '14 at 14:17
  • $\begingroup$ The Array/ToExpression/ToString stuff is arguably unnecessary. Creation of sequentially numbered symbols is the job of Unique. $\endgroup$ – Oleksandr R. Jun 6 '14 at 15:41
  • $\begingroup$ @OleksandrR. I have added another definition based on what you suggested. Thanks ! $\endgroup$ – SpaceMonkey Jun 6 '14 at 16:32
  • $\begingroup$ @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). $\endgroup$ – SpaceMonkey Jun 6 '14 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.