What is the second derivative of a B-spline? A B-spline of degree $j$ is defined at knots $\vec k$ by the Cox-de Boor recursion formula
\begin{align} 
    B_{i,1}(x) &= \left\{
        \begin{matrix} 
            1 & \mathrm{if} \quad k_i \leq x < k_{i+1} \\
            0 & \mathrm{otherwise} 
        \end{matrix}
    \right. \\
    B_{i,j}(x) &= \frac{x - k_i}{k_{i+j-1} - k_i} B_{i,j-1}(x) + \frac{k_{i+j} - x}{k_{i+j} - k_{i+1}} B_{i+1,j-1}(x)
\end{align}
and has derivative
\begin{equation}
    \frac{\text{d}B_{i,j}(x)}{\text{d}x}
    =
    (j-1)
    \left(
        \frac{-B_{i+1,j-1}(x)}{k_{i+j}-k_{i+1}}
        +
        \frac{B_{i,j-1}(x)}{k_{i+j-1}-k_i}
    \right).
\end{equation}
I am trying to implement the O'Sullivan penalty which requires second derivatives. What is the second derivative of a B-spline? 

This document gives (with a corrected typo)
\begin{equation}
    \frac{\text{d}^{(n)}B_{i,j}(x)}{\text{d}x^{(n)}}
    =
    (j-1)
    \left(
        \frac{- \text{d}^{(n-1)} B_{i+1,j-1}(x) / \text{d}x^{(n-1)}}{k_{i+j}-k_{i+1}}
        +
        \frac{\text{d}^{(n-1)} B_{i,j-1}(x) / \text{d}x^{(n-1)}}{k_{i+j-1}-k_i}
    \right).
\end{equation}
