Exemple #1
0
 def test_eigh(self):
     n = 11
     for dt in [np.float32, np.float64, np.complex64, np.complex128]:
         A = ctf.random.random((n, n))
         A += A.conj().T()
         [D, X] = ctf.eigh(A)
         self.assertTrue(allclose(ctf.dot(A, X), X * D))
         self.assertTrue(allclose(ctf.eye(n), ctf.dot(X.conj().T(), X)))
Exemple #2
0
 def test_dot_1d(self):
     a1 = numpy.ones(4)
     self.assertTrue(
         allclose(ctf.dot(ctf.astensor(a1), a1), numpy.dot(a1, a1)))
     self.assertTrue(
         allclose(ctf.dot(a1 + 1j, ctf.astensor(a1)),
                  numpy.dot(a1 + 1j, a1)))
     a2 = ctf.astensor(a1).dot(a1 + 0j)
     self.assertTrue(a2.dtype == numpy.complex128)
Exemple #3
0
 def test_tree_ctr(self):
     X = []
     for i in range(10):
         X.append(ctf.random.random((8, 8)))
     scl = ctf.einsum("ab,ac,ad,ae,af,bg,cg,dg,eg,fg",X[0],X[1],X[2],X[3],X[4],X[5],X[6],X[7],X[8],X[9])
     C = ctf.dot(X[0],X[5])
     for i in range(1,5):
         C = C * ctf.dot(X[i],X[5+i])
     scl2 = ctf.vecnorm(C,1)
     self.assertTrue(numpy.abs(scl-scl2)<1.e-4) 
Exemple #4
0
 def test_solve(self):
     n = 11
     for k in [1, 4, 12, 15, 31]:
         for dt in [np.float32, np.float64, np.complex64, np.complex128]:
             A = ctf.random.random((n, n), dtype=dt)
             A = ctf.dot(A.T(), A)
             C = ctf.random.random((k, n), dtype=dt)
             Y = ctf.solve_spd(A, C)
             #print(C,Y)
             #print(n,k)
             self.assertTrue(allclose(ctf.dot(Y, A), C))
Exemple #5
0
 def test_cholesky(self):
     n = 4
     for dt in [numpy.float32, numpy.float64]:
         A = ctf.random.random((n,n))
         A = ctf.astensor(A,dtype=dt)
         A = ctf.dot(A.T(), A)
         L = ctf.cholesky(A)
         D = L.T() * L
         D.i("ii") << -1.0*L.i("ii")*L.i("ii")
         self.assertTrue(abs(ctf.vecnorm(D))<= 1.e-6)
         self.assertTrue(allclose(A, ctf.dot(L,L.T())))
Exemple #6
0
 def test_svd(self):
     m = 9
     n = 5
     k = 5
     for dt in [numpy.float32, numpy.float64, numpy.complex64, numpy.complex128]:
         A = ctf.random.random((m,n))
         A = ctf.astensor(A,dtype=dt)
         [U,S,VT]=ctf.svd(A,k)
         [U1,S1,VT1]=la.svd(ctf.to_nparray(A),full_matrices=False)
         self.assertTrue(allclose(A, ctf.dot(U,ctf.dot(ctf.diag(S),VT))))
         self.assertTrue(allclose(ctf.eye(k), ctf.dot(U.T(), U)))
         self.assertTrue(allclose(ctf.eye(k), ctf.dot(VT, VT.T())))
