2
$\begingroup$

I learned not use the Norm[] function when computing vector derivative, so I use the dot product instead:

In: D[x.x, x]
Out: 1.x + x.1

What does the result mean? Is 1=(1, 1, .., 1) here? Why can't it show just 2x as the result? And Mathematica won't resolve it when I define x?

In: 1.x + x .1 /. x -> {3, 4}
Out: {0.3 + 1.{3, 4}, 0.4 + 1.{3, 4}}
$\endgroup$
4
  • 3
    $\begingroup$ One way to approach this to define x = Array[a, 3]; Then you can take the derivative x = D[x . x, {x}] and you'll get more what you expect. Otherwise it doesn't know what the dimensions of x are (if its a scalar, vector, matrix). $\endgroup$ – bill s Apr 11 at 20:17
  • $\begingroup$ Thanks, now it makes sense why, since it might be a matrix. Can I tell it the "type"? I tried $Assumptions = (x) \[Element] Vectors[2, Reals]; but it didn't help. Also, if I use Array[a, 3], can I resolve a at some point? For example, take a derivative and then compute it for certain vector. D[x.x, {x}] /. x -> {1, 2, 3} doesn't seem to work $\endgroup$ – Soid Apr 11 at 20:41
  • $\begingroup$ Related: mathematica.stackexchange.com/q/89651 $\endgroup$ – Michael E2 Apr 11 at 21:35
  • $\begingroup$ There is not really a nice way to generically say "x is a 3 vector" using assumptions. But if you think about it, x = Array[a, 3]; is effectively a declaration that x is an arbitrary 3 vector. $\endgroup$ – bill s Apr 11 at 22:00
3
$\begingroup$

As suggested by @bill s, you can write

x = Array[a, 3];
deriv = D[x . x, {x}]
(* {2 a[1], 2 a[2], 2 a[3]} *)

Note that the you need to put {x} rather than x (otherwise it will attempt to interpret the 2nd item in the list as the order of the derivative - see the help for D - the syntax is rather over loaded).

The easiest way to substitute values is perhaps

deriv /. Thread[x -> {1, 2, 3}]
(* {2, 4, 6} *)
$\endgroup$

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.