# 多线程求行列式值

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<sys/time.h>
#include<stdlib.h>
#include<string.h>
#include<iomanip>
using namespace std;
#define Max_Matrix_size 1000
double Matrix[Max_Matrix_size][Max_Matrix_size];
int Matrix_size;
double result = 0.0;
double det_cal(double **mmatrix,int size){
if(size == 1){
return mmatrix[0][0];
}
else{
double res = 0.0;
double **matrix = (double **)malloc(sizeof(double*) * (size));
for(int i = 0;i < size - 1;i++){
matrix[i] = (double *)malloc(sizeof(double) * (size));
}
for(int i = 0;i < size;i++){
for(int j = 1;j < size;j++){
for(int k = 0;k < size;k++){
if(k < i){
matrix[j - 1][k] = mmatrix[j][k];
}
if(k > i){
matrix[j - 1][k - 1] = mmatrix[j][k];
}
}
}
if(i % 2 == 0){
res += mmatrix[0][i] * det_cal(matrix,size - 1);
}
else{
res -= mmatrix[0][i] * det_cal(matrix,size - 1);
}
}
for (int i = 0; i < size - 1; i++) {
free(matrix[i]);
}
free(matrix);
return res;
}
}
int q = *(int*)arg;
double **matrix = (double **)malloc(sizeof(double*) * (Matrix_size));
for(int i = 0;i < Matrix_size;i++){
matrix[i] = (double *)malloc(sizeof(double) * (Matrix_size));
}
for(int i = q * Per_thread_do;i < ((q + 1) * Per_thread_do);i++){
for(int j = 1;j < Matrix_size;j++){
for(int k = 0;k < Matrix_size;k++){
if(k < i){
matrix[j - 1][k] = Matrix[j][k];
}
if(k > i){
matrix[j - 1][k - 1] = Matrix[j][k];
}
}
}
if(i % 2 == 0){
Per_thread_cal[q] += Matrix[0][i] * det_cal(matrix,Matrix_size - 1);
}

else{
Per_thread_cal[q] -= Matrix[0][i] * det_cal(matrix,Matrix_size - 1);
}
}
}
for(int i = q * Per_thread_do;i < Matrix_size;i++){
for(int j = 1;j < Matrix_size;j++){
for(int k = 0;k < Matrix_size;k++){
if(k < i){
matrix[j - 1][k] = Matrix[j][k];
}
if(k > i){
matrix[j - 1][k - 1] = Matrix[j][k];
}
}
}
if(i % 2 == 0){
Per_thread_cal[q] += Matrix[0][i] * det_cal(matrix,Matrix_size - 1);
}

else{
Per_thread_cal[q] -= Matrix[0][i] * det_cal(matrix,Matrix_size - 1);
}
}
}
for(int i = 0;i < Matrix_size - 1;i++){
free(matrix[i]);
}
free(matrix);
}
int main(int argc,char *argv[]){
if(argc != 4){
return 0;
}
Matrix_size = atoi(argv[1]);
char *input = argv[3];
freopen(input,"r",stdin);
for(int i = 0;i < Matrix_size;i++){
for(int j = 0;j < Matrix_size;j++){
cin>>Matrix[i][j];
}
}
//int id[100];
int *id = (int *)malloc(sizeof(int) * (Thread_num));
struct timeval begin,end;
gettimeofday(&begin,NULL);
for(int i = 0;i < Thread_num;i++){
id[i] = i;
}
for(int i = 0;i < Thread_num;i++){
}
free(id);
cout<<fixed<<setprecision(2)<<result<<endl;
gettimeofday(&end,NULL);
int time = (end.tv_sec - begin.tv_sec) * 1000000 + (end.tv_usec - begin.tv_usec);
printf("time: %d us\n", time);
return 0;
}

#### 求行列式---C语言版（被逼出来的实在是不想用笔算了！！！）

2012-03-05 23:27:47

#### 实现对于n阶行列式的计算

2018-01-19 12:25:11

#### ACM矩阵行列式计算

2012-07-27 15:31:14

#### MPI并行计算与矩阵（2）

2016-10-11 21:22:11

#### MPI C++程序计算行列式的值

2016年07月09日 3KB 下载

#### MPI多进程求行列式值

2016-06-12 18:38:52

#### MPI多进程并行计算矩阵乘法实现

2014-11-15 22:02:05

#### 方阵和的行列式、方阵行列式的和

2016-09-18 09:47:41

#### 多线程编程-矩阵乘法

2016-11-24 10:37:25