Ask Your Question

Revision history [back]

For the moment, there is no builtin equivalent. Your way of implementing this is basically correct. However, I would not perform the loop on res but rather on tensor, in order to avoid looping on the items of a dictionary while modifying it, especially when items() becomes an iterator in Python3. Moreover, one can replace

res._express[k] = k.function(fun(v.expr()))

by

res.add_expr(fun(v.expr()), chart=k)

and one can use recursivity to simplify the tensor part, i.e. replace

res.comp()._comp[k] = res.domain().scalar_field(fun(v.expr()))

by

res.comp()._comp[k] = maps(fun, v)

Finally, the tensor part of your code is valid only for the manifold's default frame (because res.comp() returns the components with respect to that frame). It can easily be generalized to any frame by adding the argument frame=None to maps and replacing comp() by comp(frame).

With the above changes, the code becomes:

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