def test_first_derivative_nes_is_constant_for_line():
    n = 13
    x = np.arange(n)
    F = myl1tf.get_first_derivative_matrix_nes(x)
    xx = cvxopt.matrix(x)
    slope = F * xx
    slope_expected = [1.0] * 11
    slope_expected = cvxopt.matrix(slope_expected)
    assert max(abs(slope - slope_expected)) < 1e-13

    # add some gaps, still should be unit slope
    x_with_gaps = x[np.array([1, 4, 5, 9, 11])]
    F = myl1tf.get_first_derivative_matrix_nes(x_with_gaps)
    xx = cvxopt.matrix(x_with_gaps) * 3.0 + 9.5
    slope = F * xx
    slope_expected = [3.0] * (len(x_with_gaps) - 2)
    slope_expected = cvxopt.matrix(slope_expected)
    assert max(abs(slope - slope_expected)) < 1e-13
def test_first_derivative_nes_agrees_with_es():
    # should agree with regularly spaced version when regularly spaced
    n = 13
    F = myl1tf.get_first_derivative_matrix(n)
    F_with_gaps = myl1tf.get_first_derivative_matrix_nes(range(n))
    diff = F - F_with_gaps
    print F
    print F_with_gaps
    max_diff = max(abs(diff))
    print max_diff
    assert max_diff < 1e-13
def test_first_derivative_nes_on_quadratic():
    # should agree with regularly spaced version when regularly spaced
    n = 12
    x = np.arange(n) * 1.0
    x = x[np.array([1, 2, 5, 9, 11])]
    y = 3.0 * x * x + 5.0 * x + 99.5
    expected = cvxopt.matrix([6.0 * xxx + 5.0 for xxx in [2.0, 5.0, 9.0]])
    F = myl1tf.get_first_derivative_matrix_nes(x)
    deriv1 = F * cvxopt.matrix(y)
    diff = deriv1 - expected
    assert max(abs(diff)) < 1e-13