-
Notifications
You must be signed in to change notification settings - Fork 0
/
qmtrx.cpp
75 lines (60 loc) · 1.7 KB
/
qmtrx.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include "qmtrx.h"
using namespace std;
qmatrix::qmatrix(const int dimension): matrix(dimension, dimension)
{
}
Complex qmatrix::determinante(int dim)
{
qmatrix temp(dim); // temp - matrix
int temp_i=0, temp_j=0; // elements of temp matrix
int temp_pos=1; // current element
// i row of matrix; j column of matrix
int i, j; // elements of main matrix (a)
int s=1; // sign counter
int n=0; // ignored matrix row
int m=0; // ignored matrix column
Complex result=0; // determinante result = result1 + result2
Complex result1=0; // result for - elements (+ - exchange)
Complex result2=0; // result for + elements
// loop for matrix rank larger than 2
if(dim>2)
{
// ignore row (n) column (m)
for(m=0; m<dim; m++)
{
temp_i=0;
temp_j=0;
temp_pos=1;
// ckeck elements of main matrix
for(i=0; i<dim; i++)
{
for(j=0; j<dim; j++)
{
if(i==n || j==m); // if element ignored -> do nothing
else
{
// store selected elements to temp matrix
temp.elem[temp_i][temp_j] = this->elem[i][j];
if(temp_pos%(dim-1)==0) {temp_j=0; temp_i++;} // next elements
else temp_j++;
temp_pos++;
}
}
}
// + - exchange ... | ... function call recursiv
if(s%2==0) {result1 = result1 - this->elem[n][m] * determinante(dim-1);} // control +-+-... changes
else {result2 = result2 + this->elem[n][m] * determinante(dim-1);}
s++;
}
// n matrix
result=result1+result2;
return(result);
}
///** 2 x 2 matrix
else
{
result = (this->elem[0][0]*this->elem[1][1])-(this->elem[0][1]*this->elem[1][0]);
return(result);
}
}