2 * Operations on vectors of floats or doubles.
4 * DSet(), FSet() - set all items in vector to value.
5 * DScale(), FScale() - multiply all items in vector by scale
6 * DSum(), FSum() - return sum of values in vector
7 * DAdd(), FAdd() - add vec2 to vec1.
8 * DCopy(), FCopy() - set vec1 to be same as vec2.
9 * DDot(), FDot() - return dot product of two vectors.
10 * DMax(), FMax() - return value of maximum element in vector
11 * DMin(), FMin() - return value of minimum element in vector
12 * DArgMax(), FArgMax() - return index of maximum element in vector
13 * DArgMin(), FArgMin() - return index of minimum element in vector
15 * DNorm(), FNorm() - normalize a probability vector of length n.
16 * DLog(), FLog() - convert to log probabilities
17 * DExp(), FExp() - convert log p's back to probabilities
18 * DLogSum(), FLogSum() - given vector of log p's; return log of summed p's.
20 * SRE, Tue Oct 1 15:23:25 2002 [St. Louis]
21 * CVS $Id: vectorops.c,v 1.1 2002/10/09 14:26:09 eddy Exp)
27 #include "vectorops.h"
30 DSet(double *vec, int n, double value)
33 for (x = 0; x < n; x++) vec[x] = value;
37 FSet(float *vec, int n, float value)
40 for (x = 0; x < n; x++) vec[x] = value;
44 DScale(double *vec, int n, double scale)
47 for (x = 0; x < n; x++) vec[x] *= scale;
51 FScale(float *vec, int n, float scale)
54 for (x = 0; x < n; x++) vec[x] *= scale;
58 DSum(double *vec, int n)
62 for (x = 0; x < n; x++) sum += vec[x];
67 FSum(float *vec, int n)
71 for (x = 0; x < n; x++) sum += vec[x];
76 DAdd(double *vec1, double *vec2, int n)
79 for (x = 0; x < n; x++) vec1[x] += vec2[x];
83 FAdd(float *vec1, float *vec2, int n)
86 for (x = 0; x < n; x++) vec1[x] += vec2[x];
90 DCopy(double *vec1, double *vec2, int n)
93 for (x = 0; x < n; x++) vec1[x] = vec2[x];
97 FCopy(float *vec1, float *vec2, int n)
100 for (x = 0; x < n; x++) vec1[x] = vec2[x];
104 DDot(double *vec1, double *vec2, int n)
108 for (x = 0; x < n; x++) result += vec1[x] * vec2[x];
113 FDot(float *vec1, float *vec2, int n)
117 for (x = 0; x < n; x++) result += vec1[x] * vec2[x];
122 DMax(double *vec, int n)
128 for (i = 1; i < n; i++)
129 if (vec[i] > best) best = vec[i];
134 FMax(float *vec, int n)
140 for (i = 1; i < n; i++)
141 if (vec[i] > best) best = vec[i];
146 DMin(double *vec, int n)
152 for (i = 1; i < n; i++)
153 if (vec[i] < best) best = vec[i];
158 FMin(float *vec, int n)
164 for (i = 1; i < n; i++)
165 if (vec[i] < best) best = vec[i];
170 DArgMax(double *vec, int n)
175 for (i = 1; i < n; i++)
176 if (vec[i] > vec[best]) best = i;
181 FArgMax(float *vec, int n)
186 for (i = 1; i < n; i++)
187 if (vec[i] > vec[best]) best = i;
192 DArgMin(double *vec, int n)
196 for (i = 1; i < n; i++)
197 if (vec[i] < vec[best]) best = i;
202 FArgMin(float *vec, int n)
207 for (i = 1; i < n; i++)
208 if (vec[i] < vec[best]) best = i;
213 DNorm(double *vec, int n)
219 if (sum != 0.0) for (x = 0; x < n; x++) vec[x] /= sum;
220 else for (x = 0; x < n; x++) vec[x] = 1. / (double) n;
224 FNorm(float *vec, int n)
230 if (sum != 0.0) for (x = 0; x < n; x++) vec[x] /= sum;
231 else for (x = 0; x < n; x++) vec[x] = 1. / (float) n;
235 DLog(double *vec, int n)
238 for (x = 0; x < n; x++)
239 if (vec[x] > 0.) vec[x] = log(vec[x]);
240 else vec[x] = -DBL_MAX;
244 FLog(float *vec, int n)
247 for (x = 0; x < n; x++)
248 if (vec[x] > 0.) vec[x] = log(vec[x]);
249 else vec[x] = -FLT_MAX;
253 DExp(double *vec, int n)
256 for (x = 0; x < n; x++) vec[x] = exp(vec[x]);
260 FExp(float *vec, int n)
263 for (x = 0; x < n; x++) vec[x] = exp(vec[x]);
267 DLogSum(double *vec, int n)
274 for (x = 0; x < n; x++)
275 if (vec[x] > max - 50.)
276 sum += exp(vec[x] - max);
277 sum = log(sum) + max;
282 FLogSum(float *vec, int n)
289 for (x = 0; x < n; x++)
290 if (vec[x] > max - 50.)
291 sum += exp(vec[x] - max);
292 sum = log(sum) + max;