Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Support indices raising and lowering in N-dimensional numpy arrays #13965

Open
ritzvik opened this issue Jul 11, 2019 · 3 comments

Comments

@ritzvik
Copy link

ritzvik commented Jul 11, 2019

Indices raising and lowering are a very crucial part of differential geometry and particularly important in relativistic physics.

See https://en.wikipedia.org/wiki/Raising_and_lowering_indices

Example :

  • A 4D tensor can have index configuration ulll, and can be changed to lulu with the help of a metric tensor, here u denotes contravariant index and l denotes covariant index.

Reproducing code example:

import numpy as np
T = np.zeros(shape=(4,4,4,4), dtype=float)
# substitute some values in T
M = np.zeros(shape=(4,4), dtype=float)
# substitute some values in M(Metric Tensor)
T_ = np.change_config(T, metric=M, old='ulll', new='lulu')
print(T_)
# Example snippet
@eric-wieser
Copy link
Member

eric-wieser commented Jul 11, 2019

If I understand that function, the implementation is:

def change_config(arr, *, metric, old, new):
    for axis, (o, n) in enumerate(zip(old, new)):
        # assumes metric is its own inverse
        if o != n:
            arr = np.moveaxis(np.moveaxis(arr, axis, -1) @ metric), -1, axis)
    return arr

You might find it's better to make an ndarray subclass that keeps track of the lower/upper-ness of the axes

@ritzvik
Copy link
Author

ritzvik commented Jul 11, 2019

Yes moveaxis is required, and it also requires tensorproduct and tensorcontraction which is provided einsum in np, I guess. I couldn't understand the @ metric thing. Sorry for the trouble!

See https://github.com/einsteinpy/einsteinpy/blob/master/src/einsteinpy/symbolic/tensor.py#L29-L63 . And because of change to any arbitrary configuration, it may require a series of tensorproducts and contractions.

@eric-wieser
Copy link
Member

eric-wieser commented Jul 11, 2019

@ here is matrix multiplication, which I believe that when combined with moveaxis, subsumes contraction and product for the cases that matter here - assuming that a metric tensor is always 2D, which your links seem to suggest is so.

I'm not sure this is a good fit for numpy - on the one hand, we don't really have a true concept of tensors - but on the other, we do provide einsum and tensordot...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants