矩阵计算复杂度(简洁版)(Computational complexity of matrix)

This blog mainly focuses on the complexity of matrix calculation. I will introduce this topic in three parts: main results, analysis, and proof, code.

I、 Results

Let B\in{\mathbb R}^{n\times m}C\in{\mathbb R}^{m\times l} and invertible matrix A\in{\mathbb R}^{n\times n}. Then we have following computational complexity \Theta

(1)  \Theta(AB)=O(mn^2);

(2) \Theta(ABC)=O(mn^2)+O(mnl);

(3) \Theta(A^{-1})=O(n^3);

II、 Analysis and proof

2.1 Definition

The usual computation for integer multiplication has a complexity of O(n^2). This means that there is a constant \eta such that the multiplication of two integers of at most n digits may be done in a time less than \eta n^2.

2.2 Analysis

For model (1): Since A is a n rows and n column and B is n rows and m column, then there is n\times m \times n times to multiplyA B, i.e., O(mn^2).

For model (2): Similar reason as (1) enable us to get there is n\times m \times n times plus m\times l \times n  times to multiplyA B, i.e., O(mn^2)+O(mnl).

For model (3): Based on PA = E, we can get invertible matrix through elementary row transform, i.e.,

[E; A] = [A^{-1}; E].

Hence we only need to consider how many times multiplication in the process of elementary row transforms. By easy calculation, we get n\times n+ (n-1)\times n + ...+1\times n +n = \frac{(1+n)n}{2}n+n=\frac{1}{2}n^3+\frac{3}{2}n = O(n^3).

III、 CODE

I omit the code for model(1) and model(2) as it is too easy. 

code idea for model(3) :(Gaussian Elimination)

# input an invertible matrix A
# Suppose a ideal condition: no row transform

import sys
 
 
class MatrixInverse:
    def __init__(self, matrix):
        self.matrix = matrix
        self.a = len(self.matrix)
        self.b = len(self.matrix[0])
        if self.a != self.b:
            print("This is a vertible matrix")
 
    def judge(self):
        c = 1
        e = 0
        m = self.matrix
        for i in range(self.a):
            for j in range(self.a):
                c = c * m[j % self.a][(i + j) % self.a]
                # print(f"{j % self.a},{(i + j) % self.a}")
            e = e + c
            c = 1
        for i in range(self.a):
            for j, k in zip(range(0, self.a*2, 2), range(self.a)):
                c = c * m[k % self.a][(i + j) % self.a]
                # print(f"{k % self.a},{(i + j) % self.a}")
            e = e - c
            c = 1
        print(f"该矩阵的值为:{e}", end=",")
        if e != 0:
            print("Exist invertible matrix。")
        else:
            print("No invertible matrix。")
        return e
 
    def calculate(self):
        """Use basic row change mathod"""
        if MatrixInverse.judge(self) == 0:
            sys.exit()
        d = [[0 for i in range(self.a*2)] for j in range(self.a)]
        e = [[0 for i in range(self.a)] for j in range(self.a)]
        for i, j in zip(range(self.a), range(self.a)):
            e[i][j] = 1
        for i in range(self.a):
            for j in range(self.a):
                d[i][j] = self.matrix[i][j]
        for i in range(self.a):
            for j in range(self.a, self.a*2):
                d[i][j] = e[i][j-self.a]
        """Choose pivot again"""
        m1 = []
        for i in range(self.a):
            m1.append(i)
        for i in range(self.a):
            m = 0
            while m < self.a:  # choose a suitable pivot
                if d[m][i] != 0 and i < self.a and m in m1:
                    c2 = d[m][i]  # c2 is pivot, m is row,i is column。
                    for x in range(self.a*2):  # let pivot be 1
                        d[m][x] = d[m][x]/c2  # 
                    for j in range(self.a):  # divide by pivot row
                        c3 = d[j][i]
                        for k in range(self.a*2):
                            if j != m:
                                d[j][k] = d[j][k]/c3 - d[m][k]
                    m1.remove(m)
                    m = self.a  # this column finished next column
                else:
                    m += 1
        for i in range(self.a):
            for j in range(self.a):
                if d[i][j] != 0:
                    c5 = d[i][j]
                    for k in range(self.a*2):
                        d[i][k] = d[i][k]/c5
        """Save invertible matrxi into d2"""
        d2 = [[0 for i in range(self.a)] for j in range(self.a)]
        for i in range(self.a):
            for j in range(self.a, self.a*2):
                d2[i][j-self.a] = float('%.2f' % d[i][j])
        print("The invertible matrix of A is:")
        print(d2)
 
 
n = [[1, 2, 3], [2, 1, 3], [3, 4, 3]]
s = MatrixInverse(n)
s.calculate()

计算复杂性:Sanjeev Arora》是一本深入解析计算复杂性领域的重要著作,该书由计算机科学家Sanjeev Arora撰写。 这本书主要关注计算问题的复杂性理论,并探讨了许多重要的计算难题。计算复杂性是研究计算问题的难度和可解性的一门学科。在现实世界中,许多问题是不易解决的,尤其是当问题规模变大时。通过分析问题的计算复杂性,我们可以判断一个问题是否可行,并且找到解决问题的最好方法。 Arora在书中介绍了一些计算复杂性的基本概念和原则。他介绍了三个重要的计算问题类别:P类、NP类和困难问题。P类包括那些可以在多项式时间内解决的问题。NP类则包括那些可以在多项式时间内验证的问题,但尚未找到快速解决方案。困难问题是那些尚未找到多项式时间解决方案,并且在理论上被认为是难解的问题。 Arora详细介绍了各种计算难题,并提出了一些重要的研究方向,例如近似算法和证明等。近似算法是在有限时间内找到问题的次优解的研究。证明则是使用数学技术来论证问题的复杂性和可解性。 这本书是一部重要的参考资料,适用于计算机科学家、研究人员和学生。它提供了一个深入的理论基础,使读者能够更好地理解和解决计算复杂性问题。这本书的贡献在于它的全面性和剖析良好的讲解方式。无论是理论研究还是实际应用,这本书都为我们提供了有价值的洞察和指导。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yita_matrix

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值