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

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

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

  1 /*
  2  *
  3  * ivec.c
  4  *
  5  * Routines for dynamically allocated vectors of ints.
  6  *
  7  */
  8 #include <stdarg.h>
  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 ints.
 16 */
 17 Ivec *ivec_make(int n)
 18 {
 19     Ivec *v = talloc(Ivec);
 20     v->n = n;
 21     v->el = tvector_alloc(0, n, int);
 22     return(v);
 23 }
 24 
 25 /*
 26 Free a vector of ints.
 27 */
 28 void ivec_free(Ivec *v)
 29 {
 30     if(v == NULL)
 31         return;
 32     tvector_free(v->el, 0, int);
 33     rfree(v);
 34 }
 35 
 36 /*
 37 Copy of a vector of ints.
 38 */
 39 void ivec_copy(Ivec *v, Ivec *w)
 40 {
 41     if(v == NULL || w == NULL)
 42         return;
 43     tvector_copy_inplace(v->el, w->el, 0, v->n, int);
 44 }
 45 
 46 /*
 47 Return a copy of a vector of ints.
 48 */
 49 Ivec *ivec_make_copy(Ivec *v)
 50 {
 51     Ivec *newv = ivec_make(v->n);
 52     ivec_copy(newv, v);
 53     return(newv);
 54 }
 55  
 56 /*
 57 Allocate and fill a vector of ints.
 58 */
 59 Ivec *ivec(int n, ...)
 60 {
 61     va_list ap;
 62     int     i;
 63     Ivec *v = ivec_make(n);
 64 
 65     va_start(ap, n);
 66     for (i = 0; i < n; i++)
 67         v->el[i] = (int) va_arg(ap, int);
 68     va_end(ap);
 69     return(v);
 70 }
 71 
 72 Ivec *ivec_range(int l, int u)
 73 {
 74     Ivec *v = ivec_make(u-l);
 75     int i;
 76     for(i = l; i < u; i++)
 77         v->el[i-l] = i;
 78     return(v);
 79 }
 80 
 81 /*
 82 Set zero vector.
 83 */
 84 void ivec_zero(Ivec *v)
 85 {
 86     int i;
 87     if(v == NULL)
 88         return;
 89     for(i = 0; i < v->n; i++)
 90         v->el[i] = 0;
 91 }
 92 
 93 /*
 94 Set uniform random vector.
 95 */
 96 void ivec_rand_unif(Ivec *v, int p, int q)
 97 {
 98     int i;
 99     if(v == NULL)
100         return;
101     for(i = 0; i < v->n; i++)
102         v->el[i] = (int)floor(rand_unif(p, q)); /* NAT 3/5/95 */
103 }
104 
105 /*
106 Set Gaussian random vector.
107 */
108 void ivec_rand_normal(Ivec *v, int m, int s)
109 {
110     int i;
111     if(v == NULL)
112         return;
113     for(i = 0; i < v->n; i++)
114         v->el[i] = (int)floor(rand_normal(m, s)); /* NAT 3/5/95 */
115 }
116 
117 /*
118 Return dot (scalar) product of two vectors.
119 */
120 int ivec_dot(Ivec *v, Ivec *w)
121 {
122     int i;
123     int sum;
124     if(v == NULL || w == NULL)
125         return(0);
126     for(sum = 0, i = 0; i < v->n; i++)
127         sum += v->el[i]*w->el[i];
128     return(sum);
129 }
130 
131 /*
132 Form vector sum of v and w in vector v.
133 */
134 void ivec_sum(Ivec *v, Ivec *w)
135 {
136     int i;
137     if(v == NULL || w == NULL)
138         return;
139     for(i = 0; i < v->n; i++)
140         v->el[i] += w->el[i];
141 }
142 
143 /*
144 Form vector difference of v and w in vector v.
145 */
146 void ivec_diff(Ivec *v, Ivec *w)
147 {
148     int i;
149     if(v == NULL || w == NULL)
150         return;
151     for(i = 0; i < v->n; i++)
152         v->el[i] -= w->el[i];
153 }
154 
155 /*
156 Form product of scalar k and vector v in vector v.
157 */
158 void ivec_times(int k, Ivec *v)
159 {
160     int i;
161     if(v == NULL)
162         return;
163     for(i = 0; i < v->n; i++)
164         v->el[i] *= k;
165 }
166 
167 /*
168 Negate components of vector v.
169 */
170 void ivec_minus(Ivec *v)
171 {
172     int i;
173     if(v == NULL)
174         return;
175     for(i = 0; i < v->n; i++)
176         v->el[i] = -v->el[i];
177 }
178 
179 /*
180 Increment components of v by scalar k times those of vector w.
181 */
182 void ivec_accum(Ivec *v, int k, Ivec *w)
183 {
184     int i;
185     if(v == NULL || w == NULL)
186         return;
187     for(i = 0; i < v->n; i++)
188         v->el[i] += k*w->el[i];
189 }
190 
191 /*
192 Return squared modulus (length) of vector v.
193 */
194 int ivec_sqrmod(Ivec *v)
195 {
196     int i;
197     int sum = 0;
198     if(v == NULL)
199         return(0);
200     for(i = 0; i < v->n; i++)
201         sum += v->el[i]*v->el[i];
202     return(sum);
203 }
204 
205 /*
206 Return modulus (length) of vector v.
207 */
208 int ivec_mod(Ivec *v)
209 {
210     int i;
211     int sum = 0;
212     if(v == NULL)
213         return(0);
214     for(i = 0; i < v->n; i++)
215         sum += abs(v->el[i]);
216     return((int)sqrt(sum));
217 }
218 
219 /*
220 Return L1 modulus (max element) of vector v.
221 */
222 int ivec_mod1(Ivec *v)
223 {
224     int i;
225     int xmax = 0;
226     if(v == NULL)
227         return(0);
228     for(i = 0; i < v->n; i++)
229         xmax = MAX(xmax, abs(v->el[i]));
230     return(xmax);
231 }
232 
233 /*
234 Return squared distance between points (vectors) v and w.
235 */
236 int ivec_sqrdist(Ivec *v, Ivec *w)
237 {
238     int i;
239     int sum = 0;
240     if(v == NULL || w == NULL)
241         return(0);
242     for(i = 0; i < v->n; i++)
243     {
244         int dx = w->el[i]-v->el[i];
245         sum += dx*dx;
246     }
247     return(sum);
248 }
249 
250 /*
251 Return distance between points (vectors) v and w.
252 */
253 int ivec_dist(Ivec *v, Ivec *w)
254 {
255     int i;
256     int sum = 0;
257     if(v == NULL || w == NULL)
258         return(0);
259     for(i = 0; i < v->n; i++)
260     {
261         int dx = w->el[i]-v->el[i];
262         sum += abs(dx);
263     }
264     return((int)sqrt(sum));
265 }
266 
267 void ivec_print(FILE *fp, char *fmt, Ivec *v)
268 {
269     int i;
270     if(v == NULL)
271     {
272         fprintf(fp, "0 :\n");
273         return;
274     }
275     fprintf(fp, "%d : ", v->n);
276     for(i = 0; i < v->n; i++)
277         fprintf(fp, fmt, v->el[i]);
278     fprintf(fp, "\n");
279 }
280 
281 Ivec *ivec_read(FILE *fp)
282 {
283     int i, n;
284     Ivec *v;
285     fscanf(fp, "%d %*s", &n);
286     v = ivec_make(n);
287     for(i = 0; i < v->n; i++)
288         fscanf(fp, "%d", &v->el[i]);
289     return(v);
290 }
291 
292 
293 /*
294 Reverse components of vector w into vector v.
295 */
296 void ivec_reverse(Ivec *v, Ivec *w)
297 {
298     int i, n1;
299     if(v == NULL)
300         return;
301     n1 = v->n-1;
302     for(i = 0; i < v->n; i++)
303         v->el[i] = w->el[n1-i];
304 }
305 
306 void ivec_block_get(Ivec *u, int li, Ivec *v)
307 {
308     int i;
309     if(u == NULL || v == NULL)
310         return;
311     for(i = 0; i < v->n; i++)
312         v->el[i] = u->el[i+li];
313 }
314 
315 void ivec_block_set(Ivec *u, int li, Ivec *v)
316 {
317     int i;
318     if(u == NULL || v == NULL)
319         return;
320     for(i = 0; i < v->n; i++)
321         u->el[i+li] = v->el[i];
322 }
323 
324 void ivec_index_get(Ivec *u, Ivec *index, Ivec *v)
325 {
326     int i;
327     if(u == NULL || v == NULL)
328         return;
329     for(i = 0; i < v->n; i++)
330         v->el[i] = u->el[index->el[i]];
331 }
332 
333 void ivec_index_set(Ivec *u, Ivec *index, Ivec *v)
334 {
335     int i;
336     if(u == NULL || v == NULL)
337         return;
338     for(i = 0; i < v->n; i++)
339         u->el[index->el[i]] = v->el[i];
340 }
341 

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