예제 #1
0
    def test_index(self):
        """Test indexing/slicing into coefficients.
        """
        # Index.
        sum_coeffs = cu.add(self.x.coefficients(), self.y.coefficients())
        key = ku.validate_key((1, 0), self.x.shape)
        coeffs = cu.index(sum_coeffs, key)
        self.assertItemsEqual(coeffs.keys(), [self.x, self.y])
        blocks = coeffs[self.y]
        self.assertEqual(len(blocks), 1)
        self.assertEqual(blocks[0].size, (1,2))
        self.assertEqual(blocks[0][0,0], 0)

        # Slice.
        sum_coeffs = cu.add(self.A.coefficients(), self.C.coefficients())
        key = ku.validate_key((slice(None, None, None), 1), self.A.shape)
        coeffs = cu.index(sum_coeffs, key)
        self.assertItemsEqual(coeffs.keys(), [self.A, s.CONSTANT])
        # Variable.
        blocks = coeffs[self.A]
        self.assertEqual(len(blocks), 1)
        self.assertEqual(blocks[0].size, (2,4))
        # Constant.
        blocks = coeffs[s.CONSTANT]
        self.assertEqual(len(blocks), 1)
        self.assertEqual(blocks[0].size, (2,1))
예제 #2
0
    def test_mul(self):
        """Test multiplying coefficients.
        """
        coeffs = cu.add(self.x.coefficients(), self.y.coefficients())
        coeffs = cu.mul(self.C.coefficients(), coeffs)
        self.assertItemsEqual(coeffs.keys(), [self.x, self.y])
        blocks = coeffs[self.y]
        self.assertEqual(len(blocks), 1)
        self.assertEqual(blocks[0].size, (2,2))
        self.assertEqual(blocks[0][1,0], 2)

        # Scalar by Matrix multiplication.
        coeffs = cu.add(self.x.coefficients(), self.y.coefficients())
        coeffs = cu.mul(self.c.coefficients(), coeffs)
        self.assertItemsEqual(coeffs.keys(), [self.x, self.y])
        blocks = coeffs[self.y]
        self.assertEqual(len(blocks), 1)
        self.assertEqual(blocks[0].size, (2,2))
        self.assertEqual(blocks[0][0,0], 3)

        # Matrix by Scalar multiplication.
        coeffs = self.a.coefficients()
        coeffs = cu.mul(self.C.coefficients(), coeffs)
        self.assertItemsEqual(coeffs.keys(), [self.a])
        blocks = coeffs[self.a]
        self.assertEqual(len(blocks), 2)
        self.assertEqual(blocks[0].size, (2,1))
        self.assertEqual(blocks[0][1,0], 2)
        self.assertEqual(blocks[1].size, (2,1))
        self.assertEqual(blocks[1][1,0], 2)
예제 #3
0
    def test_index(self):
        """Test indexing/slicing into coefficients.
        """
        # Index.
        sum_coeffs = cu.add(self.x.coefficients(), self.y.coefficients())
        key = ku.validate_key((1, 0), self.x.shape)
        coeffs = cu.index(sum_coeffs, key)
        self.assertItemsEqual(coeffs.keys(), [self.x, self.y])
        blocks = coeffs[self.y]
        self.assertEqual(len(blocks), 1)
        self.assertEqual(blocks[0].shape, (1, 2))
        self.assertEqual(blocks[0][0, 0], 0)

        # Slice.
        sum_coeffs = cu.add(self.A.coefficients(), self.C.coefficients())
        key = ku.validate_key((slice(None, None, None), 1), self.A.shape)
        coeffs = cu.index(sum_coeffs, key)
        self.assertItemsEqual(coeffs.keys(), [self.A, s.CONSTANT])
        # Variable.
        blocks = coeffs[self.A]
        self.assertEqual(len(blocks), 1)
        self.assertEqual(blocks[0].shape, (2, 4))
        # Constant.
        blocks = coeffs[s.CONSTANT]
        self.assertEqual(len(blocks), 1)
        self.assertEqual(blocks[0].shape, (2, 1))
