张量指数从0开始(Starting tensor indices at 0)

编程入门 行业动态 更新时间:2024-10-28 21:14:13
张量指数从0开始(Starting tensor indices at 0)

前一段时间,我在广义相对论中为张量微积分写了一个包。 为了使其他人能够轻松访问,应该稍加修改。

有像Christoffel这样的函数来计算Christoffel符号:

Christoffel[g_, xx_] := Block[{ig, res, n}, n = 4; ig = Simplify[Inverse[g]]; res = Table[(1/2)*Sum[ig[[i,s]]*(-D[g[[j,k]], xx[[s]]] + D[g[[j,s]], xx[[k]]] + D[g[[s,k]], xx[[j]]]), {s, 1, n}], {i, 1, n}, {j, 1, n}, {k, 1, n}]; res ]

其中g和xx分别是度量张量和坐标,我在Mathematica会话中定义了一个简单的方法,例如在静态球对称时空中添加ansatz: 这种方式涉及到缺点,因为索引范围是{1, 2, 3, 4}而相对论物理学中的普遍做法则建议放置{0, 1, 2, 3} ,其中0代表时间坐标, {1, 2, 3} {0, 1, 2, 3} {1, 2, 3}代表空间般的。 为了解决这个问题,让我们定义一个表,其索引从0开始,即

V = Table[i - j, {i, 0, 3}, {j, 0, 3}] {{0, -1, -2, -3}, {1, 0, -1, -2}, {2, 1, 0, -1}, {3, 2, 1, 0}}

但是当我评估V[[0, 0]]我得到了Symbol - V的头, 而对于V[[1, 2]]我应该得到-1 。

我的问题是:

我如何重新定义V才能评估“table”组件[0, 0] ? 从0开始引入矩阵g的最方便的方法是什么? 因为我不得不放弃使用Part来访问张量组件0,0如何在包中引入其他对象的索引范围的自由选择,例如Christoffel(假设默认索引范围 - {0, 1, 2, 3}或者如果有人喜欢 - {1, 2, 3, 4} )?

虽然这些问题乍一看似乎微不足道,但任何全面的答案都是值得欢迎的。 任何使用包的人都不应该对Mathematica的微妙之处感到头疼。

Some time ago I wrote a package for tensor calculus in General Relativity. In order to make it easily accessible for others it should be slightly modified.

There are functions like i.e. Christoffel for calculation of Christoffel symbol :

Christoffel[g_, xx_] := Block[{ig, res, n}, n = 4; ig = Simplify[Inverse[g]]; res = Table[(1/2)*Sum[ig[[i,s]]*(-D[g[[j,k]], xx[[s]]] + D[g[[j,s]], xx[[k]]] + D[g[[s,k]], xx[[j]]]), {s, 1, n}], {i, 1, n}, {j, 1, n}, {k, 1, n}]; res ]

where g and xx are metric tensor and coordinates respectively, which I define in a Mathematica session after uploading the package in a straightforward way putting for example an ansatz for a static spherically symmetric spacetime : This way involves drawbacks since index ranges are {1, 2, 3, 4} while the common practice in relativistic physics suggests to put {0, 1, 2, 3} where 0 stands for timelike coordinate, and {1, 2, 3} stand for spacelike ones. To ilustrate the problem let's define a table where indices start from 0 , i.e.

V = Table[i - j, {i, 0, 3}, {j, 0, 3}] {{0, -1, -2, -3}, {1, 0, -1, -2}, {2, 1, 0, -1}, {3, 2, 1, 0}}

but when I evaluate V[[0, 0]] I get Symbol - the head of V, while for V[[1, 2]] I get -1 as it should be.

My questions are :

How could I redefine V in order be able to evaluate "table" component [0, 0] ? What would be the most convenient way to introduce matrix g with its indices starting at 0? Since I have to give up using Part to access tensor components 0,0 how to introduce in a package a freedom of choice of index ranges of other objects like Christoffel (let's say by default index ranges - {0, 1, 2, 3} or if one prefers - {1, 2, 3, 4}) ?

Although these questions seem trivial at first sight but any comprehensive answers are welcome. Anyone using a package shouldn't bother his head about Mathematica subtleties.

最满意答案

(1)Mathematica列表索引,通过Part([[]]以更常用的符号表示)从1开始.0部分是表达式的头部。

(2)可以定义一个“函数”,它接受你想要的索引,并给每个索引加1。

xx = {t, x, \[Theta], \[Phi]}; g = {{-E^(2*\[Nu][x]), 0, 0, 0}, {0, E^(2*\[Lambda][x]), 0, 0}, {0, 0, x^2, 0}, {0, 0, 0, x^2*Sin[\[Theta]]^2}}; gg[indices___] := g[[Sequence @@ ({indices} + 1)]]

例子:

In[121]:= gg[0] Out[121]= {-E^(2*\[Nu][x]), 0, 0, 0} In[123]:= gg[2, 2] Out[123]= x^2

(3)参见(2)可能的方法。 参见(1)了解Part不是直接的路线。

丹尼尔Lichtblau

(1) Mathematica list indexing, via Part ( [[]] in more common notation) starts at 1. The 0 part is the head of the expression.

(2) Can define a "function" that takes the indices you want and adds 1 to each.

xx = {t, x, \[Theta], \[Phi]}; g = {{-E^(2*\[Nu][x]), 0, 0, 0}, {0, E^(2*\[Lambda][x]), 0, 0}, {0, 0, x^2, 0}, {0, 0, 0, x^2*Sin[\[Theta]]^2}}; gg[indices___] := g[[Sequence @@ ({indices} + 1)]]

Examples:

In[121]:= gg[0] Out[121]= {-E^(2*\[Nu][x]), 0, 0, 0} In[123]:= gg[2, 2] Out[123]= x^2

(3) See (2) for a possible approach. See (1) to understand that Part is NOT the direct way to go.

Daniel Lichtblau

更多推荐

本文发布于:2023-08-02 13:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1375958.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:张量   指数   Starting   tensor   indices

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!