00001 #include "stack-c.h" 00002 00003 /*-------------------------------------------------------- 00004 * An example of an hand written interface 00005 * passing a Scilab function as input of function ex8c 00006 * 00007 * call in Scilab:-->ex8c(x1,x2,a_function) 00008 * x1<->1 (double array) 00009 * x2<->2 (double array) 00010 * a_function <-> 3 (a scilab function). 00011 * a_function is the function "myfunction" defined 00012 * in ex8c.sce. It has mlhs=2 inputs and mrhs=3 outputs. 00013 * 00014 *--------------------------------------------------------*/ 00015 00016 int intex8c(fname) 00017 char *fname; 00018 { 00019 int ibegin; 00020 static int l1, m1, n1, m2, n2, l2,mlhs,mrhs,lf, l4,l5,i; 00021 static int minlhs=1, minrhs=3, maxlhs=3, maxrhs=3; 00022 00023 CheckRhs(minrhs,maxrhs) ; 00024 CheckLhs(minlhs,maxlhs) ; 00025 00026 /* Getting x1 and x2 at l1 and l2 . x1 is m1 x n1 and x2 is m2 x n2 */ 00027 GetRhsVar(1, "d", &m1, &n1, &l1); 00028 GetRhsVar(2, "d", &m2, &n2, &l2); 00029 /* Getting the Scilab function: 00030 It is the 3rd input parameter of ex8c and it is a function ("f") */ 00031 GetRhsVar(3, "f", &mlhs, &mrhs, &lf); 00032 /* It is at lf and has mlhs outputs and mrhs inputs */ 00033 00034 if ( mrhs != 2 ) 00035 { 00036 Scierror(999,"%s: Third argument has an invalid rhs\r\n",fname); 00037 return 0; 00038 } 00039 00040 /* 00041 * To call a_function it is required that its input arguments are 00042 * stored in the last positions of the variables stack. NOTE that when 00043 * called, the function destroys its input variables and replaces them by 00044 * the output variables. so in this case we need to make a copy of 00045 * them. 00046 * Remark: if the calling sequence of ex8c had been ex8c(a_function,x1,x2) 00047 * the following two copies would be un-necessary. 00048 */ 00049 00050 CreateVar(3+1, "d", &m1, &n1, &l4); 00051 for (i =0 ; i < m1*n1 ; i++) *stk(l4+i) = *stk(l1+i); 00052 00053 CreateVar(3+mrhs, "d", &m2, &n2, &l5); 00054 for (i =0 ; i < m1*n1 ; i++) *stk(l5+i) = *stk(l2+i); 00055 00056 /* 00057 * Here a_function takes variables 4 and 5 as inputs and generates output 00058 * variables at positions 4 to 4-1+mlhs 00059 * ibegin must be the index of the first input variable of a_function 00060 */ 00061 00062 ibegin=3+1; 00063 00064 /* execute the function */ 00065 00066 SciFunction(&ibegin,&lf,&mlhs,&mrhs); 00067 /* check if an error has occured while running a_function */ 00068 if (Err > 0 ) return 0; 00069 00070 /* output variables: 4 and 5 (created by a_function) and possibly 6 00071 * if a_function has 3 output parameters 00072 */ 00073 00074 LhsVar(1) = 4; 00075 LhsVar(2) = 5; 00076 if ( mlhs == 3) LhsVar(3) = 6; 00077 return 0; 00078 } 00079 00080 00081 00082
1.5.1