3.3 高斯法求逆矩阵

首先必须要判断矩阵是不是一个方阵。然后把在矩阵右边放一个单位矩阵,然后再进行行变换,左边变成单位矩阵后吗,右边的矩阵就是逆矩阵。
举个例子,以下矩阵:
[ 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]1213112211111112
右接一个单位矩阵
[ 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]12131122111111121000010000100001
进行行变换
[ 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]10001100111011111252013200110001
单位化,就变成这样了:
[ 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]10000100001000011372125201010011
Java代码:

public Matrix<T> 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

版权声明:本文为m0_66201040原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。