-
Notifications
You must be signed in to change notification settings - Fork 2
/
testLapackInverse.cpp
171 lines (144 loc) · 3.94 KB
/
testLapackInverse.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <iostream>
#include <math.h>
#include "Utilities/MatrixTypes.h"
#include "Utilities/UtilFunctions.h"
using easymath::zeros ;
using easymath::rand ;
using easymath::matrix_mult ;
extern "C" {
// LU decomoposition of a general matrix
void dgetrf_(int* M, int *N, double* A, int* lda, int* IPIV, int* INFO);
// generate inverse of a matrix given its LU decomposition
void dgetri_(int* N, double* A, int* lda, int* IPIV, double* WORK, int* lwork, int* INFO);
// // compute matrix determinant
// void dgeicd_(double* A, int* lda, int* N, int* IOPT, double* RCOND, double* det, double* AUX, int* NAUX) ;
}
void inverse(double* A, int N)
{
int *IPIV = new int[N];
int LWORK = N*N;
double *WORK = new double[LWORK];
int INFO;
dgetrf_(&N,&N,A,&N,IPIV,&INFO);
dgetri_(&N,A,&N,IPIV,WORK,&LWORK,&INFO);
delete [] IPIV;
delete [] WORK;
}
void LUdecomposition(double* A, int N){
int *IPIV = new int[N];
int INFO;
dgetrf_(&N,&N,A,&N,IPIV,&INFO);
for (size_t i = 0; i < N; i++)
printf("%d ",IPIV[i]) ;
printf("\n") ;
delete [] IPIV;
}
//void determinant(double* A, double* det, int N){
// int IOPT = 2 ;
// double* RCOND ;
// int LWORK = N*N;
// double *WORK = new double[LWORK];
// dgeicd_(A,&N,&N,&IOPT,RCOND,det,WORK,&LWORK) ;
//}
int main(){
size_t N = 3 ;
matrix2d A = zeros(N,N) ;
for (size_t i = 0; i < A.size(); i++)
for (size_t j = i; j < A[i].size(); j++)
A[i][j] = rand(1.0, 10.0) ;
matrix2d At = zeros(N,N) ;
for (size_t i = 0; i < A.size(); i++)
for (size_t j = 0; j < i+1; j++)
At[i][j] = A[j][i] ;
printf("A:\n") ;
for (size_t i = 0; i < A.size(); i++){
for (size_t j = 0; j < A[i].size(); j++){
printf("%f ",A[i][j]) ;
}
printf("\n") ;
}
printf("A transpose:\n") ;
for (size_t i = 0; i < At.size(); i++){
for (size_t j = 0; j < At[i].size(); j++){
printf("%f ",At[i][j]) ;
}
printf("\n") ;
}
matrix2d B = matrix_mult(A,At) ;
printf("B = A*A: \n") ;
for (size_t i = 0; i < B.size(); i++){
for (size_t j = 0; j < B[i].size(); j++){
printf("%f ",B[i][j]) ;
}
printf("\n") ;
}
double BB[N*N] ;
size_t k = 0 ;
printf("double BB[]: \n") ;
for (size_t i = 0; i < B.size(); i++){
for (size_t j = 0; j < B[i].size(); j++){
BB[k++] = B[i][j] ;
printf("%f ",BB[k-1]) ;
}
printf("\n") ;
}
double CC[N*N] ;
for (size_t i = 0; i < k; i++)
CC[i] = BB[i] ;
inverse(BB, N) ;
k = 0 ;
printf("Overwritten BB now stores inverse: \n") ;
for (size_t i = 0; i < B.size(); i++){
for (size_t j = 0; j < B[i].size(); j++){
printf("%f ",BB[k++]) ;
}
printf("\n") ;
}
matrix2d invB = zeros(N,N) ;
k = 0 ;
printf("matrix2d invB: \n") ;
for (size_t i = 0; i < invB.size(); i++){
for (size_t j = 0; j < invB[i].size(); j++){
invB[i][j] = BB[k++] ;
printf("%f ",invB[i][j]) ;
}
printf("\n") ;
}
matrix2d BinvB = matrix_mult(B,invB) ;
printf("B*invB: \n") ;
for (size_t i = 0; i < BinvB.size(); i++){
for (size_t j = 0; j < BinvB[i].size(); j++){
printf("%f ",BinvB[i][j]) ;
}
printf("\n") ;
}
LUdecomposition(CC,N) ;
k = 0 ;
printf("LU decomposition: \n") ;
for (size_t i = 0; i < B.size(); i++){
for (size_t j = 0; j < B[i].size(); j++){
printf("%f ",CC[k++]) ;
}
printf("\n") ;
}
// double det[2] ;
// determinant(CC,det,N) ;
//
// k = 0 ;
// printf("Overwritten CC now stores inverse: \n") ;
// for (size_t i = 0; i < B.size(); i++){
// for (size_t j = 0; j < B[i].size(); j++){
// printf("%f ",CC[k++]) ;
// }
// printf("\n") ;
// }
//
// double detB = det[0]*pow(10.0,det[1]) ;
// printf("Determinant of B: %f\n", detB) ;
// printf("Determinant of invB = 1/det(B): %f\n", 1.0/detB) ;
//
// determinant(CC,det,N) ;
// double detInvB = det[0]*pow(10.0,det[1]) ;
// printf("Determinant of invB: %f\n", detInvB) ;
return 0;
}