def chain_2(d2f_dg2, dg_dx, df_dg, d2g_dx2):
if np.all(dg_dx==<NUM_LIT:1.>) and np.all(d2g_dx2 == <NUM_LIT:0>):<EOL><INDENT>return d2f_dg2<EOL><DEDENT>dg_dx_2 = np.clip(dg_dx, -np.inf, _lim_val_square)**<NUM_LIT:2><EOL>return d2f_dg2*(dg_dx_2) + df_dg*d2g_dx2<EOL>
Generic chaining function for second derivative

.. math::
    \\frac{d^{2}(f . g)}{dx^{2}} = \\frac{d^{2}f}{dg^{2}}(\\frac{dg}{dx})^{2} + \\frac{df}{dg}\\frac{d^{2}g}{dx^{2}}