示例#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])
示例#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])
示例#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)
示例#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)
示例#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
示例#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
示例#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
示例#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])
示例#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])
示例#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
示例#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.]])
示例#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.]])
示例#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.]])
示例#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])
示例#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])
示例#17
0
 def test_condition_is_list_of_single_bool_list(self):
     x = piecewise([[True, False]], [1], [0, 0])
     assert_allclose(x, [1, 0])
示例#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])
示例#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])
示例#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
示例#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])
示例#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])
示例#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])
示例#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.])
示例#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.])
示例#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])
示例#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)
示例#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])
示例#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])
示例#30
0
 def test_step_function_with_scalar(self):
     x = 1
     vals = piecewise([x < 0, x >= 0], [-1, 1], x)
     assert vals == 1
示例#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])
示例#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])
示例#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])
示例#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])
示例#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.])
示例#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])
示例#37
0
 def test_step_function_with_scalar(self):
     x = 1
     vals = piecewise([x < 0, x >= 0], [-1, 1], x)
     assert_(vals == 1)
示例#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])