def test_matrixization(): def mult_assemble(a, basis): return MultiplicationOperator(a(0), basis) mean_func = ConstFunction(2) a = [ConstFunction(3), ConstFunction(4)] rvs = [UniformRV(), NormalRV(mu=0.5)] coeff_field = ListCoefficientField(mean_func, a, rvs) A = MultiOperator(coeff_field, mult_assemble) mis = [Multiindex([0]), # Multiindex([1]), # Multiindex([0, 1]), Multiindex([0, 2])] mesh = UnitSquare(1, 1) fs = FunctionSpace(mesh, "CG", 2) F = [interpolate(Expression("*".join(["x[0]"] * i)), fs) for i in range(1, 5)] vecs = [FEniCSVector(f) for f in F] w = MultiVector() for mi, vec in zip(mis, vecs): w[mi] = vec P = w.to_euclidian_operator Q = w.from_euclidian_operator Pw = P.apply(w) assert type(Pw) == np.ndarray and len(Pw) == sum((v for v in w.dim.values())) QPw = Q.apply(Pw) assert w.active_indices() == QPw.active_indices() for mu in w.active_indices(): assert_equal(w[mu].array, QPw[mu].array) A_linear = P * A * Q A_mat = evaluate_operator_matrix(A_linear)
def test_neg(): mv1 = MultiVector() mis1 = MultiindexSet.createCompleteOrderSet(3, 4) mv1.set_defaults(mis1, FlatVector([3, 4, 5])) mv = -mv1 assert_equal(mv[Multiindex(mis1[-1])], FlatVector([-3, -4, -5])) assert_equal(mv1[Multiindex(mis1[-1])], FlatVector([3, 4, 5]))
def test_set_defaults(): mv = MultiVector() mis = MultiindexSet.createCompleteOrderSet(3, 4) mv.set_defaults(mis, FlatVector([3, 4, 5])) assert_equal(mv[Multiindex([1, 2, 1])], FlatVector([3, 4, 5])) assert_equal(mv[Multiindex()], FlatVector([3, 4, 5])) assert_raises(KeyError, mv.__getitem__, Multiindex([1, 2, 2]))
def test_keys(): mv = MultiVector() mi1 = Multiindex([1, 2, 3]) mi2 = Multiindex([1, 2, 4]) mv[mi1] = FlatVector([3, 4, 5]) mv[mi2] = FlatVector([3, 4, 7]) keys = mv.keys() assert_equal(sorted(keys), sorted([mi1, mi2])) inds = mv.active_indices() assert_equal(sorted(inds), sorted([mi1, mi2]))
def test_add(): mv1 = MultiVector() mv2 = MultiVector() mv3 = MultiVector() mis1 = MultiindexSet.createCompleteOrderSet(3, 4) mv1.set_defaults(mis1, FlatVector([3, 4, 5])) mv2.set_defaults(mis1, FlatVector([6, 8, 12])) mv3.set_defaults(mis1, FlatVector([9, 12, 17])) assert_equal(mv1 + mv2, mv3)
def test_copy(): # make sure the vectors are copied and each have their own copy of # the data mv = MultiVector() mv[Multiindex([1, 2, 3])] = FlatVector([3, 4, 5]) mv[Multiindex([1, 2, 4])] = FlatVector([3, 4, 7]) assert_equal(mv[Multiindex([1, 2, 3])], FlatVector([3, 4, 5])) assert_equal(mv[Multiindex([1, 2, 4])], FlatVector([3, 4, 7])) mv2 = mv.copy() mv[Multiindex([1, 2, 3])].coeffs[2] = 8 assert_equal(mv2[Multiindex([1, 2, 3])], FlatVector([3, 4, 5])) assert_equal(mv2[Multiindex([1, 2, 4])], FlatVector([3, 4, 7])) assert_equal(mv[Multiindex([1, 2, 3])], FlatVector([3, 4, 8]))
def test_mvwp_copy(): # compares equal to copied MultiVectorWP but not to MultiVector mv0 = MultiVector() mv1 = MultiVectorWithProjection() mis1 = MultiindexSet.createCompleteOrderSet(3, 4) mv0.set_defaults(mis1, FlatVector([3, 4, 5])) mv1.set_defaults(mis1, FlatVector([3, 4, 5])) mv2 = mv1.copy() assert_equal(mv2, mv1) assert_not_equal(mv2, mv0) # compares equal if project methods match, make sure project method is copied mv3 = MultiVectorWithProjection(project=lambda : None) mv3.set_defaults(mis1, FlatVector([3, 4, 5])) mv4 = mv3.copy() assert_not_equal(mv2, mv3) assert_equal(mv4, mv3)
def test_sub(): mv1 = MultiVector() mv2 = MultiVector() mi1 = Multiindex([1, 2, 1]) mi2 = Multiindex([3, 2, 1, 7]) mis1 = MultiindexSet.createCompleteOrderSet(3, 4) mv1.set_defaults(mis1, FlatVector([3, 4, 5])) mv2.set_defaults(mis1, FlatVector([6, 8, 10])) mv1[mi2] = FlatVector([7, 10, 6]) mv2[mi2] = FlatVector([2, 6, 13]) mv3 = mv1 - mv2 assert_equal(mv3[mi1], FlatVector([-3, -4, -5])) assert_equal(mv3[mi2], FlatVector([5, 4, -7]))
def test_mul(): mv1 = MultiVector() mv2 = MultiVector() mis1 = MultiindexSet.createCompleteOrderSet(3, 4) #mis2 = MultiindexSet.createCompleteOrderSet(3, 5) mv1.set_defaults(mis1, FlatVector([3, 4, 5])) mv2.set_defaults(mis1, FlatVector([6, 8, 10])) assert_equal(2 * mv1, mv2) assert_equal(mv1 * 2, mv2) assert_equal(2.0 * mv1, mv2) assert_equal(mv1 * 2.0, mv2) assert_equal(mv1[Multiindex()], FlatVector([3, 4, 5])) assert_equal(mv1[Multiindex([1, 2, 1])], FlatVector([3, 4, 5])) mv1 *= 2.0 assert_equal(mv1, mv2)
def test_equality(): mv1 = MultiVector() mv2 = MultiVector() mv3 = MultiVector() mv4 = MultiVector() mis1 = MultiindexSet.createCompleteOrderSet(3, 4) mis2 = MultiindexSet.createCompleteOrderSet(3, 5) mv1.set_defaults(mis1, FlatVector([3, 4, 5])) mv2.set_defaults(mis1, FlatVector([3, 4, 5])) mv3.set_defaults(mis2, FlatVector([3, 4, 5])) mv4.set_defaults(mis1, FlatVector([3, 4, 6])) assert_true(mv1 == mv2) assert_false(mv1 != mv2) assert_true(mv1 != mv3) assert_false(mv1 == mv3) assert_true(mv1 != mv4) assert_false(mv1 == mv4)