# 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 $$\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).$$

I am trying to implement the O'Sullivan penalty $$S(x) = \sum_{i=1}^N \left\{ y_i - \sum_{j=1}^n \alpha_j B_j(x) \right\}^2 + \lambda \int_x \left\{ \sum_{j=1}^n \alpha_j \frac{\text{d}^2 B_j(x)}{\text{d}x^2} \right\} \text{d}x$$ which requires second derivatives. What is the second derivative of a B-spline?

$$\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).$$