Exemple #7
0
 def test_batch_svd(self):
     m = 9
     n = 5
     q = 7
     k = 5
     for dt in [np.float32, np.float64, np.complex64, np.complex128]:
         A = ctf.random.random((q, m, n))
         A = ctf.astensor(A, dtype=dt)
         [U, S, VT] = ctf.svd_batch(A, k)
         for i in range(q):
             self.assertTrue(
                 allclose(A[i], ctf.dot(U[i],
                                        ctf.dot(ctf.diag(S[i]), VT[i]))))
             self.assertTrue(allclose(ctf.eye(k), ctf.dot(U[i].T(), U[i])))
             self.assertTrue(allclose(ctf.eye(k), ctf.dot(VT[i],
                                                          VT[i].T())))
     m = 7
     n = 8
     q = 2
     k = 3
     for dt in [np.float32, np.float64, np.complex64, np.complex128]:
         A = ctf.random.random((q, m, n))
         A = ctf.astensor(A, dtype=dt)
         [U, S, VT] = ctf.svd_batch(A, k)
         for i in range(q):
             [nU, nS, nVT] = np.linalg.svd(A[i].to_nparray())
             self.assertTrue(
                 allclose(
                     np.dot(nU[:, :k], np.dot(np.diag(nS[:k]), nVT[:k, :])),
                     ctf.dot(U[i], ctf.dot(ctf.diag(S[i]), VT[i]))))
             self.assertTrue(allclose(ctf.eye(k), ctf.dot(U[i].T(), U[i])))
             self.assertTrue(allclose(ctf.eye(k), ctf.dot(VT[i],
                                                          VT[i].T())))
Exemple #8
0
def run_bench(num_iter, s, k):
    wrld = ctf.comm()
    M = ctf.random.random((s,s))
    X = ctf.random.random((k,s))
    [U,S,VT] = ctf.svd(M)
    S = np.arange(0,s)+1
    M = ctf.dot(U*S,U.T())
    te = ctf.timer_epoch("BENCHMARK: SPD SOLVE")
    te.begin()
    times = []
    for i in range(num_iter):
        t0 = time.time()
        X = ctf.solve_spd(M,X)
        times.append(time.time()-t0)
    te.end()
    if ctf.comm().rank() == 0:
        print("ctf.solve_spd average time:",np.sum(times)/num_iter,"sec")
        print("ctf.solve_spd iteration timings:",times)
    te = ctf.timer_epoch("BENCHMARK: Manual Cholesky+TRSM SPD SOLVE")
    te.begin()
    times = []
    for i in range(num_iter):
        t0 = time.time()
        L = ctf.cholesky(M)
        X = ctf.solve_tri(M,X,from_left=False)
        times.append(time.time()-t0)
    te.end()
    if ctf.comm().rank() == 0:
        print("ctf.cholesky+solve_tri average time:",np.sum(times)/num_iter,"sec")
        print("ctf.cholesky+solve_tri iteration timings:",times)
Exemple #9
0
 def test_svd_rand(self):
     m = 19
     n = 15
     k = 13
     for dt in [numpy.float32, numpy.float64, numpy.complex64, numpy.complex128]:
         A = ctf.random.random((m,n))
         A = ctf.astensor(A,dtype=dt)
         [U,S,VT]=ctf.svd_rand(A,k,5,1)
         self.assertTrue(allclose(ctf.eye(k),ctf.dot(U.T(),U)))
         self.assertTrue(allclose(ctf.eye(k),ctf.dot(VT,VT.T())))
         [U2,S2,VT2]=ctf.svd(A,k)
         rs1 = ctf.vecnorm(A - ctf.dot(U*S,VT))
         rs2 = ctf.vecnorm(A - ctf.dot(U2*S2,VT2))
         rA = ctf.vecnorm(A)
         self.assertTrue(rs1 < rA)
         self.assertTrue(rs2 < rs1)
         self.assertTrue(numpy.abs(rs1 - rs2)<3.e-1)
