Problem Statement
Let $f(x):\mathbb{R}^n \to \mathbb{R}^n$ be a function with Jacobian $J_f\in\mathbb{R}^{n\times n}$. Let $v\in\mathbb{R}^n$ be a vector that does not depend on $x$. I want to find the Jacobian of the following dot product function $g:\mathbb{R}^n\to\mathbb{R}^n$ $$ g(x) = (f(x)^\top v) f(x). $$
My Attempt
Using the product rule $$ J_{g}(x) = J_f(x) (f(x)^\top v) + f(x)f(x)^\top $$ however I have tried this numerically (using automatic differentiation as well) but it doesn't work, so this formula must be wrong.
$g = (f^T v) f$
$J_g = [\dfrac{\partial g_i}{\partial x_j} ]$
Now,
$\begin{equation} \begin{split} \dfrac{\partial g_i}{\partial x_j} &= f_i ( \dfrac{\partial}{\partial x_j} (f^T v) ) + (f^T v) \dfrac{\partial f_i}{\partial x_j} \\ &= f_i ( v^T \dfrac{\partial}{\partial x_j} f ) +(f^T v) \dfrac{\partial f_i}{\partial x_j} \\ \end{split} \end{equation}$
Hence,
$J_g = f (v^T J_f ) + (f^T v) J_f = ( (f^T v) I + f v^T ) J_f $
Let's verify this formula, suppose
$f = \begin{bmatrix} x_1 +5 x_2 \\ 2 x_1 + 3 x_2 \end{bmatrix} $
$ v = \begin{bmatrix} 3 \\ -1 \end{bmatrix} $
Then
$g = \begin{bmatrix} (x_1 + 5 x_2)( x_1 + 12 x_2 ) \\ (2 x_1 + 3 x_2)(x_1 + 12 x_2 ) \end{bmatrix} $
Direct evaluation of $J_g$ gives
$J_g = \begin{bmatrix} 2 x_1 + 17 x_2 && 17 x_1 + 120 x_2 \\ 4 x_1 + 27 x_2 && 27 x_1 + 72 x_2 \end{bmatrix}$
Direct evaluation of $J_f $ gives,
$f = \begin{bmatrix} x_1 +5 x_2 \\ 2 x_1 + 3 x_2 \end{bmatrix} $
$J_f = \begin{bmatrix} 1 && 5 \\ 2 && 3 \end{bmatrix}$
$(f^T v) I = \begin{bmatrix} x_1 + 12 x_2 && 0 \\ 0 && x_1 + 12 x_2 \end{bmatrix} $
$ f v^T = \begin{bmatrix} 3 x_1 + 15 x_2 && -x_1 - 5 x_2 \\ 6 x_1 + 9 x_2 && -2x_1 - 3x_2 \end{bmatrix} $
Hence,
$( (f^T v) I + f v^T ) J_f ) = \begin{bmatrix} 4x_1 + 27 x_2 &&-x_1 - 5 x_2 \\ 6 x_1 + 9 x_2 && -x_1 + 9 x_2 \end{bmatrix} \begin{bmatrix} 1 && 5 \\ 2 && 3 \end{bmatrix} = \begin{bmatrix} 2 x_1 + 17 x_2 && 17 x_1 + 120 x_2 \\ 4 x_1 + 27 x_2 && 27 x_1 + 72 x_2 \end{bmatrix} $