示例#1
0
    def test_randomize(self):
        ttOp = pitts_py.TensorTrainOperator_double([2, 4, 2], [2, 3, 3])
        self.assertEqual([1, 1], ttOp.getTTranks())

        pitts_py.randomize(ttOp)
        self.assertEqual([1, 1], ttOp.getTTranks())

        unique_numbers = np.unique(ttOp.getSubTensor(0))
        unique_numbers = np.append(unique_numbers,
                                   np.unique(ttOp.getSubTensor(1)))
        unique_numbers = np.append(unique_numbers,
                                   np.unique(ttOp.getSubTensor(2)))
        unique_numbers = np.unique(unique_numbers)
        self.assertEqual(2 * 2 + 4 * 3 + 2 * 3, unique_numbers.size)

        ttOp.setTTranks([2, 3])
        pitts_py.randomize(ttOp)
        self.assertEqual([2, 3], ttOp.getTTranks())

        unique_numbers = np.append(unique_numbers,
                                   np.unique(ttOp.getSubTensor(0)))
        unique_numbers = np.append(unique_numbers,
                                   np.unique(ttOp.getSubTensor(1)))
        unique_numbers = np.append(unique_numbers,
                                   np.unique(ttOp.getSubTensor(2)))
        self.assertEqual(
            2 * 2 + 4 * 3 + 2 * 3 + 1 * 2 * 2 * 2 + 2 * 4 * 3 * 3 +
            3 * 2 * 3 * 1, unique_numbers.size)
示例#2
0
    def test_norm(self):
        tt = pitts_py.TensorTrain_double([2, 5, 3])
        tt.setTTranks([2, 2])
        pitts_py.randomize(tt)

        np.testing.assert_almost_equal(np.sqrt(pitts_py.dot(tt, tt)),
                                       pitts_py.norm2(tt))
示例#3
0
    def test_example(self):
        pitts_py.initialize()

        # do something interesting...
        tt = pitts_py.TensorTrain_double([5,5,5])
        pitts_py.randomize(tt)

        pitts_py.finalize()
示例#4
0
    def test_apply_zero(self):
        ttOp = pitts_py.TensorTrainOperator_double([2, 3, 2], [3, 4, 1])
        ttX = pitts_py.TensorTrain_double([3, 4, 1])
        ttY = pitts_py.TensorTrain_double([2, 3, 2])

        ttOp.setZero()
        pitts_py.randomize(ttX)
        pitts_py.apply(ttOp, ttX, ttY)
        self.assertEqual(0, pitts_py.norm2(ttY))
示例#5
0
    def test_normalize(self):
        tt = pitts_py.TensorTrain_double([2, 5, 3])
        tt.setTTranks([2, 2])
        pitts_py.randomize(tt)

        norm_ref = pitts_py.norm2(tt)
        norm = pitts_py.normalize(tt)
        np.testing.assert_almost_equal(norm_ref, norm)
        np.testing.assert_almost_equal(1., pitts_py.norm2(tt))
示例#6
0
    def test_apply_identity(self):
        ttOp = pitts_py.TensorTrainOperator_double([5, 3, 3], [5, 3, 3])
        ttX = pitts_py.TensorTrain_double([5, 3, 3])
        ttY = pitts_py.TensorTrain_double([5, 3, 3])

        ttOp.setEye()
        pitts_py.randomize(ttX)
        pitts_py.apply(ttOp, ttX, ttY)
        err = pitts_py.axpby(-1, ttX, 1, ttY)
        self.assertLess(err, 1.e-8)
示例#7
0
 def test_setGetSubTensor_large(self):
     ttOp = pitts_py.TensorTrainOperator_double([10, 20, 15], [10, 15, 10])
     ttOp.setTTranks([2, 3])
     pitts_py.randomize(ttOp)
     t1_ref = np.random.rand(1, 10, 10, 2)
     t2_ref = np.random.rand(2, 20, 15, 3)
     t3_ref = np.random.rand(3, 15, 10, 1)
     ttOp.setSubTensor(0, t1_ref)
     ttOp.setSubTensor(1, t2_ref)
     ttOp.setSubTensor(2, t3_ref)
     np.testing.assert_array_almost_equal(t1_ref, ttOp.getSubTensor(0))
     np.testing.assert_array_almost_equal(t2_ref, ttOp.getSubTensor(1))
     np.testing.assert_array_almost_equal(t3_ref, ttOp.getSubTensor(2))
