~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina4/src/math/matvec/vec.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  *
  3  * vec.c
  4  *
  5  * Routines for dynamically allocated vectors of doubles.
  6  *
  7  */
  8 
  9 #include <tina/sys.h>
 10 #include <tina/sysfuncs.h>
 11 #include <tina/math.h>
 12 #include <tina/mathfuncs.h>
 13 
 14 /*
 15 Return an n-vector of doubles.
 16 */
 17 Vec *vec_make(int n)
 18 {
 19     Vec *v = talloc(Vec);
 20     v->n = n;
 21     v->el = tvector_alloc(0, n, double);
 22     return(v);
 23 }
 24 
 25 /*
 26 Free a vector of doubles.
 27 */
 28 void vec_free(Vec *v)
 29 {
 30     if(v == NULL)
 31         return;
 32     tvector_free(v->el, 0, double);
 33     rfree(v);
 34 }
 35 
 36 /*
 37 Copy of a vector of doubles.
 38 */
 39 void vec_copy(Vec *v, Vec *w)
 40 {
 41     if(v == NULL || w == NULL)
 42         return;
 43     tvector_copy_inplace(v->el, w->el, 0, v->n, double);
 44 }
 45 
 46 /*
 47 Allocate copy of a vector of doubles.
 48 */
 49 Vec *vec_make_copy(Vec *w)
 50 {
 51     Vec *v = vec_make(w->n);
 52     tvector_copy_inplace(v->el, w->el, 0, v->n, double);
 53     return(v);
 54 }
 55  
 56 /*
 57 Set zero vector.
 58 */
 59 void vec_zero(Vec *v)
 60 {
 61     int i;
 62     if(v == NULL)
 63         return;
 64     for(i = 0; i < v->n; i++)
 65         v->el[i] = 0;
 66 }
 67 
 68 /*
 69 Set uniform random vector.
 70 */
 71 void vec_rand_unif(Vec *v, double p, double q)
 72 {
 73     int i;
 74     if(v == NULL)
 75         return;
 76     for(i = 0; i < v->n; i++)
 77         v->el[i] = rand_unif(p, q);
 78 }
 79 
 80 /*
 81 Set Gaussian random vector.
 82 */
 83 void vec_rand_normal(Vec *v, double m, double s)
 84 {
 85     int i;
 86     if(v == NULL)
 87         return;
 88     for(i = 0; i < v->n; i++)
 89         v->el[i] = rand_normal(m, s);
 90 }
 91 
 92 /*
 93 Return dot (scalar) product of two vectors.
 94 */
 95 double vec_dot(Vec *v, Vec *w)
 96 {
 97     int i;
 98     double sum;
 99     if(v == NULL || w == NULL)
100         return(0.0);
101     for(sum = 0, i = 0; i < v->n; i++)
102         sum += v->el[i]*w->el[i];
103     return(sum);
104 }
105 
106 /*
107 Form vector sum of v and w in vector v.
108 */
109 void vec_sum(Vec *v, Vec *w)
110 {
111     int i;
112     if(v == NULL || w == NULL)
113         return;
114     for(i = 0; i < v->n; i++)
115         v->el[i] += w->el[i];
116 }
117 
118 /*
119 Form vector difference of v and w in vector v.
120 */
121 void vec_diff(Vec *v, Vec *w)
122 {
123     int i;
124     if(v == NULL || w == NULL)
125         return;
126     for(i = 0; i < v->n; i++)
127         v->el[i] -= w->el[i];
128 }
129 
130 /*
131 Form component-wise product of v and w in vector v.
132 */
133 void vec_prod(Vec *v, Vec *w)
134 {
135     int i;
136     if(v == NULL || w == NULL)
137         return;
138     for(i = 0; i < v->n; i++)
139         v->el[i] *= w->el[i];
140 }
141 
142 /*
143 Form component-wise quotient of v and w in vector v.
144 */
145 void vec_divide(Vec *v, Vec *w)
146 {
147     int i;
148     if(v == NULL || w == NULL)
149         return;
150     for(i = 0; i < v->n; i++)
151         v->el[i] /= w->el[i];
152 }
153 
154 /*
155 Form product of scalar k and vector v in vector v.
156 */
157 void vec_times(double k, Vec *v)
158 {
159     int i;
160     if(v == NULL)
161         return;
162     for(i = 0; i < v->n; i++)
163         v->el[i] *= k;
164 }
165 
166 /*
167 Negate components of vector v.
168 */
169 void vec_minus(Vec *v)
170 {
171     int i;
172     if(v == NULL)
173         return;
174     for(i = 0; i < v->n; i++)
175         v->el[i] = -v->el[i];
176 }
177 
178 /*
179 Increment components of v by scalar k times those of vector w.
180 */
181 void vec_accum(Vec *v, double k, Vec *w)
182 {
183     int i;
184     if(v == NULL || w == NULL)
185         return;
186     for(i = 0; i < v->n; i++)
187         v->el[i] += k*w->el[i];
188 }
189 
190 /*
191 Return squared modulus (length) of vector v.
192 */
193 double vec_sqrmod(Vec *v)
194 {
195     int i;
196     double sum = 0;
197     if(v == NULL)
198         return(0);
199     for(i = 0; i < v->n; i++)
200         sum += v->el[i]*v->el[i];
201     return(sum);
202 }
203 
204 /*
205 Return modulus (length) of vector v.
206 */
207 double vec_mod(Vec *v)
208 {
209     int i;
210     double sum = 0;
211     if(v == NULL)
212         return(0);
213     for(i = 0; i < v->n; i++)
214         sum += v->el[i]*v->el[i];
215     return(sqrt(sum));
216 }
217 
218 /*
219 Normalise vector to unit length.
220 */
221 void vec_unit(Vec *v)
222 {
223     double l = vec_mod(v);
224     if(l == 0)
225         return;
226     vec_times(1.0/l, v);
227 }
228 
229 /*
230 Return L1 modulus (max element) of vector v.
231 */
232 double vec_mod1(Vec *v)
233 {
234     int i;
235     double xmax = 0;
236     if(v == NULL)
237         return(0);
238     for(i = 0; i < v->n; i++)
239         xmax = MAX(xmax, fabs(v->el[i]));
240     return(xmax);
241 }
242 
243 /*
244 Return squared distance between points (vectors) v and w.
245 */
246 double vec_sqrdist(Vec *v, Vec *w)
247 {
248     int i;
249     double sum = 0;
250     if(v == NULL || w == NULL)
251         return(0);
252     for(i = 0; i < v->n; i++)
253     {
254         double dx = w->el[i]-v->el[i];
255         sum += dx*dx;
256     }
257     return(sum);
258 }
259 
260 /*
261 Return distance between points (vectors) v and w.
262 */
263 double vec_dist(Vec *v, Vec *w)
264 {
265     int i;
266     double sum = 0;
267     if(v == NULL || w == NULL)
268         return(0);
269     for(i = 0; i < v->n; i++)
270     {
271         double dx = w->el[i]-v->el[i];
272         sum += dx*dx;
273     }
274     return(sqrt(sum));
275 }
276 
277 void vec_print(FILE *fp, char *fmt, Vec *v)
278 {
279     int i;
280     if(v == NULL)
281     {
282         fprintf(fp, "0 :\n");
283         return;
284     }
285     fprintf(fp, "%d :\n", v->n);
286     for(i = 0; i < v->n; i++)
287         fprintf(fp, fmt, v->el[i]);
288     fprintf(fp, "\n");
289 }
290 
291 Vec *vec_read(FILE *fp)
292 {
293     int i, n;
294     Vec *v;
295     fscanf(fp, "%d %*s", &n);
296     v = vec_make(n);
297     for(i = 0; i < v->n; i++)
298         fscanf(fp, "%lf", &v->el[i]);
299     return(v);
300 }
301 
302 /*
303 Reverse components of vector w into vector v.
304 */
305 void vec_reverse(Vec *v, Vec *w)
306 {
307     int i, n1;
308     if(v == NULL)
309         return;
310     n1 = v->n-1;
311     for(i = 0; i < v->n; i++)
312         v->el[i] = w->el[n1-i];
313 }
314 
315 void vec_block_get(Vec *u, int li, Vec *v)
316 {
317     int i;
318     if(u == NULL || v == NULL)
319         return;
320     for(i = 0; i < v->n; i++)
321         v->el[i] = u->el[i+li];
322 }
323 
324 void vec_block_set(Vec *u, int li, Vec *v)
325 {
326     int i;
327     if(u == NULL || v == NULL)
328         return;
329     for(i = 0; i < v->n; i++)
330         u->el[i+li] = v->el[i];
331 }
332 
333 void vec_index_get(Vec *u, Ivec *index, Vec *v)
334 {
335     int i;
336     if(u == NULL || v == NULL)
337         return;
338     for(i = 0; i < v->n; i++)
339         v->el[i] = u->el[index->el[i]];
340 }
341 
342 void vec_index_set(Vec *u, Ivec *index, Vec *v)
343 {
344     int i;
345     if(u == NULL || v == NULL)
346         return;
347     for(i = 0; i < v->n; i++)
348         u->el[index->el[i]] = v->el[i];
349 }
350 
351 double vec_max(Vec *v, int *imax)
352 {
353     int i;
354     double vmax = - MAXFLOAT;
355     for(i = 0; i < v->n; i++)
356     {
357         double vi = v->el[i];
358         if(vmax < vi)
359         {
360             vmax = vi;
361             *imax = i;
362         }
363     }
364     return(vmax);
365 }
366 
367 double vec_min(Vec *v, int *imin)
368 {
369     int i;
370     double vmin = MAXFLOAT;
371     for(i = 0; i < v->n; i++)
372     {
373         double vi = v->el[i];
374         if(vmin > vi)
375         {
376             vmin = vi;
377             *imin = i;
378         }
379     }
380     return(vmin);
381 }
382 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.