Beispiel #1
0
def test_pwc_integral():
    # some random data
    x = [0.0, 1.0, 2.0, 2.5, 4.0]
    y = [1.0, -0.5, 1.5, 0.75]
    f1 = spk.PieceWiseConstFunc(x, y)

    # test full interval
    full = 1.0 * 1.0 + 1.0 * -0.5 + 0.5 * 1.5 + 1.5 * 0.75
    assert_equal(f1.integral(), full)
    assert_equal(f1.integral((np.min(x), np.max(x))), full)
    # test part interval, spanning an edge
    assert_equal(f1.integral((0.5, 1.5)), 0.5 * 1.0 + 0.5 * -0.5)
    # test part interval, just over two edges
    assert_almost_equal(f1.integral((1.0 - 1e-16, 2 + 1e-16)),
                        1.0 * -0.5,
                        decimal=14)
    # test part interval, between two edges
    assert_equal(f1.integral((1.0, 2.0)), 1.0 * -0.5)
    assert_equal(f1.integral((1.2, 1.7)), (1.7 - 1.2) * -0.5)
    # test part interval, start to before and after edge
    assert_equal(f1.integral((0.0, 0.7)), 0.7 * 1.0)
    assert_equal(f1.integral((0.0, 1.1)), 1.0 * 1.0 + 0.1 * -0.5)
    # test part interval, before and after edge till end
    assert_equal(f1.integral((2.6, 4.0)), (4.0 - 2.6) * 0.75)
    assert_equal(f1.integral((2.4, 4.0)), (2.5 - 2.4) * 1.5 + (4 - 2.5) * 0.75)
Beispiel #2
0
def test_pwc_avrg():
    # some random data
    x = [0.0, 1.0, 2.0, 2.5, 4.0]
    y = [1.0, -0.5, 1.5, 0.75]
    f1 = spk.PieceWiseConstFunc(x, y)

    x = [0.0, 0.75, 2.0, 2.5, 2.7, 4.0]
    y = [0.5, 1.0, -0.25, 0.0, 1.5]
    f2 = spk.PieceWiseConstFunc(x, y)

    f1.add(f2)
    f1.mul_scalar(0.5)
    x_expected = [0.0, 0.75, 1.0, 2.0, 2.5, 2.7, 4.0]
    y_expected = [0.75, 1.0, 0.25, 0.625, 0.375, 1.125]
    assert_array_almost_equal(f1.x, x_expected, decimal=16)
    assert_array_almost_equal(f1.y, y_expected, decimal=16)