示例#8
0
 def test_setGetSubTensor_large(self):
     tt = pitts_py.TensorTrain_double([50, 100, 20])
     tt.setTTranks([2, 3])
     pitts_py.randomize(tt)
     t1_ref = np.random.rand(1, 50, 2)
     t2_ref = np.random.rand(2, 100, 3)
     t3_ref = np.random.rand(3, 20, 1)
     tt.setSubTensor(0, t1_ref)
     tt.setSubTensor(1, t2_ref)
     tt.setSubTensor(2, t3_ref)
     np.testing.assert_array_almost_equal(t1_ref, tt.getSubTensor(0))
     np.testing.assert_array_almost_equal(t2_ref, tt.getSubTensor(1))
     np.testing.assert_array_almost_equal(t3_ref, tt.getSubTensor(2))
示例#9
0
 def test_cdist2(self):
     X = pitts_py.MultiVector_double(20, 5)
     Y = pitts_py.MultiVector_double(20, 3)
     pitts_py.randomize(X)
     pitts_py.randomize(Y)
     dist2 = pitts_py.cdist2(X, Y)
     dist_ref = np.zeros([5, 3])
     X = np.array(X)
     Y = np.array(Y)
     for i in range(5):
         for j in range(3):
             dist_ref[i, j] = np.linalg.norm(X[:, i] - Y[:, j])
     dist = np.sqrt(dist2)
     np.testing.assert_array_almost_equal(dist_ref, dist)
示例#10
0
    def test_randomize(self):
        mv = pitts_py.MultiVector_double(20, 5)

        pitts_py.randomize(mv)
        self.assertEqual(20, mv.rows())
        self.assertEqual(5, mv.cols())
        mv1 = np.array(mv)

        pitts_py.randomize(mv)
        self.assertEqual(20, mv.rows())
        self.assertEqual(5, mv.cols())
        mv2 = np.array(mv)

        self.assertTrue(np.linalg.norm(mv1 - mv2) > 1.e-4)
示例#11
0
 def test_setGetSubTensor(self):
     tt = pitts_py.TensorTrain_double([3, 2, 5])
     tt.setTTranks([2, 3])
     pitts_py.randomize(tt)
     t1_ref = np.array([1, 2, 3, 4, 5, 6]).reshape([1, 3, 2])
     t2_ref = np.array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
                        21]).reshape([2, 2, 3])
     t3_ref = np.array([
         101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113,
         114, 115
     ]).reshape([3, 5, 1])
     tt.setSubTensor(0, t1_ref)
     tt.setSubTensor(1, t2_ref)
     tt.setSubTensor(2, t3_ref)
     np.testing.assert_array_almost_equal(t1_ref, tt.getSubTensor(0))
     np.testing.assert_array_almost_equal(t2_ref, tt.getSubTensor(1))
     np.testing.assert_array_almost_equal(t3_ref, tt.getSubTensor(2))
示例#12
0
    def test_axpby(self):
        ttOp1 = pitts_py.TensorTrainOperator_double([2, 3, 4], [3, 2, 2])
        ttOp1.setTTranks(2)
        ttOp2 = pitts_py.TensorTrainOperator_double([2, 3, 4], [3, 2, 2])

        pitts_py.randomize(ttOp1)
        pitts_py.randomize(ttOp2)

        ttOp12 = pitts_py.TensorTrainOperator_double([2, 3, 4], [3, 2, 2])
        pitts_py.copy(ttOp2, ttOp12)
        pitts_py.axpby(0.33, ttOp1, -0.97, ttOp12)

        ttX = pitts_py.TensorTrain_double([3, 2, 2])
        ttX.setTTranks(2)
        pitts_py.randomize(ttX)

        ttY = pitts_py.TensorTrain_double([2, 3, 4])
        pitts_py.apply(ttOp12, ttX, ttY)

        ttY1 = pitts_py.TensorTrain_double([2, 3, 4])
        pitts_py.apply(ttOp1, ttX, ttY1)

        ttY2 = pitts_py.TensorTrain_double([2, 3, 4])
        pitts_py.apply(ttOp2, ttX, ttY2)

        ttY12 = pitts_py.TensorTrain_double([2, 3, 4])
        pitts_py.copy(ttY2, ttY12)
        nrm = pitts_py.axpby(0.33, ttY1, -0.97, ttY12)

        err = pitts_py.axpby(-nrm, ttY12, 1., ttY)
        self.assertLess(err, 1.e-8)
