线性代数行列式的计算方法(通过代数余子式计算)

permutation.h/***********************************//*NonstandardPermutationheaderCopyright(C)2021—@情久小羊,#FreeThereisnodataerrorcheck*//***********************************/#pragmaonce#include<initializer_list>#include<xutil.

大家好,又见面了,我是你们的朋友全栈君。

 permutation.h

/***********************************/
/*
Nonstandard Permutation header
Copyright (C) 2021---@情久小羊,#Free
There is no data error check
*/
/***********************************/
#pragma once
#include<initializer_list>
#include <xutility>
#include <xstring>
#include<vector>
#include <stdexcept>
using std::vector;
using std::initializer_list;
class Permutation {
	int rank;//n阶行列式
	vector<vector<int>>data;
public:
	Permutation(int rank, vector<vector<int>> data) {
		if (data.size() != rank)throw std::range_error("行列式大小异常");
		for (int i = 0; i < data.size(); ++i) {
			if (data.at(i).size() != rank)throw std::range_error("行列式大小异常");
		}
		this->data = data;
		this->rank = rank;
	}
	Permutation(int rank, initializer_list<int>data_list) {
		if (data_list.size() != rank * rank)throw std::range_error("行列式大小异常");
		vector<int>v;
		for (auto i = data_list.begin(); i != data_list.end(); ++i) {
			v.push_back(*i);
			if (v.size() == rank) {
				this->data.push_back(v);
				v.clear();
			}
		}
		this->rank = rank;
	}
	void print_permutation() {
		for (auto i = this->data.begin(); i != this->data.end(); ++i) {
			for (auto j = i->begin(); j != i->end(); ++j) {
				cout << *j << " ";
			}
			cout << '\n';
		}
	}
	//计算余子式
	Permutation cal_Cofactor(int row,int col) {
		vector<int>row_data; vector<vector<int>>temp_data;
		for (int i = 0; i < this->rank; ++i) {
			if (i == row - 1)continue;
			for (int j = 0; j < this->rank; ++j) {
				if (j == col - 1)continue;
				row_data.push_back(this->data.at(i).at(j));
			}
			temp_data.push_back(row_data);
			row_data.clear();
		}
		return Permutation(this->rank - 1, temp_data);
	}
	//递归通过代数余子式计算
	int calculate() {
		int n = 0;
		if (this->rank == 1)return this->data.at(0).at(0);
		for (int i = 0; i < this->rank; ++i) {
			n += this->data.at(0).at(i) * pow(-1, i + 2) * this->cal_Cofactor(1, i + 1).calculate();
		}
		return n;
	}
};

 \left| \begin{array}{cccc} x_{11} & x_{12} & \cdots& x_{1n} \\ x_{2_1} & x_{22} & \cdots &x_{2n}\\ \vdots & \vdots & \ddots & \vdots \\ 3 & 3 & \cdots & x_{nn} \end{array} \right|

\sum_{i=1}^nx_{1i}A_{1i}\\

然后继续递归得到结果

 main.cpp

#include"permutation.h"
int main() {
   cout<< Permutation(4,
        { 3, -1, 0, 7,
          1, 0, 1, 5,
          2, 3, -3, 1,
          0, 0, 1, -2}).calculate()<<'\n';

    cout << "~~~~~~~~~~~~~~~~~~~~~~" << '\n';
    Permutation(1, { 1 }).print_permutation();
    return 0;
    
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/126576.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • oracle 优化GROUP BY[通俗易懂]

    提高GROUPBY语句的效率,可以通过将不需要的记录在GROUPBY之前过滤掉.下面两个查询返回相同结果但第二个明显就快了许多.低效:SELECTJOB,AVG(SAL)FROMEMPGROUPJOBHAVINGJOB=‘PRESIDENT’ORJOB=‘MANAGER’高效:SELECTJOB…

    2022年4月10日
    60
  • stata 求输出相关系数矩阵命令_一文读懂结果输出命令大全(上)

    stata 求输出相关系数矩阵命令_一文读懂结果输出命令大全(上)目录描述统计量helptabstat//Stata官方命令描述统计量组间均值差异检验helpttesthelpttable2helpestout相关分析命令helppwcorrhelppwcorr_a回归相关分析命令helpesttabhelpoutreg2helplogoutstata命令汇总helpsum2docx//输出基…

    2022年6月15日
    217
  • goland2021.11.4 激活【中文破解版】「建议收藏」

    (goland2021.11.4 激活)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月29日
    37
  • js获取当前系统时间

    js获取当前系统时间js获取当前年,月,日,时分秒,季度,星期几

    2022年10月18日
    0
  • 关于记忆化搜索

    关于记忆化搜索转载自:http://blog.csdn.net/urecvbnkuhbh_54245df/article/details/5847876记忆化搜索: 算法上依然是搜索的流程,但是搜索到的一些解用动态规划的那种思想和模式作一些保存。一般说来,动态规划总要遍历所有的状态,而搜索可以排除一些无效状态。更重要的是搜索还可以剪枝,可能剪去大量不必要的状态,因此在空间开销上往往比动

    2022年7月26日
    6
  • python 如何安装numpy库?

    python 如何安装numpy库?我正在参与CSDN200进20,希望得到您的支持,扫码续投票5次。感谢您!(为表示感谢,您投票后私信我,我把我总结的人工智能手推笔记和思维导图发送给您,感谢!)首先我们要找到python安装的位置win+R打开进入以后输入:wherepython找到安装目录后,找到Scripts文件夹所在位置:如Programs\Python\Python36\Scripts…

    2022年9月28日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号