Exemple #10
0
    def test_qr(self):
        for (m, n) in [(8, 4), (4, 7), (3, 3)]:
            for dt in [np.float32, np.float64, np.complex64, np.complex128]:
                A = ctf.random.random((m, n))
                A = ctf.astensor(A, dtype=dt)
                [Q, R] = ctf.qr(A)
                self.assertTrue(allclose(A, ctf.dot(Q, R)))
                if (m >= n):
                    self.assertTrue(allclose(ctf.eye(n), ctf.dot(Q.T(), Q)))
                else:
                    self.assertTrue(allclose(ctf.eye(m), ctf.dot(Q, Q.T())))

            A = ctf.tensor((m, n), dtype=np.complex64)
            rA = ctf.tensor((m, n), dtype=np.float32)
            rA.fill_random()
            A.real(rA)
            iA = ctf.tensor((m, n), dtype=np.float32)
            iA.fill_random()
            A.imag(iA)

            [Q, R] = ctf.qr(A)

            self.assertTrue(allclose(A, ctf.dot(Q, R)))
            if (m >= n):
                self.assertTrue(
                    allclose(ctf.eye(n, dtype=np.complex64),
                             ctf.dot(ctf.conj(Q.T()), Q)))
            else:
                self.assertTrue(
                    allclose(ctf.eye(m, dtype=np.complex64),
                             ctf.dot(Q, ctf.conj(Q.T()))))

            A = ctf.tensor((m, n), dtype=np.complex128)
            rA = ctf.tensor((m, n), dtype=np.float64)
            rA.fill_random()
            A.real(rA)
            iA = ctf.tensor((m, n), dtype=np.float64)
            iA.fill_random()
            A.imag(iA)

            [Q, R] = ctf.qr(A)

            self.assertTrue(allclose(A, ctf.dot(Q, R)))
            if (m >= n):
                self.assertTrue(
                    allclose(ctf.eye(n, dtype=np.complex128),
                             ctf.dot(ctf.conj(Q.T()), Q)))
            else:
                self.assertTrue(
                    allclose(ctf.eye(m, dtype=np.complex128),
                             ctf.dot(Q, ctf.conj(Q.T()))))
Exemple #11
0
 def test_hypersparse_dot(self):
     A = ctf.tensor((37, 513), sp=True)
     A.fill_sp_random(0., 1., 1. / 2047)
     B = ctf.tensor((513, 17))
     C = ctf.tensor((37, 17), sp=True)
     C.i("ij") << A.i("ik") * B.i("kj")
     C2 = ctf.tensor((37, 17))
     C2 = ctf.dot(A, B)
     self.assertTrue(allclose(C, C2))
Exemple #12
0
    def test_solve_tri(self):
        n = 4
        m = 7
        for dt in [numpy.float32, numpy.float64]:
            B = ctf.random.random((n,m))
            B = ctf.astensor(B,dtype=dt)
            L = ctf.random.random((n,n))
            L = ctf.astensor(L,dtype=dt)
            L = ctf.tril(L)
            D = L.T() * L
            D.i("ii") << -1.0*L.i("ii")*L.i("ii")
            self.assertTrue(abs(ctf.vecnorm(D))<= 1.e-6)
            X = ctf.solve_tri(L,B)
            self.assertTrue(allclose(B, ctf.dot(L,X)))

            U = ctf.random.random((n,n))
            U = ctf.astensor(U,dtype=dt)
            U = ctf.triu(U)
            D = U.T() * U
            D.i("ii") << -1.0*U.i("ii")*U.i("ii")
            self.assertTrue(abs(ctf.vecnorm(D))<= 1.e-6)
            X = ctf.solve_tri(U,B,False)
            self.assertTrue(allclose(B, ctf.dot(U,X)))

            U = ctf.random.random((m,m))
            U = ctf.astensor(U,dtype=dt)
            U = ctf.triu(U)
            D = U.T() * U
            D.i("ii") << -1.0*U.i("ii")*U.i("ii")
            self.assertTrue(abs(ctf.vecnorm(D))<= 1.e-6)
            X = ctf.solve_tri(U,B,False,False)
            self.assertTrue(allclose(B, ctf.dot(X,U)))

            U = ctf.random.random((m,m))
            U = ctf.astensor(U,dtype=dt)
            U = ctf.triu(U)
            D = U.T() * U
            D.i("ii") << -1.0*U.i("ii")*U.i("ii")
            self.assertTrue(abs(ctf.vecnorm(D))<= 1.e-6)
            X = ctf.solve_tri(U,B,False,False,True)
            self.assertTrue(allclose(B, ctf.dot(X,U.T())))
