栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

高等数学逆矩阵怎么求_高斯约尔当法求逆矩阵?

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

高等数学逆矩阵怎么求_高斯约尔当法求逆矩阵?

  首先必须要判断矩阵是不是一个方阵。然后把在矩阵右边放一个单位矩阵,然后再进行行变换,左边变成单位矩阵后吗,右边的矩阵就是逆矩阵。
  举个例子,以下矩阵:
[ 1 1 1 1 2 1 1 1 − 1 2 1 1 − 3 2 1 2 ] left[begin{matrix} 1 & 1 & 1 & 1\ 2 & 1& 1& 1\ -1 & 2& 1& 1\ -3& 2& 1& 2\ end{matrix}right] ⎣⎢⎢⎡​12−1−3​1122​1111​1112​⎦⎥⎥⎤​
  右接一个单位矩阵
[ 1 1 1 1 1 0 0 0 2 1 1 1 0 1 0 0 − 1 2 1 1 0 0 1 0 − 3 2 1 2 0 0 0 1 ] left[begin{matrix} 1 & 1 & 1 & 1 & 1 & 0 & 0 & 0\ 2 & 1& 1& 1 & 0 & 1 & 0 & 0\ -1 & 2& 1& 1 & 0 & 0 & 1 & 0\ -3& 2& 1& 2 & 0 & 0 & 0 & 1\ end{matrix}right] ⎣⎢⎢⎡​12−1−3​1122​1111​1112​1000​0100​0010​0001​⎦⎥⎥⎤​
  进行行变换
[ 1 1 1 1 1 0 0 0 0 1 1 1 2 − 1 0 0 0 0 − 1 − 1 − 5 3 1 0 0 0 0 1 − 2 2 − 1 1 ] left[begin{matrix} 1& 1 & 1 &1 & 1 & 0 & 0 &0\ 0& 1 & 1 &1& 2 &-1 & 0& 0\ 0 &0 &-1 &-1 &-5 &3 &1 &0\ 0 &0& 0& 1& -2& 2 &-1& 1\ end{matrix}right] ⎣⎢⎢⎡​1000​1100​11−10​11−11​12−5−2​0−132​001−1​0001​⎦⎥⎥⎤​
  单位化,就变成这样了:
[ 1 0 0 0 − 1 1 0 0 0 1 0 0 − 3 2 1 0 0 0 1 0 7 − 5 0 − 1 0 0 0 1 − 2 2 − 1 1 ] left[begin{matrix} 1 & 0 & 0 & 0 & -1 & 1 & 0 & 0\ 0 & 1 & 0 & 0 & -3 & 2 & 1 & 0\ 0 & 0 & 1 & 0 & 7 & -5 & 0 & -1\ 0 & 0 & 0 & 1 & -2 & 2 & -1 & 1\ end{matrix}right] ⎣⎢⎢⎡​1000​0100​0010​0001​−1−37−2​12−52​010−1​00−11​⎦⎥⎥⎤​
  Java代码:

public Matrix inverse() {
    // 必须是个方阵
    if (this.array.length != this.array[0].length) {
        throw new IllegalArgumentException("方阵才有双侧逆矩阵");
    }
    // 创建一个矩阵,单位矩阵放在右边
    final T[][] newArray = newArray(this.array.length, this.array.length << 1);
    for (int i = 0; i < newArray.length; i++) {
        final T[] line = newArray[i];
        final T[] thisLine = this.array[i];
        for (int j = 0; j < thisLine.length; j++) {
            line[j] = thisLine[j];
        }
        for (int j = thisLine.length; j < line.length; j++) {
            if (j == thisLine.length + i) {
                line[j] = onevalue();
            } else {
                line[j] = zeroValue();
            }
        }
    }
    return createMatrix(newArray).toUpperTriangular().toLowerTriangle().toUnipotent()
        .subMatrix(0, this.array.length, this.array.length, this.array.length * 2);
}

  python代码:

def __invert__(self):
    # 首先新建一个单位矩阵
    size = len(self.__lines)
    unit = self.unit_matrix(size)
    new_matrix = self.append(unit)
    new_matrix.to_upper_triangle()
    new_matrix.to_lower_triangle()
    new_matrix.to_unipotent()
    return new_matrix.sub_matrix(0, size, 0, size)

# 创建单位矩阵方法
# 简化后如下:
@staticmethod
def unit_matrix(size):
    return [[1 if i == j else 0 for j in range(0, size)] for i in range(0, size)]

# 拼接矩阵方法
def append(self, matrix):
    # define an array
    rows = len(self.__lines)
    columns = len(self.__lines[0]) + len(matrix.__lines[0])
    unit = [[0 for _ in range(0, columns)] for _ in range(0, rows)]
    for y in range(0, len(unit)):
        self_line = self.__lines[y]
        other_line = matrix.__lines[y]
        # 0~ this this ~other
        for i in range(0, len(self_line)):
            unit[y][i] = self_line[i]

        for i in range(0, len(other_line)):
            unit[y][i+len(self_line)] = other_line[i]
    return Matrix(unit)
# 下三角矩阵方法
def to_lower_triangle(self):
    for i in range(len(self.__lines) - 1, -1, -1):
        # 循环遍历其前的所有行, 其前所有行进行改变
        for j in range(0, i):
            Matrix.row_operate(self.__lines[i], self.__lines[j], self.__lines[j][i], self.__lines[i][i],
                               0, len(self.__lines[i]))

# 单位矩阵方法
def to_unipotent(self):
    for i in range(0, len(self.__lines)):
        line = self.__lines[i]
        for j in range(0, len(line)):
            if i != j and line[i] != 0:
                line[j] /= line[i]
# 行变换方法
@staticmethod
def __row_operate(line1, line2, coefficient1, coefficient2, start_column, end_column):
    for i in range(start_column, end_column):
        line2[i] = line1[i] * coefficient1 - line2[i] * coefficient2
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/786614.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号