Example #1
0
    def test_ScaledBlockOperatorSingleScalar(self):
        ig = [ ImageGeometry(10,20,30) , \
               ImageGeometry(10,20,30) , \
               ImageGeometry(10,20,30) ]
        x = [g.allocate() for g in ig]
        ops = [IdentityOperator(g) for g in ig]

        val = 1
        # test limit as non Scaled
        scalar = 1
        k = BlockOperator(*ops)
        K = scalar * k
        X = BlockDataContainer(*x) + val

        Y = K.T.direct(X)
        self.assertTrue(Y.shape == (1, 1))
        zero = numpy.zeros(X.get_item(0).shape)
        xx = numpy.asarray([val for _ in x])
        numpy.testing.assert_array_equal(
            Y.get_item(0).as_array(), ((scalar * xx).sum() + zero))

        scalar = 0.5
        k = BlockOperator(*ops)
        K = scalar * k
        X = BlockDataContainer(*x) + 1

        Y = K.T.direct(X)
        self.assertTrue(Y.shape == (1, 1))
        zero = numpy.zeros(X.get_item(0).shape)
        numpy.testing.assert_array_equal(
            Y.get_item(0).as_array(), scalar * (len(x) + zero))
Example #2
0
    def test_ScaledBlockOperatorScalarList(self):
        ig = [ ImageGeometry(2,3) , \
               #ImageGeometry(10,20,30) , \
               ImageGeometry(2,3    ) ]
        x = [g.allocate() for g in ig]
        ops = [IdentityOperator(g) for g in ig]

        # test limit as non Scaled
        scalar = numpy.asarray([1 for _ in x])
        k = BlockOperator(*ops)
        K = scalar * k
        val = 1
        X = BlockDataContainer(*x) + val

        Y = K.T.direct(X)
        self.assertTrue(Y.shape == (1, 1))
        zero = numpy.zeros(X.get_item(0).shape)
        xx = numpy.asarray([val for _ in x])
        numpy.testing.assert_array_equal(
            Y.get_item(0).as_array(), (scalar * xx).sum() + zero)

        scalar = numpy.asarray([i + 1 for i, el in enumerate(x)])
        #scalar = numpy.asarray([6,0])
        k = BlockOperator(*ops)
        K = scalar * k
        X = BlockDataContainer(*x) + val
        Y = K.T.direct(X)
        self.assertTrue(Y.shape == (1, 1))
        zero = numpy.zeros(X.get_item(0).shape)
        xx = numpy.asarray([val for _ in x])

        numpy.testing.assert_array_equal(
            Y.get_item(0).as_array(), (scalar * xx).sum() + zero)
Example #3
0
    def test_axpby4(self):
        # test axpby with nested BlockDataContainer
        ig0 = ImageGeometry(2,3,4)
        ig1 = ImageGeometry(2,3,5)
        
        data0 = ig0.allocate(-1)
        data2 = ig0.allocate(1)

        # data1 = ig0.allocate(2)
        data3 = ig1.allocate(3)
        
        cp0 = BlockDataContainer(data0,data2)
        cp1 = BlockDataContainer(cp0 *0. +  [2, -2], data3)
        print (cp1.get_item(0).get_item(0).as_array())
        print (cp1.get_item(0).get_item(1).as_array())
        print (cp1.get_item(1).as_array())
        print ("###############################")
        


        out = cp1 * 0. 
        cp2 = out + [1,3]


        print (cp2.get_item(0).get_item(0).as_array())
        print (cp2.get_item(0).get_item(1).as_array())
        print (cp2.get_item(1).as_array())

        cp2.axpby(3,-2, cp1 ,out, num_threads=4)

        # output should be [ [ -1 , 7 ] , 3]
        res0 = ig0.allocate(-1)
        res2 = ig0.allocate(7)
        res3 = ig1.allocate(3)
        res = BlockDataContainer(BlockDataContainer(res0, res2), res3)

        # print ("res0", res0.as_array())
        # print ("res2", res2.as_array())

        print ("###############################")
        
        # print ("out_0", out.get_item(0).as_array())
        # print ("out_1", out.get_item(1).as_array())
        self.assertBlockDataContainerEqual(out, res)
