-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmddet.c
More file actions
47 lines (38 loc) · 1.22 KB
/
mddet.c
File metadata and controls
47 lines (38 loc) · 1.22 KB
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
#include <mex.h>
#include <string.h>
#include <lapack.h>
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
double *data, *dataEnd, *lu, *det;
const int *dims;
int *detDims;
ptrdiff_t *ipiv;
ptrdiff_t ndims, m, n, dataInc, info;
if (nrhs != 1) mexErrMsgTxt("Exactly one argument is expected.");
ndims = mxGetNumberOfDimensions(prhs[0]);
dims = mxGetDimensions(prhs[0]);
m = dims[0];
n = dims[1];
if (m != n) mexErrMsgTxt("Matrices must be square.");
data = mxGetPr(prhs[0]);
dataInc = n*n;
dataEnd = data + mxGetNumberOfElements(prhs[0]);
lu = mxMalloc(dataInc*sizeof(double));
ipiv = mxMalloc(n*sizeof(long));
detDims = mxMalloc(ndims*sizeof(int));
memcpy(detDims, dims, ndims*sizeof(int));
detDims[0] = 1;
detDims[1] = 1;
plhs[0] = mxCreateNumericArray(ndims, detDims, mxDOUBLE_CLASS, mxREAL);
det = mxGetPr(plhs[0]);
for (; data < dataEnd; data += dataInc, det++) {
int i;
double detLcl = 1, *diag = lu;
memcpy(lu, data, dataInc*sizeof(double));
dgetrf(&n, &n, lu, &n, ipiv, &info);
for (i = 0; i < n; ++i, diag += n + 1)
detLcl *= ((ipiv[i] - 1) != i ? -1 : 1)*(*diag);
*det = detLcl;
}
mxFree(ipiv);
mxFree(lu);
}