Exemple #13
0
    def test_qr(self):
        m = 8
        n = 4
        for dt in [numpy.float32, numpy.float64]:
            A = ctf.random.random((m,n))
            A = ctf.astensor(A,dtype=dt)
            [Q,R]=ctf.qr(A)
            self.assertTrue(allclose(A, ctf.dot(Q,R)))
            self.assertTrue(allclose(ctf.eye(n), ctf.dot(Q.T(), Q)))

        A = ctf.tensor((m,n),dtype=numpy.complex64)
        rA = ctf.tensor((m,n),dtype=numpy.float32)
        rA.fill_random()
        A.real(rA)
        iA = ctf.tensor((m,n),dtype=numpy.float32)
        iA.fill_random()
        A.imag(iA)

        [Q,R]=ctf.qr(A)

        self.assertTrue(allclose(A, ctf.dot(Q,R)))
        self.assertTrue(allclose(ctf.eye(n,dtype=numpy.complex64), ctf.dot(ctf.conj(Q.T()), Q)))

        A = ctf.tensor((m,n),dtype=numpy.complex128)
        rA = ctf.tensor((m,n),dtype=numpy.float64)
        rA.fill_random()
        A.real(rA)
        iA = ctf.tensor((m,n),dtype=numpy.float64)
        iA.fill_random()
        A.imag(iA)

        [Q,R]=ctf.qr(A)

        self.assertTrue(allclose(A, ctf.dot(Q,R)))
        self.assertTrue(allclose(ctf.eye(n,dtype=numpy.complex128), ctf.dot(ctf.conj(Q.T()), Q)))
Exemple #14
0
    def test_qr(self):
        m = 8
        n = 4
        for dt in [numpy.float32, numpy.float64, numpy.complex64, numpy.complex128]:
            A = ctf.random.random((m,n))
            A = ctf.astensor(A,dtype=dt)
            [Q,R]=ctf.qr(A)
            self.assertTrue(allclose(A, ctf.dot(Q,R)))
            self.assertTrue(allclose(ctf.eye(n), ctf.dot(Q.T(), Q)))

        A = ctf.tensor((m,n),dtype=numpy.complex64)
        rA = ctf.tensor((m,n),dtype=numpy.float32)
        rA.fill_random()
        A.real(rA)
        iA = ctf.tensor((m,n),dtype=numpy.float32)
        iA.fill_random()
        A.imag(iA)

        [Q,R]=ctf.qr(A)

        self.assertTrue(allclose(A, ctf.dot(Q,R)))
        self.assertTrue(allclose(ctf.eye(n,dtype=numpy.complex64), ctf.dot(ctf.conj(Q.T()), Q)))

        A = ctf.tensor((m,n),dtype=numpy.complex128)
        rA = ctf.tensor((m,n),dtype=numpy.float64)
        rA.fill_random()
        A.real(rA)
        iA = ctf.tensor((m,n),dtype=numpy.float64)
        iA.fill_random()
        A.imag(iA)

        [Q,R]=ctf.qr(A)

        self.assertTrue(allclose(A, ctf.dot(Q,R)))
        self.assertTrue(allclose(ctf.eye(n,dtype=numpy.complex128), ctf.dot(ctf.conj(Q.T()), Q)))