Example #4
0
    def direct(self, x, out=None):
        '''Direct operation for the BlockOperator

        BlockOperator work on BlockDataContainer, but they will work on DataContainers
        and inherited classes by simple wrapping the input in a BlockDataContainer of shape (1,1)
        '''

        if not isinstance(x, BlockDataContainer):
            x_b = BlockDataContainer(x)
        else:
            x_b = x
        shape = self.get_output_shape(x_b.shape)
        res = []

        if out is None:

            for row in range(self.shape[0]):
                for col in range(self.shape[1]):
                    if col == 0:
                        prod = self.get_item(row,
                                             col).direct(x_b.get_item(col))
                    else:
                        prod += self.get_item(row,
                                              col).direct(x_b.get_item(col))
                res.append(prod)
            return BlockDataContainer(*res, shape=shape)

        else:

            tmp = self.range_geometry().allocate()
            for row in range(self.shape[0]):
                for col in range(self.shape[1]):
                    if col == 0:
                        self.get_item(row, col).direct(x_b.get_item(col),
                                                       out=out.get_item(row))
                    else:
                        a = out.get_item(row)
                        self.get_item(row, col).direct(x_b.get_item(col),
                                                       out=tmp.get_item(row))
                        a += tmp.get_item(row)
Example #5
0
    def adjoint(self, x, out=None):
        '''Adjoint operation for the BlockOperator

        BlockOperator may contain both LinearOperator and Operator
        This method exists in BlockOperator as it is not known what type of
        Operator it will contain.

        BlockOperator work on BlockDataContainer, but they will work on DataContainers
        and inherited classes by simple wrapping the input in a BlockDataContainer of shape (1,1)

        Raises: ValueError if the contained Operators are not linear
        '''
        if not self.is_linear():
            raise ValueError('Not all operators in Block are linear.')
        if not isinstance(x, BlockDataContainer):
            x_b = BlockDataContainer(x)
        else:
            x_b = x
        shape = self.get_output_shape(x_b.shape, adjoint=True)
        if out is None:
            res = []
            for col in range(self.shape[1]):
                for row in range(self.shape[0]):
                    if row == 0:
                        prod = self.get_item(row,
                                             col).adjoint(x_b.get_item(row))
                    else:
                        prod += self.get_item(row,
                                              col).adjoint(x_b.get_item(row))
                res.append(prod)
            if self.shape[1] == 1:
                # the output is a single DataContainer, so we can take it out
                return res[0]
            else:
                return BlockDataContainer(*res, shape=shape)
        else:

            for col in range(self.shape[1]):
                for row in range(self.shape[0]):
                    if row == 0:
                        if issubclass(out.__class__, DataContainer) or \
                ( has_sirf and issubclass(out.__class__, SIRFDataContainer) ):
                            self.get_item(row, col).adjoint(x_b.get_item(row),
                                                            out=out)
                        else:
                            op = self.get_item(row, col)
                            self.get_item(row,
                                          col).adjoint(x_b.get_item(row),
                                                       out=out.get_item(col))
                    else:
                        if issubclass(out.__class__, DataContainer) or \
                ( has_sirf and issubclass(out.__class__, SIRFDataContainer) ):
                            out += self.get_item(row, col).adjoint(
                                x_b.get_item(row))
                        else:
                            a = out.get_item(col)
                            a += self.get_item(row, col).adjoint(
                                x_b.get_item(row), )
