forked from Craigacp/MIToolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRenyiMIToolboxMex.c
190 lines (170 loc) · 5.18 KB
/
RenyiMIToolboxMex.c
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
/*******************************************************************************
** RenyiMIToolboxMex.c
** is the MATLAB entry point for the Renyi Entropy and MI MIToolbox functions
** when called from a MATLAB/OCTAVE script.
**
** Copyright 2010 Adam Pocock, The University Of Manchester
** www.cs.manchester.ac.uk
**
** This file is part of MIToolbox, licensed under the 3-clause BSD license.
*******************************************************************************/
#include "MIToolbox.h"
#include "ArrayOperations.h"
#include "RenyiEntropy.h"
#include "RenyiMutualInformation.h"
/*******************************************************************************
**entry point for the mex call
**nlhs - number of outputs
**plhs - pointer to array of outputs
**nrhs - number of inputs
**prhs - pointer to array of inputs
*******************************************************************************/
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
/*****************************************************************************
** this function takes a flag and 2 or 3 other arguments
** the first is a scalar alpha value, and the remainder are
** arrays. It returns a Renyi entropy or mutual information using the
** alpha divergence.
*****************************************************************************/
int flag, numberOfSamples, checkSamples, numberOfFeatures, checkFeatures;
double alpha;
double *dataVector, *firstVector, *secondVector, *output;
/*if (nlhs != 1)
{
printf("Incorrect number of output arguments\n");
}//if not 1 output
*/
switch (nrhs)
{
case 3:
{
/*printf("Must be H_\alpha(X)\n");*/
break;
}
case 4:
{
/*printf("Must be H_\alpha(XY), I_\alpha(X;Y)\n");*/
break;
}
default:
{
printf("Incorrect number of arguments, format is RenyiMIToolbox(\"FLAG\",varargin)\n");
break;
}
}
/* number to function map
** 1 = H(X)
** 2 = H(XY)
** 3 = I(X;Y)
*/
flag = *mxGetPr(prhs[0]);
switch (flag)
{
case 1:
{
/*
**H_{\alpha}(X)
*/
alpha = mxGetScalar(prhs[1]);
numberOfSamples = mxGetM(prhs[2]);
numberOfFeatures = mxGetN(prhs[2]);
dataVector = (double *) mxGetPr(prhs[2]);
plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
output = (double *) mxGetPr(plhs[0]);
if (numberOfFeatures == 1)
{
/*double calculateRenyiEntropy(double alpha, double *dataVector, long vectorLength);*/
*output = calculateRenyiEntropy(alpha,dataVector,numberOfSamples);
}
else
{
printf("No columns in input\n");
*output = -1.0;
}
break;
}/*case 1 - H_{\alpha}(X)*/
case 2:
{
/*
**H_{\alpha}(XY)
*/
alpha = mxGetScalar(prhs[1]);
numberOfSamples = mxGetM(prhs[2]);
checkSamples = mxGetM(prhs[3]);
numberOfFeatures = mxGetN(prhs[2]);
checkFeatures = mxGetN(prhs[3]);
firstVector = mxGetPr(prhs[2]);
secondVector = mxGetPr(prhs[3]);
plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
output = (double *)mxGetPr(plhs[0]);
if ((numberOfFeatures == 1) && (checkFeatures == 1))
{
if ((numberOfSamples == 0) || (checkSamples == 0))
{
*output = 0.0;
}
else if (numberOfSamples == checkSamples)
{
/*double calculateJointRenyiEntropy(double alpha, double *firstVector, double *secondVector, long vectorLength);*/
*output = calculateJointRenyiEntropy(alpha,firstVector,secondVector,numberOfSamples);
}
else
{
printf("Vector lengths do not match, they must be the same length");
*output = -1.0;
}
}
else
{
printf("No columns in input\n");
*output = -1.0;
}
break;
}/*case 2 - H_{\alpha}(XY)*/
case 3:
{
/*
**I_{\alpha}(X;Y)
*/
alpha = mxGetScalar(prhs[1]);
numberOfSamples = mxGetM(prhs[2]);
checkSamples = mxGetM(prhs[3]);
numberOfFeatures = mxGetN(prhs[2]);
checkFeatures = mxGetN(prhs[3]);
firstVector = mxGetPr(prhs[2]);
secondVector = mxGetPr(prhs[3]);
plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
output = (double *)mxGetPr(plhs[0]);
if ((numberOfFeatures == 1) && (checkFeatures == 1))
{
if ((numberOfSamples == 0) || (checkSamples == 0))
{
*output = 0.0;
}
else if (numberOfSamples == checkSamples)
{
/*double calculateRenyiMIDivergence(double alpha, double *dataVector, double *targetVector, long vectorLength);*/
*output = calculateRenyiMIDivergence(alpha,firstVector,secondVector,numberOfSamples);
}
else
{
printf("Vector lengths do not match, they must be the same length");
*output = -1.0;
}
}
else
{
printf("No columns in input\n");
*output = -1.0;
}
break;
}/*case 3 - I_{\alpha}(X;Y)*/
default:
{
printf("Unrecognised flag\n");
break;
}/*default*/
}/*switch(flag)*/
return;
}/*mexFunction()*/