Exemple #15
0
    def test_dot_2d(self):
        a1 = numpy.random.random(4)
        a2 = numpy.random.random((4,3))
        self.assertTrue(ctf.dot(ctf.astensor(a1), ctf.astensor(a2)).shape == (3,))
        self.assertTrue(allclose(ctf.dot(a1, ctf.astensor(a2)), numpy.dot(a1, a2)))
        self.assertTrue(ctf.dot(ctf.astensor(a2).T(), a1).shape == (3,))
        self.assertTrue(allclose(ctf.dot(ctf.astensor(a2).T(), a1), numpy.dot(a2.T, a1)))

        with self.assertRaises(ValueError):
            ctf.dot(a2, a2)
        self.assertTrue(allclose(ctf.dot(ctf.astensor(a2).T(), a2), numpy.dot(a2.T, a2)))
        self.assertTrue(allclose(ctf.astensor(a2).dot(a2.T), a2.dot(a2.T)))
def LS_CG(Ax0,b,Z,x0,r,regParam):
    rk = b - Ax0
    sk = rk
    xk = x0
    for i in range(sk.shape[0]): # how many iterations?
        Ask = ctf.tensor(r)
        Ask.i("i") << Z.i("ti") * Z.i("tj") * sk.i("j")  # A @ sk
        Ask += regParam*sk
        rnorm = ctf.dot(rk,rk)
        #print("rnorm",rnorm.to_nparray())
        if rnorm.to_nparray() < 1.e-16:
            break
        #print(ctf.dot(sk, Ask))
        alpha = rnorm/ctf.dot(sk, Ask)
        xk1 = xk + alpha * sk
        rk1 = rk - alpha * Ask
        beta = ctf.dot(rk1,rk1)/rnorm
        sk1 = rk1 + beta*sk
        rk = rk1
        xk = xk1
        sk = sk1
        #print("rk",ctf.vecnorm(rk))
    return xk
Exemple #17
0
    def test_svd(self):
        m = 9
        n = 5
        k = 5
        for dt in [numpy.float32, numpy.float64]:
            A = ctf.random.random((m,n))
            A = ctf.astensor(A,dtype=dt)
            [U,S,VT]=ctf.svd(A,k)
            [U1,S1,VT1]=la.svd(ctf.to_nparray(A),full_matrices=False)
            self.assertTrue(allclose(A, ctf.dot(U,ctf.dot(ctf.diag(S),VT))))
            self.assertTrue(allclose(ctf.eye(k), ctf.dot(U.T(), U)))
            self.assertTrue(allclose(ctf.eye(k), ctf.dot(VT, VT.T())))

        A = ctf.tensor((m,n),dtype=numpy.complex64)
        rA = ctf.tensor((m,n),dtype=numpy.float32)
        rA.fill_random()
        A.real(rA)
        iA = ctf.tensor((m,n),dtype=numpy.float32)
        iA.fill_random()
        A.imag(iA)

        [U,S,VT]=ctf.svd(A,k)
        self.assertTrue(allclose(A, ctf.dot(U,ctf.dot(ctf.diag(S),VT))))

        self.assertTrue(allclose(ctf.eye(k,dtype=numpy.complex64), ctf.dot(ctf.conj(U.T()), U)))
        self.assertTrue(allclose(ctf.eye(k,dtype=numpy.complex64), ctf.dot(VT, ctf.conj(VT.T()))))

        A = ctf.tensor((m,n),dtype=numpy.complex128)
        rA = ctf.tensor((m,n),dtype=numpy.float64)
        rA.fill_random()
        A.real(rA)
        iA = ctf.tensor((m,n),dtype=numpy.float64)
        iA.fill_random()
        A.imag(iA)

        [U,S,VT]=ctf.svd(A,k)
        self.assertTrue(allclose(A, ctf.dot(U,ctf.dot(ctf.diag(S),VT))))
        self.assertTrue(allclose(ctf.eye(k,dtype=numpy.complex128), ctf.dot(ctf.conj(U.T()), U)))
        self.assertTrue(allclose(ctf.eye(k,dtype=numpy.complex128), ctf.dot(VT, ctf.conj(VT.T()))))