Example #6
0
    def test_BlockOperator(self):
        print("test_BlockOperator")
        ig = [ ImageGeometry(10,20,30) , \
               ImageGeometry(10,20,30) , \
               ImageGeometry(10,20,30) ]
        x = [g.allocate() for g in ig]
        ops = [IdentityOperator(g) for g in ig]

        K = BlockOperator(*ops)
        X = BlockDataContainer(x[0])
        Y = K.direct(X)
        self.assertTrue(Y.shape == K.shape)

        numpy.testing.assert_array_equal(
            Y.get_item(0).as_array(),
            X.get_item(0).as_array())
        numpy.testing.assert_array_equal(
            Y.get_item(1).as_array(),
            X.get_item(0).as_array())
        #numpy.testing.assert_array_equal(Y.get_item(2).as_array(),X.get_item(2).as_array())

        X = BlockDataContainer(*x) + 1
        Y = K.T.direct(X)
        # K.T (1,3) X (3,1) => output shape (1,1)
        self.assertTrue(Y.shape == (1, 1))
        zero = numpy.zeros(X.get_item(0).shape)
        numpy.testing.assert_array_equal(
            Y.get_item(0).as_array(),
            len(x) + zero)

        K2 = BlockOperator(*(ops + ops), shape=(3, 2))
        Y = K2.T.direct(X)
        # K.T (2,3) X (3,1) => output shape (2,1)
        self.assertTrue(Y.shape == (2, 1))

        try:
            # this should fail as the domain is not compatible
            ig = [ ImageGeometry(10,20,31) , \
                ImageGeometry(10,20,30) , \
                ImageGeometry(10,20,30) ]
            x = [g.allocate() for g in ig]
            ops = [IdentityOperator(g) for g in ig]

            K = BlockOperator(*ops)
            self.assertFalse(K.column_wise_compatible())
        except ValueError as ve:
            print(ve)
            self.assertTrue(True)

        try:
            # this should fail as the range is not compatible
            ig = [ ImageGeometry(10,20,30) , \
                ImageGeometry(10,20,30) , \
                ImageGeometry(10,20,30) ]
            rg0 = [ ImageGeometry(10,20,31) , \
                ImageGeometry(10,20,31) , \
                ImageGeometry(10,20,31) ]
            rg1 = [ ImageGeometry(10,22,31) , \
                   ImageGeometry(10,22,31) , \
                   ImageGeometry(10,20,31) ]
            x = [g.allocate() for g in ig]
            ops = [
                IdentityOperator(g, range_geometry=r) for g, r in zip(ig, rg0)
            ]
            ops += [
                IdentityOperator(g, range_geometry=r) for g, r in zip(ig, rg1)
            ]

            K = BlockOperator(*ops, shape=(2, 3))
            print("K col comp? ", K.column_wise_compatible())
            print("K row comp? ", K.row_wise_compatible())
            for op in ops:
                print("range", op.range_geometry().shape)
            for op in ops:
                print("domain", op.domain_geometry().shape)
            self.assertFalse(K.row_wise_compatible())
        except ValueError as ve:
            print(ve)
            self.assertTrue(True)
Example #7
0
    def test_Nested_BlockDataContainer(self):
        ig0 = ImageGeometry(2, 3, 4)
        ig1 = ImageGeometry(2, 3, 4)

        # data0 = ImageData(geometry=ig0)
        # data1 = ImageData(geometry=ig1) + 1

        # data2 = ImageData(geometry=ig0) + 2
        # data3 = ImageData(geometry=ig1) + 3
        data0 = ig0.allocate(0.)
        data1 = ig1.allocate(1.)

        data2 = ig0.allocate(2.)
        data3 = ig1.allocate(3.)

        cp0 = BlockDataContainer(data0, data1)
        cp1 = BlockDataContainer(data2, data3)

        nbdc = BlockDataContainer(cp0, cp1)
        nbdc2 = nbdc + 2
        numpy.testing.assert_almost_equal(
            nbdc2.get_item(0).get_item(0).as_array()[0][0][0], 2., decimal=5)
        numpy.testing.assert_almost_equal(
            nbdc2.get_item(0).get_item(1).as_array()[0][0][0], 3., decimal=5)
        numpy.testing.assert_almost_equal(
            nbdc2.get_item(1).get_item(0).as_array()[0][0][0], 4., decimal=5)
        numpy.testing.assert_almost_equal(
            nbdc2.get_item(1).get_item(1).as_array()[0][0][0], 5., decimal=5)

        nbdc2 = 2 + nbdc
        numpy.testing.assert_almost_equal(
            nbdc2.get_item(0).get_item(0).as_array()[0][0][0], 2., decimal=5)
        numpy.testing.assert_almost_equal(
            nbdc2.get_item(0).get_item(1).as_array()[0][0][0], 3., decimal=5)
        numpy.testing.assert_almost_equal(
            nbdc2.get_item(1).get_item(0).as_array()[0][0][0], 4., decimal=5)
        numpy.testing.assert_almost_equal(
            nbdc2.get_item(1).get_item(1).as_array()[0][0][0], 5., decimal=5)

        nbdc2 = nbdc * 2
        numpy.testing.assert_almost_equal(
            nbdc2.get_item(0).get_item(0).as_array()[0][0][0], 0., decimal=5)
        numpy.testing.assert_almost_equal(
            nbdc2.get_item(0).get_item(1).as_array()[0][0][0], 2., decimal=5)
        numpy.testing.assert_almost_equal(
            nbdc2.get_item(1).get_item(0).as_array()[0][0][0], 4., decimal=5)
        numpy.testing.assert_almost_equal(
            nbdc2.get_item(1).get_item(1).as_array()[0][0][0], 6., decimal=5)

        nbdc2 = 2 * nbdc
        numpy.testing.assert_almost_equal(
            nbdc2.get_item(0).get_item(0).as_array()[0][0][0], 0., decimal=5)
        numpy.testing.assert_almost_equal(
            nbdc2.get_item(0).get_item(1).as_array()[0][0][0], 2., decimal=5)
        numpy.testing.assert_almost_equal(
            nbdc2.get_item(1).get_item(0).as_array()[0][0][0], 4., decimal=5)
        numpy.testing.assert_almost_equal(
            nbdc2.get_item(1).get_item(1).as_array()[0][0][0], 6., decimal=5)

        nbdc2 = nbdc / 2
        numpy.testing.assert_almost_equal(
            nbdc2.get_item(0).get_item(0).as_array()[0][0][0], 0., decimal=5)
        numpy.testing.assert_almost_equal(
            nbdc2.get_item(0).get_item(1).as_array()[0][0][0], .5, decimal=5)
        numpy.testing.assert_almost_equal(
            nbdc2.get_item(1).get_item(0).as_array()[0][0][0], 1., decimal=5)
        numpy.testing.assert_almost_equal(
            nbdc2.get_item(1).get_item(1).as_array()[0][0][0],
            3. / 2,
            decimal=5)

        c5 = nbdc.get_item(0).power(2).sum()
        c5a = nbdc.power(2).sum()

        cp0 = BlockDataContainer(data0, data2)
        a = cp0 * data2
        b = data2 * cp0
        self.assertBlockDataContainerEqual(a, b)
