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

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

        # Should set x[1] to 3
        x = piecewise([1, 2], [[True, False]], [2, 3])
        assert_array_equal(x, [2, 3])
示例#3
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)
示例#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_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,), [x < 0, ], [fun0, fun1], args=(2.,), scale=2.)
        assert_array_equal(vals, [2.5,  1.5,  0.5,  0.5,  1.5,  2.5])
示例#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), [
         X * Y < 0,
     ], [lambda x, y: -x * y, lambda 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.]])
示例#7
0
 def test_function_with_two_args(self):
     x = np.linspace(-2, 2, 5)
     X, Y = np.meshgrid(x, x)
     vals = piecewise(
         (X, Y), [X * Y < 0, ], [lambda x, y: -x * y, lambda 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.]])
示例#8
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.]])
示例#9
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.]])
示例#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, ), [
            x < 0,
        ], [fun0, fun1], args=(2., ), scale=2.)
        assert_array_equal(vals, [2.5, 1.5, 0.5, 0.5, 1.5, 2.5])
示例#11
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])
示例#12
0
 def test_conditions_is_list_of_single_bool_array(self):
     x = piecewise([0, 0], [np.array([True, False])], [1])
     assert_array_equal(x, [1, 0])
示例#13
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])
示例#14
0
    def test_simple(self):
        x = piecewise([0, 0], [[False, True]], [lambda x:-1])
        assert_array_equal(x, [0, -1])

        x = piecewise([1, 2], [[True, False], [False, True]], [3, 4])
        assert_array_equal(x, [3, 4])
示例#15
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])
示例#16
0
 def test_step_function_with_scalar(self):
     x = 1
     vals = piecewise(x, [x < 0, x >= 0], [-1, 1])
     assert_(vals == 1)
示例#17
0
 def test_condition_is_list_of_single_bool_list(self):
     x = piecewise([0, 0], [[True, False]], [1])
     assert_array_equal(x, [1, 0])
示例#18
0
 def test_step_function_with_scalar(self):
     x = 1
     vals = piecewise(x, [x < 0, x >= 0], [-1, 1])
     assert_(vals == 1)
示例#19
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.])
示例#20
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])
示例#21
0
 def test_abs_function_with_scalar(self):
     x = np.array(-2.5)
     vals = piecewise((x, ), [x < 0, x >= 0], [lambda x: -x, lambda x: x])
     assert_(vals == 2.5)
示例#22
0
 def test_abs_function_with_scalar(self):
     x = np.array(-2.5)
     vals = piecewise((x,), [x < 0, x >= 0], [lambda x: -x, lambda x: x])
     assert_(vals == 2.5)
示例#23
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])
示例#24
0
 def test_condition_is_list_of_single_bool_list(self):
     x = piecewise([0, 0], [[True, False]], [1])
     assert_array_equal(x, [1, 0])
示例#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_conditions_is_list_of_single_bool_array(self):
     x = piecewise([0, 0], [np.array([True, False])], [1])
     assert_array_equal(x, [1, 0])
示例#27
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])
示例#28
0
    def test_simple(self):
        x = piecewise([0, 0], [[False, True]], [lambda x: -1])
        assert_array_equal(x, [0, -1])

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