矩阵的n次幂

问题:
给定任一矩阵,求其n次幂,复杂度小于O(n)
提示:矩阵相乘,用左边的行乘以右边的列

再分析这个问题前,先思考如何对矩阵进行乘操作,为了使问题更具普遍性,我们来分析行列式相乘的问题
用(x1,y1)的行列式乘以(x2,y2)的行列式

行列式相乘

两个行列式能够相乘 axb 必须满足:a的行等于b的列,最后得到的结果形式和a一模一样(行列和a相等)
用三个for循环

	void ret(const deter&obj){
		deter temp(*this);
		for (int i = 0; i < this->m_row; i++){
			for (int j = 0; j < obj.m_col; j++){
				for (int k = 0; k < this->m_row;k++){
					temp._elem[i][j] += (this->_elem[i][k]*
					obj._elem[k][j]);
				}
			}
		}
		temp.show();
	}

矩阵相乘

	deter nth_power(int n){
		deter temp(*this);
		deter ret(*this);
		ret.unit();
		while (n)
		{
			n & 1 ? ret = ret.ret(temp):1;
			temp = temp.ret(temp);
			n >>= 1;
		}
		ret.show();
		return ret;
	}
	deter nth_power2(int n){
		deter temp(*this);
		while (n>1)
		{
			temp = temp.ret(*this);
			--n;
		}
		temp.show();
		return temp;
	}
发布了145 篇原创文章 · 获赞 12 · 访问量 9639

猜你喜欢

转载自blog.csdn.net/weixin_44997886/article/details/105044556
今日推荐