示例#13
0
    def test_rightNormalize(self):
        tt = pitts_py.TensorTrain_double([2, 5, 3])
        tt.setTTranks([2, 2])
        pitts_py.randomize(tt)

        norm_ref = pitts_py.norm2(tt)
        norm = pitts_py.rightNormalize(tt)
        np.testing.assert_almost_equal(norm_ref, norm)
        np.testing.assert_almost_equal(1., pitts_py.norm2(tt))

        t1 = tt.getSubTensor(0)
        t2 = tt.getSubTensor(1)
        t3 = tt.getSubTensor(2)
        r1 = tt.getTTranks()[0]
        r2 = tt.getTTranks()[1]
        t1_mat = t1.reshape([1, t1.size // 1])
        t2_mat = t2.reshape([r1, t2.size // r1])
        t3_mat = t3.reshape([r2, t3.size // r2])
        np.testing.assert_array_almost_equal(
            np.eye(1, 1), np.dot(t1_mat, t1_mat.transpose()))
        np.testing.assert_array_almost_equal(
            np.eye(r1, r1), np.dot(t2_mat, t2_mat.transpose()))
        np.testing.assert_array_almost_equal(
            np.eye(2, 2), np.dot(t3_mat, t3_mat.transpose()))
示例#14
0
    def test_randomize(self):
        tt = pitts_py.TensorTrain_double([2, 5, 3])
        self.assertEqual([1, 1], tt.getTTranks())

        fullTensor1 = pitts_py.toDense(tt)

        pitts_py.randomize(tt)
        self.assertEqual([1, 1], tt.getTTranks())
        fullTensor2 = pitts_py.toDense(tt)

        pitts_py.randomize(tt)
        fullTensor3 = pitts_py.toDense(tt)

        tt.setTTranks([2, 3])
        pitts_py.randomize(tt)
        self.assertEqual([2, 3], tt.getTTranks())
        fullTensor4 = pitts_py.toDense(tt)

        # check for big enough differences...
        self.assertTrue(np.linalg.norm(fullTensor1 - fullTensor2) > 1.e-4)
        self.assertTrue(np.linalg.norm(fullTensor2 - fullTensor3) > 1.e-4)
        self.assertTrue(np.linalg.norm(fullTensor3 - fullTensor4) > 1.e-4)
示例#15
0
def numpy_qr(X):
    R = np.linalg.qr(X, mode='r')

@timer
def pitts_qr(X):
    R = pitts_py.block_TSQR(X, 20, False)

if __name__ == '__main__':

    pitts_py.initialize(True)

    n = 10000000
    m = 50

    X = pitts_py.MultiVector_double(n, m)
    pitts_py.randomize(X)

    for j in range(1, m+1):
        print("n: ", n, ", m: ", j)
        X.resize(n,j)
        pitts_qr(X)
        numpy_qr(X)

    n = n//10
    m = 100
    print("n: ", n, ", m: ", m)
    X.resize(n, m)
    pitts_py.randomize(X)
    pitts_qr(X)
    numpy_qr(X)
示例#16
0
    x.setZero()
    nrm_x = 0
    for i in range(len(y)):
        nrm_x = pitts_py.axpby(y[i], V[i], nrm_x, x, eps / m)
    if verbose:
        print("TT-GMRES: solution max rank %d" % np.max(x.getTTranks()))
    return x, nrm_x


if __name__ == '__main__':
    pitts_py.initialize()

    TTOp = pitts_py.TensorTrainOperator_double([2, 3, 3, 2, 4, 10, 7],
                                               [2, 3, 3, 2, 4, 10, 7])
    TTOp.setTTranks(1)
    pitts_py.randomize(TTOp)
    TTOpEye = pitts_py.TensorTrainOperator_double([2, 3, 3, 2, 4, 10, 7],
                                                  [2, 3, 3, 2, 4, 10, 7])
    TTOpEye.setEye()
    pitts_py.axpby(1, TTOpEye, 0.1, TTOp)

    xref = pitts_py.TensorTrain_double(TTOp.row_dimensions())
    xref.setOnes()
    b = pitts_py.TensorTrain_double(TTOp.row_dimensions())
    pitts_py.apply(TTOp, xref, b)
    nrm_b = pitts_py.normalize(b)

    def AOp(x, y, eps):
        pitts_py.apply(TTOp, x, y)
        y_nrm = pitts_py.normalize(y, eps)
        return y_nrm