comparison toolboxes/FullBNT-1.0.7/bnt/potentials/Tables/repmat_and_mult.c @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 /****************************************************
2 A = mult_by_array(big, small)
3 implicitely copies small |big|/|small| times
4 and then does element-wise multiplication.
5
6 i.e.,
7 C = repmat(small(:), 1, length(big(:))/length(small(:)))
8 A = reshape(big(:) .* C(:), size(big))
9
10 However, this C version avoids the expense of the repmat.
11
12 Written by wei.hu@intel.com, 28 Jan 2002.
13 /****************************************************/
14
15
16 #include "mex.h"
17
18 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
19 {
20 double *sp, *zp;
21 int i, j, NB, NS, xnd, ynd, ndim;
22 const int *xdim, *ydim;
23 int *s, *sx, *sy, *cpsy, *subs, *cpsy2;
24
25 if (nrhs != 2)
26 mexErrMsgTxt("Incorrect number of inputs.");
27
28 if (nlhs > 1)
29 mexErrMsgTxt("Too many output arguments.");
30
31 plhs[0] = mxDuplicateArray(prhs[0]);
32 zp = mxGetPr(plhs[0]);
33 sp = mxGetPr(prhs[1]);
34
35 xnd = mxGetNumberOfDimensions(prhs[0]);
36 ynd = mxGetNumberOfDimensions(prhs[1]);
37 xdim = mxGetDimensions(prhs[0]);
38 ydim = mxGetDimensions(prhs[1]);
39 ndim = xnd;
40
41 NB = mxGetNumberOfElements(prhs[0]);
42 NS = mxGetNumberOfElements(prhs[1]);
43
44 if(NS == 1){
45 for(i=0; i<NB; i++){
46 *zp++ *= *sp;
47 }
48 return;
49 }
50
51 if(NS == NB){
52 for(i=0; i<NB; i++){
53 *zp++ *= *sp++;
54 }
55 return;
56 }
57
58 sx = (int *)malloc(sizeof(int)*ndim);
59 sy = (int *)malloc(sizeof(int)*ndim);
60 s = (int *)malloc(sizeof(int)*ndim);
61 *(cpsy = (int *)malloc(sizeof(int)*ndim)) = 1;
62 subs = (int *)malloc(sizeof(int)*ndim);
63 cpsy2 = (int *)malloc(sizeof(int)*ndim);
64 for(i=0; i<ndim; i++){
65 subs[i] = 0;
66 sx[i] = xdim[i];
67 sy[i] = (i < ynd) ? ydim[i] : 1;
68 s[i] = sx[i] - 1;
69 }
70
71 for (i = 0; i < ndim-1; i++){
72 cpsy[i+1] = cpsy[i]*sy[i]--;
73 cpsy2[i] = cpsy[i]*sy[i];
74 }
75 cpsy2[ndim-1] = cpsy[ndim-1]*(--sy[ndim-1]);
76
77 for(j=0; j<NB; j++){
78 *zp++ *= *sp;
79 for(i=0; i<ndim; i++){
80 if(subs[i] == s[i]){
81 subs[i] = 0;
82 if(sy[i]) sp -= cpsy2[i];
83 }
84 else{
85 subs[i]++;
86 if(sy[i]) sp += cpsy[i];
87 break;
88 }
89 }
90 }
91 free(sx);
92 free(sy);
93 free(s);
94 free(cpsy);
95 free(subs);
96 free(cpsy2);
97 }