Wrapper for Clustal Omega.
[jabaws.git] / binaries / src / clustalo / src / squid / vectorops.c
1 /* vectorops.c
2  * Operations on vectors of floats or doubles.
3  * 
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
14  * 
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.
19  *                        
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)                       
22  */                      
23   
24 #include <stdlib.h>
25 #include <math.h>
26 #include <float.h>
27 #include "vectorops.h"
28
29 void
30 DSet(double *vec, int n, double value)
31 {
32   int x; 
33   for (x = 0; x < n; x++) vec[x] = value;
34 }
35
36 void
37 FSet(float *vec, int n, float value)
38 {
39   int x; 
40   for (x = 0; x < n; x++) vec[x] = value;
41 }
42
43 void
44 DScale(double *vec, int n, double scale)
45 {
46   int x;
47   for (x = 0; x < n; x++) vec[x] *= scale;
48 }
49
50 void
51 FScale(float *vec, int n, float scale)
52 {
53   int x;
54   for (x = 0; x < n; x++) vec[x] *= scale;
55 }
56
57 double 
58 DSum(double *vec, int n)
59 {
60   double sum = 0.;
61   int    x;
62   for (x = 0; x < n; x++) sum += vec[x];
63   return sum;
64 }
65
66 float 
67 FSum(float *vec, int n)
68 {
69   float sum = 0.;
70   int   x;
71   for (x = 0; x < n; x++) sum += vec[x];
72   return sum;
73 }
74
75 void
76 DAdd(double *vec1, double *vec2, int n)
77 {
78   int x;
79   for (x = 0; x < n; x++) vec1[x] += vec2[x];
80 }
81
82 void
83 FAdd(float *vec1, float *vec2, int n)
84 {
85   int x;
86   for (x = 0; x < n; x++) vec1[x] += vec2[x];
87 }
88
89 void
90 DCopy(double *vec1, double *vec2, int n)
91 {
92   int x;
93   for (x = 0; x < n; x++) vec1[x] = vec2[x];
94 }
95
96 void
97 FCopy(float *vec1, float *vec2, int n)
98 {
99   int x;
100   for (x = 0; x < n; x++) vec1[x] = vec2[x];
101 }
102
103 double
104 DDot(double *vec1, double *vec2, int n)
105 {
106   double result = 0.;
107   int x;
108   for (x = 0; x < n; x++) result += vec1[x] * vec2[x];
109   return result;
110 }
111
112 float
113 FDot(float *vec1, float *vec2, int n)
114 {
115   float result = 0.;
116   int x;
117   for (x = 0; x < n; x++) result += vec1[x] * vec2[x];
118   return result;
119 }
120
121 double
122 DMax(double *vec, int n)
123 {
124   int i;
125   double best;
126
127   best = vec[0];
128   for (i = 1; i < n; i++)
129     if (vec[i] > best) best = vec[i];
130   return best;
131 }
132
133 float
134 FMax(float *vec, int n)
135 {
136   int   i;
137   float best;
138
139   best = vec[0];
140   for (i = 1; i < n; i++)
141     if (vec[i] > best) best = vec[i];
142   return best;
143 }
144
145 double
146 DMin(double *vec, int n)
147 {
148   int i;
149   double best;
150
151   best = vec[0];
152   for (i = 1; i < n; i++)
153     if (vec[i] < best) best = vec[i];
154   return best;
155 }
156
157 float
158 FMin(float *vec, int n)
159 {
160   int   i;
161   float best;
162
163   best = vec[0];
164   for (i = 1; i < n; i++)
165     if (vec[i] < best) best = vec[i];
166   return best;
167 }
168
169 double
170 DArgMax(double *vec, int n)
171 {
172   int i;
173   int best = 0;
174
175   for (i = 1; i < n; i++)
176     if (vec[i] > vec[best]) best = i;
177   return best;
178 }
179
180 float
181 FArgMax(float *vec, int n)
182 {
183   int i;
184   int best = 0;
185
186   for (i = 1; i < n; i++)
187     if (vec[i] > vec[best]) best = i;
188   return best;
189 }
190
191 double
192 DArgMin(double *vec, int n)
193 {
194   int i;
195   int best = 0;
196   for (i = 1; i < n; i++)
197     if (vec[i] < vec[best]) best = i;
198   return best;
199 }
200
201 float
202 FArgMin(float *vec, int n)
203 {
204   int   i;
205   int   best = 0;
206
207   for (i = 1; i < n; i++)
208     if (vec[i] < vec[best]) best = i;
209   return best;
210 }
211
212 void
213 DNorm(double *vec, int n)
214 {
215   int    x;
216   double sum;
217
218   sum = DSum(vec, 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;
221 }
222
223 void
224 FNorm(float *vec, int n)
225 {
226   int    x;
227   float  sum;
228
229   sum = FSum(vec, 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;
232 }
233
234 void
235 DLog(double *vec, int n)
236 {
237   int x;
238   for (x = 0; x < n; x++) 
239     if (vec[x] > 0.) vec[x] = log(vec[x]);
240     else vec[x] = -DBL_MAX;
241 }
242
243 void
244 FLog(float *vec, int n)
245 {
246   int x;
247   for (x = 0; x < n; x++) 
248     if (vec[x] > 0.) vec[x] = log(vec[x]);
249     else vec[x] = -FLT_MAX;
250 }
251
252 void
253 DExp(double *vec, int n)
254 {
255   int x;
256   for (x = 0; x < n; x++) vec[x] = exp(vec[x]);
257 }
258
259 void
260 FExp(float *vec, int n)
261 {
262   int x;
263   for (x = 0; x < n; x++) vec[x] = exp(vec[x]);
264 }
265
266 float
267 DLogSum(double *vec, int n)
268 {
269   int x;
270   double max, sum;
271   
272   max = DMax(vec, n);
273   sum = 0.0;
274   for (x = 0; x < n; x++)
275     if (vec[x] > max - 50.)
276       sum += exp(vec[x] - max);
277   sum = log(sum) + max;
278   return sum;
279 }
280
281 float
282 FLogSum(float *vec, int n)
283 {
284   int x;
285   float max, sum;
286   
287   max = FMax(vec, n);
288   sum = 0.0;
289   for (x = 0; x < n; x++)
290     if (vec[x] > max - 50.)
291       sum += exp(vec[x] - max);
292   sum = log(sum) + max;
293   return sum;
294 }
295
296
297