我将包含大部分代码,因为我不知道错误的来源。这是:
import numpy as np
import sympy as sy
from sympy import Matrix
from numpy import linalg
mu = list(range(16)) # Represent all possible components (of the metric) in one line array.
t, x, y, z = sy.symbols("t x y z") # spacetime coordinates.
lettercoords = [t, x, y, z] # coords in list for easy access
numcoords = [0, 1, 2, 3] # coords in numbers
class MetricTensor: # The Metric Tensor Class
def __init__(self, g0, g1, g2, g3, g4, g5, g6, g7, g8, g9, g10, g11, g12, g13, g14, g15): # Metric components
self.g0 = g0
self.g1 = g1
self.g2 = g2
self.g3 = g3
self.g4 = g4
self.g5 = g5
self.g6 = g6
self.g7 = g7
self.g8 = g8
self.g9 = g9
self.g10 = g10
self.g11 = g11
self.g12 = g12
self.g13 = g13
self.g14 = g14
self.g15 = g15
def LMetric(self):
lstmetric = [self.g0, self.g1, self.g2, self.g3, self.g4, self.g5, self.g6, self.g7, self.g8, self.g9, self.g10, self.g11, self.g12, self.g13, self.g14, self.g15]
lmetric = list(lstmetric)
return lmetric
def Mmetric(self, lmetric):
mmetric = Matrix([[lmetric[0], lmetric[1], lmetric[2], lmetric[3]], [lmetric[8], lmetric[9], lmetric[10], lmetric[11]], [lmetric[4], lmetric[5], lmetric[6], lmetric[7]], [lmetric[12], lmetric[13], lmetric[14], lmetric[15]]])
return mmetric
def InvMmetric(self, mmetric):
invmmetric = np.linalg.inv(mmetric)
return invmmetric
def LInvMetric(self, invmmetric):
linvmetric = list(invmmetric)
return linvmetric
这是输入组件g0 ... g15的代码。例如:
g0 = input("Enter g0:")
...
g15 = input("Enter g15:")
mt = MetricTensor(g0, g1, g2, g3, g4, g5, g6, g7, g8, g9, g10, g11, g12, g13, g14, g15)
lmetric = mt.LMetric()
mmetric = mt.Mmetric(lmetric)
invmmetric = mt.InvMmetric(mmetric)
linvmetric = mt.LInvMetric(invmmetric)
我正在尝试创建一个程序,从给定的度量张量计算爱因斯坦张量。我已经覆盖了'数学'部分,但我的代码出现了问题。
当我运行程序并输入16 g(n)组件时,这就是我得到的:
Traceback (most recent call last):
File "/Users/Irene/PycharmProjects/EFE/EFE.py", line 70, in <module>
invmmetric = mt.InvMmetric(mmetric)
File "/Users/Irene/PycharmProjects/EFE/EFE.py", line 42, in InvMmetric
invmmetric = np.linalg.inv(mmetric)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/numpy/linalg/linalg.py", line 526, in inv
ainv = _umath_linalg.inv(a, signature=signature, extobj=extobj)
TypeError: No loop matching the specified signature and casting
was found for ufunc inv
似乎问题是象征性地反转矩阵。 如果有人可以提供帮助,请提前感谢。