Ask Your Question

Patrick's profile - activity

2022-08-06 00:54:08 +0200 received badge  Notable Question (source)
2021-07-05 14:49:38 +0200 received badge  Popular Question (source)
2018-04-23 08:54:01 +0200 commented answer Basic work on tensor components

Thank you very much! I think it would make sense to add this to the lib (as well as a method doing this in place instead of a root namespace function returning a new object). I use SageManifolds only when computations are complicated, and then I always need simplify and his friends expand and factor.

2018-04-23 08:51:20 +0200 received badge  Scholar (source)
2018-04-21 01:04:56 +0200 received badge  Student (source)
2018-04-21 01:04:05 +0200 asked a question Basic work on tensor components

My SageManifolds notebooks typically start with a bunch of home made function designed to access components of tensors. But they are based on a common hacky function relying on very shallow understanding of the internals of SageManifolds. Am I missing some builtin equivalent? What is the proper way of doing this?

def maps(fun, tensor):
    """ Applies fun to all components of a copy of tensor """
    res = tensor.copy()
    if tensor.tensor_type() == (0, 0):
        for k, v in res._express.items():
            res._express[k] = k.function(fun(v.expr()))
    else:
        for k, v in res.comp()._comp.items():
            res.comp()._comp[k] = res.domain().scalar_field(fun(v.expr()))
    return res

def simp(tensor):
    return maps(lambda f: simplify(f), tensor)

def dev(tensor):
    return maps(lambda f: expand(f), tensor)

def factorize(tensor):
    return maps(lambda f: factor(f), tensor)

def subs(tensor, d):
    return maps(lambda f: f.subs(d), tensor)

```