comparison toolboxes/FullBNT-1.0.7/bnt/potentials/Tables/rep_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 /* rep_mult.c repmat first two operands to the size provided by */
2 /* the third operand, then perform point multiply */
3 /* 3 input, 1 output */
4 /* C = rep_mult(A, B, sizes) */
5
6 #include "mex.h"
7
8 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
9 {
10 double *xp, *yp, *zp, *pSizes;
11 int xnd, ynd, numElements = 1;
12 const int *xdim, *ydim;
13 int i, j, ndim;
14 int *s, *sx, *sy, *cpsx, *cpsy;
15 int *subs, *s1, *cpsx2, *cpsy2;
16
17 if (nrhs != 3)
18 mexErrMsgTxt("Incorrect number of inputs.");
19
20 if (nlhs > 1)
21 mexErrMsgTxt("Too many output arguments.");
22
23 xnd = mxGetNumberOfDimensions(prhs[0]);
24 ynd = mxGetNumberOfDimensions(prhs[1]);
25 xdim = mxGetDimensions(prhs[0]);
26 ydim = mxGetDimensions(prhs[1]);
27 ndim = mxGetNumberOfElements(prhs[2]);
28
29 pSizes = mxGetPr(prhs[2]);
30
31 sx = (int *)malloc(sizeof(int)*ndim);
32 sy = (int *)malloc(sizeof(int)*ndim);
33 s = (int *)malloc(sizeof(int)*ndim);
34 s1 = (int *)malloc(sizeof(int)*ndim);
35 *(cpsx = (int *)malloc(sizeof(int)*ndim)) = 1;
36 *(cpsy = (int *)malloc(sizeof(int)*ndim)) = 1;
37 subs = (int *)malloc(sizeof(int)*ndim);
38 cpsx2 = (int *)malloc(sizeof(int)*ndim);
39 cpsy2 = (int *)malloc(sizeof(int)*ndim);
40 for(i=0; i<ndim; i++){
41 subs[i] = 0;
42 sx[i] = (i < xnd) ? xdim[i] : 1;
43 sy[i] = (i < ynd) ? ydim[i] : 1;
44 s[i] = (int)pSizes[i];
45 s1[i] = s[i] - 1;
46 numElements *= s[i];
47 }
48
49 for(i=0; i<ndim-1; i++){
50 cpsx[i+1] = cpsx[i]*sx[i]--;
51 cpsy[i+1] = cpsy[i]*sy[i]--;
52 cpsx2[i] = cpsx[i]*sx[i];
53 cpsy2[i] = cpsy[i]*sy[i];
54 }
55 cpsx2[ndim-1] = cpsx[ndim-1]*(--sx[ndim-1]);
56 cpsy2[ndim-1] = cpsy[ndim-1]*(--sy[ndim-1]);
57
58 plhs[0] = mxCreateNumericArray(ndim, s, mxDOUBLE_CLASS, mxREAL);
59 zp = mxGetPr(plhs[0]);
60 xp = mxGetPr(prhs[0]);
61 yp = mxGetPr(prhs[1]);
62
63 for(j=0; j<numElements; j++){
64 *zp++ = *xp * *yp;
65 for(i=0; i<ndim; i++){
66 if(subs[i] == s1[i]){
67 subs[i] = 0;
68 if(sx[i])
69 xp -= cpsx2[i];
70 if(sy[i])
71 yp -= cpsy2[i];
72 }
73 else{
74 subs[i]++;
75 if(sx[i])
76 xp += cpsx[i];
77 if(sy[i])
78 yp += cpsy[i];
79 break;
80 }
81 }
82 }
83 free(sx);
84 free(sy);
85 free(s);
86 free(s1);
87 free(cpsx);
88 free(cpsy);
89 free(subs);
90 free(cpsx2);
91 free(cpsy2);
92 }