ivan@137
|
1 /**************************************************************************
|
ivan@137
|
2 *
|
ivan@137
|
3 * File name: myblas.c
|
ivan@137
|
4 *
|
ivan@137
|
5 * Ron Rubinstein
|
ivan@137
|
6 * Computer Science Department
|
ivan@137
|
7 * Technion, Haifa 32000 Israel
|
ivan@137
|
8 * ronrubin@cs
|
ivan@137
|
9 *
|
ivan@137
|
10 * Version: 1.1
|
ivan@137
|
11 * Last updated: 13.8.2009
|
ivan@137
|
12 *
|
ivan@137
|
13 *************************************************************************/
|
ivan@137
|
14
|
ivan@137
|
15
|
ivan@137
|
16 #include "myblas.h"
|
ivan@137
|
17 #include <ctype.h>
|
ivan@137
|
18
|
ivan@137
|
19
|
ivan@137
|
20 /* find maximum of absolute values */
|
ivan@137
|
21
|
ivan@137
|
22 mwIndex maxabs(double c[], mwSize m)
|
ivan@137
|
23 {
|
ivan@137
|
24 mwIndex maxid=0, k;
|
ivan@137
|
25 double absval, maxval = SQR(*c); /* use square which is quicker than absolute value */
|
ivan@137
|
26
|
ivan@137
|
27 for (k=1; k<m; ++k) {
|
ivan@137
|
28 absval = SQR(c[k]);
|
ivan@137
|
29 if (absval > maxval) {
|
ivan@137
|
30 maxval = absval;
|
ivan@137
|
31 maxid = k;
|
ivan@137
|
32 }
|
ivan@137
|
33 }
|
ivan@137
|
34 return maxid;
|
ivan@137
|
35 }
|
ivan@137
|
36
|
ivan@137
|
37
|
ivan@137
|
38 /* compute y := alpha*x + y */
|
ivan@137
|
39
|
ivan@137
|
40 void vec_sum(double alpha, double x[], double y[], mwSize n)
|
ivan@137
|
41 {
|
ivan@137
|
42 mwIndex i;
|
ivan@137
|
43
|
ivan@137
|
44 for (i=0; i<n; ++i) {
|
ivan@137
|
45 y[i] += alpha*x[i];
|
ivan@137
|
46 }
|
ivan@137
|
47 }
|
ivan@137
|
48
|
ivan@137
|
49 /* compute y := alpha*x .* y */
|
ivan@137
|
50
|
ivan@137
|
51 void vec_smult(double alpha, double x[], double y[], mwSize n)
|
ivan@137
|
52 {
|
ivan@137
|
53 mwIndex i;
|
ivan@137
|
54
|
ivan@137
|
55 for (i=0; i<n; ++i) {
|
ivan@137
|
56 y[i] *= alpha*x[i];
|
ivan@137
|
57 }
|
ivan@137
|
58 }
|
ivan@137
|
59
|
ivan@137
|
60 /* compute y := alpha*A*x */
|
ivan@137
|
61
|
ivan@137
|
62 void mat_vec(double alpha, double A[], double x[], double y[], mwSize n, mwSize m)
|
ivan@137
|
63 {
|
ivan@137
|
64 mwIndex i, j, i_n;
|
ivan@137
|
65 double *Ax;
|
ivan@137
|
66
|
ivan@137
|
67 Ax = mxCalloc(n,sizeof(double));
|
ivan@137
|
68
|
ivan@137
|
69 for (i=0; i<m; ++i) {
|
ivan@137
|
70 i_n = i*n;
|
ivan@137
|
71 for (j=0; j<n; ++j) {
|
ivan@137
|
72 Ax[j] += A[i_n+j] * x[i];
|
ivan@137
|
73 }
|
ivan@137
|
74 }
|
ivan@137
|
75
|
ivan@137
|
76 for (j=0; j<n; ++j) {
|
ivan@137
|
77 y[j] = alpha*Ax[j];
|
ivan@137
|
78 }
|
ivan@137
|
79
|
ivan@137
|
80 mxFree(Ax);
|
ivan@137
|
81 }
|
ivan@137
|
82
|
ivan@137
|
83
|
ivan@137
|
84 /* compute y := alpha*A'*x */
|
ivan@137
|
85
|
ivan@137
|
86 void matT_vec(double alpha, double A[], double x[], double y[], mwSize n, mwSize m)
|
ivan@137
|
87 {
|
ivan@137
|
88 mwIndex i, j, n_i;
|
ivan@137
|
89 double sum0, sum1, sum2, sum3;
|
ivan@137
|
90
|
ivan@137
|
91 for (j=0; j<m; ++j) {
|
ivan@137
|
92 y[j] = 0;
|
ivan@137
|
93 }
|
ivan@137
|
94
|
ivan@137
|
95 /* use loop unrolling to accelerate computation */
|
ivan@137
|
96
|
ivan@137
|
97 for (i=0; i<m; ++i) {
|
ivan@137
|
98 n_i = n*i;
|
ivan@137
|
99 sum0 = sum1 = sum2 = sum3 = 0;
|
ivan@137
|
100 for (j=0; j+4<n; j+=4) {
|
ivan@137
|
101 sum0 += A[n_i+j]*x[j];
|
ivan@137
|
102 sum1 += A[n_i+j+1]*x[j+1];
|
ivan@137
|
103 sum2 += A[n_i+j+2]*x[j+2];
|
ivan@137
|
104 sum3 += A[n_i+j+3]*x[j+3];
|
ivan@137
|
105 }
|
ivan@137
|
106 y[i] += alpha * ((sum0 + sum1) + (sum2 + sum3));
|
ivan@137
|
107 while (j<n) {
|
ivan@137
|
108 y[i] += alpha*A[n_i+j]*x[j];
|
ivan@137
|
109 j++;
|
ivan@137
|
110 }
|
ivan@137
|
111 }
|
ivan@137
|
112 }
|
ivan@137
|
113
|
ivan@137
|
114
|
ivan@137
|
115 /* compute y := alpha*A*x */
|
ivan@137
|
116
|
ivan@137
|
117 void mat_sp_vec(double alpha, double pr[], mwIndex ir[], mwIndex jc[], double x[], double y[], mwSize n, mwSize m)
|
ivan@137
|
118 {
|
ivan@137
|
119
|
ivan@137
|
120 mwIndex i, j, j1, j2;
|
ivan@137
|
121
|
ivan@137
|
122 for (i=0; i<n; ++i) {
|
ivan@137
|
123 y[i] = 0;
|
ivan@137
|
124 }
|
ivan@137
|
125
|
ivan@137
|
126 j2 = jc[0];
|
ivan@137
|
127 for (i=0; i<m; ++i) {
|
ivan@137
|
128 j1 = j2; j2 = jc[i+1];
|
ivan@137
|
129 for (j=j1; j<j2; ++j) {
|
ivan@137
|
130 y[ir[j]] += alpha * pr[j] * x[i];
|
ivan@137
|
131 }
|
ivan@137
|
132 }
|
ivan@137
|
133
|
ivan@137
|
134 }
|
ivan@137
|
135
|
ivan@137
|
136
|
ivan@137
|
137 /* compute y := alpha*A'*x */
|
ivan@137
|
138
|
ivan@137
|
139 void matT_sp_vec(double alpha, double pr[], mwIndex ir[], mwIndex jc[], double x[], double y[], mwSize n, mwSize m)
|
ivan@137
|
140 {
|
ivan@137
|
141
|
ivan@137
|
142 mwIndex i, j, j1, j2;
|
ivan@137
|
143
|
ivan@137
|
144 for (i=0; i<m; ++i) {
|
ivan@137
|
145 y[i] = 0;
|
ivan@137
|
146 }
|
ivan@137
|
147
|
ivan@137
|
148 j2 = jc[0];
|
ivan@137
|
149 for (i=0; i<m; ++i) {
|
ivan@137
|
150 j1 = j2; j2 = jc[i+1];
|
ivan@137
|
151 for (j=j1; j<j2; ++j) {
|
ivan@137
|
152 y[i] += alpha * pr[j] * x[ir[j]];
|
ivan@137
|
153 }
|
ivan@137
|
154 }
|
ivan@137
|
155
|
ivan@137
|
156 }
|
ivan@137
|
157
|
ivan@137
|
158
|
ivan@137
|
159 /* compute y := alpha*A*x */
|
ivan@137
|
160
|
ivan@137
|
161 void mat_vec_sp(double alpha, double A[], double pr[], mwIndex ir[], mwIndex jc[], double y[], mwSize n, mwSize m)
|
ivan@137
|
162 {
|
ivan@137
|
163
|
ivan@137
|
164 mwIndex i, j, j_n, k, kend;
|
ivan@137
|
165
|
ivan@137
|
166 for (i=0; i<n; ++i) {
|
ivan@137
|
167 y[i] = 0;
|
ivan@137
|
168 }
|
ivan@137
|
169
|
ivan@137
|
170 kend = jc[1];
|
ivan@137
|
171 if (kend==0) { /* x is empty */
|
ivan@137
|
172 return;
|
ivan@137
|
173 }
|
ivan@137
|
174
|
ivan@137
|
175 for (k=0; k<kend; ++k) {
|
ivan@137
|
176 j = ir[k];
|
ivan@137
|
177 j_n = j*n;
|
ivan@137
|
178 for (i=0; i<n; ++i) {
|
ivan@137
|
179 y[i] += alpha * A[i+j_n] * pr[k];
|
ivan@137
|
180 }
|
ivan@137
|
181 }
|
ivan@137
|
182
|
ivan@137
|
183 }
|
ivan@137
|
184
|
ivan@137
|
185
|
ivan@137
|
186 /* compute y := alpha*A'*x */
|
ivan@137
|
187
|
ivan@137
|
188 void matT_vec_sp(double alpha, double A[], double pr[], mwIndex ir[], mwIndex jc[], double y[], mwSize n, mwSize m)
|
ivan@137
|
189 {
|
ivan@137
|
190
|
ivan@137
|
191 mwIndex i, j, j_n, k, kend;
|
ivan@137
|
192
|
ivan@137
|
193 for (i=0; i<m; ++i) {
|
ivan@137
|
194 y[i] = 0;
|
ivan@137
|
195 }
|
ivan@137
|
196
|
ivan@137
|
197 kend = jc[1];
|
ivan@137
|
198 if (kend==0) { /* x is empty */
|
ivan@137
|
199 return;
|
ivan@137
|
200 }
|
ivan@137
|
201
|
ivan@137
|
202 for (j=0; j<m; ++j) {
|
ivan@137
|
203 j_n = j*n;
|
ivan@137
|
204 for (k=0; k<kend; ++k) {
|
ivan@137
|
205 i = ir[k];
|
ivan@137
|
206 y[j] += alpha * A[i+j_n] * pr[k];
|
ivan@137
|
207 }
|
ivan@137
|
208 }
|
ivan@137
|
209
|
ivan@137
|
210 }
|
ivan@137
|
211
|
ivan@137
|
212
|
ivan@137
|
213 /* compute y := alpha*A*x */
|
ivan@137
|
214
|
ivan@137
|
215 void mat_sp_vec_sp(double alpha, double pr[], mwIndex ir[], mwIndex jc[], double prx[], mwIndex irx[], mwIndex jcx[], double y[], mwSize n, mwSize m)
|
ivan@137
|
216 {
|
ivan@137
|
217
|
ivan@137
|
218 mwIndex i, j, k, kend, j1, j2;
|
ivan@137
|
219
|
ivan@137
|
220 for (i=0; i<n; ++i) {
|
ivan@137
|
221 y[i] = 0;
|
ivan@137
|
222 }
|
ivan@137
|
223
|
ivan@137
|
224 kend = jcx[1];
|
ivan@137
|
225 if (kend==0) { /* x is empty */
|
ivan@137
|
226 return;
|
ivan@137
|
227 }
|
ivan@137
|
228
|
ivan@137
|
229 for (k=0; k<kend; ++k) {
|
ivan@137
|
230 i = irx[k];
|
ivan@137
|
231 j1 = jc[i]; j2 = jc[i+1];
|
ivan@137
|
232 for (j=j1; j<j2; ++j) {
|
ivan@137
|
233 y[ir[j]] += alpha * pr[j] * prx[k];
|
ivan@137
|
234 }
|
ivan@137
|
235 }
|
ivan@137
|
236
|
ivan@137
|
237 }
|
ivan@137
|
238
|
ivan@137
|
239
|
ivan@137
|
240 /* compute y := alpha*A'*x */
|
ivan@137
|
241
|
ivan@137
|
242 void matT_sp_vec_sp(double alpha, double pr[], mwIndex ir[], mwIndex jc[], double prx[], mwIndex irx[], mwIndex jcx[], double y[], mwSize n, mwSize m)
|
ivan@137
|
243 {
|
ivan@137
|
244
|
ivan@137
|
245 mwIndex i, j, k, jend, kend, jadd, kadd, delta;
|
ivan@137
|
246
|
ivan@137
|
247 for (i=0; i<m; ++i) {
|
ivan@137
|
248 y[i] = 0;
|
ivan@137
|
249 }
|
ivan@137
|
250
|
ivan@137
|
251 kend = jcx[1];
|
ivan@137
|
252 if (kend==0) { /* x is empty */
|
ivan@137
|
253 return;
|
ivan@137
|
254 }
|
ivan@137
|
255
|
ivan@137
|
256 for (i=0; i<m; ++i) {
|
ivan@137
|
257 j = jc[i];
|
ivan@137
|
258 jend = jc[i+1];
|
ivan@137
|
259 k = 0;
|
ivan@137
|
260 while (j<jend && k<kend) {
|
ivan@137
|
261
|
ivan@137
|
262 delta = ir[j] - irx[k];
|
ivan@137
|
263
|
ivan@137
|
264 if (delta) { /* if indices differ - increment the smaller one */
|
ivan@137
|
265 jadd = delta<0;
|
ivan@137
|
266 kadd = 1-jadd;
|
ivan@137
|
267 j += jadd;
|
ivan@137
|
268 k += kadd;
|
ivan@137
|
269 }
|
ivan@137
|
270
|
ivan@137
|
271 else { /* indices are equal - add to result and increment both */
|
ivan@137
|
272 y[i] += alpha * pr[j] * prx[k];
|
ivan@137
|
273 j++; k++;
|
ivan@137
|
274 }
|
ivan@137
|
275 }
|
ivan@137
|
276 }
|
ivan@137
|
277
|
ivan@137
|
278 }
|
ivan@137
|
279
|
ivan@137
|
280
|
ivan@137
|
281 /* matrix-matrix multiplication */
|
ivan@137
|
282
|
ivan@137
|
283 void mat_mat(double alpha, double A[], double B[], double X[], mwSize n, mwSize m, mwSize k)
|
ivan@137
|
284 {
|
ivan@137
|
285 mwIndex i1, i2, i3, iX, iA, i2_n;
|
ivan@137
|
286 double b;
|
ivan@137
|
287
|
ivan@137
|
288 for (i1=0; i1<n*k; i1++) {
|
ivan@137
|
289 X[i1] = 0;
|
ivan@137
|
290 }
|
ivan@137
|
291
|
ivan@137
|
292 for (i2=0; i2<m; ++i2) {
|
ivan@137
|
293 i2_n = i2*n;
|
ivan@137
|
294 iX = 0;
|
ivan@137
|
295 for (i3=0; i3<k; ++i3) {
|
ivan@137
|
296 iA = i2_n;
|
ivan@137
|
297 b = B[i2+i3*m];
|
ivan@137
|
298 for (i1=0; i1<n; ++i1) {
|
ivan@137
|
299 X[iX++] += A[iA++]*b;
|
ivan@137
|
300 }
|
ivan@137
|
301 }
|
ivan@137
|
302 }
|
ivan@137
|
303
|
ivan@137
|
304 for (i1=0; i1<n*k; i1++) {
|
ivan@137
|
305 X[i1] *= alpha;
|
ivan@137
|
306 }
|
ivan@137
|
307 }
|
ivan@137
|
308
|
ivan@137
|
309
|
ivan@137
|
310 /* matrix-transpose-matrix multiplication */
|
ivan@137
|
311
|
ivan@137
|
312 void matT_mat(double alpha, double A[], double B[], double X[], mwSize n, mwSize m, mwSize k)
|
ivan@137
|
313 {
|
ivan@137
|
314 mwIndex i1, i2, i3, iX, iA, i2_n;
|
ivan@137
|
315 double *x, sum0, sum1, sum2, sum3;
|
ivan@137
|
316
|
ivan@137
|
317 for (i2=0; i2<m; ++i2) {
|
ivan@137
|
318 for (i3=0; i3<k; ++i3) {
|
ivan@137
|
319 sum0 = sum1 = sum2 = sum3 = 0;
|
ivan@137
|
320 for (i1=0; i1+4<n; i1+=4) {
|
ivan@137
|
321 sum0 += A[i1+0+i2*n]*B[i1+0+i3*n];
|
ivan@137
|
322 sum1 += A[i1+1+i2*n]*B[i1+1+i3*n];
|
ivan@137
|
323 sum2 += A[i1+2+i2*n]*B[i1+2+i3*n];
|
ivan@137
|
324 sum3 += A[i1+3+i2*n]*B[i1+3+i3*n];
|
ivan@137
|
325 }
|
ivan@137
|
326 X[i2+i3*m] = (sum0+sum1) + (sum2+sum3);
|
ivan@137
|
327 while(i1<n) {
|
ivan@137
|
328 X[i2+i3*m] += A[i1+i2*n]*B[i1+i3*n];
|
ivan@137
|
329 i1++;
|
ivan@137
|
330 }
|
ivan@137
|
331 }
|
ivan@137
|
332 }
|
ivan@137
|
333
|
ivan@137
|
334 for (i1=0; i1<m*k; i1++) {
|
ivan@137
|
335 X[i1] *= alpha;
|
ivan@137
|
336 }
|
ivan@137
|
337 }
|
ivan@137
|
338
|
ivan@137
|
339
|
ivan@137
|
340 /* tensor-matrix product */
|
ivan@137
|
341
|
ivan@137
|
342 void tens_mat(double alpha, double A[], double B[], double X[], mwSize n, mwSize m, mwSize k, mwSize l)
|
ivan@137
|
343 {
|
ivan@137
|
344 mwIndex i1, i2, i3, i4, i2_n, nml;
|
ivan@137
|
345 double b;
|
ivan@137
|
346
|
ivan@137
|
347 nml = n*m*l;
|
ivan@137
|
348 for (i1=0; i1<nml; ++i1) {
|
ivan@137
|
349 X[i1] = 0;
|
ivan@137
|
350 }
|
ivan@137
|
351
|
ivan@137
|
352 for (i2=0; i2<m; ++i2) {
|
ivan@137
|
353 i2_n = i2*n;
|
ivan@137
|
354 for (i3=0; i3<k; ++i3) {
|
ivan@137
|
355 for (i4=0; i4<l; ++i4) {
|
ivan@137
|
356 b = B[i4+i3*l];
|
ivan@137
|
357 for (i1=0; i1<n; ++i1) {
|
ivan@137
|
358 X[i1 + i2_n + i4*n*m] += A[i1 + i2_n + i3*n*m] * b;
|
ivan@137
|
359 }
|
ivan@137
|
360 }
|
ivan@137
|
361 }
|
ivan@137
|
362 }
|
ivan@137
|
363
|
ivan@137
|
364 for (i1=0; i1<nml; ++i1) {
|
ivan@137
|
365 X[i1] *= alpha;
|
ivan@137
|
366 }
|
ivan@137
|
367 }
|
ivan@137
|
368
|
ivan@137
|
369
|
ivan@137
|
370 /* tensor-matrix-transpose product */
|
ivan@137
|
371
|
ivan@137
|
372 void tens_matT(double alpha, double A[], double B[], double X[], mwSize n, mwSize m, mwSize k, mwSize l)
|
ivan@137
|
373 {
|
ivan@137
|
374 mwIndex i1, i2, i3, i4, i2_n, nml;
|
ivan@137
|
375 double b;
|
ivan@137
|
376
|
ivan@137
|
377 nml = n*m*l;
|
ivan@137
|
378 for (i1=0; i1<nml; ++i1) {
|
ivan@137
|
379 X[i1] = 0;
|
ivan@137
|
380 }
|
ivan@137
|
381
|
ivan@137
|
382 for (i2=0; i2<m; ++i2) {
|
ivan@137
|
383 i2_n = i2*n;
|
ivan@137
|
384 for (i4=0; i4<l; ++i4) {
|
ivan@137
|
385 for (i3=0; i3<k; ++i3) {
|
ivan@137
|
386 b = B[i3+i4*k];
|
ivan@137
|
387 for (i1=0; i1<n; ++i1) {
|
ivan@137
|
388 X[i1 + i2_n + i4*n*m] += A[i1 + i2_n + i3*n*m] * b;
|
ivan@137
|
389 }
|
ivan@137
|
390 }
|
ivan@137
|
391 }
|
ivan@137
|
392 }
|
ivan@137
|
393
|
ivan@137
|
394 for (i1=0; i1<nml; ++i1) {
|
ivan@137
|
395 X[i1] *= alpha;
|
ivan@137
|
396 }
|
ivan@137
|
397 }
|
ivan@137
|
398
|
ivan@137
|
399
|
ivan@137
|
400 /* dot product */
|
ivan@137
|
401
|
ivan@137
|
402 double dotprod(double a[], double b[], mwSize n)
|
ivan@137
|
403 {
|
ivan@137
|
404 double sum = 0;
|
ivan@137
|
405 mwIndex i;
|
ivan@137
|
406 for (i=0; i<n; ++i)
|
ivan@137
|
407 sum += a[i]*b[i];
|
ivan@137
|
408 return sum;
|
ivan@137
|
409 }
|
ivan@137
|
410
|
ivan@137
|
411
|
ivan@137
|
412 /* find maximum of vector */
|
ivan@137
|
413
|
ivan@137
|
414 mwIndex maxpos(double c[], mwSize m)
|
ivan@137
|
415 {
|
ivan@137
|
416 mwIndex maxid=0, k;
|
ivan@137
|
417 double val, maxval = *c;
|
ivan@137
|
418
|
ivan@137
|
419 for (k=1; k<m; ++k) {
|
ivan@137
|
420 val = c[k];
|
ivan@137
|
421 if (val > maxval) {
|
ivan@137
|
422 maxval = val;
|
ivan@137
|
423 maxid = k;
|
ivan@137
|
424 }
|
ivan@137
|
425 }
|
ivan@137
|
426 return maxid;
|
ivan@137
|
427 }
|
ivan@137
|
428
|
ivan@137
|
429
|
ivan@137
|
430 /* solve L*x = b */
|
ivan@137
|
431
|
ivan@137
|
432 void backsubst_L(double L[], double b[], double x[], mwSize n, mwSize k)
|
ivan@137
|
433 {
|
ivan@137
|
434 mwIndex i, j;
|
ivan@137
|
435 double rhs;
|
ivan@137
|
436
|
ivan@137
|
437 for (i=0; i<k; ++i) {
|
ivan@137
|
438 rhs = b[i];
|
ivan@137
|
439 for (j=0; j<i; ++j) {
|
ivan@137
|
440 rhs -= L[j*n+i]*x[j];
|
ivan@137
|
441 }
|
ivan@137
|
442 x[i] = rhs/L[i*n+i];
|
ivan@137
|
443 }
|
ivan@137
|
444 }
|
ivan@137
|
445
|
ivan@137
|
446
|
ivan@137
|
447 /* solve L'*x = b */
|
ivan@137
|
448
|
ivan@137
|
449 void backsubst_Lt(double L[], double b[], double x[], mwSize n, mwSize k)
|
ivan@137
|
450 {
|
ivan@137
|
451 mwIndex i, j;
|
ivan@137
|
452 double rhs;
|
ivan@137
|
453
|
ivan@137
|
454 for (i=k; i>=1; --i) {
|
ivan@137
|
455 rhs = b[i-1];
|
ivan@137
|
456 for (j=i; j<k; ++j) {
|
ivan@137
|
457 rhs -= L[(i-1)*n+j]*x[j];
|
ivan@137
|
458 }
|
ivan@137
|
459 x[i-1] = rhs/L[(i-1)*n+i-1];
|
ivan@137
|
460 }
|
ivan@137
|
461 }
|
ivan@137
|
462
|
ivan@137
|
463
|
ivan@137
|
464 /* solve U*x = b */
|
ivan@137
|
465
|
ivan@137
|
466 void backsubst_U(double U[], double b[], double x[], mwSize n, mwSize k)
|
ivan@137
|
467 {
|
ivan@137
|
468 mwIndex i, j;
|
ivan@137
|
469 double rhs;
|
ivan@137
|
470
|
ivan@137
|
471 for (i=k; i>=1; --i) {
|
ivan@137
|
472 rhs = b[i-1];
|
ivan@137
|
473 for (j=i; j<k; ++j) {
|
ivan@137
|
474 rhs -= U[j*n+i-1]*x[j];
|
ivan@137
|
475 }
|
ivan@137
|
476 x[i-1] = rhs/U[(i-1)*n+i-1];
|
ivan@137
|
477 }
|
ivan@137
|
478 }
|
ivan@137
|
479
|
ivan@137
|
480
|
ivan@137
|
481 /* solve U'*x = b */
|
ivan@137
|
482
|
ivan@137
|
483 void backsubst_Ut(double U[], double b[], double x[], mwSize n, mwSize k)
|
ivan@137
|
484 {
|
ivan@137
|
485 mwIndex i, j;
|
ivan@137
|
486 double rhs;
|
ivan@137
|
487
|
ivan@137
|
488 for (i=0; i<k; ++i) {
|
ivan@137
|
489 rhs = b[i];
|
ivan@137
|
490 for (j=0; j<i; ++j) {
|
ivan@137
|
491 rhs -= U[i*n+j]*x[j];
|
ivan@137
|
492 }
|
ivan@137
|
493 x[i] = rhs/U[i*n+i];
|
ivan@137
|
494 }
|
ivan@137
|
495 }
|
ivan@137
|
496
|
ivan@137
|
497
|
ivan@137
|
498 /* back substitution solver */
|
ivan@137
|
499
|
ivan@137
|
500 void backsubst(char ul, double A[], double b[], double x[], mwSize n, mwSize k)
|
ivan@137
|
501 {
|
ivan@137
|
502 if (tolower(ul) == 'u') {
|
ivan@137
|
503 backsubst_U(A, b, x, n, k);
|
ivan@137
|
504 }
|
ivan@137
|
505 else if (tolower(ul) == 'l') {
|
ivan@137
|
506 backsubst_L(A, b, x, n, k);
|
ivan@137
|
507 }
|
ivan@137
|
508 else {
|
ivan@137
|
509 mexErrMsgTxt("Invalid triangular matrix type: must be ''U'' or ''L''");
|
ivan@137
|
510 }
|
ivan@137
|
511 }
|
ivan@137
|
512
|
ivan@137
|
513
|
ivan@137
|
514 /* solve equation set using cholesky decomposition */
|
ivan@137
|
515
|
ivan@137
|
516 void cholsolve(char ul, double A[], double b[], double x[], mwSize n, mwSize k)
|
ivan@137
|
517 {
|
ivan@137
|
518 double *tmp;
|
ivan@137
|
519
|
ivan@137
|
520 tmp = mxMalloc(k*sizeof(double));
|
ivan@137
|
521
|
ivan@137
|
522 if (tolower(ul) == 'l') {
|
ivan@137
|
523 backsubst_L(A, b, tmp, n, k);
|
ivan@137
|
524 backsubst_Lt(A, tmp, x, n, k);
|
ivan@137
|
525 }
|
ivan@137
|
526 else if (tolower(ul) == 'u') {
|
ivan@137
|
527 backsubst_Ut(A, b, tmp, n, k);
|
ivan@137
|
528 backsubst_U(A, tmp, x, n, k);
|
ivan@137
|
529 }
|
ivan@137
|
530 else {
|
ivan@137
|
531 mexErrMsgTxt("Invalid triangular matrix type: must be either ''U'' or ''L''");
|
ivan@137
|
532 }
|
ivan@137
|
533
|
ivan@137
|
534 mxFree(tmp);
|
ivan@137
|
535 }
|
ivan@137
|
536
|
ivan@137
|
537
|
ivan@137
|
538 /* perform a permutation assignment y := x(ind(1:k)) */
|
ivan@137
|
539
|
ivan@137
|
540 void vec_assign(double y[], double x[], mwIndex ind[], mwSize k)
|
ivan@137
|
541 {
|
ivan@137
|
542 mwIndex i;
|
ivan@137
|
543
|
ivan@137
|
544 for (i=0; i<k; ++i)
|
ivan@137
|
545 y[i] = x[ind[i]];
|
ivan@137
|
546 }
|
ivan@137
|
547
|
ivan@137
|
548
|
ivan@137
|
549 /* matrix transpose */
|
ivan@137
|
550
|
ivan@137
|
551 void transpose(double X[], double Y[], mwSize n, mwSize m)
|
ivan@137
|
552 {
|
ivan@137
|
553 mwIndex i, j, i_m, j_n;
|
ivan@137
|
554
|
ivan@137
|
555 if (n<m) {
|
ivan@137
|
556 for (j=0; j<m; ++j) {
|
ivan@137
|
557 j_n = j*n;
|
ivan@137
|
558 for (i=0; i<n; ++i) {
|
ivan@137
|
559 Y[j+i*m] = X[i+j_n];
|
ivan@137
|
560 }
|
ivan@137
|
561 }
|
ivan@137
|
562 }
|
ivan@137
|
563 else {
|
ivan@137
|
564 for (i=0; i<n; ++i) {
|
ivan@137
|
565 i_m = i*m;
|
ivan@137
|
566 for (j=0; j<m; ++j) {
|
ivan@137
|
567 Y[j+i_m] = X[i+j*n];
|
ivan@137
|
568 }
|
ivan@137
|
569 }
|
ivan@137
|
570 }
|
ivan@137
|
571 }
|
ivan@137
|
572
|
ivan@137
|
573
|
ivan@137
|
574 /* print contents of matrix */
|
ivan@137
|
575
|
ivan@137
|
576 void printmat(double A[], int n, int m, char* matname)
|
ivan@137
|
577 {
|
ivan@137
|
578 int i, j;
|
ivan@137
|
579 mexPrintf("\n%s = \n\n", matname);
|
ivan@137
|
580
|
ivan@137
|
581 if (n*m==0) {
|
ivan@137
|
582 mexPrintf(" Empty matrix: %d-by-%d\n\n", n, m);
|
ivan@137
|
583 return;
|
ivan@137
|
584 }
|
ivan@137
|
585
|
ivan@137
|
586 for (i=0; i<n; ++i) {
|
ivan@137
|
587 for (j=0; j<m; ++j)
|
ivan@137
|
588 mexPrintf(" %lf", A[j*n+i]);
|
ivan@137
|
589 mexPrintf("\n");
|
ivan@137
|
590 }
|
ivan@137
|
591 mexPrintf("\n");
|
ivan@137
|
592 }
|
ivan@137
|
593
|
ivan@137
|
594
|
ivan@137
|
595 /* print contents of sparse matrix */
|
ivan@137
|
596
|
ivan@137
|
597 void printspmat(mxArray *a, char* matname)
|
ivan@137
|
598 {
|
ivan@137
|
599 mwIndex *aJc = mxGetJc(a);
|
ivan@137
|
600 mwIndex *aIr = mxGetIr(a);
|
ivan@137
|
601 double *aPr = mxGetPr(a);
|
ivan@137
|
602
|
ivan@137
|
603 int i;
|
ivan@137
|
604
|
ivan@137
|
605 mexPrintf("\n%s = \n\n", matname);
|
ivan@137
|
606
|
ivan@137
|
607 for (i=0; i<aJc[1]; ++i)
|
ivan@137
|
608 printf(" (%d,1) = %lf\n", aIr[i]+1,aPr[i]);
|
ivan@137
|
609
|
ivan@137
|
610 mexPrintf("\n");
|
ivan@137
|
611 }
|
ivan@137
|
612
|
ivan@137
|
613
|
ivan@137
|
614
|
ivan@137
|
615 /* matrix multiplication using Winograd's algorithm */
|
ivan@137
|
616
|
ivan@137
|
617 /*
|
ivan@137
|
618 void mat_mat2(double alpha, double A[], double B[], double X[], mwSize n, mwSize m, mwSize k)
|
ivan@137
|
619 {
|
ivan@137
|
620
|
ivan@137
|
621 mwIndex i1, i2, i3, iX, iA, i2_n;
|
ivan@137
|
622 double b, *AA, *BB;
|
ivan@137
|
623
|
ivan@137
|
624 AA = mxCalloc(n,sizeof(double));
|
ivan@137
|
625 BB = mxCalloc(k,sizeof(double));
|
ivan@137
|
626
|
ivan@137
|
627 for (i1=0; i1<n*k; i1++) {
|
ivan@137
|
628 X[i1] = 0;
|
ivan@137
|
629 }
|
ivan@137
|
630
|
ivan@137
|
631 for (i1=0; i1<n; ++i1) {
|
ivan@137
|
632 for (i2=0; i2<m/2; ++i2) {
|
ivan@137
|
633 AA[i1] += A[i1+2*i2*n]*A[i1+(2*i2+1)*n];
|
ivan@137
|
634 }
|
ivan@137
|
635 }
|
ivan@137
|
636
|
ivan@137
|
637 for (i2=0; i2<k; ++i2) {
|
ivan@137
|
638 for (i1=0; i1<m/2; ++i1) {
|
ivan@137
|
639 BB[i2] += B[2*i1+i2*m]*B[2*i1+1+i2*m];
|
ivan@137
|
640 }
|
ivan@137
|
641 }
|
ivan@137
|
642
|
ivan@137
|
643 for (i2=0; i2<k; ++i2) {
|
ivan@137
|
644 for (i3=0; i3<m/2; ++i3) {
|
ivan@137
|
645 for (i1=0; i1<n; ++i1) {
|
ivan@137
|
646 X[i1+i2*n] += (A[i1+(2*i3)*n]+B[2*i3+1+i2*m])*(A[i1+(2*i3+1)*n]+B[2*i3+i2*m]);
|
ivan@137
|
647 }
|
ivan@137
|
648 }
|
ivan@137
|
649 }
|
ivan@137
|
650
|
ivan@137
|
651 if (m%2) {
|
ivan@137
|
652 for (i2=0; i2<k; ++i2) {
|
ivan@137
|
653 for (i1=0; i1<n; ++i1) {
|
ivan@137
|
654 X[i1+i2*n] += A[i1+(m-1)*n]*B[m-1+i2*m];
|
ivan@137
|
655 }
|
ivan@137
|
656 }
|
ivan@137
|
657 }
|
ivan@137
|
658
|
ivan@137
|
659 for (i2=0; i2<k; ++i2) {
|
ivan@137
|
660 for (i1=0; i1<n; ++i1) {
|
ivan@137
|
661 X[i1+i2*n] -= (AA[i1] + BB[i2]);
|
ivan@137
|
662 X[i1+i2*n] *= alpha;
|
ivan@137
|
663 }
|
ivan@137
|
664 }
|
ivan@137
|
665
|
ivan@137
|
666 mxFree(AA);
|
ivan@137
|
667 mxFree(BB);
|
ivan@137
|
668 }
|
ivan@137
|
669 */
|
ivan@137
|
670
|
ivan@137
|
671
|
ivan@137
|
672
|
ivan@137
|
673
|