def test_linear_add_reduce(): ops = LinearOperations() assert_almost_equal(ops.add_reduce(np.array([0,0,0])), 0) assert_almost_equal(ops.add_reduce(np.array([0,1,2])), 3) assert_almost_equal(ops.add_reduce(np.array([1,1,1])), 3) assert_almost_equal(ops.add_reduce(np.array([-1,0,1])), 0) assert_almost_equal(ops.add_reduce(np.array([2,0,-2])), 0) assert_almost_equal(ops.add_reduce(np.array([-1,-1,-1])), -3)
def test_linear_mult(): ops = LinearOperations() assert_almost_equal(ops.mult(0, 0), 0) assert_almost_equal(ops.mult(1, 0), 0) assert_almost_equal(ops.mult(0, 1), 0) assert_almost_equal(ops.mult(-1, 0), 0) assert_almost_equal(ops.mult(0, -1), 0) assert_almost_equal(ops.mult(1, -1), -1) assert_almost_equal(ops.mult(-1, 1), -1) assert_almost_equal(ops.mult(1, 1), 1) assert_almost_equal(ops.mult(-1, -1), 1) assert_almost_equal(ops.mult(2, 1), 2) assert_almost_equal(ops.mult(2, 2), 4) assert_almost_equal(ops.mult(2, -2), -4)
def test_linear_add(): ops = LinearOperations() assert_almost_equal(ops.add(0, 0), 0) assert_almost_equal(ops.add(1, 0), 1) assert_almost_equal(ops.add(0, 1), 1) assert_almost_equal(ops.add(-1, 0), -1) assert_almost_equal(ops.add(0, -1), -1) assert_almost_equal(ops.add(1, -1), 0) assert_almost_equal(ops.add(-1, 1), 0) assert_almost_equal(ops.add(1, 1), 2) assert_almost_equal(ops.add(-1, -1), -2)
def setup_class(self): self.ops = LinearOperations()
class TestLinear(object): def setup_class(self): self.ops = LinearOperations() def test_add(self): X = np.array([0, 1, 0, -1, 0, 1, -1, 1, -1]) Y = np.array([0, 0, 1, 0, -1, -1, 1, 1, -1]) Z = np.array([0, 1, 1, -1, -1, 0, 0, 2, -2]) for x, y, z in zip(X, Y, Z): assert self.ops.add(x, y) == pytest.approx(z) assert np.allclose(self.ops.add(X, Y), Z) def test_add_inplace(self): X = np.array([0, 1, 0, -1, 0, 1, -1, 1, -1]) Y = np.array([0, 0, 1, 0, -1, -1, 1, 1, -1]) Z = np.array([0, 1, 1, -1, -1, 0, 0, 2, -2]) self.ops.add_inplace(X, Y) assert np.allclose(X, Z) def test_add_reduce(self): X = np.array([[0, 0, 0], [0, 1, 2], [1, 1, 1], [-1, 0, 1], [2, 0, -2], [-1, -1, -1]]) Y = np.array([0, 3, 3, 0, 0, -3]) for x, y in zip(X, Y): assert self.ops.add_reduce(x) == pytest.approx(y) def test_mult(self): X = np.array([0, 1, 0, -1, 0, 1, -1, 1, -1, 2, 2, 2]) Y = np.array([0, 0, 1, 0, -1, -1, 1, 1, -1, 1, 2, -2]) Z = np.array([0, 0, 0, 0, 0, -1, -1, 1, 1, 2, 4, -4]) for x, y, z in zip(X, Y, Z): assert self.ops.mult(x, y) == pytest.approx(z) assert np.allclose(self.ops.mult(X, Y), Z) def test_mult_inplace(self): X = np.array([0, 1, 0, -1, 0, 1, -1, 1, -1, 2, 2, 2]) Y = np.array([0, 0, 1, 0, -1, -1, 1, 1, -1, 1, 2, -2]) Z = np.array([0, 0, 0, 0, 0, -1, -1, 1, 1, 2, 4, -4]) self.ops.mult_inplace(X, Y) assert np.allclose(X, Z) def test_invert(self): X = np.array([1, 2, -1, 10], dtype=float) Y = np.array([1, 1 / 2, -1, 1 / 10]) for x, y in zip(X, Y): assert self.ops.invert(x) == pytest.approx(y) def test_mult_reduce(self): prods = [1, 2, 6, 24, 120] for i, p in enumerate(prods): assert self.ops.mult_reduce(np.arange(1, i + 2)) == pytest.approx(p) def test_normalize(self): X = np.ones(3) Y = self.ops.normalize(X) Y_ = X / 3 assert np.allclose(Y, Y_)
def setUp(self): self.ops = LinearOperations()
class TestLinear(object): def setUp(self): self.ops = LinearOperations() def test_add(self): X = np.array([0, 1, 0, -1, 0, 1, -1, 1, -1]) Y = np.array([0, 0, 1, 0, -1, -1, 1, 1, -1]) Z = np.array([0, 1, 1, -1, -1, 0, 0, 2, -2]) for x, y, z in zip(X, Y, Z): assert_almost_equal(self.ops.add(x, y), z) npt.assert_allclose(self.ops.add(X, Y), Z) def test_add_inplace(self): X = np.array([0, 1, 0, -1, 0, 1, -1, 1, -1]) Y = np.array([0, 0, 1, 0, -1, -1, 1, 1, -1]) Z = np.array([0, 1, 1, -1, -1, 0, 0, 2, -2]) self.ops.add_inplace(X, Y) npt.assert_allclose(X, Z) def test_add_reduce(self): X = np.array([[0, 0, 0], [0, 1, 2], [1, 1, 1], [-1, 0, 1], [2, 0, -2], [-1, -1, -1]]) Y = np.array([0, 3, 3, 0, 0, -3]) for x, y in zip(X, Y): assert_almost_equal(self.ops.add_reduce(x), y) def test_mult(self): X = np.array([0, 1, 0, -1, 0, 1, -1, 1, -1, 2, 2, 2]) Y = np.array([0, 0, 1, 0, -1, -1, 1, 1, -1, 1, 2, -2]) Z = np.array([0, 0, 0, 0, 0, -1, -1, 1, 1, 2, 4, -4]) for x, y, z in zip(X, Y, Z): assert_almost_equal(self.ops.mult(x, y), z) npt.assert_allclose(self.ops.mult(X, Y), Z) def test_mult_inplace(self): X = np.array([0, 1, 0, -1, 0, 1, -1, 1, -1, 2, 2, 2]) Y = np.array([0, 0, 1, 0, -1, -1, 1, 1, -1, 1, 2, -2]) Z = np.array([0, 0, 0, 0, 0, -1, -1, 1, 1, 2, 4, -4]) self.ops.mult_inplace(X, Y) npt.assert_allclose(X, Z) def test_invert(self): X = np.array([1, 2, -1, 10], dtype=float) Y = np.array([1, 1/2, -1, 1/10]) for x, y in zip(X, Y): assert_almost_equal(self.ops.invert(x), y) def test_mult_reduce(self): prods = [1, 2, 6, 24, 120] for i, p in enumerate(prods): assert_almost_equal(self.ops.mult_reduce(np.arange(1, i+2)), p) def test_normalize(self): X = np.ones(3) Y = self.ops.normalize(X) Y_ = X / 3 npt.assert_allclose(Y, Y_)
def test_linear_invert(): ops = LinearOperations() assert_almost_equal(ops.invert(1), 1) assert_almost_equal(ops.invert(2.0), 1/2) assert_almost_equal(ops.invert(-1), -1) assert_almost_equal(ops.invert(10.0), 1/10)