下面的错误信息对于逆矩阵意味着什么?

2024-09-29 09:27:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我将包括我的大部分代码,因为我不知道错误来自哪里。这里是:

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的代码。示例:

^{pr2}$

我正在尝试创建一个程序,从给定的度量张量计算爱因斯坦张量。我已经涵盖了“数学”部分,但我的代码有问题。在

当我运行程序并输入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

问题似乎是象征性地逆矩阵。 如果有人能帮忙,请提前谢谢。在


Tags: inimportselfdeflinelistinvg1