Example #1
0
    def test_default(self):
        # No value specified for x[1], should be 0
        x = piecewise([[True, False]], [2], [1, 2],)
        assert_allclose(x, [2, 0])

        # Should set x[1] to 3
        x = piecewise([[True, False]], [2, 3], [1, 2])
        assert_allclose(x, [2, 3])
Example #2
0
    def test_default(self):
        # No value specified for x[1], should be 0
        x = piecewise([[True, False]], [2], [1, 2],)
        assert_array_equal(x, [2, 0])

        # Should set x[1] to 3
        x = piecewise([[True, False]], [2, 3], [1, 2])
        assert_array_equal(x, [2, 3])
Example #3
0
    def test_0d(self):
        x = np.array(3)
        y = piecewise([x > 3], [4, 0], x)
        assert_(y.ndim == 0)
        assert_(y == 0)

        x = 5
        y = piecewise([[True], [False]], [1, 0], x)
        assert_(y == 1)
        assert_(y.ndim == 0)
Example #4
0
    def test_0d(self):
        x = np.array(3)
        y = piecewise(x, [x > 3], [4, 0])
        assert_(y.ndim == 0)
        assert_(y == 0)

        x = 5
        y = piecewise(x, [[True], [False]], [1, 0])
        assert_(y.ndim == 0)
        assert_(y == 1)
Example #5
0
    def test_0d(self):
        x = np.array(3)
        y = piecewise([x > 3], [4, 0], x)
        assert y.ndim == 0
        assert y == 0

        x = 5
        y = piecewise([[True], [False]], [1, 0], x)
        assert y == 1
        assert y.ndim == 0
Example #6
0
 def test_function_with_two_args(self):
     x = np.linspace(-2, 2, 5)
     X, Y = np.meshgrid(x, x)
     vals = piecewise([X * Y < 0, ], [lambda x, y: -x * y, lambda x, y: x * y], (X, Y))
     assert_allclose(vals, [[4., 2., -0., 2., 4.],
                            [2., 1., -0., 1., 2.],
                            [-0., -0., 0., 0., 0.],
                            [2., 1., 0., 1., 2.],
                            [4., 2., 0., 2., 4.]])
def adaptive_levin_points(M, delta):
    m = M - 1
    prm = 0.5
    while prm * m / delta >= 1:
        delta = 2 * delta
    k = np.arange(M)
    x = piecewise([k < prm * m, k == np.ceil(prm * m)],
                  [-1 + k / delta, 0 * k, 1 - (m - k) / delta])
    return x
Example #8
0
def adaptive_levin_points(m, delta):
    m_1 = m - 1
    prm = 0.5
    while prm * m_1 / delta >= 1:
        delta = 2 * delta
    k = np.arange(m)
    x = piecewise([k < prm * m_1, k == np.ceil(prm * m_1)],
                  [-1 + k / delta, 0 * k, 1 - (m_1 - k) / delta])
    return x
Example #9
0
    def test_passing_further_args_to_fun(self):
        def fun0(x, y, scale=1.):
            return -x * y / scale

        def fun1(x, y, scale=1.):
            return x * y / scale
        x = np.linspace(-2.5, 2.5, 6)
        vals = piecewise([x < 0, ], [fun0, fun1], (x,), args=(2.,), scale=2.)
        assert_array_equal(vals, [2.5, 1.5, 0.5, 0.5, 1.5, 2.5])
Example #10
0
    def test_passing_further_args_to_fun(self):
        def fun0(x, y, scale=1.):
            return -x * y / scale

        def fun1(x, y, scale=1.):
            return x * y / scale
        x = np.linspace(-2.5, 2.5, 6)
        vals = piecewise([x < 0, ], [fun0, fun1], (x,), args=(2.,), scale=2.)
        assert_allclose(vals, [2.5, 1.5, 0.5, 0.5, 1.5, 2.5])
