-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtraceABtrans.c
executable file
·55 lines (47 loc) · 1.42 KB
/
traceABtrans.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*==========================================================
% Mex version to compute the trace of the multiplication of A and B'
%
% The function uses the trace trick to speed up estimation times:
% trace(A*B')=sum(sum(A.*B))
%
% INPUT:
% A: NxK matrix
% B: NxK matrix
%
% OUTPUT:
% tr : the trace of A times B'
%
% WARING: for speed reasons the mex version of this function skips all
% checks involving the size of the matrices, these checks should be
% implemented in the calling function
% (c) Naveed Ejaz 2013 [email protected]
*========================================================*/
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
// (1) check for proper number of input and output arguments
if(nrhs!=2)
mexErrMsgIdAndTxt("MyToolbox:traceAB_faster:nrhs","2 inputs required.");
int N,K;
int i,j, idx1,idx2;
double *ptrA, *ptrB;
double tr=0;
// getting dimensions
N = mxGetM(prhs[0]);
K = mxGetN(prhs[0]);
// array pointers
ptrA = mxGetPr(prhs[0]);
ptrB = mxGetPr(prhs[1]);
// simple matrix multiplication
for (j=0; j<K; j++) {
idx1=j*N;
for(i=0; i<N; i++) {
idx2=i+idx1;
tr += ptrA[idx2]*ptrB[idx2];
}
}
// (5) assigning outputs and returning to matlab
plhs[0] = mxCreateDoubleScalar(tr);
return;
}