Add base code from SciPy Doc example