Example #11
0
def adaptive_levin_points(M, delta):
    m = M - 1
    prm = 0.5
    while prm * m / delta >= 1:
        delta = 2 * delta
    k = np.arange(M)
    x = piecewise([k < prm * m, k == np.ceil(prm * m)],
                  [-1 + k / delta, 0 * k, 1 - (m - k) / delta])
    return x
Example #12
0
 def test_function_with_two_args(self):
     x = np.linspace(-2, 2, 5)
     X, Y = np.meshgrid(x, x)
     vals = piecewise(
         [X * Y < 0, ], [lambda x, y: -x * y, lambda x, y: x * y], (X, Y))
     assert_array_equal(vals, [[4., 2., -0., 2., 4.],
                               [2., 1., -0., 1., 2.],
                               [-0., -0., 0., 0., 0.],
                               [2., 1., 0., 1., 2.],
                               [4., 2., 0., 2., 4.]])
Example #13
0
 def test_fill_value2_and_function_with_two_args(self):
     x = np.linspace(-2, 2, 5)
     X, Y = np.meshgrid(x, x)
     vals = piecewise((X, Y), [X * Y < -0.5, X * Y > 0.5],
                      [lambda x, y: -x * y, lambda x, y: x * y, np.nan])
     nan = np.nan
     assert_array_equal(vals, [[4.,   2.,  nan,   2.,   4.],
                               [2.,   1.,  nan,   1.,   2.],
                               [nan,  nan,  nan,  nan,  nan],
                               [2.,   1.,  nan,   1.,   2.],
                               [4.,   2.,  nan,   2.,   4.]])
Example #14
0
 def test_fill_value2_and_function_with_two_args(self):
     x = np.linspace(-2, 2, 5)
     X, Y = np.meshgrid(x, x)
     vals = piecewise([X * Y < -0.5, X * Y > 0.5],
                      [lambda x, y: -x * y, lambda x, y: x * y, np.nan],
                      (X, Y))
     nan = np.nan
     assert_array_equal(vals,
                        [[4., 2., nan, 2., 4.], [2., 1., nan, 1., 2.],
                         [nan, nan, nan, nan, nan], [2., 1., nan, 1., 2.],
                         [4., 2., nan, 2., 4.]])
Example #15
0
 def test_condition_is_list_of_single_int_array(self):
     x = piecewise([np.array([1, 0])], [1], [0, 0])
     assert_allclose(x, [1, 0])
Example #16
0
 def test_conditions_is_list_of_single_bool_array(self):
     x = piecewise([np.array([True, False])], [1], [0, 0])
     assert_allclose(x, [1, 0])
Example #17
0
 def test_condition_is_list_of_single_bool_list(self):
     x = piecewise([[True, False]], [1], [0, 0])
     assert_allclose(x, [1, 0])
Example #18
0
 def test_abs_function(self):
     x = np.linspace(-2.5, 2.5, 6)
     vals = piecewise([x < 0, x >= 0], [lambda x: -x, lambda x: x], (x,))
     assert_array_equal(vals,
                        [2.5, 1.5, 0.5, 0.5, 1.5, 2.5])
Example #19
0
 def test_conditions_is_list_of_single_bool_array(self):
     x = piecewise([np.array([True, False])], [1], [0, 0])
     assert_array_equal(x, [1, 0])
Example #20
0
 def test_abs_function_with_scalar(self):
     x = np.array(-2.5)
     vals = piecewise([x < 0, x >= 0], [lambda x: -x, lambda x: x], (x,))
     assert vals == 2.5
Example #21
0
 def test_condition_is_list_of_single_int_array(self):
     x = piecewise([0, 0], [np.array([1, 0])], [1])
     assert_array_equal(x, [1, 0])
Example #22
0
    def test_simple(self):
        x = piecewise([[False, True]], [lambda x:-1], [0, 0])
        assert_array_equal(x, [0, -1])

        x = piecewise([[True, False], [False, True]], [3, 4], [1, 2])
        assert_array_equal(x, [3, 4])
Example #23
0
 def test_condition_is_list_of_single_bool_list(self):
     x = piecewise([0, 0], [[True, False]], [1])
     assert_array_equal(x, [1, 0])