Exemple #18
0
 def test_dot_1d(self):
     a1 = numpy.ones(4)
     self.assertTrue(allclose(ctf.dot(ctf.astensor(a1), a1), numpy.dot(a1, a1)))
     self.assertTrue(allclose(ctf.dot(a1+1j, ctf.astensor(a1)), numpy.dot(a1+1j, a1)))
     a2 = ctf.astensor(a1).dot(a1+0j)
     self.assertTrue(a2.dtype == numpy.complex128)
Exemple #19
0
def davidson(mult_by_A, N, neig, x0=None, Adiag=None, verbose=4):
    """Diagonalize a matrix via non-symmetric Davidson algorithm.

    mult_by_A() is a function which takes a vector of length N
        and returns a vector of length N.
    neig is the number of eigenvalues requested
    """

    if rank == 0:
        log = lib.logger.Logger(sys.stdout, verbose)
    else:
        log = lib.logger.Logger(sys.stdout, 0)

    cput1 = (time.clock(), time.time())

    Mmin = min(neig, N)
    Mmax = min(N, 2000)
    tol = 1e-6

    def mult(arg):
        return mult_by_A(arg)

    if Adiag is None:
        Adiag = np.zeros(N, np.complex)
        for i in range(N):
            test = np.zeros(N, np.complex)
            test[i] = 1.0
            Adiag[i] = mult(test)[i]
    else:
        Adiag = Adiag.to_nparray()

    idx = Adiag.argsort()
    lamda_k_old = 0
    lamda_k = 0
    target = 0
    conv = False
    if x0 is not None:
        assert x0.shape == (Mmin, N)
        b = x0.copy()
        Ab = tuple([mult(b[m, :]) for m in range(Mmin)])
        Ab = ctf.vstack(Ab).transpose()

    evals = np.zeros(neig, dtype=np.complex)
    evecs = []
    for istep, M in enumerate(range(Mmin, Mmax + 1)):
        if M == Mmin:
            b = ctf.zeros((N, M))
            if rank == 0:
                ind = [i * M + m for m, i in zip(range(M), idx)]
                fill = np.ones(len(ind))
                b.write(ind, fill)
            else:
                b.write([], [])
            Ab = tuple([mult(b[:, m]) for m in range(M)])
            Ab = ctf.vstack(Ab).transpose()

        else:

            Ab = ctf.hstack((Ab, mult(b[:, M - 1]).reshape(N, -1)))

        Atilde = ctf.dot(b.conj().transpose(), Ab)
        Atilde = Atilde.to_nparray()

        lamda, alpha = diagonalize_asymm(Atilde)
        lamda_k_old, lamda_k = lamda_k, lamda[target]
        alpha_k = ctf.astensor(alpha[:, target])
        if M == Mmax:
            break

        q = ctf.dot(Ab - lamda_k * b, alpha_k)
        qnorm = ctf.norm(q)
        log.info(
            'davidson istep = %d  root = %d  E = %.15g  dE = %.9g  residual = %.6g',
            istep, target, lamda_k.real, (lamda_k - lamda_k_old).real, qnorm)
        cput1 = log.timer('davidson iter', *cput1)

        if ctf.norm(q) < tol:
            evecs.append(ctf.dot(b, alpha_k))
            evals[target] = lamda_k
            if target == neig - 1:
                conv = True
                break
            else:
                target += 1
        eps = 1e-10
        xi = q / (lamda_k - Adiag + eps)
        bxi, R = ctf.qr(ctf.hstack((b, xi.reshape(N, -1))))
        nlast = bxi.shape[-1] - 1
        b = ctf.hstack(
            (b, bxi[:, nlast].reshape(N, -1))
        )  #can not replace nlast with -1, (inconsistent between numpy and ctf)
    evecs = ctf.vstack(tuple(evecs))

    return conv, evals, evecs
