KerMor  0.9
Model order reduction for nonlinear dynamical systems and nonlinear approximation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
dontuse_evaluate.c
Go to the documentation of this file.
1 #include <mex.h>
2 #include <math.h>
3 
4 /* Nice macro for matlab-style indication! */
5 #define X(i,j) x[j*n+i]
6 #define Y(i,j) y[j*n+i]
7 
8 /* Implements the sum(x.^2,1) matlab command for a matrix x */
9 void sumsq(double* xsq, double* x, int n, int m) {
10  int i, j;
11 
12 #ifdef DEBUG
13  char temp[100];
14 #endif
15 
16  for (j=0;j<m;j++) {
17  xsq[j] = 0;
18  for (i=0;i<n;i++) {
19  xsq[j] += X(i,j)*X(i,j);
20 #ifdef DEBUG
21  sprintf(temp, "j(m)=%d(%d), i(n)=%d(%d), x=%.12f\n", j, m, i, n, X(i,j));
22  mexPrintf(temp);
23 #endif
24  }
25  }
26 }
27 
28 void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) {
29  double *x, *y, *res, gamma, hlp;
30 
31  int i, j, k, l, m, n;
32 
33 #ifdef DEBUG
34  char temp[100];
35 #endif
36 
37  /* const mxArray *net; */
38 
39  /*
40  * if (nrhs != 3)
41  * {
42  * mexErrMsgTxt("Wrong number input arguments.");
43  * }
44  * else if (nlhs > 1)
45  * {
46  * mexErrMsgTxt("Too many output arguments.");
47  * }*/
48 
49  /*
50  * if (!mxIsDouble(prhs[1]) || mxIsComplex(prhs[1]))
51  * {
52  * mexErrMsgTxt("x1 must be a double matrix.");
53  * }
54  */
55  n = mxGetM(prhs[1]);
56  m = mxGetN(prhs[1]);
57  x = mxGetPr(prhs[1]);
58 
59  /*
60  * if (!mxIsDouble(prhs[2]) || mxIsComplex(prhs[2]))
61  * {
62  * mexErrMsgTxt("x2 must be a double matrix.");
63  * }
64  */
65 
66  double xsq[m];
67  sumsq(xsq, x, n, m);
68 
69  /* Get gamma value */
70  gamma = mxGetScalar(mxGetProperty(prhs[0], 0, "Gamma"));
71 
72 #ifdef DEBUG
73  sprintf(temp, "Gamma=%.12f\n", gamma);
74  mexPrintf(temp);
75 #endif
76 
77 
78 
79  /* One argument case (symmetric matrix) */
80  if (nrhs == 2) {
81  /* Output init */
82  plhs[0] = mxCreateDoubleMatrix(m, m, mxREAL);
83  res = mxGetPr(plhs[0]);
84 
85 #ifdef DEBUG
86  sprintf(temp, "Call with one parameter!\n");
87  mexPrintf(temp);
88 #endif
89  /* Diagonal with ones */
90  for(i=0;i<m;i++) {
91  res[i*m+i] = 1;
92  }
93 
94  /* Only compute lower left triangular & copy values */
95  for (i = 1; i < m; i++) {
96  for (j = 0; j < i; j++) {
97  hlp=0;
98  for (l = 0; l < n; l++) {
99 #ifdef DEBUG
100  sprintf(temp, "ScaP i=%d, j=%d, l=%d, x=%.12f\n", i, j, l, X(l,i));
101  mexPrintf(temp);
102 #endif
103  hlp += X(l,i)*X(l,j);
104  }
105 #ifdef DEBUG
106  sprintf(temp, "xsq[i]=%.12f, xsq[j]=%.12f, x*y=%.12f\n", xsq[i], xsq[j], hlp);
107  mexPrintf(temp);
108 #endif
109  res[i+j*m] = exp(-(xsq[i]+xsq[j]-2*hlp)/gamma);
110  /* Copy lower left triangular to upper right */
111  res[j+i*m] = res[i+j*m];
112  }
113  }
114 
115 
116  /* Two argument case */
117  } else {
118 
119 #ifdef DEBUG
120  sprintf(temp, "Call with two parameters!\n");
121  mexPrintf(temp);
122 #endif
123  if (n != mxGetM(prhs[2]))
124  mexErrMsgTxt("GaussKernel.evaluate (mex): Array dimensions must match");
125 
126  k = mxGetN(prhs[2]);
127  y = mxGetPr(prhs[2]);
128 
129  plhs[0] = mxCreateDoubleMatrix(m, k, mxREAL);
130  res = mxGetPr(plhs[0]);
131 
132  double ysq[k];
133  sumsq(ysq, y, n, k);
134  for (i = 0; i < m; i++) {
135  for (j = 0; j < k; j++) {
136 
137  hlp=0;
138  for (l = 0; l < n; l++) {
139 #ifdef DEBUG
140  sprintf(temp, "ScaP i=%d, j=%d, l=%d, x=%.12f, y=%.12f\n", i, j, l, X(l,i), Y(l,i));
141  mexPrintf(temp);
142 #endif
143  hlp += X(l,i)*Y(l,j);
144  }
145 #ifdef DEBUG
146  sprintf(temp, "xsq=%.12f, ysq=%.12f, x*y=%.12f\n", xsq[i], ysq[j], hlp);
147  mexPrintf(temp);
148 #endif
149  res[i+j*m] = exp(-(xsq[i]+ysq[j]-2*hlp)/gamma);
150  }
151  }
152  }
153 }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
void sumsq(double *xsq, double *x, int n, int m)
#define X(i, j)
#define Y(i, j)