Example #8
0
    def test_BlockDataContainer(self):
        ig0 = ImageGeometry(2, 3, 4)
        ig1 = ImageGeometry(2, 3, 5)

        # data0 = ImageData(geometry=ig0)
        # data1 = ImageData(geometry=ig1) + 1
        data0 = ig0.allocate(0.)
        data1 = ig1.allocate(1.)

        # data2 = ImageData(geometry=ig0) + 2
        # data3 = ImageData(geometry=ig1) + 3
        data2 = ig0.allocate(2.)
        data3 = ig1.allocate(3.)

        cp0 = BlockDataContainer(data0, data1)
        cp1 = BlockDataContainer(data2, data3)

        cp2 = BlockDataContainer(data0 + 1, data2 + 1)
        d = cp2 + data0
        self.assertEqual(d.get_item(0).as_array()[0][0][0], 1)
        try:
            d = cp2 + data1
            self.assertTrue(False)
        except ValueError as ve:
            self.assertTrue(True)
        d = cp2 - data0
        self.assertEqual(d.get_item(0).as_array()[0][0][0], 1)
        try:
            d = cp2 - data1
            self.assertTrue(False)
        except ValueError as ve:
            self.assertTrue(True)
        d = cp2 * data2
        self.assertEqual(d.get_item(0).as_array()[0][0][0], 2)
        try:
            d = cp2 * data1
            self.assertTrue(False)
        except ValueError as ve:
            self.assertTrue(True)

        a = [(el, ot) for el, ot in zip(cp0.containers, cp1.containers)]
        #cp2 = BlockDataContainer(*a)
        cp2 = cp0.add(cp1)
        self.assertEqual(cp2.get_item(0).as_array()[0][0][0], 2.)
        self.assertEqual(cp2.get_item(1).as_array()[0][0][0], 4.)

        cp2 = cp0 + cp1
        self.assertTrue(cp2.get_item(0).as_array()[0][0][0] == 2.)
        self.assertTrue(cp2.get_item(1).as_array()[0][0][0] == 4.)
        cp2 = cp0 + 1
        numpy.testing.assert_almost_equal(cp2.get_item(0).as_array()[0][0][0],
                                          1.,
                                          decimal=5)
        numpy.testing.assert_almost_equal(cp2.get_item(1).as_array()[0][0][0],
                                          2.,
                                          decimal=5)
        cp2 = cp0 + [1, 2]
        numpy.testing.assert_almost_equal(cp2.get_item(0).as_array()[0][0][0],
                                          1.,
                                          decimal=5)
        numpy.testing.assert_almost_equal(cp2.get_item(1).as_array()[0][0][0],
                                          3.,
                                          decimal=5)
        cp2 += cp1
        numpy.testing.assert_almost_equal(cp2.get_item(0).as_array()[0][0][0],
                                          +3.,
                                          decimal=5)
        numpy.testing.assert_almost_equal(cp2.get_item(1).as_array()[0][0][0],
                                          +6.,
                                          decimal=5)

        cp2 += 1
        numpy.testing.assert_almost_equal(cp2.get_item(0).as_array()[0][0][0],
                                          +4.,
                                          decimal=5)
        numpy.testing.assert_almost_equal(cp2.get_item(1).as_array()[0][0][0],
                                          +7.,
                                          decimal=5)

        cp2 += [-2, -1]
        numpy.testing.assert_almost_equal(cp2.get_item(0).as_array()[0][0][0],
                                          2.,
                                          decimal=5)
        numpy.testing.assert_almost_equal(cp2.get_item(1).as_array()[0][0][0],
                                          6.,
                                          decimal=5)

        cp2 = cp0.subtract(cp1)
        assert (cp2.get_item(0).as_array()[0][0][0] == -2.)
        assert (cp2.get_item(1).as_array()[0][0][0] == -2.)
        cp2 = cp0 - cp1
        assert (cp2.get_item(0).as_array()[0][0][0] == -2.)
        assert (cp2.get_item(1).as_array()[0][0][0] == -2.)

        cp2 = cp0 - 1
        numpy.testing.assert_almost_equal(cp2.get_item(0).as_array()[0][0][0],
                                          -1.,
                                          decimal=5)
        numpy.testing.assert_almost_equal(cp2.get_item(1).as_array()[0][0][0],
                                          0,
                                          decimal=5)
        cp2 = cp0 - [1, 2]
        numpy.testing.assert_almost_equal(cp2.get_item(0).as_array()[0][0][0],
                                          -1.,
                                          decimal=5)
        numpy.testing.assert_almost_equal(cp2.get_item(1).as_array()[0][0][0],
                                          -1.,
                                          decimal=5)

        cp2 -= cp1
        numpy.testing.assert_almost_equal(cp2.get_item(0).as_array()[0][0][0],
                                          -3.,
                                          decimal=5)
        numpy.testing.assert_almost_equal(cp2.get_item(1).as_array()[0][0][0],
                                          -4.,
                                          decimal=5)

        cp2 -= 1
        numpy.testing.assert_almost_equal(cp2.get_item(0).as_array()[0][0][0],
                                          -4.,
                                          decimal=5)
        numpy.testing.assert_almost_equal(cp2.get_item(1).as_array()[0][0][0],
                                          -5.,
                                          decimal=5)

        cp2 -= [-2, -1]
        numpy.testing.assert_almost_equal(cp2.get_item(0).as_array()[0][0][0],
                                          -2.,
                                          decimal=5)
        numpy.testing.assert_almost_equal(cp2.get_item(1).as_array()[0][0][0],
                                          -4.,
                                          decimal=5)

        cp2 = cp0.multiply(cp1)
        assert (cp2.get_item(0).as_array()[0][0][0] == 0.)
        assert (cp2.get_item(1).as_array()[0][0][0] == 3.)
        cp2 = cp0 * cp1
        assert (cp2.get_item(0).as_array()[0][0][0] == 0.)
        assert (cp2.get_item(1).as_array()[0][0][0] == 3.)

        cp2 = cp0 * 2
        numpy.testing.assert_almost_equal(cp2.get_item(0).as_array()[0][0][0],
                                          0.,
                                          decimal=5)
        numpy.testing.assert_almost_equal(cp2.get_item(1).as_array()[0][0][0],
                                          2,
                                          decimal=5)
        cp2 = 2 * cp0
        numpy.testing.assert_almost_equal(cp2.get_item(0).as_array()[0][0][0],
                                          0.,
                                          decimal=5)
        numpy.testing.assert_almost_equal(cp2.get_item(1).as_array()[0][0][0],
                                          2,
                                          decimal=5)
        cp2 = cp0 * [3, 2]
        numpy.testing.assert_almost_equal(cp2.get_item(0).as_array()[0][0][0],
                                          0.,
                                          decimal=5)
        numpy.testing.assert_almost_equal(cp2.get_item(1).as_array()[0][0][0],
                                          2.,
                                          decimal=5)
        cp2 = cp0 * numpy.asarray([3, 2])
        numpy.testing.assert_almost_equal(cp2.get_item(0).as_array()[0][0][0],
                                          0.,
                                          decimal=5)
        numpy.testing.assert_almost_equal(cp2.get_item(1).as_array()[0][0][0],
                                          2.,
                                          decimal=5)

        cp2 = [3, 2] * cp0
        numpy.testing.assert_almost_equal(cp2.get_item(0).as_array()[0][0][0],
                                          0.,
                                          decimal=5)
        numpy.testing.assert_almost_equal(cp2.get_item(1).as_array()[0][0][0],
                                          2.,
                                          decimal=5)
        cp2 = numpy.asarray([3, 2]) * cp0
        numpy.testing.assert_almost_equal(cp2.get_item(0).as_array()[0][0][0],
                                          0.,
                                          decimal=5)
        numpy.testing.assert_almost_equal(cp2.get_item(1).as_array()[0][0][0],
                                          2.,
                                          decimal=5)

        try:
            cp2 = [3, 2, 3] * cp0
            #numpy.testing.assert_almost_equal(cp2.get_item(0).as_array()[0][0][0] , 0. , decimal=5)
            #numpy.testing.assert_almost_equal(cp2.get_item(1).as_array()[0][0][0] , 2., decimal = 5)
            self.assertTrue(False)
        except ValueError as ve:
            self.assertTrue(True)
        cp2 *= cp1
        numpy.testing.assert_almost_equal(cp2.get_item(0).as_array()[0][0][0],
                                          0,
                                          decimal=5)
        numpy.testing.assert_almost_equal(cp2.get_item(1).as_array()[0][0][0],
                                          +6.,
                                          decimal=5)

        cp2 *= 1
        numpy.testing.assert_almost_equal(cp2.get_item(0).as_array()[0][0][0],
                                          0.,
                                          decimal=5)
        numpy.testing.assert_almost_equal(cp2.get_item(1).as_array()[0][0][0],
                                          +6.,
                                          decimal=5)

        cp2 *= [-2, -1]
        numpy.testing.assert_almost_equal(cp2.get_item(0).as_array()[0][0][0],
                                          0.,
                                          decimal=5)
        numpy.testing.assert_almost_equal(cp2.get_item(1).as_array()[0][0][0],
                                          -6.,
                                          decimal=5)

        try:
            cp2 *= [2, 3, 5]
            self.assertTrue(False)
        except ValueError as ve:
            self.assertTrue(True)

        cp2 = cp0.divide(cp1)
        assert (cp2.get_item(0).as_array()[0][0][0] == 0.)
        numpy.testing.assert_almost_equal(cp2.get_item(1).as_array()[0][0][0],
                                          1. / 3.,
                                          decimal=4)
        cp2 = cp0 / cp1
        assert (cp2.get_item(0).as_array()[0][0][0] == 0.)
        numpy.testing.assert_almost_equal(cp2.get_item(1).as_array()[0][0][0],
                                          1. / 3.,
                                          decimal=4)

        cp2 = cp0 / 2
        numpy.testing.assert_almost_equal(cp2.get_item(0).as_array()[0][0][0],
                                          0.,
                                          decimal=5)
        numpy.testing.assert_almost_equal(cp2.get_item(1).as_array()[0][0][0],
                                          0.5,
                                          decimal=5)
        cp2 = cp0 / [3, 2]
        numpy.testing.assert_almost_equal(cp2.get_item(0).as_array()[0][0][0],
                                          0.,
                                          decimal=5)
        numpy.testing.assert_almost_equal(cp2.get_item(1).as_array()[0][0][0],
                                          0.5,
                                          decimal=5)
        cp2 = cp0 / numpy.asarray([3, 2])
        numpy.testing.assert_almost_equal(cp2.get_item(0).as_array()[0][0][0],
                                          0.,
                                          decimal=5)
        numpy.testing.assert_almost_equal(cp2.get_item(1).as_array()[0][0][0],
                                          0.5,
                                          decimal=5)
        cp3 = numpy.asarray([3, 2]) / (cp0 + 1)
        numpy.testing.assert_almost_equal(cp3.get_item(0).as_array()[0][0][0],
                                          3.,
                                          decimal=5)
        numpy.testing.assert_almost_equal(cp3.get_item(1).as_array()[0][0][0],
                                          1,
                                          decimal=5)

        cp2 += 1
        cp2 /= cp1
        # TODO fix inplace division

        numpy.testing.assert_almost_equal(cp2.get_item(0).as_array()[0][0][0],
                                          1. / 2,
                                          decimal=5)
        numpy.testing.assert_almost_equal(cp2.get_item(1).as_array()[0][0][0],
                                          1.5 / 3.,
                                          decimal=5)

        cp2 /= 1
        numpy.testing.assert_almost_equal(cp2.get_item(0).as_array()[0][0][0],
                                          0.5,
                                          decimal=5)
        numpy.testing.assert_almost_equal(cp2.get_item(1).as_array()[0][0][0],
                                          0.5,
                                          decimal=5)

        cp2 /= [-2, -1]
        numpy.testing.assert_almost_equal(cp2.get_item(0).as_array()[0][0][0],
                                          -0.5 / 2.,
                                          decimal=5)
        numpy.testing.assert_almost_equal(cp2.get_item(1).as_array()[0][0][0],
                                          -0.5,
                                          decimal=5)
        ####

        cp2 = cp0.power(cp1)
        assert (cp2.get_item(0).as_array()[0][0][0] == 0.)
        numpy.testing.assert_almost_equal(cp2.get_item(1).as_array()[0][0][0],
                                          1.,
                                          decimal=4)
        cp2 = cp0**cp1
        assert (cp2.get_item(0).as_array()[0][0][0] == 0.)
        numpy.testing.assert_almost_equal(cp2.get_item(1).as_array()[0][0][0],
                                          1.,
                                          decimal=4)

        cp2 = cp0**2
        numpy.testing.assert_almost_equal(cp2.get_item(0).as_array()[0][0][0],
                                          0.,
                                          decimal=5)
        numpy.testing.assert_almost_equal(cp2.get_item(1).as_array()[0][0][0],
                                          1.,
                                          decimal=5)

        cp2 = cp0.maximum(cp1)
        assert (cp2.get_item(0).as_array()[0][0][0] == cp1.get_item(
            0).as_array()[0][0][0])
        numpy.testing.assert_almost_equal(cp2.get_item(1).as_array()[0][0][0],
                                          cp2.get_item(1).as_array()[0][0][0],
                                          decimal=4)

        cp2 = cp0.abs()
        numpy.testing.assert_almost_equal(cp2.get_item(0).as_array()[0][0][0],
                                          0.,
                                          decimal=4)
        numpy.testing.assert_almost_equal(cp2.get_item(1).as_array()[0][0][0],
                                          1.,
                                          decimal=4)

        cp2 = cp0.subtract(cp1)
        s = cp2.sign()
        numpy.testing.assert_almost_equal(s.get_item(0).as_array()[0][0][0],
                                          -1.,
                                          decimal=4)
        numpy.testing.assert_almost_equal(s.get_item(1).as_array()[0][0][0],
                                          -1.,
                                          decimal=4)

        cp2 = cp0.add(cp1)
        s = cp2.sqrt()
        numpy.testing.assert_almost_equal(s.get_item(0).as_array()[0][0][0],
                                          numpy.sqrt(2),
                                          decimal=4)
        numpy.testing.assert_almost_equal(s.get_item(1).as_array()[0][0][0],
                                          numpy.sqrt(4),
                                          decimal=4)

        s = cp0.sum()
        size = functools.reduce(lambda x, y: x * y, data1.shape, 1)
        numpy.testing.assert_almost_equal(s, 0 + size, decimal=4)
        s0 = 1
        s1 = 1
        for i in cp0.get_item(0).shape:
            s0 *= i
        for i in cp0.get_item(1).shape:
            s1 *= i