0
$\begingroup$

I want to calculate the derivative of a matrix with respect to a vector with mathematica, and I would like to know if it is possible to have a formula as an answer instead of an expanded matrix.

For example, I want to have such answer : $$\frac{d }{dx}x^\top A x= (A+A^\top)x $$

A = {{a11, a12}, {a21, a22}};
x = {{x1}, {x2}};
f = Transpose[x].A.x;
Grad[f,x];

Instead of this one:

Grad[{{x1 (a11 x1 + a21 x2) + x2 (a12 x1 + a22 x2)}},{{x1}, {x2}}]
$\endgroup$
1
  • $\begingroup$ Why do you expect such a result? The formula does not work if, say, A = {{a11 x1, a12}, {a21, a22}}. $\endgroup$ Mar 10 at 18:04

1 Answer 1

1
$\begingroup$

Try the following:

A = {{a11, a12}, {a21, a22}};
x = {{x1}, {x2}};
xt = Transpose@x;
vars = Variables[x];
f = First @@ (xt . A . x);

lhs = Plus @@ Reverse@Transpose@CoefficientList[Grad[f, vars], vars] . x;

rhs = (A + Transpose@A) . x;

lhs === rhs

(*True*)
$\endgroup$
1
  • $\begingroup$ Thank you for your interesting response. However, I am looking to have explicit formulas of which I do not know the result. $\endgroup$
    – ZchGarinch
    Mar 10 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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