예제 #1
0
# setup tensor vector
u = prepare_vectors(J, FS)
u = FullTensor.from_list(u)
print "FullTensor u", u.dim()
print "u as matrix shape", u.as_matrix().shape, u.flatten().as_array().shape


# test tensor operator
# ====================

# test application of operator
w = A * u

# print matricisation of tensor operator
M = A.as_matrix()
print M.shape, norm(M)

# plot mesh
#import dolfin
#dolfin.plot(mesh, interactive=True)

# plot sparsity pattern
#fig = figure()
##spy(M)
#spy(K[0].as_matrix())
#show()

from spuq.application.egsz.pcg import pcg
P = TensorOperator([K0inv], [D[0]])
print 0*w
예제 #2
0
    # prepare "stochastic" matrices
    D = [sps.csr_matrix(np.random.rand(d,d)) for _ in range(m)]
    # convert to efficient sparse format
    D = [ScipyOperator(D_.asformat('csr'), domain=CanonicalBasis(D_.shape[0]), codomain=CanonicalBasis(D_.shape[1])) for D_ in D]

    # prepare vector
    u = [np.random.rand(k) for _ in range(d)]
    return K, D, u


# test tensor operator application
# ================================
I, J = 100, 15
for M in [1,2,5]:
    # prepare data
    K, D, u = construct_data(I, J, M)
    A = TensorOperator(K, D)
    u = FullTensor.from_list(u)
    # print matricisation of tensor operator
    Amat = A.as_matrix()
    print Amat.shape

    # compare with numpy kronecker product
    M2 = [sps.kron(K_.matrix, D_.matrix) for K_, D_ in zip(K, D)]
    M2 = np.sum(M2)

    print "error norm: ", norm(Amat-M2.todense()), " == ", norm(Amat-M2)

    # test application of operator
    w = A * u