Example #24
0
 def test_step_function(self):
     x = np.linspace(-2.5, 2.5, 6)
     vals = piecewise([x < 0, x >= 0], [-1, 1], x)
     assert_array_equal(vals, [-1., -1., -1., 1., 1., 1.])
Example #25
0
 def test_step_function(self):
     x = np.linspace(-2.5, 2.5, 6)
     vals = piecewise(x, [x < 0, x >= 0], [-1, 1])
     assert_array_equal(vals, [-1., -1., -1.,  1.,  1.,  1.])
Example #26
0
 def test_otherwise_condition(self):
     x = np.linspace(-2.5, 2.5, 6)
     vals = piecewise([x < 0, ], [lambda x: -x, lambda x: x], (x,))
     assert_array_equal(vals, [2.5, 1.5, 0.5, 0.5, 1.5, 2.5])
Example #27
0
 def test_abs_function_with_scalar(self):
     x = np.array(-2.5)
     vals = piecewise([x < 0, x >= 0], [lambda x: -x, lambda x: x], (x,))
     assert_(vals == 2.5)
Example #28
0
    def test_simple(self):
        x = piecewise([[False, True]], [lambda _: -1], [0, 0])
        assert_allclose(x, [0, -1])

        x = piecewise([[True, False], [False, True]], [3, 4], [1, 2])
        assert_allclose(x, [3, 4])
Example #29
0
 def test_abs_function(self):
     x = np.linspace(-2.5, 2.5, 6)
     vals = piecewise((x,), [x < 0, x >= 0], [lambda x: -x, lambda x: x])
     assert_array_equal(vals,
                        [2.5,  1.5,  0.5,  0.5,  1.5,  2.5])
Example #30
0
 def test_step_function_with_scalar(self):
     x = 1
     vals = piecewise([x < 0, x >= 0], [-1, 1], x)
     assert vals == 1
Example #31
0
 def test_abs_function(self):
     x = np.linspace(-2.5, 2.5, 6)
     vals = piecewise([x < 0, x >= 0], [lambda x: -x, lambda x: x], (x,))
     assert_allclose(vals, [2.5, 1.5, 0.5, 0.5, 1.5, 2.5])
Example #32
0
 def test_condition_is_list_of_single_bool_list(self):
     x = piecewise([[True, False]], [1], [0, 0])
     assert_array_equal(x, [1, 0])
Example #33
0
 def test_otherwise_condition(self):
     x = np.linspace(-2.5, 2.5, 6)
     vals = piecewise([x < 0, ], [lambda x: -x, lambda x: x], (x,))
     assert_allclose(vals, [2.5, 1.5, 0.5, 0.5, 1.5, 2.5])
Example #34
0
 def test_condition_is_list_of_single_int_array(self):
     x = piecewise([np.array([1, 0])], [1], [0, 0])
     assert_array_equal(x, [1, 0])
Example #35
0
 def test_step_function(self):
     x = np.linspace(-2.5, 2.5, 6)
     vals = piecewise([x < 0, x >= 0], [-1, 1], x)
     assert_allclose(vals, [-1., -1., -1., 1., 1., 1.])
Example #36
0
 def test_otherwise_condition(self):
     x = np.linspace(-2.5, 2.5, 6)
     vals = piecewise((x,), [x < 0, ], [lambda x: -x, lambda x: x])
     assert_array_equal(vals, [2.5,  1.5,  0.5,  0.5,  1.5,  2.5])
Example #37
0
 def test_step_function_with_scalar(self):
     x = 1
     vals = piecewise([x < 0, x >= 0], [-1, 1], x)
     assert_(vals == 1)
Example #38
0
    def test_simple(self):
        x = piecewise([[False, True]], [lambda x: -1], [0, 0])
        assert_array_equal(x, [0, -1])

        x = piecewise([[True, False], [False, True]], [3, 4], [1, 2])
        assert_array_equal(x, [3, 4])