今天在看一篇论文是讲fast multipole method的。好久之前就看过相关的知识,主要是用来加速矩阵和向量之间的乘积。在introduction中提到了Ax = b. 线性方程组一般的解法的算法复杂度是O(N3)。之前就一直想实现一下高斯消元法,但是手懒emmmm。今天终于不懒了,仿照着https://blog.csdn.net/pengwill97/article/details/77200372这篇博客(用C++写的),我顺便用python重写了一遍,主要练习python,哈哈哈哈。附代码如下:
# 2018.8.24 by Jason Wang # reference https://blog.csdn.net/pengwill97/article/details/77200372 import numpy as np # store the solution x= [] # maximum common divisor def gcd(a,b): if b == 0: return a else: return gcd(b,a%b) # least common multiple def lcm(a,b): return a/gcd(a,b)*b #Gauss-Jordan elimination -2:float solution, no integer solution -1: no solution 0:unique solution >0:infinite solution def Gauss(a,equ,var): i = 0 j = 0 k = 0 # convert to step matrix col = 0 for k in range(equ): if c