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

Linux Cross Reference
Tina6/tina-libs/tina/math/mathMatv_ivec.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_ivec.c,v $
 37  * Date    :  $Date: 2003/09/23 11:32:20 $
 38  * Version :  $Revision: 1.4 $
 39  * CVS Id  :  $Id: mathMatv_ivec.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 ints.
 44  *
 45  *********
 46 */
 47 /** 
 48  *  @file
 49  *  @brief   Routines for dynamically allocated vectors of ints.        
 50  *
 51  *  Includes functions for allocating and initialising vectors of ints, as well as
 52  *  numerical functions such as finding the dot product, adding two vectors etc.
 53  * 
 54 */
 55 
 56 #include "mathMatv_ivec.h"
 57 
 58 #if HAVE_CONFIG_H
 59 #include <config.h>
 60 #endif
 61 
 62 #include <math.h>
 63 #include <stdlib.h>
 64 
 65 #if HAVE_STDARG_H
 66 #include <stdarg.h>
 67 #define VA_START(a, f)    va_start(a, f)
 68 #else
 69 #if HAVE_VARARGS_H
 70 #include <varargs.h>
 71 #define VA_START(a, f)  va_start(a)
 72 #endif
 73 #endif                          /* HAVE_STDARG_H */
 74 #ifndef VA_START
 75 error no variadic api available
 76 #endif
 77 
 78 #include <tina/sys/sysDef.h>
 79 #include <tina/sys/sysPro.h>
 80 #include <tina/math/math_MatvDef.h>
 81 #include <tina/math/mathUtil_rand.h>
 82 
 83 
 84 /*
 85 Return an n-vector of ints.
 86 */
 87                 Ivec * ivec_make(int n)
 88 {
 89         Ivec           *v = talloc(Ivec);
 90         v->n = n;
 91         v->el = tvector_alloc(0, n, int);
 92         return (v);
 93 }
 94 
 95 /*
 96 Free a vector of ints.
 97 */
 98 void            ivec_free(Ivec * v)
 99 {
100         if (v == NULL)
101                 return;
102         tvector_free(v->el, 0, int);
103         rfree(v);
104 }
105 
106 /*
107 Copy of a vector of ints.
108 */
109 void            ivec_copy(Ivec * v, Ivec * w)
110 {
111         if (v == NULL || w == NULL)
112                 return;
113         tvector_copy_inplace(v->el, w->el, 0, v->n, int);
114 }
115 
116 /*
117 Return a copy of a vector of ints.
118 */
119 Ivec           *ivec_make_copy(Ivec * v)
120 {
121         Ivec           *newv = ivec_make(v->n);
122         ivec_copy(newv, v);
123         return (newv);
124 }
125 
126 /*
127 Allocate and fill a vector of ints.
128 */
129 Ivec
130 #if HAVE_STDARG_H
131 * ivec(int n,...)
132 #else
133 * ivec(n, va_alist)
134 #endif                          /* HAVE_STDARG_H */
135 {
136         va_list         ap;
137         int             i;
138         Ivec           *v = ivec_make(n);
139 
140         VA_START(ap, n);
141         for (i = 0; i < n; i++)
142                 v->el[i] = (int) va_arg(ap, int);
143         va_end(ap);
144         return (v);
145 }
146 
147 Ivec           *ivec_range(int l, int u)
148 {
149         Ivec           *v = ivec_make(u - l);
150         int             i;
151         for (i = l; i < u; i++)
152                 v->el[i - l] = i;
153         return (v);
154 }
155 
156 /*
157 Set zero vector.
158 */
159 void            ivec_zero(Ivec * v)
160 {
161         int             i;
162         if (v == NULL)
163                 return;
164         for (i = 0; i < v->n; i++)
165                 v->el[i] = 0;
166 }
167 
168 /*
169 Set uniform random vector.
170 */
171 void            ivec_rand_unif(Ivec * v, int p, int q)
172 {
173         int             i;
174         if (v == NULL)
175                 return;
176         for (i = 0; i < v->n; i++)
177                 v->el[i] = (int) floor(rand_unif(p, q));        /* NAT 3/5/95 */
178 }
179 
180 /*
181 Set Gaussian random vector.
182 */
183 void            ivec_rand_normal(Ivec * v, int m, int s)
184 {
185         int             i;
186         if (v == NULL)
187                 return;
188         for (i = 0; i < v->n; i++)
189                 v->el[i] = (int) floor(rand_normal(m, s));      /* NAT 3/5/95 */
190 }
191 
192 /*
193 Return dot (scalar) product of two vectors.
194 */
195 int             ivec_dot(Ivec * v, Ivec * w)
196 {
197         int             i;
198         int             sum;
199         if (v == NULL || w == NULL)
200                 return (0);
201         for (sum = 0, i = 0; i < v->n; i++)
202                 sum += v->el[i] * w->el[i];
203         return (sum);
204 }
205 
206 /*
207 Form vector sum of v and w in vector v.
208 */
209 void            ivec_sum(Ivec * v, Ivec * w)
210 {
211         int             i;
212         if (v == NULL || w == NULL)
213                 return;
214         for (i = 0; i < v->n; i++)
215                 v->el[i] += w->el[i];
216 }
217 
218 /*
219 Form vector difference of v and w in vector v.
220 */
221 void            ivec_diff(Ivec * v, Ivec * w)
222 {
223         int             i;
224         if (v == NULL || w == NULL)
225                 return;
226         for (i = 0; i < v->n; i++)
227                 v->el[i] -= w->el[i];
228 }
229 
230 /*
231 Form product of scalar k and vector v in vector v.
232 */
233 void            ivec_times(int k, Ivec * v)
234 {
235         int             i;
236         if (v == NULL)
237                 return;
238         for (i = 0; i < v->n; i++)
239                 v->el[i] *= k;
240 }
241 
242 /*
243 Negate components of vector v.
244 */
245 void            ivec_minus(Ivec * v)
246 {
247         int             i;
248         if (v == NULL)
249                 return;
250         for (i = 0; i < v->n; i++)
251                 v->el[i] = -v->el[i];
252 }
253 
254 /*
255 Increment components of v by scalar k times those of vector w.
256 */
257 void            ivec_accum(Ivec * v, int k, Ivec * w)
258 {
259         int             i;
260         if (v == NULL || w == NULL)
261                 return;
262         for (i = 0; i < v->n; i++)
263                 v->el[i] += k * w->el[i];
264 }
265 
266 /*
267 Return squared modulus (length) of vector v.
268 */
269 int             ivec_sqrmod(Ivec * v)
270 {
271         int             i;
272         int             sum = 0;
273         if (v == NULL)
274                 return (0);
275         for (i = 0; i < v->n; i++)
276                 sum += v->el[i] * v->el[i];
277         return (sum);
278 }
279 
280 /*
281 Return modulus (length) of vector v.
282 */
283 int             ivec_mod(Ivec * v)
284 {
285         int             i;
286         int             sum = 0;
287         if (v == NULL)
288                 return (0);
289         for (i = 0; i < v->n; i++)
290                 sum += abs(v->el[i]);
291         return ((int) sqrt(sum));
292 }
293 
294 /*
295 Return L1 modulus (max element) of vector v.
296 */
297 int             ivec_mod1(Ivec * v)
298 {
299         int             i;
300         int             xmax = 0;
301         if (v == NULL)
302                 return (0);
303         for (i = 0; i < v->n; i++)
304                 xmax = MAX(xmax, abs(v->el[i]));
305         return (xmax);
306 }
307 
308 /*
309 Return squared distance between points (vectors) v and w.
310 */
311 int             ivec_sqrdist(Ivec * v, Ivec * w)
312 {
313         int             i;
314         int             sum = 0;
315         if (v == NULL || w == NULL)
316                 return (0);
317         for (i = 0; i < v->n; i++)
318         {
319                 int             dx = w->el[i] - v->el[i];
320                 sum += dx * dx;
321         }
322         return (sum);
323 }
324 
325 /*
326 Return distance between points (vectors) v and w.
327 */
328 int             ivec_dist(Ivec * v, Ivec * w)
329 {
330         int             i;
331         int             sum = 0;
332         if (v == NULL || w == NULL)
333                 return (0);
334         for (i = 0; i < v->n; i++)
335         {
336                 int             dx = w->el[i] - v->el[i];
337                 sum += abs(dx);
338         }
339         return ((int) sqrt(sum));
340 }
341 
342 void            ivec_print(FILE * fp, char *fmt, Ivec * v)
343 {
344         int             i;
345         if (v == NULL)
346         {
347                 fprintf(fp, "0 :\n");
348                 return;
349         }
350         fprintf(fp, "%d : ", v->n);
351         for (i = 0; i < v->n; i++)
352                 fprintf(fp, fmt, v->el[i]);
353         fprintf(fp, "\n");
354 }
355 
356 Ivec           *ivec_read(FILE * fp)
357 {
358         int             i, n;
359         Ivec           *v;
360         fscanf(fp, "%d %*s", &n);
361         v = ivec_make(n);
362         for (i = 0; i < v->n; i++)
363                 fscanf(fp, "%d", &v->el[i]);
364         return (v);
365 }
366 
367 
368 /*
369 Reverse components of vector w into vector v.
370 */
371 void            ivec_reverse(Ivec * v, Ivec * w)
372 {
373         int             i, n1;
374         if (v == NULL)
375                 return;
376         n1 = v->n - 1;
377         for (i = 0; i < v->n; i++)
378                 v->el[i] = w->el[n1 - i];
379 }
380 
381 void            ivec_block_get(Ivec * u, int li, Ivec * v)
382 {
383         int             i;
384         if (u == NULL || v == NULL)
385                 return;
386         for (i = 0; i < v->n; i++)
387                 v->el[i] = u->el[i + li];
388 }
389 
390 void            ivec_block_set(Ivec * u, int li, Ivec * v)
391 {
392         int             i;
393         if (u == NULL || v == NULL)
394                 return;
395         for (i = 0; i < v->n; i++)
396                 u->el[i + li] = v->el[i];
397 }
398 
399 void            ivec_index_get(Ivec * u, Ivec * index, Ivec * v)
400 {
401         int             i;
402         if (u == NULL || v == NULL)
403                 return;
404         for (i = 0; i < v->n; i++)
405                 v->el[i] = u->el[index->el[i]];
406 }
407 
408 void            ivec_index_set(Ivec * u, Ivec * index, Ivec * v)
409 {
410         int             i;
411         if (u == NULL || v == NULL)
412                 return;
413         for (i = 0; i < v->n; i++)
414                 u->el[index->el[i]] = v->el[i];
415 }
416 

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