# can i define a metric using 'tensorwithindices', such that it is defined as one entity only and not by 16 components?

I've been trying to derive equations of general relativity on sagemath, I would want to define my metric as g_ij instead of having to define it component by component. Can you help me with how to do that? I tried using the object 'tensor with indices' but that does not allow me to use attributes from the metric object.

Or if there is any other way to deal with this problem, please let me know.

edit retag close merge delete

Please provide an actual code illustrating the issue.

( 2024-08-24 04:48:11 +0200 )edit

okay so first i tried doing it component by component. It is a simple code, just finding riemann tensor for a metric, but the computation time exceeds 16 hours.

from sage.tensor.modules.tensor_with_indices import TensorWithIndices
h=M.metric('h')
for i in range(4):
for j in range(4):
h[i, j] = function(f'h{i}{j}')(t, x, y, z)
h[i, j] = h[j, i]

h[:]
riemann=h.riemann()
riemann.display()


So i wanted it to be a bit generlised, like instead of having 16 components, with 16 memory allocations if i could do it just one component like this

p=M.tensor_field(0,2,'p')
from sage.tensor.modules.tensor_with_indices import TensorWithIndices
f= TensorWithIndices(p, '_ij') ; f
This was the output
p_ij
but then when i try to compute further, i get this error
f.riemann()


(to be cont)

( 2024-08-24 14:17:34 +0200 )edit

the error is

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[8], line 1
----> 1 f.riemann()

AttributeError: 'TensorWithIndices' object has no attribute 'riemann'

( 2024-08-24 14:18:04 +0200 )edit

Please define M, and best reedit the question showing the mathematical intention and the attempt to mimic the situation inside sage. Which is in fact the intention, what should be calculated after having the instance initialized? The doc sting of TensorWithIndices gives:

This is a technical class to allow one to write some tensor
operations (contractions and symmetrizations) in index notation.


And examples are shown.

( 2024-08-25 22:21:19 +0200 )edit

the manifold is lorentzian. M= Manifold(4, 'M'). Also what examples are shown, I didn't get that. Can you please provide a link?

( 2024-08-26 12:25:12 +0200 )edit