ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 26 Sep 2015 01:34:05 -0500Index notationhttp://ask.sagemath.org/question/29595/index-notation/ I am taking a course in general relativity, and I was planing to use sage for doing calculations.
I have
from sage.tensor.modules.tensor_with_indices import TensorWithIndices
M = FiniteRankFreeModule(QQ, 4, name='M')
e = M.basis('e')
a = M.tensor((2,0), name='a')
a[:] = [[2,0,1,-1], [1,0,3,2], [-1,1,0,0], [-2,1,1,-2]]
g = M.tensor((0,2), name='g')
g[:] = [[1,0,0,0], [0,1,0,0], [0,0,1,0],[0,0,0,1]]
v=M.tensor((1,0), name='v')
v[:]=[-1,2,0,-2]
I wanted to calculate $a^{^i,^j}$
Is there some way to tell Sage what is the metric, and do the index lowering automatically? I did it manually, but it's cumbersome for more complicated calculations.
A=a['^ik']*g['_jk'];A[:]
Also, I don't know how to compute $vî v_i$, since
v['^i']*g['_ij']*v['i']
does not work.
Apart from that, I would like to know if there is a way to do this kind of computations with tensor fields and index notation, including derivatives, etc.Fri, 25 Sep 2015 17:51:23 -0500http://ask.sagemath.org/question/29595/index-notation/Answer by eric_g for <p>I am taking a course in general relativity, and I was planing to use sage for doing calculations.</p>
<p>I have</p>
<pre><code>from sage.tensor.modules.tensor_with_indices import TensorWithIndices
M = FiniteRankFreeModule(QQ, 4, name='M')
e = M.basis('e')
a = M.tensor((2,0), name='a')
a[:] = [[2,0,1,-1], [1,0,3,2], [-1,1,0,0], [-2,1,1,-2]]
g = M.tensor((0,2), name='g')
g[:] = [[1,0,0,0], [0,1,0,0], [0,0,1,0],[0,0,0,1]]
v=M.tensor((1,0), name='v')
v[:]=[-1,2,0,-2]
</code></pre>
<p>I wanted to calculate $a^{^i,^j}$
Is there some way to tell Sage what is the metric, and do the index lowering automatically? I did it manually, but it's cumbersome for more complicated calculations.</p>
<pre><code> A=a['^ik']*g['_jk'];A[:]
</code></pre>
<p>Also, I don't know how to compute $vî v_i$, since</p>
<pre><code>v['^i']*g['_ij']*v['i']
</code></pre>
<p>does not work.</p>
<p>Apart from that, I would like to know if there is a way to do this kind of computations with tensor fields and index notation, including derivatives, etc.</p>
http://ask.sagemath.org/question/29595/index-notation/?answer=29596#post-id-29596Hi,
In the tensor calculus on free modules, the concept of metric is not implemented (yet), so you have to manipulate metrics as symmetric tensors of type (0,2) and perform the contractions explicitely, as you did. If you really want to use some metric on a differentiable manifold, I would advise you to install [SageManifolds](http://sagemanifolds.obspm.fr/) : with it, once you have defined a pseudo-Riemannian metric g, you may write `a.up(g)` or `a.down(g)` to raise or lower indices with g. SageManifolds is aimed to be fully integrated in SageMath, but for the time being, only the part regarding tensor calculus on free modules is. To use the pseudo-Riemannian part, you have to install SageManifolds in SageMath by following [these instructions](http://sagemanifolds.obspm.fr/download.html). Note that SageManifolds is already installed in the [SageMathCloud](https://cloud.sagemath.com/), so you can use it directly there.
A few remarks about your code:
The line
from sage.tensor.modules.tensor_with_indices import TensorWithIndices
is not necessary.
You should declare g as a symmetric tensor: either by
g = M.tensor((0,2), name='g', sym=(0,1))
or by
g = M.sym_bilinear_form(name='g')
In the current setting, one cannot use index notations to perform contractions on more than two tensors, which explains why `v['^i']*g['_ij']*v['^j']` does not work. You can either do it in two steps:
w = v['^i']*g['_ij']
s = w['_j']*v['^j']
or not use the index notation:
s = v.contract(g).contract(v)
or, even shorter:
s = g(v,v)
Best wishes,
Eric.
Sat, 26 Sep 2015 01:34:05 -0500http://ask.sagemath.org/question/29595/index-notation/?answer=29596#post-id-29596