File: ML_package.cpp

package info (click to toggle)
r-cran-pcapp 1.9-73-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 800 kB
  • sloc: cpp: 5,961; ansic: 917; sh: 13; makefile: 2
file content (475 lines) | stat: -rw-r--r-- 10,525 bytes parent folder | download
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#ifdef MATLAB_MEX_FILE


#include "ML_package.h"
#include "L1Median.h"

#include "qnn.h"

#include "PCAgrid.h"
#include "PCAproj.h"


	void mexFunction(int nlhs, mxArray* plhs[], int nrhs, mxArray *prhs[])
	{
		if (nrhs < 1)
		{
			meal_error ("At least one argument needed.") ;
			return ;
		}

		mxArray *pSwitch = prhs [0] ;

		if (mxGetM (pSwitch) != 1 || 
			mxGetN (pSwitch) != 1)
		{
			meal_error ("First argument has to be of size 1x1.") ;
			return ;
		}

		int nSwitch = (int) *mxGetPr (pSwitch) ;

		switch (nSwitch)
		{
		case 0:
			ML_l1median_HoCr (nlhs, plhs, nrhs - 1, prhs + 1) ;
			break ;

		case 1:
			ML_l1median_VaZh (nlhs, plhs, nrhs - 1, prhs + 1) ;
			break ;

		case 2:
			ML_qn (nlhs, plhs, nrhs - 1, prhs + 1) ;
			break ;

		case 3:
			ML_PCAgrid (nlhs, plhs, nrhs - 1, prhs + 1) ;
			break ;

		case 4:
			ML_sPCAgrid (nlhs, plhs, nrhs - 1, prhs + 1) ;
			break ;

		case 5:
			ML_PCAproj (nlhs, plhs, nrhs - 1, prhs + 1) ;
			break ;

		case 6:
			ML_PCAprojU (nlhs, plhs, nrhs - 1, prhs + 1) ;
			break ;

		
		default:
			meal_error ("Unknown switch argument received.") ;

			break ;
				

		} ;
	}

	void ML_l1median_HoCr (int nlhs, mxArray* plhs[], int nrhs, mxArray *prhs[])
		//(int *pnParIn, int *pnParOut, double *pdParIn, double *pdX, double *pdMed)
	{
		if (nlhs != 1)
			meal_error ("1 output argument expected") ;

		if (nrhs != 3)
			meal_error ("3 input arguments expected") ;

			//	Data Matrix

		mxArray *mxX = prhs [0] ;
		const int n = mxGetM (mxX), p = mxGetN (mxX) ;
		double *pdX = mxGetPr (mxX) ;

			//	Median Vector

		mxArray *mxMed = prhs [1] ;

		if (mxGetM (mxMed) * mxGetN (mxMed) != (t_size) p)
			meal_error ("Length of the median vector is expected to be p.") ;
		double *pdMed = mxGetPr (mxMed) ;

			//	Parameter Vector
		mxArray *mxPar = prhs [2] ;

		if (mxGetM (mxPar) * mxGetN (mxPar)!= 4)
			meal_error ("Parameter array of dimension 5x1 expected") ;

		double *pdParIn = mxGetPr (mxPar) ;
		const int &dwMaxit = (int) pdParIn[0], dwTrace = (int) pdParIn[1] ;
		const double &dTol = pdParIn[2], &dZeroTol = pdParIn[3] ;

		plhs [0] = mxCreateDoubleMatrix (1, 2, mxREAL) ;
		double *pdParOut = mxGetPr (plhs [0]) ;
		double &dCode = pdParOut [0] ;
		

		int nIterCount ;

		TRY( 
			dCode = l1median_HoCr (SMatD (pdX, n, p), SVecD (pdMed, p), dZeroTol, dTol, dwMaxit, dwTrace, &nIterCount) ;
			)

		//	Out Parameter Vector
		pdParOut [1] = nIterCount ;

		return ;
	}


	void ML_l1median_VaZh (int nlhs, mxArray* plhs[], int nrhs, mxArray *prhs[])
		//(int *pnParIn, int *pnParOut, double *pdParIn, double *pdX, double *pdMed)
	{
		if (nlhs != 1)
			meal_error ("1 output argument expected") ;

		if (nrhs != 3)
			meal_error ("3 input arguments expected") ;

			//	Data Matrix

		mxArray *mxX = prhs [0] ;
		const int n = mxGetM (mxX), p = mxGetN (mxX) ;
		double *pdX = mxGetPr (mxX) ;

			//	Median Vector

		mxArray *mxMed = prhs [1] ;

		if (mxGetM (mxMed) * mxGetN (mxMed) != (t_size) p)
			meal_error ("Length of the median vector is expected to be p.") ;
		double *pdMed = mxGetPr (mxMed) ;

			//	Parameter Vector
		mxArray *mxPar = prhs [2] ;

		if (mxGetM (mxPar) * mxGetN (mxPar)!= 4)
			meal_error ("Parameter array of dimension 5x1 expected") ;

		double *pdParIn = mxGetPr (mxPar) ;
		const int &dwMaxit = (int) pdParIn[0], dwTrace = (int) pdParIn[1] ;
		const double &dTol = pdParIn[2], &dZeroTol = pdParIn[3] ;

		int nCode, nIterCount ;

		TRY( 
			CL1Median_VZ (n, p, nCode, nIterCount, pdParIn, pdX, pdMed, NULL) ; //, pdWeights) ;
			)

		//	Out Parameter Vector
		plhs [0] = mxCreateDoubleMatrix (1, 2, mxREAL) ;
		double *pdParOut = mxGetPr (plhs [0]) ;
		pdParOut [0] = nCode ;
		pdParOut [1] = nIterCount ;

		return ;
	}


	void ML_qn (int nlhs, mxArray* plhs[], int nrhs, mxArray *prhs[])
	{

		if (nlhs != 1)
			meal_error ("1 output argument expected") ;

		if (nrhs != 2)
			meal_error ("2 input arguments expected") ;

	//	Get data

		const int n = mxGetM (prhs[0]) * mxGetN (prhs[0]) ;
		double *pdX = mxGetPr (prhs[0]) ;

	//	Get Corrfact

		if (mxGetM (prhs[1]) != 1 ||
			mxGetN (prhs[1]) != 1)
			meal_error  ("Parameter 2 is supposed to be a scalar.") ;

		const double dCorrFact = *mxGetPr (prhs[1]) ;

	//	GetQn (output)

		plhs[0] = mxCreateDoubleMatrix (1, 1, mxREAL) ;
		double &dQn = *mxGetPr (plhs [0]) ;

		TRY( 
			qn_nc (dQn, pdX, n) ;
			dQn *= qn_corrN (n, dCorrFact) ;
			)
	}

	void ML_PCAgrid (int nlhs, mxArray* plhs[], int nrhs, mxArray *prhs[])
	{

		if (nlhs != 3)
			meal_error ("3 output argument expected") ;

		if (nrhs != 4)
			meal_error ("4 input arguments expected") ;
		

	//	Get pnParIn
		if (mxGetM (prhs[0]) * mxGetN (prhs[0]) != 7)
			meal_error ("pnParamIn: vector of length 7 expected.") ;

		double *pdnParamIn = mxGetPr (prhs[0]) ;
		int pnParamIn[9] ;
		Double2Int (pnParamIn + 2, pdnParamIn, 7) ;

		int nk = pnParamIn[2] ;

	//	Get pdParIn

		if (mxGetM (prhs[1]) * mxGetN (prhs[1]) != 1)
			meal_error ("pdParIn: vector of length 1 expected.") ;

		double *pdParamIn = mxGetPr (prhs[1]) ;

	//	Get pdData

		int &n = pnParamIn[0], &p = pnParamIn[1] ;

		n = mxGetM (prhs[2]) ;
		p = mxGetN (prhs[2]) ;

		double *pdData = mxGetPr (prhs[2]) ;

	//	Get pdLoadings

		if (mxGetM (prhs[3]) != (t_size) p ||
			mxGetN (prhs[3]) != (t_size)	p)
			meal_error ("pdLoadings: matrix of dimension pxp expected.") ;

		double *pdLoadings = mxGetPr (prhs[3]) ;


	//	Get pnParOut (output)
		plhs[0] = mxCreateDoubleMatrix (1, 1, mxREAL) ;
		double *pdnParamOut = mxGetPr (plhs[0]) ;
		int pnParamOut [1] ;

	//	Get pdSdev (output)

		plhs[1] = mxCreateDoubleMatrix (1, nk, mxREAL) ;
		double *pdSDev = mxGetPr (plhs[1]) ;

	//	Get	pdObj (output)

		plhs[2] = mxCreateDoubleMatrix (1, nk, mxREAL) ;
		double *pdObj = mxGetPr (plhs[2]) ;


		TRY( 
			CPCAGrid (pnParamIn, pnParamOut, pdParamIn, pdData, pdLoadings, pdSDev, pdObj/*, pdMaxMaha*/).Calc () ;
			)		

		Int2Double (pdnParamOut, pnParamOut, 1) ;

	}


	void ML_sPCAgrid (int nlhs, mxArray* plhs[], int nrhs, mxArray *prhs[])
	{

		if (nlhs != 3)
			meal_error ("expected 3 output argument") ;

		if (nrhs != 6)
			meal_error ("expected 6 input arguments") ;
		

	//	Get pnParIn
		if (mxGetM (prhs[0]) * mxGetN (prhs[0]) != 10)
			meal_error ("pnParamIn: expected vector of length 10.") ;

		double *pdnParamIn = mxGetPr (prhs[0]) ;
		int pnParamIn[12] ;
		Double2Int (pnParamIn + 2, pdnParamIn, 10) ;

		int nk = pnParamIn[2] ;
		int nPhd = pnParamIn[10] ;

	//	Get pdParIn

		if (mxGetM (prhs[1]) * mxGetN (prhs[1]) != 1)
			meal_error ("pdParIn: expected vector of length 1.") ;

		double *pdParamIn = mxGetPr (prhs[1]) ;

	//	Get pdData

		int &n = pnParamIn[0], &p = pnParamIn[1] ;

		n = mxGetM (prhs[2]) ;
		p = mxGetN (prhs[2]) ;

		double *pdData = mxGetPr (prhs[2]) ;

	//	Get pdLoadings

		if (mxGetM (prhs[3]) != (t_size) p ||
			mxGetN (prhs[3]) != (t_size)	p)
			meal_error ("pdLoadings: expected matrix of dimension pxp.") ;

		double *pdLoadings = mxGetPr (prhs[3]) ;

	//	Get pdLambda
		if (mxGetM (prhs[4]) * mxGetN (prhs[4]) != (t_size)	nk)
			meal_error ("pdLambda: expected vector of length k.") ;

		double *pdLambda = mxGetPr (prhs[4]) ;

	//	Get pdBackTransHD

		double *pdBackTransHD = NULL ;
		if( mxGetM (prhs[5]) * mxGetN (prhs[5]))		//	only if this matrix holds any values..
		{
			if (mxGetM (prhs[5]) != (t_size) nPhd ||
				mxGetN (prhs[5]) != (t_size) p)

				meal_error ("pdBackTransHD: expected matrix of dimension k.") ;

			pdBackTransHD = mxGetPr (prhs[5]) ;
		}


	//	Get pnParOut (output)
		plhs[0] = mxCreateDoubleMatrix (1, 1, mxREAL) ;
		double *pdnParamOut = mxGetPr (plhs[0]) ;
		int pnParamOut [1] ;

	//	Get pdSdev (output)

		plhs[1] = mxCreateDoubleMatrix (1, nk, mxREAL) ;
		double *pdSDev = mxGetPr (plhs[1]) ;

	//	Get	pdObj (output)

		plhs[2] = mxCreateDoubleMatrix (1, nk, mxREAL) ;
		double *pdObj = mxGetPr (plhs[2]) ;


		TRY( 
			CsPCAGrid (pnParamIn, pnParamOut, pdParamIn, pdData, pdLoadings, pdSDev, pdObj/*, pdMaxMaha*/, pdLambda, pdBackTransHD).Calc () ;
			)		

		Int2Double (pdnParamOut, pnParamOut, 1) ;
	}

	void ML_PCAproj (int nlhs, mxArray* plhs[], int nrhs, mxArray *prhs[])
	{

		if (nlhs != 3)
			meal_error ("3 output argument expected") ;

		if (nrhs != 3)
			meal_error ("3 input arguments expected") ;
		

	//	Get pnParIn
		if (mxGetM (prhs[0]) * mxGetN (prhs[0]) != 4)
			meal_error ("pnParamIn: vector of length 4 expected.") ;

		double *pdnParamIn = mxGetPr (prhs[0]) ;
		int pnParamIn[6] ;
		Double2Int (pnParamIn + 2, pdnParamIn, 4) ;

		int nk = pnParamIn[3] ;

	//	Get pdParIn

		if (mxGetM (prhs[1]) * mxGetN (prhs[1]) != 1)
			meal_error ("pdParIn: vector of length 1 expected.") ;

		double *pdParamIn = mxGetPr (prhs[1]) ;

	//	Get pdX

		int &n = pnParamIn[0], &p = pnParamIn[1] ;

		n = mxGetM (prhs[2]) ;
		p = mxGetN (prhs[2]) ;

		double *pdX = mxGetPr (prhs[2]) ;

	//	Get pdZ (Scores)

		plhs[0] = mxCreateDoubleMatrix (n, nk, mxREAL) ;
		double *pdZ = mxGetPr (plhs[0]) ;

	//	Get pdL (Loadings)

		plhs[1] = mxCreateDoubleMatrix (p, nk, mxREAL) ;
		double *pdL = mxGetPr (plhs[1]) ;

	//	Get pdSdev (output)

		plhs[2] = mxCreateDoubleMatrix (1, nk, mxREAL) ;
		double *pdSDev = mxGetPr (plhs[2]) ;

		TRY( 
			CPCAproj (pnParamIn, pdParamIn, pdX, pdZ, pdL, pdSDev).Calc () ;
			)		
	}

	void ML_PCAprojU (int nlhs, mxArray* plhs[], int nrhs, mxArray *prhs[])
	{

		if (nlhs != 3)
			meal_error ("3 output argument expected") ;

		if (nrhs != 3)
			meal_error ("3 input arguments expected") ;

	//	Get pnParIn
		if (mxGetM (prhs[0]) * mxGetN (prhs[0]) != 6)
			meal_error ("pnParamIn: vector of length 4 expected.") ;

		double *pdnParamIn = mxGetPr (prhs[0]) ;
		int pnParamIn[8] ;
		Double2Int (pnParamIn + 2, pdnParamIn, 6) ;

		int nk = pnParamIn[3] ;

	//	Get pdParIn

		if (mxGetM (prhs[1]) * mxGetN (prhs[1]) != 1)
			meal_error ("pdParIn: vector of length 1 expected.") ;

		double *pdParamIn = mxGetPr (prhs[1]) ;

	//	Get pdX

		int &n = pnParamIn[0], &p = pnParamIn[1] ;

		n = mxGetM (prhs[2]) ;
		p = mxGetN (prhs[2]) ;

		double *pdX = mxGetPr (prhs[2]) ;

	//	Get pdZ (Scores)

		plhs[0] = mxCreateDoubleMatrix (n, nk, mxREAL) ;
		double *pdZ = mxGetPr (plhs[0]) ;

	//	Get pdL (Loadings)

		plhs[1] = mxCreateDoubleMatrix (p, nk, mxREAL) ;
		double *pdL = mxGetPr (plhs[1]) ;

	//	Get pdSdev (output)

		plhs[2] = mxCreateDoubleMatrix (1, nk, mxREAL) ;
		double *pdSDev = mxGetPr (plhs[2]) ;

		TRY( 
			CPCAprojU (pnParamIn, pdParamIn, pdX, pdZ, pdL, pdSDev).Calc () ;
			)		

	}

#endif	//	#ifdef MATLAB_MEX_FILE