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

Linux Cross Reference
Tina6/tina-libs/tina/math/mathMatv_vec.c

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

  1 /**********
  2  *
  3  * Copyright (c) 2003, Division of Imaging Science and Biomedical Engineering,
  4  * University of Manchester, UK.  All rights reserved.
  5  * 
  6  * Redistribution and use in source and binary forms, with or without modification, 
  7  * are permitted provided that the following conditions are met:
  8  * 
  9  *   . Redistributions of source code must retain the above copyright notice, 
 10  *     this list of conditions and the following disclaimer.
 11  *    
 12  *   . Redistributions in binary form must reproduce the above copyright notice,
 13  *     this list of conditions and the following disclaimer in the documentation 
 14  *     and/or other materials provided with the distribution.
 15  * 
 16  *   . Neither the name of the University of Manchester nor the names of its
 17  *     contributors may be used to endorse or promote products derived from this 
 18  *     software without specific prior written permission.
 19  * 
 20  * 
 21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  * POSSIBILITY OF SUCH DAMAGE.
 32  *
 33  **********
 34  *
 35  * Program :    TINA
 36  * File    :  $Source: /home/tina/cvs/tina-libs/tina/math/mathMatv_vec.c,v $
 37  * Date    :  $Date: 2003/09/23 11:32:20 $
 38  * Version :  $Revision: 1.4 $
 39  * CVS Id  :  $Id: mathMatv_vec.c,v 1.4 2003/09/23 11:32:20 matts Exp $
 40  *
 41  * Author  :  Legacy TINA
 42  *
 43  * Notes : Routines for dynamically allocated vectors of doubles.
 44  *
 45  *********
 46 */
 47 /** 
 48  *  @file
 49  *  @brief  Functions pertaining to Vec's, dynamically allocated vectors of doubles.    
 50  *
 51  *  Functions include;
 52  *
 53  *  - Vec allocation and initialisation.
 54  *  - Calculations using Vec's (eg, Vec addition, multiplication).
 55  *  - I/O functions.
 56  *  - Mat access/set/get functions.
 57  * 
 58 */
 59 #include "mathMatv_vec.h"
 60 
 61 #if HAVE_CONFIG_H
 62 #include <config.h>
 63 #endif
 64 
 65 #include <math.h>
 66 #include <float.h>
 67 #include <tina/sys/sysDef.h>
 68 #include <tina/sys/sysPro.h>
 69 #include <tina/math/math_MatvDef.h>
 70 #include <tina/math/math_UtilPro.h>
 71 
 72 /*
 73 Return an n-vector of doubles.
 74 */
 75 Vec            *vec_make(int n)
 76 {
 77         Vec            *v = talloc(Vec);
 78         v->n = n;
 79         v->el = tvector_alloc(0, n, double);
 80         return (v);
 81 }
 82 
 83 /*
 84 Free a vector of doubles.
 85 */
 86 void            vec_free(Vec * v)
 87 {
 88         if (v == NULL)
 89                 return;
 90         tvector_free(v->el, 0, double);
 91         rfree(v);
 92 }
 93 
 94 /*
 95 Copy of a vector of doubles.
 96 */
 97 void            vec_copy(Vec * v, Vec * w)
 98 {
 99         if (v == NULL || w == NULL)
100                 return;
101         tvector_copy_inplace(v->el, w->el, 0, v->n, double);
102 }
103 
104 /*
105 Allocate copy of a vector of doubles.
106 */
107 Vec            *vec_make_copy(Vec * w)
108 {
109         Vec            *v = vec_make(w->n);
110         tvector_copy_inplace(v->el, w->el, 0, v->n, double);
111         return (v);
112 }
113 
114 /*
115 Set zero vector.
116 */
117 void            vec_zero(Vec * v)
118 {
119         int             i;
120         if (v == NULL)
121                 return;
122         for (i = 0; i < v->n; i++)
123                 v->el[i] = 0;
124 }
125 
126 /*
127 Set uniform random vector.
128 */
129 void            vec_rand_unif(Vec * v, double p, double q)
130 {
131         int             i;
132         if (v == NULL)
133                 return;
134         for (i = 0; i < v->n; i++)
135                 v->el[i] = rand_unif(p, q);
136 }
137 
138 /*
139 Set Gaussian random vector.
140 */
141 void            vec_rand_normal(Vec * v, double m, double s)
142 {
143         int             i;
144         if (v == NULL)
145                 return;
146         for (i = 0; i < v->n; i++)
147                 v->el[i] = rand_normal(m, s);
148 }
149 
150 /*
151 Return dot (scalar) product of two vectors.
152 */
153 double          vec_dot(Vec * v, Vec * w)
154 {
155         int             i;
156         double          sum;
157         if (v == NULL || w == NULL)
158                 return (0.0);
159         for (sum = 0, i = 0; i < v->n; i++)
160                 sum += v->el[i] * w->el[i];
161         return (sum);
162 }
163 
164 /*
165 Form vector sum of v and w in vector v.
166 */
167 void            vec_sum(Vec * v, Vec * w)
168 {
169         int             i;
170         if (v == NULL || w == NULL)
171                 return;
172         for (i = 0; i < v->n; i++)
173                 v->el[i] += w->el[i];
174 }
175 
176 /*
177 Form vector difference of v and w in vector v.
178 */
179 void            vec_diff(Vec * v, Vec * w)
180 {
181         int             i;
182         if (v == NULL || w == NULL)
183                 return;
184         for (i = 0; i < v->n; i++)
185                 v->el[i] -= w->el[i];
186 }
187 
188 /*
189 Form component-wise product of v and w in vector v.
190 */
191 void            vec_prod(Vec * v, Vec * w)
192 {
193         int             i;
194         if (v == NULL || w == NULL)
195                 return;
196         for (i = 0; i < v->n; i++)
197                 v->el[i] *= w->el[i];
198 }
199 
200 /*
201 Form component-wise quotient of v and w in vector v.
202 */
203 void            vec_divide(Vec * v, Vec * w)
204 {
205         int             i;
206         if (v == NULL || w == NULL)
207                 return;
208         for (i = 0; i < v->n; i++)
209                 v->el[i] /= w->el[i];
210 }
211 
212 /*
213 Form product of scalar k and vector v in vector v.
214 */
215 void            vec_times(double k, Vec * v)
216 {
217         int             i;
218         if (v == NULL)
219                 return;
220         for (i = 0; i < v->n; i++)
221                 v->el[i] *= k;
222 }
223 
224 /*
225 Negate components of vector v.
226 */
227 void            vec_minus(Vec * v)
228 {
229         int             i;
230         if (v == NULL)
231                 return;
232         for (i = 0; i < v->n; i++)
233                 v->el[i] = -v->el[i];
234 }
235 
236 /*
237 Increment components of v by scalar k times those of vector w.
238 */
239 void            vec_accum(Vec * v, double k, Vec * w)
240 {
241         int             i;
242         if (v == NULL || w == NULL)
243                 return;
244         for (i = 0; i < v->n; i++)
245                 v->el[i] += k * w->el[i];
246 }
247 
248 /*
249 Return squared modulus (length) of vector v.
250 */
251 double          vec_sqrmod(Vec * v)
252 {
253         int             i;
254         double          sum = 0;
255         if (v == NULL)
256                 return (0);
257         for (i = 0; i < v->n; i++)
258                 sum += v->el[i] * v->el[i];
259         return (sum);
260 }
261 
262 /*
263 Return modulus (length) of vector v.
264 */
265 double          vec_mod(Vec * v)
266 {
267         int             i;
268         double          sum = 0;
269         if (v == NULL)
270                 return (0);
271         for (i = 0; i < v->n; i++)
272                 sum += v->el[i] * v->el[i];
273         return (sqrt(sum));
274 }
275 
276 /*
277 Normalise vector to unit length.
278 */
279 void            vec_unit(Vec * v)
280 {
281         double          l = vec_mod(v);
282         if (l == 0)
283                 return;
284         vec_times(1.0 / l, v);
285 }
286 
287 /*
288 Return L1 modulus (max element) of vector v.
289 */
290 double          vec_mod1(Vec * v)
291 {
292         int             i;
293         double          xmax = 0;
294         if (v == NULL)
295                 return (0);
296         for (i = 0; i < v->n; i++)
297                 xmax = MAX(xmax, fabs(v->el[i]));
298         return (xmax);
299 }
300 
301 /*
302 Return squared distance between points (vectors) v and w.
303 */
304 double          vec_sqrdist(Vec * v, Vec * w)
305 {
306         int             i;
307         double          sum = 0;
308         if (v == NULL || w == NULL)
309                 return (0);
310         for (i = 0; i < v->n; i++)
311         {
312                 double          dx = w->el[i] - v->el[i];
313                 sum += dx * dx;
314         }
315         return (sum);
316 }
317 
318 /*
319 Return distance between points (vectors) v and w.
320 */
321 double          vec_dist(Vec * v, Vec * w)
322 {
323         int             i;
324         double          sum = 0;
325         if (v == NULL || w == NULL)
326                 return (0);
327         for (i = 0; i < v->n; i++)
328         {
329                 double          dx = w->el[i] - v->el[i];
330                 sum += dx * dx;
331         }
332         return (sqrt(sum));
333 }
334 
335 void            vec_print(FILE * fp, char *fmt, Vec * v)
336 {
337         int             i;
338         if (v == NULL)
339         {
340                 fprintf(fp, "0 :\n");
341                 return;
342         }
343         fprintf(fp, "%d :\n", v->n);
344         for (i = 0; i < v->n; i++)
345                 fprintf(fp, fmt, v->el[i]);
346         fprintf(fp, "\n");
347 }
348 
349 Vec            *vec_read(FILE * fp)
350 {
351         int             i, n;
352         Vec            *v;
353         fscanf(fp, "%d %*s", &n);
354         v = vec_make(n);
355         for (i = 0; i < v->n; i++)
356                 fscanf(fp, "%lf", &v->el[i]);
357         return (v);
358 }
359 
360 /*
361 Reverse components of vector w into vector v.
362 */
363 void            vec_reverse(Vec * v, Vec * w)
364 {
365         int             i, n1;
366         if (v == NULL)
367                 return;
368         n1 = v->n - 1;
369         for (i = 0; i < v->n; i++)
370                 v->el[i] = w->el[n1 - i];
371 }
372 
373 void            vec_block_get(Vec * u, int li, Vec * v)
374 {
375         int             i;
376         if (u == NULL || v == NULL)
377                 return;
378         for (i = 0; i < v->n; i++)
379                 v->el[i] = u->el[i + li];
380 }
381 
382 void            vec_block_set(Vec * u, int li, Vec * v)
383 {
384         int             i;
385         if (u == NULL || v == NULL)
386                 return;
387         for (i = 0; i < v->n; i++)
388                 u->el[i + li] = v->el[i];
389 }
390 
391 void            vec_index_get(Vec * u, Ivec * index, Vec * v)
392 {
393         int             i;
394         if (u == NULL || v == NULL)
395                 return;
396         for (i = 0; i < v->n; i++)
397                 v->el[i] = u->el[index->el[i]];
398 }
399 
400 void            vec_index_set(Vec * u, Ivec * index, Vec * v)
401 {
402         int             i;
403         if (u == NULL || v == NULL)
404                 return;
405         for (i = 0; i < v->n; i++)
406                 u->el[index->el[i]] = v->el[i];
407 }
408 
409 double          vec_max(Vec * v, int *imax)
410 {
411         int             i;
412         double          vmax = -FLT_MAX;
413         for (i = 0; i < v->n; i++)
414         {
415                 double          vi = v->el[i];
416                 if (vmax < vi)
417                 {
418                         vmax = vi;
419                         *imax = i;
420                 }
421         }
422         return (vmax);
423 }
424 
425 double          vec_min(Vec * v, int *imin)
426 {
427         int             i;
428         double          vmin = FLT_MAX;
429         for (i = 0; i < v->n; i++)
430         {
431                 double          vi = v->el[i];
432                 if (vmin > vi)
433                 {
434                         vmin = vi;
435                         *imin = i;
436                 }
437         }
438         return (vmin);
439 }
440 

~ [ 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.