Пример #1
0
    def tensorize(args, sparse=None):

        for a in args:
            if not isinstance(a, Varf):
                raise ValueError("Invalid argument(s)")

        if len(args) == 1:
            return args[0]

        sparse = rc.settings['sparse'] if sparse is None else sparse
        index_set, degree = args[0].index_set, args[0].degree
        mats = {}
        factor = 1
        for a in args:
            if not isinstance(a, Varf) or \
               not a.index_set == index_set or \
               not a.degree == degree:
                raise ValueError("Invalid argument(s)")
            key = frozenset(a.position.dirs)
            mats[key] = a.matrix
            factor *= a.factor.sym

        matrix = core.tensorize(mats, sparse=sparse, index_set=index_set)
        position = pos.Position.tensorize([a.position for a in args])
        factor = func.Function(factor, dirs=position.dirs)
        return Varf(matrix, position, factor=factor, index_set=index_set)
Пример #2
0
 def test_tensorize_vector(self):
     n_points = 200
     degree = 10
     quad_1d = hm.Quad.gauss_hermite(n_points, dim=1)
     quad_2d = hm.Quad.gauss_hermite(n_points, dim=2)
     coeffs_1d = quad_1d.transform('exp(x)', degree).coeffs
     coeffs_2d = quad_2d.transform('exp(x)', degree).coeffs
     tensorized_coeffs_1d = core.tensorize(coeffs_1d, 2, 0)
     self.assertAlmostEqual(la.norm(coeffs_2d - tensorized_coeffs_1d, 2), 0)
Пример #3
0
 def tensorize(args):
     assert len(args) > 1
     vecs = []
     for a in args:
         assert type(a) is Series
         vecs.append(a.coeffs)
     tens_vec = core.tensorize(vecs)
     tens_pos = pos.Position.tensorize([a.position for a in args])
     return Series(tens_vec, tens_pos)
Пример #4
0
 def test_tensorize_matrix(self):
     n_points = 200
     degree = 10
     function = 'exp(x)'
     quad_1d = hm.Quad.gauss_hermite(n_points, dim=1)
     quad_2d = hm.Quad.gauss_hermite(n_points, dim=2)
     varf_1d = quad_1d.varf(function, degree).matrix
     varf_2d = quad_2d.varf(function, degree).matrix
     tensorized_varf_1d = core.tensorize(varf_1d, 2, 0)
     diff = (la.norm(varf_2d - tensorized_varf_1d, 2))
     self.assertAlmostEqual(diff, 0)
Пример #5
0
 def test_tensorize_matrices(self):
     n_points = 200
     degree = 10
     f, fx, fy = 'exp(x) * (y*y*y)', 'exp(x)', 'x*x*x'
     quad_1d = hm.Quad.gauss_hermite(n_points, dim=1)
     quad_2d = hm.Quad.gauss_hermite(n_points, dim=2)
     varf_x = quad_1d.varf(fx, degree).matrix
     varf_y = quad_1d.varf(fy, degree).matrix
     varf_2d = quad_2d.varf(f, degree).matrix
     tensorized_varf = core.tensorize([varf_x, varf_y])
     diff = la.norm(varf_2d - tensorized_varf, 2)
     self.assertAlmostEqual(diff, 0)
Пример #6
0
 def test_tensorize_vectors(self):
     n_points = 200
     degree = 10
     f, fx, fy = 'exp(x) * (y*y*y)', 'exp(x)', 'x*x*x'
     quad_1d = hm.Quad.gauss_hermite(n_points, dim=1)
     quad_2d = hm.Quad.gauss_hermite(n_points, dim=2)
     coeffs_x = quad_1d.transform(fx, degree).coeffs
     coeffs_y = quad_1d.transform(fy, degree).coeffs
     coeffs_2d = quad_2d.transform(f, degree).coeffs
     tensorized_coeffs = core.tensorize([coeffs_x, coeffs_y])
     diff = la.norm(coeffs_2d - tensorized_coeffs, 2)
     self.assertAlmostEqual(diff, 0)
Пример #7
0
 def tensorize(args, sparse=False):
     assert len(args) > 1
     mats = []
     for a in args:
         assert type(a) is Varf
         if type(a.matrix) is ss.csr_matrix:
             mats.append(a.matrix.todense().A)
         else:
             mats.append(a.matrix)
     tens_mat = core.tensorize(mats, sparse=sparse)
     tens_pos = pos.Position.tensorize([a.position for a in args])
     return Varf(tens_mat, tens_pos)
Пример #8
0
            def wrapper(*args, **kwargs):
                tensorize = kwargs['tensorize'] if 'tensorize' in kwargs \
                        else None
                do_tensorize = hm.settings['tensorize'] if \
                    tensorize is None else tensorize

                # <- Fix bug with integrate(series)
                if isinstance(args[arg_num], hm.Series):
                    do_tensorize = False
                # ->

                if not do_tensorize:
                    return func(*args, **kwargs)

                quad, function = args[0], args[arg_num]
                if not quad.position.is_diag or \
                        isinstance(function, np.ndarray):
                    return func(*args, **kwargs)

                results = []
                if not isinstance(function, hm.Function):
                    function = hm.Function(function, dirs=quad.position.dirs)

                for add in function.split(legacy=False):
                    multiplicator = add[frozenset()]
                    del add[frozenset()]

                    factor_split = quad.factor.split(legacy=False)
                    if len(factor_split) != 1:
                        raise ValueError("Tensorization not possible!")
                    finest_division = lib.finest_common(
                        set(add), set(factor_split[0]))
                    new_add = {
                        sub: hm.Function('1')
                        for sub in finest_division
                    }
                    for dirs, term in add.items():
                        for s in new_add:
                            if dirs.issubset(s):
                                f = hm.Function(new_add[s].sym * term.sym,
                                                dirs=list(s))
                                new_add[s] = f
                    func_dirs = {}
                    for dirs, term in new_add.items():
                        new_args = list(args).copy()
                        new_args[0] = quad.project(list(dirs))
                        new_args[arg_num] = term
                        func_dirs[dirs] = func(*new_args, **kwargs)

                    if hm.settings['debug']:
                        print("Tensorizing results")

                    kwargs_func = {'sparse': kwargs['sparse']} \
                        if 'sparse' in kwargs else {}
                    t = type(list(func_dirs.values())[0])
                    if t is hm.Varf or t is hm.Series:
                        values = list(func_dirs.values())
                        tensorized = t.tensorize(values, **kwargs_func)
                    else:
                        tensorized = core.tensorize(func_dirs, **kwargs_func)
                    results.append(tensorized * float(multiplicator.sym))
                return sum(results[1:], results[0])