I am trying to numerically evaluate partial derivatives of the following expression:

$$p(\mathbb{x})=(L\circ R)(\mathbb{x})=L(R\mathbb{x})$$


$$R_{a}=\begin{pmatrix}a & 1 \\ 0 & -a\end{pmatrix}$$

$$L_{x,y,k}=\begin{pmatrix}x^{2}k \\ xyk\end{pmatrix}$$


$$\mathbb{x}=\begin{pmatrix} x \\ y \end{pmatrix}$$

All the functions are in $\mathbb{R}$ and continuously differentiable.

This is very simple when computed explicitly:

$p(\mathbb{x})= L(R(\mathbb{x}))=L\left( \begin{align*} ax+y &=x' \\ -ay &= y' \end{align*} \right)=\begin{pmatrix} x'^{2}k \\ x'y'k\end{pmatrix}=\dots=\begin{pmatrix} akx^{2}+2akx+ky^{2} \\ -a^{2}kxy-aky^{2} \end{pmatrix}$

Taking partial derivatives is then straighforwad, e.g.:

$$\frac{\partial}{\partial a}p(\mathbb{x})=\begin{pmatrix}kx^{2}+2kx \\ -2akxy-ky^{2} \end{pmatrix}$$

However, I need to compute the derivatives analytically, via chain rule. For example, when $a$ is considered variable (and $x,y,k$ are constants):

$$\frac{\partial}{\partial a}p(\mathbb{x})=\frac{\partial}{\partial R\mathbb{x}}L(R\mathbb{x})\cdot \frac{\partial}{\partial a}R\mathbb{x}$$

the second term is simple:

$$\frac{\partial}{\partial a}R\mathbb{x}=\begin{pmatrix}1 & 0 \\ 0 & -1\end{pmatrix}\mathbb{x}=\begin{pmatrix}x \\ -y\end{pmatrix}$$

However, how to evaluate the first term?

$$\frac{\partial}{\partial R\mathbb{x}}L(R\mathbb{x})=?$$

The result should be numerically the same as as with the explicit computation above. I have no clue, however, how to compute the derivative of the vector-valued function $L$ at $R\mathbb{x}$.

I always need partial derivative of the above expression with respect to just one of the variables and others considered constant.

The reason for using chain rule here is to allow computing partial derivatives using a computer program the user provider function and its derivatives and the program computes the chain.


According to Wikipedia, one approach can be to compute it in terms of Jacobians:

$$J_{a}(L\circ R)=J_{R(a)}(L)\cdot J_{a}(R)$$

Here $J_{a}(R)$ is a $2\times 1$ vector, since $R$ is a $2\times 2$ matrix with only one variable.

The $J_{R(a)}(L)$ is still mystery for me. In principle, it is also a function of $a$, but it has to be a $2\times 2$ matrix so that the result makes sense.

I tried this method but the result is different from straight differentiation (the green part is computed using Jacobians and the top black part is explicit computation):

enter image description here


What I think is confusing is the way the problem seems to be described. We start with what looks like a composite function (L∘R)(x), but then we write L(Rx) where the Rx looks like you're thinking of matrix multiplication rather applying a function R to x. Instead, lets be explicit and write $$R: \mathbf R^3 -> \mathbf R^2$$ as R(a, x, y) = (ax +y, -ay), and $$L: \mathbf R^2 -> \mathbf R^2$$ as $$L(\alpha, \beta) -> (k\alpha^2, k\alpha\beta)$$ We treat k as a constant, because otherwise we wouldn't be able to apply L to R. Now we go ahead and write $$p(a, \mathbf x) = L(R(a, \mathbf x))$$, and we can apply the chain rule to write $$D(p) = D(L)D(R)$$, where D(L) is the 2 x 2 Jacobian of L (where alpha and beta are evaluated at x, y, and a), and D(R) is the 2 X 3 Jacobian of R.

Ultimately, you seem to get what you're trying to get, even though you've lopped columns from the Jacobian of R (I think because of the way you've treated R as a matrix and not a function, which is awkward). Your answers don't match though, because I don't think your first calculation is quite right. You're missing a factor of a in the first term of the top entry, and a factor of -a in the entire lower entry.

| cite | improve this answer | |
  • $\begingroup$ Thanks. I was working with matrices (according to numerical optimization textbook - such notation is normally used in computer vision) and although $R\mathbb{x}$ is multiplication, it is also application of function $R$ to $\mathbb{x}$. I need to find partial derivatives of the composite function, which is actually a function transforming 2D points (hence the matrix). $\endgroup$ – Libor Feb 2 '15 at 23:24
  • $\begingroup$ Furthermore, $\mathbb{x}$ will be always fixed - this represents a point in space, so only variables are $a,k$. This is why I am writing $R\mathbb{x}$ - it is to be considered matrix function or $a$. $\endgroup$ – Libor Feb 2 '15 at 23:30
  • $\begingroup$ Just to put it in context - the final goal is to minimize this function: en.wikipedia.org/wiki/Bundle_adjustment#Mathematical_definition - the point projection $Q$ consists of several matrices multiplied together and some nonlinear operators - a partial derivatives with respect to all parameters are needed for a minimization routine. $\endgroup$ – Libor Feb 2 '15 at 23:35

Define the variables $$\eqalign{ e_1 &= \pmatrix{1\\0}, \quad &e_2 = \pmatrix{0\\1} \cr M &= \pmatrix{1&0\\0 &-1}, \quad &N = \pmatrix{0&1\\0 &0} \cr m &= Mx, \quad &n = Nx \cr }$$ In terms of these new variables, the quantities in this problem are $$\eqalign{ R &= aM+N, \quad &r = Rx = am+n \cr dR &= M\,da, \quad &dr = m\,da \cr L(x) &= kxx^Te_1 \cr p &= L(r) = krr^Te_1 \cr }$$ Find the differential of $p$ and then its gradient. $$\eqalign{ dp &= k\,(r\,dr^T+dr\,r^T)\,e_1 \cr &= k\,(rm^T+mr^T)\,e_1\,da \cr \frac{\partial p}{\partial a} &= krm^Te_1 + kmr^Te_1 \cr }$$

| cite | improve this answer | |

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.