# setup deterministic operators M, degree = 3, 1 K, FS = prepare_deterministic_operators(pde, coeff_field, M, mesh, degree) print "K", len(K) K0inv = pde.assemble_solve_operator(basis=FS, coeff=f).as_scipy_operator() # setup stochastic operators p1 = 2 D = prepare_stochastic_operators(M, p1, 'L') print "D", len(D), D[0].domain.dim print D[0].as_matrix() # construct combined tensor operator A = TensorOperator(K, D) I, J, M = A.dim print "TensorOperator A dim", A.dim # 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
# 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