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

Linux Cross Reference
Tina5/tina-libs/tina/math/mathVec_apply.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/mathVec_apply.c,v $
 37  * Date    :  $Date: 2003/09/23 11:32:20 $
 38  * Version :  $Revision: 1.4 $
 39  * CVS Id  :  $Id: mathVec_apply.c,v 1.4 2003/09/23 11:32:20 matts Exp $
 40  *
 41  * Author  :  Legacy TINA
 42  *
 43  * Notes : Vector handling (eg vector_apply: apply a function to every component)
 44  *
 45  *********
 46 */
 47 /** 
 48  *  @file
 49  *  @brief  Vector handling functions.  
 50  *
 51  *  Contains a mixture of functions (inplace and non-inplace for each type);
 52  *
 53  *  - meta functions for applying a function to every element in the Vector, inplace
 54  *  and non-inplace.
 55  *  - functions for multiplying a Vector by a scalar.
 56  *  - functions for the negation of all elements in the Vector.
 57  *  - functions for the fast fourier transform and inverse fft of the Vector.
 58 */
 59 
 60 #include "mathVec_apply.h"
 61 
 62 #if HAVE_CONFIG_H
 63 #include <config.h>
 64 #endif
 65 
 66 
 67 #include <tina/sys/sysDef.h>
 68 #include <tina/sys/sysPro.h>
 69 #include <tina/math/math_VecDef.h>
 70 #include <tina/math/mathVec_vector.h>
 71 #include <tina/math/math_TypPro.h>
 72 #include <tina/math/mathNum_fourier.h>
 73 
 74 
 75 /**
 76 apply functions call a function
 77     f(&input, &output, &data)
 78 on every element of a vector,
 79 input and output can be different types.
 80 
 81 A valid inplace call is
 82     vector_apply_inplace(v, v, f, data)
 83 **/
 84 
 85 void            vector_apply_inplace(Vector * w, Vector * v, void (*f) ( /* ??? */ ), void *data)
 86 {
 87         int             i, n;
 88 
 89         if (w == NULL)
 90                 return;
 91 
 92         if (v == NULL)
 93                 n = 0;
 94         else
 95                 n = MIN(v->n, w->n);
 96 
 97         for (i = 0; i < n; i++)
 98         {
 99                 void           *vi, *wi;
100 
101                 VECTOR_GETP(v, i, vi);
102                 VECTOR_GETP(w, i, wi);
103                 f(vi, wi, data);
104         }
105         for (i = n; i < w->n; i++)
106         {
107                 void           *wi;
108 
109                 VECTOR_GETP(w, i, wi);
110                 f(NULL, wi, data);
111         }
112 }
113 
114 Vector         *vector_apply(Vector * v, Vartype vtype, void (*f) ( /* ??? */ ), void *data)
115 {
116         Vector         *w;
117 
118         if (v == NULL)
119                 return (NULL);
120         w = vector_alloc(v->n, vtype);
121         vector_apply_inplace(w, v, f, data);
122         return (w);
123 }
124 
125 Vector         *vector_itimes(int k, Vector * v)
126 {
127         Vector         *w;
128         Vartype         vtype;
129         int             i, n;
130 
131         if (v == NULL)
132                 return (NULL);
133 
134         n = v->n;
135         vtype = vector_sup_vtype(int_v, v->vtype);
136         w = vector_alloc(v->n, vtype);
137         switch (vtype)
138         {
139         case char_v:
140         case uchar_v:
141         case int_v:
142         case uint_v:
143         case short_v:
144         case ushort_v:
145                 for (i = 0; i < n; i++)
146                 {
147                         double          vi, wi;
148 
149                         VECTOR_GET(v, i, vi);
150                         wi = k * vi;
151                         VECTOR_SET(w, i, wi);
152                 }
153                 break;
154         case float_v:
155         case double_v:
156                 for (i = 0; i < n; i++)
157                 {
158                         double          vi, wi;
159 
160                         VECTOR_GET(v, i, vi);
161                         wi = k * vi;
162                         VECTOR_SET(w, i, wi);
163                 }
164                 break;
165         case complex_v:
166                 for (i = 0; i < n; i++)
167                 {
168                         Complex         vi = {Complex_id};
169                         Complex         wi = {Complex_id};
170 
171                         VECTOR_GETZ(v, i, vi);
172                         wi = cmplx_times((double) k, vi);
173                         VECTOR_SETZ(w, i, wi);
174                 }
175                 break;
176         default:
177                 error("vector_itimes: unsupported type", non_fatal);
178                 break;
179         }
180         return (w);
181 }
182 
183 Vector         *vector_times(double k, Vector * v)
184 {
185         Vartype         vtype;
186         Vector         *w;
187         int             i, n;
188 
189         if (v == NULL)
190                 return (NULL);
191 
192         vtype = vector_sup_vtype(float_v, v->vtype);
193         n = v->n;
194         w = vector_alloc(v->n, vtype);
195         switch (vtype)
196         {
197         case float_v:
198         case double_v:
199                 for (i = 0; i < n; i++)
200                 {
201                         double          vi, wi;
202 
203                         VECTOR_GET(v, i, vi);
204                         wi = k * vi;
205                         VECTOR_SET(w, i, wi);
206                 }
207                 break;
208         case complex_v:
209                 for (i = 0; i < n; i++)
210                 {
211                         Complex         vi = {Complex_id};
212                         Complex         wi = {Complex_id};
213 
214                         VECTOR_GETZ(v, i, vi);
215                         wi = cmplx_times(k, vi);
216                         VECTOR_SETZ(w, i, wi);
217                 }
218                 break;
219         default:
220                 error("vector_times: unsupported type", non_fatal);
221                 break;
222         }
223         return (w);
224 }
225 
226 Vector         *vector_ztimes(Complex k, Vector * v)
227 {
228         Vector         *w;
229         int             i, n;
230 
231         if (v == NULL)
232                 return (NULL);
233 
234         n = v->n;
235         w = vector_alloc(v->n, complex_v);
236         for (i = 0; i < n; i++)
237         {
238                 Complex         vi = {Complex_id};
239                 Complex         wi = {Complex_id};
240 
241                 VECTOR_GETZ(v, i, vi);
242                 wi = cmplx_prod(k, vi);
243                 VECTOR_SETZ(w, i, wi);
244         }
245         return (w);
246 }
247 
248 void            vector_minus_inplace(Vector * v)
249 {
250         int             i, n;
251 
252         if (v == NULL)
253                 return;
254 
255         n = v->n;
256         switch (v->vtype)
257         {
258         case char_v:
259         case uchar_v:
260         case int_v:
261         case uint_v:
262         case short_v:
263         case ushort_v:
264                 for (i = 0; i < n; i++)
265                 {
266                         double          vi;
267 
268                         VECTOR_GET(v, i, vi);
269                         VECTOR_SET(v, i, -vi);
270                 }
271                 break;
272         case float_v:
273         case double_v:
274                 for (i = 0; i < n; i++)
275                 {
276                         double          vi;
277 
278                         VECTOR_GET(v, i, vi);
279                         VECTOR_SET(v, i, -vi);
280                 }
281                 break;
282         case complex_v:
283                 for (i = 0; i < n; i++)
284                 {
285                         Complex         vi = {Complex_id};
286 
287                         VECTOR_GETZ(v, i, vi);
288                         VECTOR_SETZ(v, i, cmplx_minus(vi));
289                 }
290                 break;
291         default:
292                 error("vector_minus_inplace: unsupported type", non_fatal);
293                 break;
294         }
295 }
296 
297 Vector         *vector_minus(Vector * v)
298 {
299         Vector         *w;
300         int             i, n;
301 
302         if (v == NULL)
303                 return (NULL);
304 
305         n = v->n;
306         w = vector_alloc(v->n, v->vtype);
307         switch (v->vtype)
308         {
309         case char_v:
310         case uchar_v:
311         case int_v:
312         case uint_v:
313         case short_v:
314         case ushort_v:
315                 for (i = 0; i < n; i++)
316                 {
317                         double          vi, wi;
318 
319                         VECTOR_GET(v, i, vi);
320                         wi = -vi;
321                         VECTOR_SET(w, i, wi);
322                 }
323                 break;
324         case float_v:
325         case double_v:
326                 for (i = 0; i < n; i++)
327                 {
328                         double          vi, wi;
329 
330                         VECTOR_GET(v, i, vi);
331                         wi = -vi;
332                         VECTOR_SET(w, i, wi);
333                 }
334                 break;
335         case complex_v:
336                 for (i = 0; i < n; i++)
337                 {
338                         Complex         vi = {Complex_id};
339                         Complex         wi = {Complex_id};
340 
341                         VECTOR_GETZ(v, i, vi);
342                         wi = cmplx_minus(vi);
343                         VECTOR_SETZ(w, i, wi);
344                 }
345                 break;
346         default:
347                 error("vector_minus: unsupported type", non_fatal);
348                 break;
349         }
350         return (w);
351 }
352 
353 void            vector_fft_inplace(Vector * v)
354 {
355         if (v == NULL || v->vtype != complex_v)
356                 return;
357         fft_cmplx_inplace((Complex *) v->data, v->n);
358 }
359 
360 Vector         *vector_fft(Vector * v)
361 {
362         Vector         *w;
363         int             n = 1;
364 
365         if (v == NULL)
366                 return (NULL);
367         while (n < v->n)
368                 n *= 2;
369         w = vector_extend(v, n, complex_v);
370         vector_fft_inplace(w);
371         return (w);
372 }
373 
374 void            vector_fft_inverse_inplace(Vector * v)
375 {
376         if (v == NULL || v->vtype != complex_v)
377                 return;
378         fft_inverse_cmplx_inplace((Complex *) v->data, v->n);
379 }
380 
381 Vector         *vector_fft_inverse(Vector * v)
382 {
383         Vector         *w;
384         int             n = 1;
385 
386         if (v == NULL)
387                 return (NULL);
388         while (n < v->n)
389                 n *= 2;
390         w = vector_extend(v, n, complex_v);
391         vector_fft_inverse_inplace(w);
392         return (w);
393 }
394 

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