예제 #4
0
    def test_mul(self):
        """Test multiplying coefficients.
        """
        coeffs = cu.add(self.x.coefficients(), self.y.coefficients())
        coeffs = cu.mul(self.C.coefficients(), coeffs)
        self.assertItemsEqual(coeffs.keys(), [self.x, self.y])
        blocks = coeffs[self.y]
        self.assertEqual(len(blocks), 1)
        self.assertEqual(blocks[0].shape, (2, 2))
        self.assertEqual(blocks[0][1, 0], 2)

        # Scalar by Matrix multiplication.
        coeffs = cu.add(self.x.coefficients(), self.y.coefficients())
        coeffs = cu.mul(self.c.coefficients(), coeffs)
        self.assertItemsEqual(coeffs.keys(), [self.x, self.y])
        blocks = coeffs[self.y]
        self.assertEqual(len(blocks), 1)
        self.assertEqual(blocks[0].shape, (2, 2))
        self.assertEqual(blocks[0][0, 0], 3)

        # Matrix by Scalar multiplication.
        coeffs = self.a.coefficients()
        coeffs = cu.mul(self.C.coefficients(), coeffs)
        self.assertItemsEqual(coeffs.keys(), [self.a])
        blocks = coeffs[self.a]
        self.assertEqual(len(blocks), 2)
        self.assertEqual(blocks[0].shape, (2, 1))
        self.assertEqual(blocks[0][1, 0], 2)
        self.assertEqual(blocks[1].shape, (2, 1))
        self.assertEqual(blocks[1][1, 0], 2)
예제 #5
0
    def test_add(self):
        """Test adding coefficients.
        """
        coeffs = cu.add(self.x.coefficients(), self.y.coefficients())
        self.assertItemsEqual(coeffs.keys(), [self.x, self.y])
        blocks = coeffs[self.x]
        self.assertEqual(len(blocks), 1)
        self.assertEqual(blocks[0].size, (2,2))

        coeffs = cu.add(coeffs, coeffs)
        self.assertItemsEqual(coeffs.keys(), [self.x, self.y])
        blocks = coeffs[self.x]
        self.assertEqual(len(blocks), 1)
        self.assertEqual(blocks[0].size, (2,2))
        self.assertEqual(blocks[0][0,0], 2)

        coeffs = cu.add(coeffs, self.C.coefficients())
        self.assertItemsEqual(coeffs.keys(), [self.x, self.y, s.CONSTANT])
        blocks = coeffs[s.CONSTANT]
        self.assertEqual(len(blocks), 2)
        self.assertEqual(blocks[0].size, (2,1))
        self.assertEqual(blocks[0][0,0], 1)
예제 #6
0
    def test_add(self):
        """Test adding coefficients.
        """
        coeffs = cu.add(self.x.coefficients(), self.y.coefficients())
        self.assertItemsEqual(coeffs.keys(), [self.x, self.y])
        blocks = coeffs[self.x]
        self.assertEqual(len(blocks), 1)
        self.assertEqual(blocks[0].shape, (2, 2))

        coeffs = cu.add(coeffs, coeffs)
        self.assertItemsEqual(coeffs.keys(), [self.x, self.y])
        blocks = coeffs[self.x]
        self.assertEqual(len(blocks), 1)
        self.assertEqual(blocks[0].shape, (2, 2))
        self.assertEqual(blocks[0][0, 0], 2)

        coeffs = cu.add(coeffs, self.C.coefficients())
        self.assertItemsEqual(coeffs.keys(), [self.x, self.y, s.CONSTANT])
        blocks = coeffs[s.CONSTANT]
        self.assertEqual(len(blocks), 2)
        self.assertEqual(blocks[0].shape, (2, 1))
        self.assertEqual(blocks[0][0, 0], 1)