def test_update_ref_func(): ps = Points(test_util.point_gen([1, 3, 1.5, 3.5, 1])) ref_f = Constant(2) e = Extremas(ps, ref_f, min_step=1) assert np.allclose(e.sample_points(), [(0, 1), (1, 3), (2, 1.5), (3, 3.5)]) ref_f.value = 1.25 assert np.allclose(e.sample_points(), [(0, 1), (3, 3.5)])
def test_map(): assert Constant(1).map(lambda x, y: y * 2).y(0) == 2 # updates c = Constant(1) f = c.map(lambda x, y: y * 2) c.value = 2 assert f(0) == 4
def test_func_blend(): c1 = Constant(2) c2 = Constant(3) b = c1.blend(c2, 4, 5) assert b(0) == 2 assert b(4) == 2 assert b(4.5) == 2.5 assert b(5) == 3 assert b(10) == 3
def test_infinite(): c1 = Constant(1) c2 = c1.add(3) assert c2(0) == 4 assert np.array_equal(c2.sample_points(domain=(0, 2)), [(0, 4), (2, 4)]) c3 = c1.subtract(4) assert c3(0) == -3 assert np.array_equal(c3.sample_points(domain=(0, 2)), [(0, -3), (2, -3)])
def test_sample_infinite(): f = Constant(1) with pytest.raises(Exception): f.sample_points() with pytest.raises(Exception): f.sample_points(domain=Interval.infinite()) with pytest.raises(Exception): f.sample_points(domain=Interval.gte(0)) with pytest.raises(Exception): f.sample_points(domain=Interval.lte(0))
def test_constant_step(): c1 = Constant(2) c2 = Constant(3) ds = Interval(0, 3).partition([2]) pw = Piecewise([c1, c2], ds) assert pw.domain.start == 0 assert pw.domain.end == 3 assert pw(-0.1) is None assert pw(0) == 2 assert pw(1.9) == 2 assert pw(2) == 3 assert pw(3) == 3 assert pw(3.1) is None assert np.allclose(pw.sample_points(domain=(0, 3), step=1), [(0, 2), (1, 2), (2, 3), (3, 3)])
def test_decorator_overloads(): c1 = Constant(1) c2 = Constant(2) assert (c2 + 1).y(0) == 3 assert (2 + c1).y(0) == 3 assert (c2 - 1).y(0) == 1 assert (2 - c1).y(0) == 1 assert (c2 * 1).y(0) == 2 assert (1 * c2).y(0) == 2 assert (c2 / 4).y(0) == 0.5 assert (1 / c2).y(0) == 0.5 assert (-c2).y(0) == -2 assert abs(-c2).y(0) == 2
def test_extrema(): f = Points(test_util.point_gen([1, 3, 1, 3])) ref_f = Constant(2) e = Extremas(f, ref_f, min_step=1) assert np.allclose(e.sample_points(), [(0, 1), (1, 3), (2, 1)]) assert e(0.5) == 2 assert e(2) == 1 assert e(2.1) is None f = Points(test_util.point_gen([(1), 1.1, 2.9, (3), (1), 1.1, 3])) ref_f = Constant(2) e = Extremas(f, ref_f, min_step=1) e_points = e.sample_points() assert np.allclose(e_points, [(0, 1), (3, 3), (4, 1)]) assert e(0) == 1 assert e(3) == 3 assert e(3.5) == 2 assert e(4.1) is None
def test_defaults(): f = Constant(1).integral() assert f.domain.is_negative_infinite assert f.domain.is_positive_infinite assert f(0) == 0 assert f(1) == 1 f = Points(test_util.point_gen([1, 1])).integral() assert f.domain.start == 0 assert f.domain.end == 1 assert f(1) == 1
def test_basic(): f = Constant(1).integral(uniform=False) # = 1 * x + c assert f.domain.is_negative_infinite assert f.domain.is_positive_infinite assert f(0) == 0 assert f(0.5) == 0.5 assert f(1) == 1 assert f(2) == 2 assert f(1) == 1 assert f(0.5) == 0.5 assert f(0) == 0
def test_update_func(): ps = Points(test_util.point_gen([1, 3, 1])) ref_f = Constant(2) e = Extremas(ps, ref_f, min_step=1) assert e(0) == 1 assert e(1) == 3 assert e(2) is None ps.append((3, 3)) assert e(2) == 1 assert e(3) is None ps.append((4, 1)) assert e(3) == 3
def test_varying_step(): c1 = Constant(2) c2 = Points(test_util.point_gen([3, 4], t_start=2)) ds = Interval(1, 3).partition([2]) pw = Piecewise([c1, c2], ds) assert pw.domain.start == 1 assert pw.domain.end == 3 assert pw(0.9) is None assert pw(1) == 2 assert pw(1.9) == 2 assert pw(2) == 3 assert pw(3) == 4 assert pw(3.1) is None assert np.allclose(pw.sample_points(domain=(0, 3), step=0.5), [(1, 2), (1.5, 2), (2, 3), (2.5, 3.5), (3, 4)])
def test_differential(): assert Constant(1).differential().y(0) == 0 assert Line(1, 2).differential().y(0) == 2 assert Points([(0, 0), (1, 1)]).differential().y(1) == 1
def test_constant(): c = Constant(1) assert c.y(0) == 1 assert c.y(1) == 1 assert c(0) == 1 assert c.x(0) is None assert c.x(1) == 0 assert c.d_y(0) == 0 assert c.d_y(1) == 0 assert c.x_next(0, min_step=1) == math.inf assert c.x_previous(0, min_step=1) == -math.inf assert c.x_next(0, min_step=1, limit=10) == 10 assert c.x_previous(0, min_step=1, limit=-10) == -10 assert np.allclose(c.sample_points(domain=(0, 2), step=1), [(0, 1), (1, 1), (2, 1)]) assert np.allclose(c.sample_points(domain=(0, 2)), [(0, 1), (2, 1)]) # updates f = Constant(1) f.value = 2 assert f(0) == 2