sympy / sympy

A computer algebra system written in pure Python
https://sympy.org/
Other
12.79k stars 4.39k forks source link

contract_metric(metric) in tensor module contracts indices of any TensorIndexType #26289

Open pashilkar opened 6 months ago

pashilkar commented 6 months ago

from sympy import symbols
from sympy.tensor.tensor import tensor_indices, TensorIndexType

#dimensions d
d = symbols('d', positive=True, integer=True)

# Define 2 TensorIndexTypes:
# 'Lorentz' with metric 'g' &
# 'Minkowski' with metric 'η'.
Lorentz = TensorIndexType('Lorentz', dummy_name='L', dim = d, metric_name='g')
g = Lorentz.metric
Minkowski = TensorIndexType('Minkowski', dummy_name='M', dim = d, metric_name='η')
η = Minkowski.metric

# Define 3 indices of type 'Lorentz'.
μ, ν, σ = tensor_indices("μ, ν, σ", Lorentz)

# Contracting with η gives same result as contracting with g!
(η(μ, σ) * g(-ν, -σ)).contract_metric(η) - (g(μ, σ) * g(-ν, -σ)).contract_metric(g)
# output >> 0

contractmetric(η) should contract only Minkowski indices. But when applied to $η^{μσ} g{νσ}$ with Lorentz indices, it outputs ${g_ν}^μ$. contractmetric(g), on the other hand, works as expected on $g^{μσ} g{νσ}$ outputting ${g_ν}^μ$.

AshutoshRajora commented 6 months ago

so basically, The contract_metric method should contract only indices associated with the same metric. But, it appears that it's also contracting Lorentz indices when applied to the Minkowski metric. have I got you right?

pashilkar commented 6 months ago

Yes. η[Minkowski, Minkowski] should contract only Minkowski tensor indices.

On Fri, 1 Mar, 2024, 6:41 am Ashutosh Rajora, @.***> wrote:

so basically, The contract_metric method should contract only indices associated with the same metric. But, it appears that it's also contracting Lorentz indices when applied to the Minkowski metric. have I got you right?

— Reply to this email directly, view it on GitHub https://github.com/sympy/sympy/issues/26289#issuecomment-1972263438, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABMS2CAG26JX3JYUSQMXRBDYV7IUXAVCNFSM6AAAAABD7WZ2IWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSNZSGI3DGNBTHA . You are receiving this because you authored the thread.Message ID: @.***>

Kishore96in commented 2 weeks ago

Does it make sense to pass Lorentz indices to η?

In [15]: η.index_types
Out[15]: [Minkowski, Minkowski]

It seems to me that TensorHead should raise an error if passed an index which does not match the corresponding entry in index_types.