scipvmf77.c

Go to the documentation of this file.
00001 /*------------------------------------------------------------------------
00002  *    PVM functions 
00003  *    Copyright (c) 1997-2002 by Inria Lorraine.  All Rights Reserved 
00004  *------------------------------------------------------------------------
00005  *   NAME 
00006  *     scipvmf77tosci and  scipvmscitof77 
00007  *     static functions: 
00008  *   PURPOSE 
00009  *     convert F77complex to scilab ones in a Scilab variable.
00010  *   NOTES 
00011  *   HISTORY 
00012  *     fleury - Jun 29, 1999: Created. 
00013  *     Revision 1.3  2005/01/19 14:40:36  cornet
00014  *     remove some functions not used or only for debug (not recquired by scilab)
00015  *     Cleaning Sources ...
00016  *
00017  *     Revision 1.2  2004/09/16 13:11:40  steer
00018  *     name changes in C version of fortran stack commons
00019  *
00020  *     Revision 1.1  2002/07/25 08:08:07  chanceli
00021  *     C translation and simplifications
00022  * 
00023  * 
00024  *     Highly modified : Chancelier 2002/07/19
00025  * 
00026  *     Revision 1.1.1.1  2001/04/26 07:49:01  scilab 
00027  *     Imported sources 
00028  *
00029  *     Revision 1.1  1999/07/07 18:11:13  fleury 
00030  *     Ajout des routines de conversion de complexes 
00031  *------------------------------------------------------------------------*/
00032 
00033 #include <stdio.h>
00034 #include "stack-c.h"
00035 #include "machine.h"
00036 #include "sci_pvm.h"
00037 
00038 
00039 static void swap (double*, double*, int);
00040 static void sci_to_f77 (double*, int*);
00041 static void f77_to_sci (double*, int*);
00042 
00043 typedef void (*Fm)(double *,int *); 
00044 typedef void (*Fl)(int *); 
00045 static void sci_object_walk(int il,Fm fm,int stk_pos);
00046 
00047 
00048 /*------------------------------------------------------------------------
00049  *  Given a scilab variable, stored in the stack at the position k (in 
00050  *  lstk), this function converts complex matrices contained in the 
00051  *  object from f77 representation to scilab representation. 
00052  *------------------------------------------------------------------------*/
00053 
00054 void C2F(scipvmf77tosci)(int *k)
00055 {
00056   /* call sci_object_walk 
00057    * object is given by its lstk position 
00058    */
00059   sci_object_walk(*k,f77_to_sci,1);
00060 } 
00061 
00062 
00063 /*------------------------------------------------------------------------
00064  *  Given a scilab variable, stored in the stack at the position k (in 
00065  *  lstk), this function converts complex matrices contained in the 
00066  *  object form Scilab representation to f77 representation. 
00067  *------------------------------------------------------------------------*/
00068 
00069 void C2F(scipvmscitof77)(int *k)
00070 {
00071   /* call sci_object_walk 
00072    * object is given by its lstk position 
00073    */
00074   sci_object_walk(*k,sci_to_f77,1);
00075 } 
00076 
00077 /*--------------------------------------------------------
00078  * sci2f77
00079  *    Converts Scilab complex representation 
00080  *     [r_0, r_1,..., r_n, i_0, i_1,..., i_n]
00081  *    into f77 representation 
00082        DOUBLE COMPLEX one [r_0, i_0, r_1, i_1, ..., r_n, i_n]
00083  *
00084  *     Complexity O(nlogn) for this version. One can easly have
00085  *     O(nloglogn) by pruning the recursion. Next version wil take care 
00086  *     of the cache size.
00087  *     fleury - May 7, 1999: Created.
00088  *--------------------------------------------------------*/
00089 
00090 /* utility */ 
00091 
00092 static void swap(double* ptr1, double* ptr2, int size)
00093 {
00094   double tmp;
00095   int i;
00096   for (i = 0; i < size; ++i) {
00097     tmp = ptr1[i];
00098     ptr1[i] = ptr2[i];
00099     ptr2[i] = tmp;
00100   }
00101 }
00102 
00103 static void sci_to_f77( double *tab, int *size)
00104 {
00105   int nb;
00106   
00107   if (*size == 1) {
00108     return;
00109   }
00110   nb = *size / 2;
00111   if (*size % 2) {              
00112     /* si le nbr est impaire on "coupe" un
00113      *   complexe en deux et donc il faut
00114      * reparer ce crime... 
00115      */
00116     swap(&(tab[nb]), &(tab[*size + nb]), 1);
00117     swap(&tab[*size - nb - 1], &tab[*size], nb + 1); 
00118     sci_to_f77(&tab[0], &nb);
00119     sci_to_f77(&tab[*size + 1], &nb);
00120   }
00121   else {
00122     swap(&tab[*size - nb], &tab[*size], nb); 
00123     sci_to_f77(&tab[0], &nb);
00124     sci_to_f77(&tab[*size], &nb);
00125   }
00126 }
00127 
00128 
00129 /*--------------------------------------------------------
00130  * f772sci 
00131  *    Converts f77 complex representation 
00132  *    into scilab  representation 
00133  *     Complexity O(nlogn) for this version. One can easly have
00134  *     O(nloglogn) by pruning the recursion. Next version wil take care 
00135  *     of the cache size.
00136  *     fleury - May 7, 1999: Created.
00137  *--------------------------------------------------------*/
00138 
00139 static void f77_to_sci(double *tab, int *size)
00140 {
00141   int nb;
00142   
00143   if (*size == 1) {
00144     return;
00145   }
00146   nb = *size / 2;
00147   if (*size % 2) {              
00148     /* si le nbr est impaire on "coupe" un
00149      * complexe en deux et donc il faut
00150      * reparer ce crime... 
00151      */
00152     f77_to_sci(&tab[0], &nb);
00153     f77_to_sci(&tab[*size + 1], &nb);
00154     swap(&(tab[*size - 1]), &(tab[*size]), 1);
00155     swap(&tab[*size - nb - 1], &tab[*size], nb + 1); 
00156   }
00157   else {
00158     f77_to_sci(&tab[0], &nb);
00159     f77_to_sci(&tab[*size], &nb);
00160     swap(&tab[*size - nb], &tab[*size], nb); 
00161   }
00162 } 
00163 
00164 
00165 
00166 /*--------------------------------------------------------
00167  * Utility function 
00168  * Chancelier 2002 
00169  * recursively walk on scilab object 
00170  *   if stk_pos==0 the object is given by 
00171  *        its il position 
00172  *        I.e the object is at position istk(il)+.... 
00173  *   if stk_pos==1 the object is given by its k 
00174  *        position in Lstk(k) 
00175  * 
00176  * During the walk fm is applied on some data structures 
00177  * 
00178  *--------------------------------------------------------*/
00179 
00180 static void sci_object_walk(int ilk,Fm fm,int stk_pos)
00181 {
00182   int ix1, ix2,type, m, n,id, mn, nel,ne,il,ilp,i,li,ill,l;
00183 
00184   if ( stk_pos == 1 ) 
00185     {
00186       /* object given by its stk position */ 
00187       il = iadr(*Lstk(ilk));
00188       if (*istk(il ) < 0) {
00189         il = iadr(*istk(il +1));
00190       }
00191     }
00192   else 
00193     {
00194       il = ilk;
00195     }
00196 
00197   type = *istk(il); 
00198 
00199   switch ( type ) {
00200   case sci_matrix : 
00201     if ( *istk(il + 3) == 1) {
00202       /* this is a complex scalar matrix */ 
00203       mn = *istk(il +1) * *istk(il + 2);
00204       ix1 = il + 4;
00205       fm(stk(sadr(ix1) ), &mn);
00206     } 
00207     break; 
00208   case sci_poly : 
00209     if ( *istk(il + 3) == 1) {
00210       /* this is a complex polynomial  matrix */ 
00211       id = il + 8;
00212       mn = *istk(il +1) * *istk(il+2);
00213       ix1 = il + 9 + mn;
00214       ix2 = *istk(id + mn ) - 1;
00215       fm(stk(sadr(ix1) ), &ix2);
00216     }
00217     break; 
00218   case sci_sparse : 
00219     if ( *istk(il + 3) == 1) {
00220       /* this is a complex sparse matrix */ 
00221       nel = *istk(il + 3 +1);
00222       m = *istk(il +1);
00223       n = *istk(il + 1 +1);
00224       ix1 = il + 5 + m + nel;
00225       fm(stk(sadr(ix1) ), &nel);
00226     }
00227     break; 
00228   case sci_list : 
00229   case sci_tlist : 
00230   case sci_mlist : 
00231     /* nb element of the list */
00232     ne = istk(il)[1];
00233     /* loop on objects */
00234     ilp = il + 2;
00235     l = sadr(ilp + ne + 1);
00236     for (i = 1; i <= ne; ++i) { 
00237       li  = istk(ilp)[i-1];
00238       ill = iadr(l + li -1);
00239       /* recursive call but now with an istk position 
00240        * i.e stk_pos == 0 
00241        */ 
00242       sci_object_walk(ill,fm,0);
00243     }
00244     break ; 
00245   default : 
00246     break;
00247   }
00248 }
00249 
00250 

Generated on Sun Mar 4 15:03:59 2007 for Scilab [trunk] by  doxygen 1.5.1