-
Notifications
You must be signed in to change notification settings - Fork 0
/
inverse_matrix.c
36 lines (30 loc) · 934 Bytes
/
inverse_matrix.c
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
//#include <gsl/gsl_blas.h>
#include <gsl/gsl_errno.h>
#include <lapacke.h>
#include "wmq_error.h"
int inverse_matrix(double m[], int size) {
//https://stackoverflow.com/questions/3519959/computing-the-inverse-of-a-matrix-using-lapack-in-c
int pivot[size];
int err;
dgetrf_(&size, &size, m, &size, pivot, &err);
if (err < 0) {
WMQ_LOG_ERROR("dgetrf_: invalid argument %d", -err);
return WMQE_EXTERNAL_ERROR;
}
if (err > 0) {
WMQ_LOG_ERROR("dgetrf_: singular decomposition");
return WMQE_EXTERNAL_ERROR;
}
int lwork = size;
double work[lwork];
dgetri_(&size, m, &size, pivot, work, &lwork, &err);
if (err < 0) {
WMQ_LOG_ERROR("dgetri_: invalid argument %d", -err);
return WMQE_EXTERNAL_ERROR;
}
if (err > 0) {
WMQ_LOG_ERROR("dgetri_: singular decomposition");
return WMQE_EXTERNAL_ERROR;
}
return 0;
}