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

Linux Cross Reference
Tina4/src/math/vector/vector.c

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

  1 /**@(#)Vector (1 dimensional array) allocation
  2  */
  3 
  4 #include <tina/sys.h>
  5 #include <tina/sysfuncs.h>
  6 #include <tina/math.h>
  7 #include <tina/mathfuncs.h>
  8 
  9 Vector *vector_alloc(int n, Vartype vtype)
 10 {
 11     Vector *v;
 12     unsigned int vsize;
 13 
 14     if (n <= 0)
 15         return (NULL);
 16     if ((vsize = var_size(vtype)) == -1)
 17         return (NULL);
 18 
 19     v = ts_ralloc(Vector);
 20     v->n = n;
 21     v->vtype = vtype;
 22     if (vtype == complex_v)
 23     {
 24         v->data = zvector_alloc(0, n);
 25     } else
 26     {
 27         v->data = nvector_alloc(0, n, vsize);
 28     }
 29     return (v);
 30 }
 31 
 32 void    vector_free(Vector * v)
 33 {
 34     if (v == NULL)
 35         return;
 36     rfree((void *) v->data);
 37     rfree((void *) v);
 38 }
 39 
 40 /**
 41 wrap a vector structure round already allocated memory
 42 **/
 43 
 44 Vector *vector_build(int n, Vartype vtype, void *el)
 45 {
 46     Vector *v;
 47 
 48     if (n <= 0)
 49         return (NULL);
 50     v = ts_ralloc(Vector);
 51     v->n = n;
 52     v->vtype = vtype;
 53     v->data = el;
 54     return (v);
 55 }
 56 
 57 void    vector_copy_inplace(Vector * w, Vector * v)
 58 {
 59     int     i, n;
 60 
 61     if (w == NULL || v == NULL)
 62         return;
 63 
 64     if (v->n <= w->n && v->vtype == w->vtype)
 65     {                           /** can use memcpy **/
 66         nvector_copy_inplace((char *) w->data, (char *) v->data, 0, v->n,
 67                              var_size(w->vtype));
 68         if (w->vtype == complex_v)
 69         {
 70             ts_nvector_zero_inplace((char *) w->data, v->n, w->n, Complex);
 71         } else
 72         {
 73             nvector_zero_inplace((char *) w->data, v->n, w->n, var_size(w->vtype));
 74         }
 75         return;
 76     }
 77     n = MIN(v->n, w->n);
 78     switch (v->vtype)
 79     {
 80     case char_v:
 81     case uchar_v:
 82     case short_v:
 83     case ushort_v:
 84     case int_v:
 85     case uint_v:
 86     case ptr_v:
 87         for (i = 0; i < n; i++)
 88         {
 89             double     vi;
 90 
 91             VECTOR_GET(v, i, vi);
 92             VECTOR_SET(w, i, vi);
 93         }
 94         break;
 95     case float_v:
 96     case double_v:
 97         for (i = 0; i < n; i++)
 98         {
 99             double  vi;
100 
101             VECTOR_GET(v, i, vi);
102             VECTOR_SET(w, i, vi);
103         }
104         break;
105     case complex_v:
106         for (i = 0; i < n; i++)
107         {
108             Complex vi = {Complex_id};
109 
110             VECTOR_GETZ(v, i, vi);
111             VECTOR_SETZ(w, i, vi);
112         }
113         break;
114     }
115     if (w->vtype == complex_v)
116     {
117         ts_nvector_zero_inplace((char *) w->data, n, w->n, Complex);
118     } else
119     {
120         nvector_zero_inplace((char *) w->data, n, w->n, var_size(w->vtype));
121     }
122 }
123 
124 Vector *vector_copy(Vector * v)
125 {
126     Vector *copy;
127 
128     copy = vector_alloc((int) v->n, (Vartype) v->vtype);        /* ICCBUG argument and
129                                                                  * old-style */
130     /* parameter mismatch: 'vector_alloc'  */
131     vector_copy_inplace(copy, v);
132     return (copy);
133 }
134 
135 Vector *vector_cast(Vector * v, Vartype vtype)
136 {
137     Vector *copy;
138 
139     copy = vector_alloc(v->n, vtype);
140     vector_copy_inplace(copy, v);
141     return (copy);
142 }
143 
144 
145 Vector *vector_expand(Vector *v, int n)
146 {
147   Vector *copy;
148 
149   copy = vector_alloc(n, v->vtype);
150   vector_copy_inplace(copy, v);
151   vector_free(v);
152 
153   return(copy);
154 }
155 
156 
157 Vector *vector_extend(Vector * v, int n, Vartype vtype)
158 {
159     Vector *copy;
160 
161     copy = vector_alloc(n, vtype);      /* ICCBUG argument and
162                                          * old-style parameter */
163     /* mismatch: 'vector_alloc' */
164     vector_copy_inplace(copy, v);
165 
166     return (copy);
167 }
168 
169 Vartype vector_sup_vtype(Vartype vtype1, Vartype vtype2)
170 {
171     if (vtype1 == complex_v || vtype2 == complex_v)
172         return (complex_v);
173     if (vtype1 == double_v || vtype2 == double_v)
174         return (double_v);
175     if (vtype1 == float_v || vtype2 == float_v)
176         return (float_v);
177     return (int_v);
178 }
179 
180 #ifndef ANSIFYING
181 #ifdef __STRICT_ANSI__
182 
183 Vector *vector(int n, Vartype vtype,...)
184 {
185     va_list ap;
186     int     i;
187     Vector *v;
188 
189     if (n < 1)
190         return (NULL);
191     v = vector_alloc(n, vtype); /* ICCBUG argument and old-style
192                                  * parameter */
193     /* mismatch: 'vector_alloc' */
194     va_start(ap, vtype);
195 
196     switch (vtype)
197     {
198     case ptr_v:
199         for (i = 0; i < n; i++)
200         {
201             void   *vi = (void *) va_arg(ap, void *);
202 
203             VECTOR_SET(v, i, (int) vi);
204         }
205         break;
206     case uchar_v:
207     case char_v:
208     case short_v:
209     case int_v:
210         for (i = 0; i < n; i++)
211         {
212             int     vi = (int) va_arg(ap, int);
213 
214             VECTOR_SET(v, i, vi);
215         }
216         break;
217     case float_v:
218     case double_v:
219         for (i = 0; i < n; i++)
220         {
221             double  vi = (double) va_arg(ap, double);
222 
223             VECTOR_SET(v, i, vi);
224         }
225         break;
226     }
227     va_end(ap);
228 
229     return (v);
230 }
231 
232 #else
233 
234 Vector *vector(n, vtype, va_alist)
235 int     n;
236 Vartype vtype;
237 
238 va_dcl
239 {
240     va_list ap;
241     int     i;
242     Vector *v;
243 
244     if (n < 1)
245         return (NULL);
246     v = vector_alloc(n, vtype); /* ICCBUG argument and old-style
247                                  * parameter */
248     /* mismatch: 'vector_alloc' */
249     va_start(ap);
250     switch (vtype)
251     {
252     case ptr_v:
253         for (i = 0; i < n; i++)
254         {
255             void   *vi = (void *) va_arg(ap, void *);
256 
257             VECTOR_SET(v, i, (int) vi);
258         }
259         break;
260     case uchar_v:
261     case char_v:
262     case short_v:
263     case int_v:
264         for (i = 0; i < n; i++)
265         {
266             int     vi = (int) va_arg(ap, int);
267 
268             VECTOR_SET(v, i, vi);
269         }
270         break;
271     case float_v:
272     case double_v:
273         for (i = 0; i < n; i++)
274         {
275             double  vi = (double) va_arg(ap, double);
276 
277             VECTOR_SET(v, i, vi);
278         }
279         break;
280     }
281     va_end(ap);
282 
283     return (v);
284 }
285 
286 #endif                          /* __STRICT_ANSI__ */
287 #endif                          /* ANSIFYING */
288 

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