Beispiel #3
0
def test_pwc():
    # some random data
    x = [0.0, 1.0, 2.0, 2.5, 4.0]
    y = [1.0, -0.5, 1.5, 0.75]
    f = spk.PieceWiseConstFunc(x, y)

    # function values
    assert_equal(f(0.0), 1.0)
    assert_equal(f(0.5), 1.0)
    assert_equal(f(1.0), 0.25)
    assert_equal(f(2.0), 0.5)
    assert_equal(f(2.25), 1.5)
    assert_equal(f(2.5), 2.25 / 2)
    assert_equal(f(3.5), 0.75)
    assert_equal(f(4.0), 0.75)

    assert_array_equal(f([0.0, 0.5, 1.0, 2.0, 2.25, 2.5, 3.5, 4.0]),
                       [1.0, 1.0, 0.25, 0.5, 1.5, 2.25 / 2, 0.75, 0.75])

    xp, yp = f.get_plottable_data()

    xp_expected = [0.0, 1.0, 1.0, 2.0, 2.0, 2.5, 2.5, 4.0]
    yp_expected = [1.0, 1.0, -0.5, -0.5, 1.5, 1.5, 0.75, 0.75]
    assert_array_almost_equal(xp, xp_expected, decimal=16)
    assert_array_almost_equal(yp, yp_expected, decimal=16)

    assert_almost_equal(f.avrg(), (1.0 - 0.5 + 0.5 * 1.5 + 1.5 * 0.75) / 4.0,
                        decimal=16)

    # interval averaging
    a = f.avrg([0.5, 3.5])
    assert_almost_equal(a, (0.5 - 0.5 + 0.5 * 1.5 + 1.0 * 0.75) / 3.0,
                        decimal=16)
    a = f.avrg([1.5, 3.5])
    assert_almost_equal(a, (-0.5 * 0.5 + 0.5 * 1.5 + 1.0 * 0.75) / 2.0,
                        decimal=16)
    a = f.avrg([1.0, 2.0])
    assert_almost_equal(a, (1.0 * -0.5) / 1.0, decimal=16)
    a = f.avrg([1.0, 3.5])
    assert_almost_equal(a, (-0.5 * 1.0 + 0.5 * 1.5 + 1.0 * 0.75) / 2.5,
                        decimal=16)
    a = f.avrg([1.0, 4.0])
    assert_almost_equal(a, (-0.5 * 1.0 + 0.5 * 1.5 + 1.5 * 0.75) / 3.0,
                        decimal=16)
    a = f.avrg([0.0, 2.2])
    assert_almost_equal(a, (1.0 * 1.0 - 0.5 * 1.0 + 0.2 * 1.5) / 2.2,
                        decimal=15)

    # averaging over multiple intervals
    a = f.avrg([(0.5, 1.5), (1.5, 3.5)])
    assert_almost_equal(a, (0.5 - 0.5 + 0.5 * 1.5 + 1.0 * 0.75) / 3.0,
                        decimal=16)

    # averaging over multiple intervals
    a = f.avrg([(0.5, 1.5), (2.2, 3.5)])
    assert_almost_equal(a,
                        (0.5 * 1.0 - 0.5 * 0.5 + 0.3 * 1.5 + 1.0 * 0.75) / 2.3,
                        decimal=15)
Beispiel #4
0
def test_pwc_mul():
    x = [0.0, 1.0, 2.0, 2.5, 4.0]
    y = [1.0, -0.5, 1.5, 0.75]
    f = spk.PieceWiseConstFunc(x, y)

    f.mul_scalar(1.5)
    assert_array_almost_equal(f.x, x, decimal=16)
    assert_array_almost_equal(f.y, 1.5 * np.array(y), decimal=16)
    f.mul_scalar(1.0 / 5.0)
    assert_array_almost_equal(f.y, 1.5 / 5.0 * np.array(y), decimal=16)
Beispiel #5
0
def test_pwc_add():
    # some random data
    x = [0.0, 1.0, 2.0, 2.5, 4.0]
    y = [1.0, -0.5, 1.5, 0.75]
    f = spk.PieceWiseConstFunc(x, y)

    f1 = copy(f)
    x = [0.0, 0.75, 2.0, 2.5, 2.7, 4.0]
    y = [0.5, 1.0, -0.25, 0.0, 1.5]
    f2 = spk.PieceWiseConstFunc(x, y)
    f1.add(f2)
    x_expected = [0.0, 0.75, 1.0, 2.0, 2.5, 2.7, 4.0]
    y_expected = [1.5, 2.0, 0.5, 1.25, 0.75, 2.25]
    assert_array_almost_equal(f1.x, x_expected, decimal=16)
    assert_array_almost_equal(f1.y, y_expected, decimal=16)

    f2.add(f)
    assert_array_almost_equal(f2.x, x_expected, decimal=16)
    assert_array_almost_equal(f2.y, y_expected, decimal=16)

    f1.add(f2)
    # same x, but y doubled
    assert_array_almost_equal(f1.x, f2.x, decimal=16)
    assert_array_almost_equal(f1.y, 2 * f2.y, decimal=16)
Beispiel #6
0
def test_pwc_integral_bad_bounds_oob_2():
    # some random data
    x = [0.0, 1.0, 2.0, 2.5, 4.0]
    y = [1.0, -0.5, 1.5, 0.75]
    f1 = spk.PieceWiseConstFunc(x, y)
    f1.integral((-1, 3))