forked from fieldtrip/fieldtrip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmtimes3x3.c
170 lines (147 loc) · 4.94 KB
/
mtimes3x3.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
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
#include <math.h>
#include <mex.h>
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
/*declare variables*/
const mwSize *dims;
mwIndex indx;
int i, numdims, indx1, indx2, indx3;
int numelin;
mxClassID classid;
double *input1r, *input1i, *input2r, *input2i, *output1r, *output1i;
double a[3][3], b[3][3];
double ai[3][3], bi[3][3];
/*figure out the classid*/
classid = mxGetClassID(prhs[0]);
/*check inputs*/
if (nrhs!=2)
mexErrMsgTxt("Wrong number of input arguments");
/*associate inputs*/
input1r = mxGetData(prhs[0]);
input1i = mxGetImagData(prhs[0]);
input2r = mxGetData(prhs[1]);
input2i = mxGetImagData(prhs[1]);
/*figure out dimension info and number of elements*/
dims = mxGetDimensions(prhs[0]);
numdims = mxGetNumberOfDimensions(prhs[0]);
numelin = mxGetNumberOfElements(prhs[0]);
/*associate output*/
if (input1i == NULL && input2i == NULL)
{
plhs[0] = mxCreateNumericArray(numdims, dims, classid, mxREAL);
output1r = mxGetData(plhs[0]);
}
else
{
plhs[0] = mxCreateNumericArray(numdims, dims, classid, mxCOMPLEX);
output1r = mxGetData(plhs[0]);
output1i = mxGetImagData(plhs[0]);
}
/* do the computation*/
if (input1i == NULL && input2i == NULL)
{
for (i=0; i<numelin/9; i++)
{
/* real-valued case*/
for (indx2=0; indx2<3; ++indx2)
for (indx1=0; indx1<3; ++indx1)
{
a[indx1][indx2] = input1r[i*9+indx1+indx2*3];
b[indx1][indx2] = input2r[i*9+indx1+indx2*3];
output1r[i*9+indx1+indx2*3] = 0;
}
for (indx3=0; indx3<3; ++indx3)
for (indx2=0; indx2<3; ++indx2)
for (indx1=0; indx1<3; ++indx1)
{
output1r[i*9+indx2+indx3*3] += a[indx2][indx1] * b[indx1][indx3];
}
}
return;
}
else if (input1i == NULL && input2i != NULL)
{
for (i=0; i<numelin/9; i++)
{
/* first input real-valued case, second input complex*/
for (indx2=0; indx2<3; ++indx2)
for (indx1=0; indx1<3; ++indx1)
{
a[indx1][indx2] = input1r[i*9+indx1+indx2*3];
b[indx1][indx2] = input2r[i*9+indx1+indx2*3];
bi[indx1][indx2] = input2i[i*9+indx1+indx2*3];
output1r[i*9+indx1+indx2*3] = 0;
output1i[i*9+indx1+indx2*3] = 0;
}
for (indx3=0; indx3<3; ++indx3)
for (indx2=0; indx2<3; ++indx2)
for (indx1=0; indx1<3; ++indx1)
{
output1r[i*9+indx2+indx3*3] += a[indx2][indx1] * b[indx1][indx3];
output1i[i*9+indx2+indx3*3] += a[indx2][indx1] * bi[indx1][indx3];
}
}
return;
}
else if (input1i != NULL && input2i == NULL)
{
for (i=0; i<numelin/9; i++)
{
/* first input complex-valued, second input real-valued*/
for (indx2=0; indx2<3; ++indx2)
for (indx1=0; indx1<3; ++indx1)
{
a[indx1][indx2] = input1r[i*9+indx1+indx2*3];
b[indx1][indx2] = input2r[i*9+indx1+indx2*3];
ai[indx1][indx2] = input1i[i*9+indx1+indx2*3];
output1r[i*9+indx1+indx2*3] = 0;
output1i[i*9+indx1+indx2*3] = 0;
}
for (indx3=0; indx3<3; ++indx3)
for (indx2=0; indx2<3; ++indx2)
for (indx1=0; indx1<3; ++indx1)
{
output1r[i*9+indx2+indx3*3] += a[indx2][indx1] * b[indx1][indx3];
output1i[i*9+indx2+indx3*3] += ai[indx2][indx1] * b[indx1][indx3];
}
}
return;
}
else
{
for (i=0; i<numelin/9; i++)
{
/* both inputs complex-valued*/
for (indx2=0; indx2<3; ++indx2)
for (indx1=0; indx1<3; ++indx1)
{
a[indx1][indx2] = input1r[i*9+indx1+indx2*3];
b[indx1][indx2] = input2r[i*9+indx1+indx2*3];
ai[indx1][indx2] = input1i[i*9+indx1+indx2*3];
bi[indx1][indx2] = input2i[i*9+indx1+indx2*3];
output1r[i*9+indx1+indx2*3] = 0;
output1i[i*9+indx1+indx2*3] = 0;
}
for (indx3=0; indx3<3; ++indx3)
for (indx2=0; indx2<3; ++indx2)
for (indx1=0; indx1<3; ++indx1)
{
output1r[i*9+indx2+indx3*3] += a[indx2][indx1] * b[indx1][indx3] - ai[indx2][indx1] * bi[indx1][indx3];
output1i[i*9+indx2+indx3*3] += ai[indx2][indx1] * b[indx1][indx3] + a[indx2][indx1] * bi[indx1][indx3];
}
}
return;
}
}
/*in the following c and b are swapped with respect to the above
%a b e f' a' c'
%c d f h b' d'
%
%a*e+b*f a*f'+b*h a' c'
%c*e+d*f c*f'+d*h b' d'
%
%a*e*a'+b*f*a'+a*f'*b'+b*h*b' a*e*c'+b*f*c'+a*f'*d'+b*h*d'
%c*e*a'+d*f*a'+c*f'*b'+d*h*b' c*e*c'+d*f*c'+c*f'*d'+d*h*d'
%
%e*abs(a)^2 + f*(a'*b) + f'*(a*b') + h*abs(b)^2 e*a*c' + f*b*c' + f'*a*d' + h*b*d'
%e*a'*c + f*a'*d + f'*b'*c + h*b'*d e*abs(c)^2 + f*(c'*d) + f'*(c*d') + h*abs(d)^2*/