Solving multivariate linear equations using matrices and Laplace expansion theorem

1 Introduction

I chatted with several junior high schools and asked them how they were doing in mathematics. What is a function? Because function is a very important first contact for junior high school students, they will be a little confused when they first come into contact with it, so I want these junior high school students to become familiar with it as soon as possible without feeling unfamiliar and scared.

According to the definition, a function refers to the change of one quantity as another quantity changes, and contains three elements: domain A, value range B and corresponding law f .

Of course, before getting confused about these definitions, I asked them not to memorize them by rote, so I gave a popular analogy, like buying a fish, which can be made into braised fish or steamed fish. The cooking method is a function, that is Different cooking means different functions, which will produce different results.
Since junior high school started to learn linear equations in two variables and linear equations in three variables, to put it more simply, seeing that they were still a little confused, we asked them to directly treat equations as functions.

Next, they were asked if they could solve systems of equations? It can be solved through the elimination method, so I asked them whether the system of linear equations in four variables, the system of linear equations in ten variables, or even the system of linear equations in one hundred variables can be solved. At this time, they felt complicated.

2. Unit matrix

Therefore, in response to these problems, I specially made a picture to let them understand the fun of mathematics through matrix solutions and expand the knowledge of junior high school students, as follows: 

Solving a system of multivariate linear equations belongs to linear algebra. This is also introduced in a previous article. You can check: Related calculations of linear algebra (numpy).
Let’s take a look at the system of three-dimensional linear equations. Through the linear algebra algebra that comes with numpy (np.linalg) to handle:

M=np.mat('1 1 1;0 2 -4;3 2 -6')
A=np.array([0,6,8])
print(np.round(np.linalg.solve(M,A)))
#[ 0.  1. -1.]

So the solution is x=0, y=1, z=-1 . We can verify: the solution of the coefficient matrix M and the unknown number, do the dot product operation, the result should be A (the result column of the system of equations)

print(np.array(np.dot(M,[0,1, -1])).flatten())
#[0 6 8]
M=np.mat('1 -2 1;0 2 -8;-4 5 9')
'''
matrix([[ 1, -2,  1],
        [ 0,  2, -8],
        [-4,  5,  9]])
'''
A=np.array([0,8,-9])
print(np.linalg.solve(M,A))
#[29. 16.  3.]

So the solution is x=29, y=16, z=3.
Let’s verify it in the same way.

print(np.array(np.dot(M,[29,16,3])).flatten())#[ 0  8 -9]

3. Laplace expansion

You can also use Laplace expansion to find solutions to linear equations. Here are several knowledge points, cofactors and Laplace expansion. Here is a picture taken from Baidu Encyclopedia:

In fact, we can understand it this way. The rows and columns where the elements are located are eliminated like elimination music. What is left is the remainder formula, and then the calculation is performed according to the formula.

import copy
#系数矩阵
#余子式:在n阶行列式中,把所在的第i行与第j列划去后,所留下来的n-1阶行列式
def get_minor(j, matrix):
    result = []
    for row in range(len(matrix)):
        if row == 0:
            continue
        temp = []
        for col in range(len(matrix)):
            if col == j:
                continue
            temp.append(matrix[row][col])
        result.append(temp)
    return result


#拉普拉斯展开定理,递归求解行列式的值
def laplace_expansion(matrix):
    if len(matrix) == 2:
        return matrix[1][1] * matrix[0][0] - matrix[1][0] * matrix[0][1]
    elif len(matrix) > 2:
        result = 0
        for i in range(len(matrix)):
            minor_item = get_minor(i, matrix)
            #print(minor_item)
            result += (matrix[0][i] * ((-1) ** i) * laplace_expansion(minor_item))
        return result

#将常数列(等号右边的数)加入进来
def replace(i, a, b):
    dim = len(a)
    result = copy.deepcopy(a)
    for j in range(dim):
        result[j][i] = b[j]
    return result


if __name__ == "__main__":
    A = [[1,1,1],[0,2,-4],[3,2,-6]]
    B = [0,6,8]

    A = [[1,-2,1],[0,2,-8],[-4,5,9]]
    B = [0,8,-9]

    #A = [[1,2,2,3],[0,-2,3,1],[0,2,-1,2],[1,4,2,4]]
    #B = [6,9,10,-2]

    result = []
    c = laplace_expansion(A)
    #print(c)
    if c == 0:print("没有解")
    else:
        for i in range(len(A)):
            m = replace(i,A,B)
            print(m)
            result.append(laplace_expansion(m)/c)
            #print(result)
        print(result)

Guess you like

Origin blog.csdn.net/weixin_41896770/article/details/133231249