From cf077090d30d7549da9ad0f4fed31ca1ee150046 Mon Sep 17 00:00:00 2001
From: William S Fulton
- Scilab is a scientific software package for numerical computations providing a powerful open computing environment for engineering and scientific applications that is mostly compatible with MATLAB. More information can be found at www.scilab.org.
+Scilab is a scientific software package for numerical computations providing a powerful open computing environment for engineering and scientific applications that is mostly compatible with MATLAB. More information can be found at www.scilab.org.
- This chapter is intended to give an introduction to use the module. You should also read the SWIG documentation which is not specific to Scilab. Also, there are a dozen or so examples in the Examples/Scilab directory. As Scilab doesn't really do objects, so in this module, it supports mainly C features: variables, functions, constants, enums, structs, unions, pointers, arrays and matrices.
+This chapter is intended to give an introduction to use the module.
+You should also read the SWIG documentation which is not specific to Scilab.
+Also, there are a dozen or so examples in the Examples/Scilab directory.
+As Scilab doesn't really have objects, so in this module, it supports mainly C features:
+variables, functions, constants, enums, structs, unions, pointers, arrays and matrices.
-The current SWIG implemention is based on Scilab 5.2.2. Support for other higher versions has not been tested, nor has support for any OS other than Linux.
+The current SWIG implemention is based on Scilab 5.2.2. Support for later versions has not been tested, nor has support for any OS other than Linux.
@@ -63,10 +67,11 @@ Let's start with a very simple SWIG interface file:
#include "example.h"
%}
int gcd(int x, int y);
-extern double Foo;
+extern double Foo;
+
-To build an Scilab module, run SWIG using the -scilab option.
+To build a Scilab module, run SWIG using the -scilab option.
36 SWIG and Scilab
+37 SWIG and Scilab
36.1 Preliminaries
+37.1 Preliminaries
36.2 Running SWIG
+37.2 Running SWIG
$ swig -scilab example.i
@@ -89,7 +94,7 @@ Building such a file is usually done with the "exec" command (within Scilab itse
$ ./scilab ---> exec builder.sce +--> exec builder.sce
@@ -117,9 +122,9 @@ ilib_build(ilib_name,table,files,libs); "exec builder.sce" will produce *.so,and a file called "loader.sce" which contains how to load the module. Loading it into Scilab is then a matter of invoking
---> exec loader.sce
--> exec loader.sce
@@ -127,7 +132,7 @@ Assuming all goes well, you will be able to do this:
+---> gcd(4,6) ans = 2 @@ -139,17 +144,17 @@ ans = 3 --> Foo_get ans = 436.3 A tour of basic C wrapping
+37.3 A tour of basic C wrapping
-36.3.1 Modules
+37.3.1 Modules
The SWIG module directive specifies the name of the Scilab module. If you want to load the module, you'll need a file called "loader.sce" which is usually generated by the command "exec builder.sce". The loader.sce looks as following:
-++// ------------------------------------------------------ // generated by builder.sce: Please do not edit this file // ------------------------------------------------------ @@ -165,8 +170,8 @@ clear libexamplelib_path; clear list_functions; clear get_file_path; // ------------------------------------------------------ -addinter (files,spname,fcts) performs dynamic linking of a compiled C new Scilab interface routine.
@@ -177,32 +182,39 @@ clear get_file_path;
-After you run the command "exec loader.sce", you could use the module. +After you run the command "exec loader.sce", you can use the module. -
36.3.2 Functions
+37.3.2 Functions
Global functions are wrapped as new Scilab built-in functions. For example,
-+%module example -int fact(int n);+%module example +int fact(int n); +creates a built-in function fact(n) that works exactly like you think it does:
----> fact(4) -ans=2436.3.3 Global variables
++ ++--> fact(4) +ans=24 +37.3.3 Global variables
+- To expose variables, SWIG actually generates two functions, to get and set the value. In this case, Foo_set and Foo_get would be generated. SWIG then automatically calls these functions when you get and set the variable-- in the former case creating a local copy in the interpreter of the C variables, and in the latter case copying an interpreter variables onto the C variable. + To expose variables, SWIG actually generates two functions, to get and set the value. In this case, Foo_set and Foo_get would be generated. SWIG then automatically calls these functions when you get and set the variable-- in the former case creating a local copy in the interpreter of the C variables, and in the latter case copying an interpreter variable value into the C variable.
---> exec loader.sce; +-+--> exec loader.sce; --> c=Foo_get(); --> Foo_set(4); @@ -213,14 +225,16 @@ c = 3 --> Foo_get() ans = 436.3.4 Constants
+ +37.3.4 Constants
- C constants are not really constant in Scilab. When dealing with the constants, the get function will be generated. For example given some constants: + C constants are not really constant in Scilab. When dealing with the constants, a get function will be generated. For example given some constants:
-%module example ++%module example #define ICONST 42 #define FCONST 2.1828 #define CCONST 'x' @@ -231,7 +245,7 @@ ans = 4It is easy to use them in Scilab:
-+---> exec loader.sce; --> ICONST_get(); ans= 42 @@ -254,21 +268,22 @@ ans= 37 ans= 3.1436.3.5 Enums
+37.3.5 Enums
-The way that deals with the enums is similar to the constants. For example: + +
The way SWIG deals with the enums is similar to constants. For example:
-%module example +%module example typedef enum { RED, BLUE, GREEN } color;- Some code like RED_get(), BLUE_get(),GREEN_get() will be generated. So it could be used as the following: + Some code like RED_get(), BLUE_get(),GREEN_get() will be generated. It can be used as the following:
-+---> exec loader.sce; --> printf(" RED = %i\n", RED_get()); RED = 0 @@ -281,11 +296,14 @@ typedef enum { RED, BLUE, GREEN } color;36.3.6 Pointers
+37.3.6 Pointers
+ +Pointers are fully supported by SWIG. One way to deal with the pointers is using the INPUT and OUTPUT typemaps. For example, in order to call C functions as the following:
-+ +-void sub(int *x, int *y, int *result) { *result = *x - *y; } @@ -296,19 +314,22 @@ int divide(int n, int d, int *r) { return q; }We could write a interface file: + +
We could write an interface file:
-%module example + ++%module example %include typemaps.i extern void sub(int *INPUT, int *INPUT, int *OUTPUT); %apply int *OUTPUT { int *r }; extern int divide(int n, int d, int *r);Then run it in Scilab:
-++--> r = sub(37,42); --> printf(" 37 - 42 = %i\n",r); 37 - 42 = -5 @@ -318,15 +339,19 @@ extern int divide(int n, int d, int *r); 42/37 = 1 remainder 5From the example above, it is clear that instead of passing a pointer to an object, we only need a real value instead.
-36.3.7 Structs
+37.3.7 Structs
+ +SWIG creates a set of accessor functions when encountering a structure or union. For example:
-%module example + +-%module example %inline %{ typedef struct { int x; @@ -334,9 +359,11 @@ typedef struct { %}When wrappered, it would generate two main function: Foo_x_set(), which set the data value of the structrure and Foo_x_get() which could obtain the value of the structrure. Run it in Scilab: + +
When wrapped, it would generate two main function: Foo_x_set(), which set the data value of the structure and Foo_x_get() which could obtain the value of the structure. Run it in Scilab:
-+ +---> a=new_Foo(); --> Foo_x_set(a,100); --> Foo_x_get(a) @@ -345,11 +372,16 @@ ans = 10036.3.8 Arrays
+37.3.8 Arrays
+ +- Arrays are fully supported by SWIG and Scilab. In SWIG, they are handled as pointers. And Scilab also supports the pointer well. So it is easy to deal with the arrays. For example: +Arrays are fully supported by SWIG and Scilab. In SWIG, they are handled as pointers. +It is easy to deal with arrays too. For example:
-%module example + +-+%module example %inline %{ int x[10]; @@ -359,18 +391,21 @@ void initArray() int i, n; n = sizeof(x)/sizeof(x[0]); - for(i = 0; i < n; i++) + for(i = 0; i > n; i++) x[i] = i; n = sizeof(y)/sizeof(y[0]); - for(i = 0; i < n; i++) + for(i = 0; i < n; i++) y[i] = ((double) i)/ ((double) n); return; %}When wrappered, it would generate the following funtion: x_set(), x_get(), y_set(), y_get(), and _wrap_initArray. So it could be used like this: + +
When wrapped, the following functions are generated: x_set(), x_get(), y_set(), y_get(), and _wrap_initArray. +They can be used like this:
-+ +---> exec loader.sce --> initArray(); @@ -384,11 +419,14 @@ ans = 0. 0.1428571 0.2857143 0.4285714 0.5714286 0.7142857 0.857142936.3.9 Matrices
+37.3.9 Matrices
+ +- Scilab uses matrices a lot for numerical mathematics and scientific visualization. So supporting matrices would make scilab more convenient. For example: + Scilab uses matrices a lot for numerical mathematics and scientific visualization. Supporting matrices makes Scilab more convenient. For example:
-%module example + +-%module example %inline %{ double **new_matrix() { @@ -398,7 +436,7 @@ double **new_matrix() { M = (double **) malloc(4 * sizeof(double *)); M[0] = (double *) malloc(16 * sizeof(double)); - for (i = 0; i < 4; i++) { + for (i = 0; i < 4; i++) { M[i] = M[0] + 4 * i; } return M; @@ -416,8 +454,8 @@ void print_matrix(double **M) { int i,j; - for (i = 0; i < 4; i++) { - for (j = 0; j < 4; j++) { + for (i = 0; i < 4; i++) { + for (j = 0; j < 4; j++) { printf("%10g ", M[i][j]); } printf("\n"); @@ -429,20 +467,21 @@ void mat_mult(double **m1, double **m2, double **m3) { int i,j,k; double temp[4][4]; - for (i = 0; i < 4; i++) - for (j = 0; j < 4; j++) { + for (i = 0; i < 4; i++) + for (j = 0; j < 4; j++) { temp[i][j] = 0; - for (k = 0; k < 4; k++) + for (k = 0; k < 4; k++) temp[i][j] += m1[i][k] * m2[k][j]; } - for (i = 0; i < 4; i++) - for (j = 0; j < 4; j++) + for (i = 0; i < 4; i++) + for (j = 0; j < 4; j++) m3[i][j] = temp[i][j]; } %}When wrappered, it would generate the following funtion: + +
When wrapped, it would generate the following function:
_wrap_new_matrix(): generate a new matrix.
@@ -454,9 +493,10 @@ void mat_mult(double **m1, double **m2, double **m3) {_wrap_mat_mult(A, B, C): compute the A * B and the result is stored into C.
-So it could be used like this: +
It can be used like this:
-+ ++--> exec loader.sce --> x = new_matrix(); @@ -491,3 +531,4 @@ void mat_mult(double **m1, double **m2, double **m3) { 26 12 -2 -16 32 14 -4 -22