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
|
/* vectorops.c
* Operations on vectors of floats or doubles.
*
* DSet(), FSet() - set all items in vector to value.
* DScale(), FScale() - multiply all items in vector by scale
* DSum(), FSum() - return sum of values in vector
* DAdd(), FAdd() - add vec2 to vec1.
* DCopy(), FCopy() - set vec1 to be same as vec2.
* DDot(), FDot() - return dot product of two vectors.
* DMax(), FMax() - return value of maximum element in vector
* DMin(), FMin() - return value of minimum element in vector
* DArgMax(), FArgMax() - return index of maximum element in vector
* DArgMin(), FArgMin() - return index of minimum element in vector
*
* DNorm(), FNorm() - normalize a probability vector of length n.
* DLog(), FLog() - convert to log probabilities
* DExp(), FExp() - convert log p's back to probabilities
* DLogSum(), FLogSum() - given vector of log p's; return log of summed p's.
* DEntropy(), FEntropy() - return Shannon entropy of probability vector, in bits
*
* SRE, Tue Oct 1 15:23:25 2002 [St. Louis]
* CVS $Id: vectorops.c,v 1.5 2004/05/24 15:49:07 eddy Exp $
*/
#include "squidconf.h"
#include <stdlib.h>
#include <math.h>
#include <float.h>
#include "vectorops.h"
void
DSet(double *vec, int n, double value)
{
int x;
for (x = 0; x < n; x++) vec[x] = value;
}
void
FSet(float *vec, int n, float value)
{
int x;
for (x = 0; x < n; x++) vec[x] = value;
}
void
DScale(double *vec, int n, double scale)
{
int x;
for (x = 0; x < n; x++) vec[x] *= scale;
}
void
FScale(float *vec, int n, float scale)
{
int x;
for (x = 0; x < n; x++) vec[x] *= scale;
}
double
DSum(double *vec, int n)
{
double sum = 0.;
int x;
for (x = 0; x < n; x++) sum += vec[x];
return sum;
}
float
FSum(float *vec, int n)
{
float sum = 0.;
int x;
for (x = 0; x < n; x++) sum += vec[x];
return sum;
}
void
DAdd(double *vec1, double *vec2, int n)
{
int x;
for (x = 0; x < n; x++) vec1[x] += vec2[x];
}
void
FAdd(float *vec1, float *vec2, int n)
{
int x;
for (x = 0; x < n; x++) vec1[x] += vec2[x];
}
void
DCopy(double *vec1, double *vec2, int n)
{
int x;
for (x = 0; x < n; x++) vec1[x] = vec2[x];
}
void
FCopy(float *vec1, float *vec2, int n)
{
int x;
for (x = 0; x < n; x++) vec1[x] = vec2[x];
}
double
DDot(double *vec1, double *vec2, int n)
{
double result = 0.;
int x;
for (x = 0; x < n; x++) result += vec1[x] * vec2[x];
return result;
}
float
FDot(float *vec1, float *vec2, int n)
{
float result = 0.;
int x;
for (x = 0; x < n; x++) result += vec1[x] * vec2[x];
return result;
}
double
DMax(double *vec, int n)
{
int i;
double best;
best = vec[0];
for (i = 1; i < n; i++)
if (vec[i] > best) best = vec[i];
return best;
}
float
FMax(float *vec, int n)
{
int i;
float best;
best = vec[0];
for (i = 1; i < n; i++)
if (vec[i] > best) best = vec[i];
return best;
}
double
DMin(double *vec, int n)
{
int i;
double best;
best = vec[0];
for (i = 1; i < n; i++)
if (vec[i] < best) best = vec[i];
return best;
}
float
FMin(float *vec, int n)
{
int i;
float best;
best = vec[0];
for (i = 1; i < n; i++)
if (vec[i] < best) best = vec[i];
return best;
}
int
DArgMax(double *vec, int n)
{
int i;
int best = 0;
for (i = 1; i < n; i++)
if (vec[i] > vec[best]) best = i;
return best;
}
int
FArgMax(float *vec, int n)
{
int i;
int best = 0;
for (i = 1; i < n; i++)
if (vec[i] > vec[best]) best = i;
return best;
}
int
DArgMin(double *vec, int n)
{
int i;
int best = 0;
for (i = 1; i < n; i++)
if (vec[i] < vec[best]) best = i;
return best;
}
int
FArgMin(float *vec, int n)
{
int i;
int best = 0;
for (i = 1; i < n; i++)
if (vec[i] < vec[best]) best = i;
return best;
}
void
DNorm(double *vec, int n)
{
int x;
double sum;
sum = DSum(vec, n);
if (sum != 0.0) for (x = 0; x < n; x++) vec[x] /= sum;
else for (x = 0; x < n; x++) vec[x] = 1. / (double) n;
}
void
FNorm(float *vec, int n)
{
int x;
float sum;
sum = FSum(vec, n);
if (sum != 0.0) for (x = 0; x < n; x++) vec[x] /= sum;
else for (x = 0; x < n; x++) vec[x] = 1. / (float) n;
}
void
DLog(double *vec, int n)
{
int x;
for (x = 0; x < n; x++)
if (vec[x] > 0.) vec[x] = log(vec[x]);
else vec[x] = -DBL_MAX;
}
void
FLog(float *vec, int n)
{
int x;
for (x = 0; x < n; x++)
if (vec[x] > 0.) vec[x] = log(vec[x]);
else vec[x] = -FLT_MAX;
}
void
DExp(double *vec, int n)
{
int x;
for (x = 0; x < n; x++) vec[x] = exp(vec[x]);
}
void
FExp(float *vec, int n)
{
int x;
for (x = 0; x < n; x++) vec[x] = exp(vec[x]);
}
double
DLogSum(double *vec, int n)
{
int x;
double max, sum;
max = DMax(vec, n);
sum = 0.0;
for (x = 0; x < n; x++)
if (vec[x] > max - 50.)
sum += exp(vec[x] - max);
sum = log(sum) + max;
return sum;
}
float
FLogSum(float *vec, int n)
{
int x;
float max, sum;
max = FMax(vec, n);
sum = 0.0;
for (x = 0; x < n; x++)
if (vec[x] > max - 50.)
sum += exp(vec[x] - max);
sum = log(sum) + max;
return sum;
}
double
DEntropy(double *p, int n)
{
int i;
double entropy;
entropy = 0.;
for(i = 0; i < n; i++)
if (p[i] > 0.) entropy += p[i] * log(p[i]);
return(-1.44269504 * entropy); /* converts to bits */
}
float
FEntropy(float *p, int n)
{
int i;
float entropy;
entropy = 0.;
for(i = 0; i < n; i++)
if (p[i] > 0.) entropy += p[i] * log(p[i]);
return(-1.44269504 * entropy); /* converts to bits */
}
|