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])
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)
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])
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.]])
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.]])
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.]])
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])
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])
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])
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])
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])
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])
def test_step_function_with_scalar(self): x = 1 vals = piecewise(x, [x < 0, x >= 0], [-1, 1]) assert_(vals == 1)
def test_condition_is_list_of_single_bool_list(self): x = piecewise([0, 0], [[True, False]], [1]) assert_array_equal(x, [1, 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.])
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])
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)
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)
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])
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])