Exemple #20
0
 def inv(matrix):
     U, s, V = ctf.svd(matrix)
     return ctf.dot(ctf.transpose(V),
                    ctf.dot(ctf.diag(s**-1), ctf.transpose(U)))
def naive_gemm(A, B, n, b):
    C = ctf.dot(A, B)
    C += C.reshape((n, b, n, b)).transpose([2, 1, 0, 3]).reshape(
        (n * b, n * b))
    return C
Exemple #22
0
#!/usr/bin/env python

import ctf

from ctf import random

A = ctf.random.random((32,32))

[U,S,VT]=ctf.svd(A)

err = A-ctf.dot(U,ctf.dot(ctf.diag(S),VT))

success=True

err_nrm = err.norm2()
if err_nrm > 1.E-6:
  success=False

if ctf.comm().rank() == 0:
    if success:
      print("success, norm is ", err_nrm)
    else:
      print("failure, norm is ", err_nrm)

ctf.MPI_Stop()

Exemple #23
0
#!/usr/bin/env python

import ctf
import sys

from ctf import random

A = ctf.random.random((32,32))

[U,S,VT]=ctf.svd(A)

err = A-ctf.dot(U,ctf.dot(ctf.diag(S),VT))

success=True

err_nrm = err.norm2()
if err_nrm > 1.E-6:
  success=False

if ctf.comm().rank() == 0:
    if success:
      print("success, norm is ", err_nrm)
    else:
      print("failure, norm is ", err_nrm)

ctf.MPI_Stop()
sys.exit(not success)

def dot(A, B):
    return ctf.dot(A, B)
Exemple #25
0
    def test_svd(self):
        m = 9
        n = 5
        k = 5
        for dt in [numpy.float32, numpy.float64]:
            A = ctf.random.random((m, n))
            A = ctf.astensor(A, dtype=dt)
            [U, S, VT] = ctf.svd(A, k)
            [U1, S1, VT1] = la.svd(ctf.to_nparray(A), full_matrices=False)
            self.assertTrue(allclose(A, ctf.dot(U, ctf.dot(ctf.diag(S), VT))))
            self.assertTrue(allclose(ctf.eye(k), ctf.dot(U.T(), U)))
            self.assertTrue(allclose(ctf.eye(k), ctf.dot(VT, VT.T())))

        A = ctf.tensor((m, n), dtype=numpy.complex64)
        rA = ctf.tensor((m, n), dtype=numpy.float32)
        rA.fill_random()
        A.real(rA)
        iA = ctf.tensor((m, n), dtype=numpy.float32)
        iA.fill_random()
        A.imag(iA)

        [U, S, VT] = ctf.svd(A, k)
        self.assertTrue(allclose(A, ctf.dot(U, ctf.dot(ctf.diag(S), VT))))

        self.assertTrue(
            allclose(ctf.eye(k, dtype=numpy.complex64),
                     ctf.dot(ctf.conj(U.T()), U)))
        self.assertTrue(
            allclose(ctf.eye(k, dtype=numpy.complex64),
                     ctf.dot(VT, ctf.conj(VT.T()))))

        A = ctf.tensor((m, n), dtype=numpy.complex128)
        rA = ctf.tensor((m, n), dtype=numpy.float64)
        rA.fill_random()
        A.real(rA)
        iA = ctf.tensor((m, n), dtype=numpy.float64)
        iA.fill_random()
        A.imag(iA)

        [U, S, VT] = ctf.svd(A, k)
        self.assertTrue(allclose(A, ctf.dot(U, ctf.dot(ctf.diag(S), VT))))
        self.assertTrue(
            allclose(ctf.eye(k, dtype=numpy.complex128),
                     ctf.dot(ctf.conj(U.T()), U)))
        self.assertTrue(
            allclose(ctf.eye(k, dtype=numpy.complex128),
                     ctf.dot(VT, ctf.conj(VT.T()))))