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)
```