rectangl e - klaus betzler's homepage - startseite · chapter 1 in tro duction w e will...

44

Upload: duonganh

Post on 07-Jun-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the
Page 2: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

1

0

-1

Z

-3

-0

3

Y

3

-0

-3

X

plot3d1 : z=sin(x)*cos(y)

1.0 1.4 1.8 2.2 2.6 3.0 3.4 3.8 4.2 4.6 5.0

1.0

1.9

2.8

3.7

4.6

5.5

6.4

7.3

8.2

9.1

10.0

0.19

0.19

0.19

0.190.37

0.37

0.37

0.37

0.37

0.37

0.37

0.37

0.37

0.56

0.56

0.56

0.56

0.56

0.56

0.56

0.56

0.56

0.56

0.75

0.75

0.75

0.75

0.75

0.75

0.75

contour

champ

-0.8

-0.8-0.8

-0.6

-0.6-0.6

-0.4

-0.4 -0.4

-0.2

-0.2 -0.20.0

0.0

0.0 0.2

0.2

0.2

0.4

0.4

0.4

0.6

0.6

0.6

0.8

0.8

0.8

Z

YX

plot3d and contour

Guide

For

Developpers

Scilab Inside

Scilab Group

-->plot(1:10)

-->xbasc()

-->// simple rectangle

-->xrect(0,1,3,1)

-->// filling a rectangle

-->xfrect(3.1,1,3,1)

-->// writing in the rectangle

-->xstring(0.5,0.5,"xrect(0,1,3,1)")

-->// writing black on black !

-->xstring(4.,0.5,"xfrect(3.1,1,3,1)")

-->// reversing the video

-->xset("alufunction",6)

-->xstring(4.,0.5,"xfrect(3.1,1,3,1)")

-->xset("alufunction",3)

-->// drawing a polyline

-->X=[0 1 2 3 4];

-->Y=[2.5 1.5 1.8 1.3 2.5];

-->xpoly(X,Y,"lines",1)

-->xstring(0.5,2.,"xpoly(X,Y,""lines""

-->// drawing arrows

Page 3: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

GUIDE

FOR

Lab DEVELOPPERS

Scilab GroupINRIA Meta2 Project/ENPC Cergrene

INRIA - Unit�e de recherche de Rocquencourt - Projet Meta2Domaine de Voluceau - Rocquencourt - B.P. 105 - 78153 Le Chesnay Cedex (France)E-mail : [email protected]

Page 4: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the
Page 5: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

Contents

1 Introduction 1

2 Incremental link and intersci 2

2.1 Introduction : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 22.2 Linking a routine to Scilab : : : : : : : : : : : : : : : : : : : : : : : : : : : 22.3 Adding a new interface in Scilab : : : : : : : : : : : : : : : : : : : : : : : : 32.4 Using intersci : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 3

2.4.1 Description of Scilab function : : : : : : : : : : : : : : : : : : : : : : 42.4.2 Description of Fortran subroutine : : : : : : : : : : : : : : : : : : : : 42.4.3 Description of the output of Scilab function : : : : : : : : : : : : : : 52.4.4 Dimensions of non scalar variables : : : : : : : : : : : : : : : : : : : 52.4.5 Fortran variables with external type : : : : : : : : : : : : : : : : : : 62.4.6 Using lists as input Scilab variables : : : : : : : : : : : : : : : : : : : 72.4.7 Limitations : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 72.4.8 C functions interfacing : : : : : : : : : : : : : : : : : : : : : : : : : : 7

2.5 Writing compatible code to Scilab : : : : : : : : : : : : : : : : : : : : : : : 82.5.1 Input and output : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 82.5.2 Error exit : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 8

2.6 Examples : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 82.6.1 Example 1 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 82.6.2 Example 2 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 82.6.3 Example 3 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 92.6.4 Example 4 : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 10

3 Scilab inside 12

3.1 From the Scilab call to the prompt : : : : : : : : : : : : : : : : : : : : : : : 123.1.1 The main routine matlab : : : : : : : : : : : : : : : : : : : : : : : : 123.1.2 The Scilab parsing function and the interfaces : : : : : : : : : : : : : 13

3.2 The database : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 133.2.1 The fortran structure : : : : : : : : : : : : : : : : : : : : : : : : : : 133.2.2 Coding the di�erent types of variables : : : : : : : : : : : : : : : : : 193.2.3 The code of the Scilab characters : : : : : : : : : : : : : : : : : : : : 26

3.3 Interfaces Scilab-Fortran : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 263.3.1 Interfaces handling : : : : : : : : : : : : : : : : : : : : : : : : : : : : 263.3.2 Interface routine : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 27

3.4 A working example : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 27

i

Page 6: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

Chapter 1

Introduction

We will describe the di�erent procedures for adding routines to Scilab. The simplest wayis the incremental link : it is very quick and useful for a small number of routines and fortesting programs. The disadvantage is that the calling sequence is complicated and cannotbe considered as a primitive. For this case there still remains some restrictions dependingon the machines and the compilation options. The second level is to use the Scilab utilityfunction intersci which generates automatically the fortran routine interfacing the newroutine to be added. This is done by the mean of a description �le. But there are somelimitations : all the Scilab datatypes are not allowed ... The third level is to directlywrite the interfacing fortran routine. For many cases it is useful to use intersci: if quiteall the calling parameters are \fortran datatypes" a �rst step consists to get the routinegenerated by intersci and then to modify it by hand. When the interfacing routineis written, it can be added in two manners : a \hard" way by including this code inScilab or a \soft" way by using the Scilab utility function addinter which de�nes a newfunctions interface with the incremental link. As for the �rst case there still remains somerestrictions depending on the machines and the compilation options.

1

Page 7: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

Chapter 2

Incremental link and intersci

2.1 Introduction

We present here the two \automatic" ways for a user to add his programs to Scilab. The�rst one is the incremental link : it is very easy but it is not adapted to a large numberof routines and there are some limitations depending on the machine and the compilationoptions. The second one called intersci give to the user the tool of a Scilab developper.

intersci is a program which permits to interface automatically Fortran subroutinesor C functions to Scilab.

With intersci a user can group all the calls to his Fortran or C code into a same set,called an interface, and use them in intersci as intersci functions. The interfacing ismade by creating a Fortran subroutine which has to be linked to Scilab together with theuser code. This complex Fortran subroutine is automatically generated by intersci froma description �le of the interface.

2.2 Linking a routine to Scilab

The command link links a compiled C or Fortran routine to Scilab. This commandis available with some options for the compilation of Scilab and is restricted on somemachines (see the corresponding on-line help). With no arguments link() returns thevector of linked routines. If foo.o is the �le of the object code and foo the name of thefortran or C routine, a possible command is :

link('foo.o -lfor -lm -lc','foo')

it means that the entry point foo de�ned by the object �le foo.o and the librarieslibfor, libm, libc is added (linked) to Scilab.

Then you can call the new-linked routine by using the command fort. This commandneeds the name of the routine, the input-output variables together with the types and thepositions of these variables. We recall here the simple example of the on-line help : forthe fortran routine

subroutine foo(a,b,c)

c=2*a+b

end

the unix command make -c foo gives foo.o and then with Scilab :

2

Page 8: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

CHAPTER 2. INCREMENTAL LINK AND INTERSCI 3

-->link("foo.o","foo")

linking foo_ defined in foo.o with Scilab

-->a=13;b=10;

-->c=fort("foo",a,1,"r",b,2,"r","out",[1,1],3,"r")

c =

36.

In the calling sequence of fort : foo is the name of the called routine, a is a callingparameter at the position 1 and its type is real r, b is the second calling parameter. Theinput variables and the output variables are separated by the keyword out and then the(Scilab) types of the di�erent output are de�ned : we have here one output which is a[1,1] matrix in the position 3 in the calling sequence of the routine and its type is real r.

2.3 Adding a new interface in Scilab

2.4 Using intersci

In the following, we will only speak of Fortran subroutine interfacing. The process isnearly the same for C functions (see 2.4.8).

To use intersci execute the Unix command:intercsi <interface name> <interface number>

where <interface name>.desc is the �le describing the interface.Then the �les <interface name>.f and fundef.<interface name> are created.

The �le <interface name>.desc is a sequence of descriptions of pairs formed by theScilab function and the corresponding Fortran subroutine (see table 2.1).

<Scilab function name> <function arguments>

<Scilab variable> <Scilab type> <possible arguments>...

......

......

<Fortran subroutine name> <subroutine arguments>

<Fortran argument> <Fortran type>...

......

...out <type> <formal output names>

<formal output name> <variable>...

......

...******************************

Table 2.1: Description of a pair of Scilab function and Fortran subroutine

Each description is made of three parts: description of Scilab function and its argu-ments, description of Fortran subroutine and its arguments, and �nally description of the

Page 9: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

CHAPTER 2. INCREMENTAL LINK AND INTERSCI 4

output of Scilab function with possible type conversions. Di�erent illustrating examplesare presented in the sequel.

2.4.1 Description of Scilab function

The �rst line of the description is composed by the name of the Scilab function followedby its input arguments in calling order.

The next lines describe Scilab variables: the input arguments and the outputs of theScilab function, together with the arguments of the Fortran subprogram with type work

(for which memory must be allocated). It is an error not to describe such arguments.The description of a Scilab variable begins by its name, then its type followed by

possible informations depending on the type.Types of Scilab variables are:

any any type: only used for an input argument of Scilab function.

column column vector: must be followed by its dimension.

list list: must be followed by the name of the list, <list name>. This name must cor-respond to a �le <list name>.list which describes the structure of the list (see3.10).

matrix matrix: must be followed by its two dimensions.

polynom polynomial: must be followed by its dimension (size) and the name of theunknown.

row row vector: must be followed by its dimension (length).

scalar scalar.

string character string: must be followed by its dimension (length).

vector row or column vector: must be followed by its dimension.

work working array: must be followed by its dimension. It must not correspond to aninput argument or to the output of the Scilab function.

The way dimensions are speci�ed is described in 2.4.4.

A blank line and only one ends this description.

Optional input arguments

2.4.2 Description of Fortran subroutine

The �rst line of the description is composed by the name of the Fortran subroutine followedby its arguments in calling order.

The next lines describe Fortran variables: the arguments of the Fortran subroutine. Itis an error not to describe such arguments.

The description of a Fortran variable is made of its name and its type. Most Fortranvariables correspond to Scilab variables (except for dimensions, see 2.4.4) and must havethe same name as the corresponding Scilab variable. It is the reason why possible Fortranvariable dimensions are not given here because de�ned with the Scilab variable dimension.

Types of Fortran variables are:

Page 10: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

CHAPTER 2. INCREMENTAL LINK AND INTERSCI 5

char character array.

double double precision variable.

int integer variable.

real real variable.

External types also exist, see 2.4.5.

A blank line and only one ends this description.

2.4.3 Description of the output of Scilab function

The �rst line of this description must begin by the word out followed by the type of Scilaboutput.

Types of output are:

empty the Scilab function returns nothing.

list a Scilab list: must be followed by the names of Scilab variables elements of the list.

sequence a Scilab sequence: must be followed by the names of Scilab variables elementsof the sequence.

This �rst line must be followed by other lines corresponding to output type conversion.This is the case when an output variable is also an input variable with di�erent Scilabtype: for instance an input column vector becomes an output row vector. The line whichdescribes this conversion begins by the name of Scilab output variable followed by thename of the corresponding Scilab input variable. See 2.6.3 as an example.

A line beginning with a star \*" ends the description of a pair of Scilab function andFortran subroutine. This line is compulsory even if it is the end of the �le. Do not forgetto end the �le by a carriage return.

2.4.4 Dimensions of non scalar variables

When de�ning non scalar Scilab variables (vectors, matrices, polynomials and characterstrings) dimensions must be given. There are a few ways to do that:

� It is possible to give the dimension as an integer (see 2.6.1).

� The dimension can be the dimension of an input argument of Scilab function. Thisdimension is then denoted by a formal name which is not the name of a Scilabvariable (see 2.6.2). This is an usual case.

� The dimension can be de�ned as an output of the Fortran subroutine. This meansthat the memory for the corresponding variable is allocated by the Fortran subrou-tine. The corresponding Fortran variable must necessary have an external type (see2.4.5 and 2.6.3).

intersci is not able to treat the case where the dimension is an algebraic expressionof other dimensions. A Scilab variable corresponding to this value must de�ned.

The dimension must not be an input of Scilab function.

Page 11: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

CHAPTER 2. INCREMENTAL LINK AND INTERSCI 6

2.4.5 Fortran variables with external type

When describing the Fortran subroutine, a Fortran variable can have a type di�erent thanthe ones described in 2.4.2.

External types are used when the dimension of the Fortran variable is unknown whencalling the Fortran subroutine and when its memory size is allocated in this subroutine.This dimension must be an output of the Fortran subroutine. In fact, this will typicallyhappen when we want to interface a C function in which memory is dynamically allocated.

Existing external types:

cchar character string allocated by a C function to be copied into the correspondingScilab variable.

ccharf the same as cchar but the C character string is freed after the copy.

cdouble C double array allocated by a C function to be copied into the correspondingScilab variable.

cdoublef the same as cdouble but the C double array is freed after the copy.

cint C integer array allocated by a C function to be copied into the corresponding Scilabvariable.

cintf the same as cint but the C integer array is freed after the copy.

In fact, the name of an external type corresponds to the name of a C function. ThisC function has three arguments: the dimension of the variable, an input pointer and anoutput pointer.

For instance, below is the code for external type cintf:

#include "../machine.h"

/* ip is a pointer to a Fortran variable coming from Scilab

which is itself a pointer to an array of n integers typically

coming from a C function

cintf converts this integer array into a double array in op

moreover, pointer ip is freed */

void C2F(cintf)(n,ip,op)

int *n;

int *ip[];

double *op;

{

int i;

for (i = 0; i < *n; i++)

op[i]=(double)(*ip)[i];

free((char *)(*ip));

}

For the meaning of #include "../machine.h" and C2F see 2.4.8.

Then, the user can create its own external types by creating its own C functions withthe same arguments. intersci will generate the good call of the function.

Page 12: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

CHAPTER 2. INCREMENTAL LINK AND INTERSCI 7

<comment on the variable element of the list>

<name of the variable element of list> <type> <possible arguments>

******************************

Table 2.2: Description of a variable element of a list

2.4.6 Using lists as input Scilab variables

An input argument of the Scilab function can be a Scilab list. If <list name> is the nameof this variable, a �le called <list name>.graph must describe the structure of the list.This �le permits to associate a Scilab variable to each element of the list by de�ning itsname and its Scilab type. The variables are described in order into the �le as describedby table 2.2.

Then, such a variable element of the list is referred in the �le <interface name>.desc

by its name followed by the name of the corresponding list in parenthesis. For instance,la1(g) denotes the variable named la1 element of the list named g.

An example is shown in 2.6.4.

2.4.7 Limitations

intersci does not permit to interface Fortran functions.The Fortran subroutines we want to interface must not use COMMON to pass arguments.

All the arguments must be in the calling list of the subroutine.intersci is not able to deal with complexes, matrices of strings and matrices of

polynomials as Scilab variables.See also 2.4.4 for limitations on dimensions.

2.4.8 C functions interfacing

To interface C functions instead of Fortran subroutines is easy.The C function must be considered as a procedure i.e. its type must be void or the

value returned must not be used.The arguments of the C function must be considered as Fortran arguments i.e. they

must be only pointers.Moreover, the name of the C function must be recognized by Fortran. This depends

upon the machines. For instance, on SUN and DEC machines the name of C functionsmust end by a to be recognized by Fortran, but on RS6000 (IBM) machines the namemust be the same. So, the include �le machine.h situated in the directory <Scilab

directory>/routines can be included in C functions and the macros C2F and F2C canbe used. This include �le de�nes a trailing underscore variable WTU and a leading under-score variable WLU depending on the machine and/or system and compiler types. Thefunctions C2F and F2C correct the name of a routine in C to be recognized by Fortran (andconversely).

Page 13: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

CHAPTER 2. INCREMENTAL LINK AND INTERSCI 8

2.5 Writing compatible code to Scilab

2.5.1 Input and output

If the user routines need to write text in the Scilab window, one must use the Scilab routinebasout. If the new routine uses the fortran function write or the C function fprintf theoutput will occur in the shell window.

It is also possible to read text in the Scilab window with the use of the routine basinand to read numerical data with the routine dbasin.

These input/output routines are in the directory <scilab dir>/routines/sun.

2.5.2 Error exit

If the user routine needs to interrupt the evaluation of the corresponding Scilab functionthe routine to be called is erro with the error message as argument and then return.

2.6 Examples

2.6.1 Example 1

The name of the Scilab function is calc. Its input is a string and its output is a scalar.The name of the corresponding Fortran subroutine is calc and its arguments are a

string (used as input) and an integer (used as output).We reserve a �xed dimension of 10 for the string.The description �le is the following:

calc str

str string 10

a scalar

fcalc str a

str char

a integer

out a

***********************

2.6.2 Example 2

The name of the Scilab function is som. Its inputs are two row vectors and its output is acolumn vector.

The name of the corresponding Fortran subroutine is fsom and its arguments are areal array and its dimension (used as input), another real array and its dimension (usedas input) and a real array (used as output). These dimensions m and n are determined atthe calling of the Scilab function and do not need to appear as Scilab variables.

intersci will do the job to make the necessary conversions to transform the doubleprecision (default in Scilab) row vector a into a real array and to transform the real arrayc into a double precision row vector.

The description �le is the following:

Page 14: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

CHAPTER 2. INCREMENTAL LINK AND INTERSCI 9

som a b

a row m

b row n

c column n

fsom a n b m c

str char

a real

n integer

b real

m integer

c real

out sequence c

***********************

2.6.3 Example 3

The name of the Scilab function is ext. Its input is a matrix and its outputs are a matrixand a column vector.

The name of the corresponding Fortran subroutine is fext and its arguments are aninteger array (used as input and output), its dimensions (used as input) and anotherinteger array and its dimension (used as outputs).

The dimension p of the output b is computed by the Fortran subroutine and thememory for this variable is also allocated by the Fortran subroutine (perhaps by to a callto another C function). So the type of the variable is external and we choose cintf.

Moreover, the output a of the Scilab function is the same as the input but its typechanges from a m�n matrix to a n�m matrix. This conversion is made my introducingthe Scilab variable o

The description �le is the following:

ext a

a matrix m n

b column p

o matrix n m

fext a m n b p

a integer

m integer

n integer

b cintf

p integer

out sequence o b

o a

***************************

Page 15: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

CHAPTER 2. INCREMENTAL LINK AND INTERSCI 10

2.6.4 Example 4

The name of the Scilab function is contr. Its input is a list representing a linear systemgiven by its state representation and a tolerance. Its return is a scalar (for instance thedimension of the controllable subspace).

The name of the corresponding Fortran subroutine is contr and its arguments are thedimension of the state of the system (used as input), the number of inputs of the system(used as input), the state matrix of the system (used as input), the input matrix of thesystem (used as input), an integer giving the dimension of the controllable subspace (usedas output), and the tolerance (used as input).

The description �le is the following:

contr sys tol

tol scalar

sys list lss

icontr scalar

contr nstate(sys) nin(sys) a(sys) b(sys) icontr tol

a(sys) double

b(sys) double

tol double

nstate(sys) integer

nin(sys) integer

icontr integer

out sequence icontr

******************************

The type of the list is lss and a �le describing the list lss.list is needed. It is shownbelow:

1 type

type string 3

******************************

2 state matrix

a matrix nstate nstate

******************************

3 input matrix

b matrix nstate nin

******************************

4 output matrix

c matrix nout nstate

******************************

5 direct tranfer matrix

d matrix nout nin

******************************

6 initial state

x0 column nstate

******************************

7 time domain

Page 16: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

CHAPTER 2. INCREMENTAL LINK AND INTERSCI 11

t any

******************************

The number of the elements is not compulsory in the comment describing the elementsof the list but is useful.

Page 17: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

Chapter 3

Scilab inside

We have seen in the previous chapter that adding a new fortran routine to Scilab can bedone in a \soft" manner by using the incremental link procedure with the commands linkand fort or with a new interface written with InterSci. We have seen that interfacing anew primitive (fortran or C routine) needs to create a speci�c fortran routine which hasto linked to Scilab together with the user code to be added. The interface fortran routinewas automatically generated by InterSci from a description �le. We present here how towrite directly a new interface or to modify an interface previously de�ned by InterSci.

In the sequel we describe the internal structure of Scilab with, in particular, the partdevoted to the interfaces.

Of course the system is such that the routines to be added need no modi�cation.

3.1 From the Scilab call to the prompt

We describe here the sequence of the \master" programs of Scilab which are executedbefore the prompt and how Scilab is organized.

The program main is scilab (in the sub-directory routines/default) which beginswith the call of inffic (in the sub-directory routines/sun) which initializes the names ofthe main �les needed by Scilab (for the help, save, graphics ...). Then the routine matlab(in the sub-directory routines/default) is called twice. The �rst call corresponds to asilent initialization of the database and the second one to the execution of the start-up.

3.1.1 The main routine matlab

The initializations of the database are done by the include �le <scilab dir>/routines/stack.h;the other initialization done by matlab is nunit which is the maximum number of logicalunits and then matlab calls the routine inibas (in the sub-directory routines/system).Numerous initializations are done by this routine such as : the unit numbers for the inputand the output,the prede�ned variables, the character set,...

After that matlab calls the routine matla0 (in the sub-directory routines/sytem)which is a simple call of the routine parse (in the sub-directory routines/system) followedby a call to one of the interfaces. This is done by the mean of the include �le callinter.h(in the sub-directory routines). parse is the Scilab parsing function : after examinationof a command, parse returns fun which is the number of the interface to be called bymatla0.

12

Page 18: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

CHAPTER 3. SCILAB INSIDE 13

system

Interpreter

Variables handling

Error handling

?

Interfaces handling

����������9

XXXXXXXXXXz?

Interface 1

-

Interface n

?? ? ?

Library 1 Library n

? ?

: : :

: : :

Low level routines (BLAS,: : : )

Figure 3.1: Internal structure of Scilab

3.1.2 The Scilab parsing function and the interfaces

After the analysis of a Scilab command by parse a Scilab function (written in Scilablanguage) or a fortran (or C) routine can be called. In the last case the call is done bythe corresponding interface : all the interfaces are in the sub-directory routines/interf

and each of them contains the sequence of its routines.The organization of this internal structure is represented by �gure 3.1.

3.2 The database

3.2.1 The fortran structure

The leading program of Scilab is written in fortran and so the database is organized infortran arrays. This database is composed of the 4 following arrays (in fact 3 arrays butone of them is interpreted in two di�erent manners):

� Names of the variables :IDSTK(NSIZ,LSIZ) integer array . IDSTK(1:NSIZ,K) is the code of the name of thevariable number K.

� Addresses of the starting location :LSTK(LSIZ) vector of integers. LSTK(K) is the adress of the starting location of the

Page 19: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

CHAPTER 3. SCILAB INSIDE 14

variable K in the stack STK, and LSTK(K+1)� 1 is the adress of the last word of thisvariable in STK.

� De�nitions of all the variables :STK(VSIZ) is the double oat vector of the de�nitions of all the variables known inScilab and the working area.

� ISTK vector of integers \equivalent" to STK (occupying the same place in the mem-ory).

The description of the database is completed by 4 integers :

� Maximum number of variables:ISIZ is the dimension of the arrays IDSTK and LSTK. ISIZ is the maximum numberof variables (permanent and temporary) which can be managed simultaneously bythe system (for example 500).

� Dimension of the stack STK:VSIZ . VSIZ is the size of the stack de�ned in double precision words containing thevariables (permanent and temporary) and the working area (for example 2000000).VSIZ and ISIZ are constants which can only be changed by modifying the assignedin the include �le routines/stack.h

� Location limit of temporary variables:TOP pointer on the arrays LSTK and IDSTK: the variables with a number from 1 toTOP are temporary variables (parameter of a fonction , evaluation of expressions,: : :,). LSTK(TOP+1) is the current �rst free address of the stack STK.

� Location limit of permanent variables:BOT. The variables numbered from BOT to ISIZ� 1 are permanent variables (vari-ables created by an assignment : a=expr...). LSTK(BOT)-1 is the last free addressof the stack STK. The relation TOP+ 1 < BOT must be always satis�ed (to avoidoverwritting). The �gure 3.2 presents the 3 arrays of the database. Then the �gure3.3 describes the stack, the �gure 3.4 gives the correspondance between the indicesof the array STK-ISTK. This correspondance is again presented in �gure 3.5 and thedetail of the decomposition between the description of a variable and its values is in�gure 3.6.

Remark:

A double- oat is equivalent to two integers. Converting the address from STK to ISTK isdone through the fonctions iadr and sadr.

We have the following relations:

il1 = iadr(l)

l = sadr(il1)

sadr(il2) = l + 1

The database is transmitted to the di�erent routines by the labelled commons :

COMMON /STACK/ STK

COMMON /VSTK/ BOT,TOP,IDSTK,LSTK,LEPS,BBOT,BOT0

de�ned in the include �le stack.h. LEPS is the address of STK where is stored the valueof the machine precision b(1�t) (b=base, t= length of the mantissa).

Page 20: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

CHAPTER 3. SCILAB INSIDE 15

VARIABLES

TOP

BOT

TE

MPO

RA

RY

VA

RIA

BL

ES

PER

MA

NE

NT

VA

RIA

BL

ES

Integer array LSTK(ISIZ)

of the variables in the stack

Stack containing all the

Dble float array STK(VSIZ)

variables in the stack

Integer array IDSTK(6,ISIZ)

Ex : ISIZ=500, VSIZ=2000000

ISIZ

Corresponding CODES of the ADDRESSES of the starting locations

Figure 3.2: The 3 arrays of the database

Page 21: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

CHAPTER 3. SCILAB INSIDE 16

ISTK

ISTK

ISTK

STK

STK

STK

STK

Variable description

Values

Variable description

Values

Variable description

Values

9>>>>>>>>>>>>=>>>>>>>>>>>>;

VARIABLE top� 2

9>>>>>>>=>>>>>>>;

VARIABLE top� 1

9>>>>>>>=>>>>>>>;

VARIABLE top

9>>>>>>>>>>>>>>>>=>>>>>>>>>>>>>>>>;

WORKING de AREA

iadr(lstk(top-2))

iadr(lstk(top-1))

iadr(lstk(top))

lstk(top+1)

lstk(bot)

Figure 3.3: Description of the stack

l - �

il1il2

STK ISTK

Figure 3.4: STK to ISTK conversion

Page 22: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

CHAPTER 3. SCILAB INSIDE 17

IADR and SADR are the address converters

LSTK Stack STK

Dble float array

Stack ISTK

Integer array

K-1

K

VARIABLE

K

VARIABLE

K

IADR

SADR

ISTK(IADR(LSTK(K)))STK(LSTK(K))

STK and ISTK : one (same) array

Figure 3.5: Correspondance of the arrays

Page 23: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

CHAPTER 3. SCILAB INSIDE 18

STK ISTK

DESCRITION

K

UNUSED AS

STK

DESCRIPTION

USED AS

ISTK

VALUES

USED PART AS

STK

VALUES

UNUSED AS

ISTK

ISTK(IADR(LSTK(K)))

ISTK(IADR(LSTK(K))+D)

STK(LSTK(K))

STK(SADR(P+1)) P=IADR(LSTK(K))+D)

Correspondance between STK and ISTK

VARIABLE

Figure 3.6: Description of a variable location

Page 24: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

CHAPTER 3. SCILAB INSIDE 19

3.2.2 Coding the di�erent types of variables

Let k be the number of the variable considered and il= iadr(LSTK(k)): as seen beforeil is a pointer towards the �rst word of the stack ISTK corresponding to this variable.ISTK(il) de�nes the type of the variable. We will now consider the di�erents datatypesand present their corresponding description and the organization of the part of the stackcontaining the values.

Scalar matrix type

� ISTK(il) is equal to 1.

� ISTK(il+1) contains the line number m of the matrix.

� ISTK(il+2) contains the column number n.

� ISTK(il+3) is = 0 if the matrix coe�cients are real and = 1 if they are complexnumbers.

Let l1= sadr(il+4), then

� STK(l1:l1+m*n-1) are the real parts of the matrix elements, the element (i,j) isstored in STK(l1+(i-1)+(j-1)*m).

If ISTK(il+3) is equal to 1, then

� STK(l1+m*n:l1+2*m*n-1) are the imaginary parts of the elements, the part (i; j) isstored in STK(l1+m*n+(i-1)+(j-1)*m).

The �gure 3.7 presents the description of this type.

Character string matrix

If ISTK(il) is equal to 10 :

� ISTK(il+1) contains the number of the lines m of the matrix,

� ISTK(il+2) contains the number of the columns n of the matrix,

� ISTK(il+3) not used.

The character matrix datatype is represented by �g. 3.8.In Scilab the characters are coded by integers (cf 3.2.3), the function cvstr (see

routines/system/cvstr.f) translates the ASCII code to the Scilab code and conver-sly.

Polynomial matrix

This datatype is represented by �g. 3.9.

Page 25: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

CHAPTER 3. SCILAB INSIDE 20

Imaginarypart

Realpart

0 or 1

number of columns

number of lines

1 il

il+1

l=sadr(il+4)

ISTK

STK

STK

Figure 3.7: Real or complex matrix

Page 26: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

CHAPTER 3. SCILAB INSIDE 21

charactersof

the stringnumbered

m.n

: : :

charactersof

the �rststring

pointer

: : :

pointer

1

0

m

n

10

-

-

-

ilil+1

il+4

il+4+m?n

l1=il+m?n+5

l2=l1-1+istk(il+3+2)

lmn=l1-1+istk(il+3+m?n)

ISTK

ISTK

ISTK

Figure 3.8: Character string matrix. m : number of lines ,n : number of columns

Page 27: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

CHAPTER 3. SCILAB INSIDE 22

coe�cientsof the

polynomialnumbered

m.n

: : :

coe�cientsof the�rst

polynomial

pointer

: : :

pointer

1

variable

the

of

code

0 or 1

m

n

2

-

-

-

il

il+1

il+4

il+8

il+8+m?n

l1=sadr(il+9+m?n)

l2=l1+istk(il+7+2)-1

lmn=l1+istk(il+7+m?n)-1

ISTK

ISTK

ISTK

STK

Figure 3.9: Polynomial matrix. m : number of lines , n : number of columns

Page 28: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

CHAPTER 3. SCILAB INSIDE 23

structureof thevariablenumbered

n

: : :

structureof the�rst

variable

pointer

: : :

pointer

1

n

15

-

-

-

ilil+1

il+2

il+2+n

l1=sadr(il+n+3)

l2=l1+ISTK(il+1+2)-1)

ln=l1+ISTK(il+1+n)-1)

ISTK

ISTK

STK

STK

STK

Figure 3.10: List. n : number of the elements of the list

Lists

ISTK(il)=15

� ISTK(il+1) contains the number of elements n of the list.

If ilp=il+2 and l=sadr(ilp+n+1)-1, then:

� ISTK(ilp+(i-1)) contains the pointer li such that STK(l+li) is the �rst word ofthe element i, the number of the words ( de mots (in STK) of the element numberedi is given byni=ISTK(ilp+i) - ISTK(ilp+i-1).

� STK(l+li:l+li+ni-1) contains the whole structure of the variable correspondingto this element.

This datatype is described by �g. 3.10.

Page 29: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

CHAPTER 3. SCILAB INSIDE 24

Functions

ISTK(il) is equal to 11. In this case its description is divided in 3 �elds : the �rst onedescribes the output parameters of the function, the second one is devoted to the inputparameter and the last one contains the set of instructions.

Si ils = il+ 1:

� ISTK(ils) contains the number m of the output parameters.

� ISTK(ils+1:ils+NSIZ*n) contains the names of the output variables in Scilab codeform compressed on NSIZ integers.

Let ile = ils+NSIZ � n+ 1, then

� ISTK(ile) contains the number m of the input parameters.

� ISTK(ile+1:ile+NSIZ*m) contains the names of the output variables in Scilab codeform compressed on NSIZ integers.

Let ilt = ile+NSIZ � n+ 1, then:

� ISTK(ilt) contains the length (number of characters) l of the code of the function.

� ISTK(ilt+1:ilt+l) the code of the function in Scilab code.

For the compiled functions ISTK(ilt+1:ilt+l) contains a sequence of integers fe�ningthe compiled code. The integers equal to 99 are lines separator.

Library

ISTK(il) = 14

� ISTK(il+1) contains the number nf of characters of the name of the �le containingthe functions.

� ISTK(il+2:il+1+nf) contains the sequence of characters in Scilab code form.

If ilh = il+ 2+ nf:

� ISTK(ilh) contains the number nh of characters of the name of the �le containingthe \help".

� ISTK(ilh+1:ilh+nh) contains the Scilab code of the characters.

Let iln = ilh+ nh+ 1:

� ISTK(iln) contains the number nm of functions, and ISTK(iln+2i-1:iln+2i) con-tains the compact code of the name of the i-th function, for i from 1 to nm.

Page 30: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

CHAPTER 3. SCILAB INSIDE 25

CHARACTERS SCILAB CODES

0-9 0-9

a-z or A-Z 10-35

36

# 37

~ 38

blank 40

( 41

) 42

; 43

: 44

+ 45

- 46

* 47

/ 48

\ or $ 49

= 50

. 51

, 52

' or " 53

[ or f 54

] or g 55

% 56

| 57

& 58

< or ` 59

> 60

~ or @ 61

Table 3.1: Scilab codes for known characters

Page 31: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

CHAPTER 3. SCILAB INSIDE 26

3.2.3 The code of the Scilab characters

The following table 3.1 gives the internal code of the the Scilab characters.The upper-case characters and some equivalents are coded by the lower-case code with

a sign change.The function cvstr (see routines/system/cvstr.f) translates the code ASCII to the

Scilab code and conversely.

subroutine cvstr(n,line,str,job)

c!purpose

c translates a character string written in Scilab code

c to a standard string

c the eol (99) are replaced by !

c

c!calling sequence

c call cvstr(n,line,str,job)

c

c with

c

c n: integer, length of the string to be translated

c

c line: vector of integers which are the codes of the characters

c

c string: character, contains ASCII characters

c

c job: integer, if equal to 1: code-->ascii

c if equal to 0: ascii-->code

3.3 Interfaces Scilab-Fortran

This section describes the rules to follow for writing an interface allowing to add a newprimitive to the system; of course we forget here the parts depending on the host computer(compiler, linker,: : :). The Scilab structure is resumed in �gure 3.1.

3.3.1 Interfaces handling

The link between the Scilab primitives and the corresponding interfaces is done by theroutine funtab. This routine is automatically produced by the program bin/newfun

with the �le routines/default/fundef. This routine handle two tables initialized byDATA.

The �rst table (funn(NSIZ,funl)) contains the coded names of the functions knownby Scilab, funl being the number of these functions.

The other table (funp(funl)) de�ne 2 integers fun and fin for each known function;these 2 integers are represented by the integer 100?fun+fin, where :

� fun indicates the interface program implementing the function.

� fin indicates the function inside the interface program.

Page 32: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

CHAPTER 3. SCILAB INSIDE 27

For adding a new primitive it is necessary to add its name and the value 100?fun+fin

in the �le routines/default/fundef following the format speci�cation.Example :

abs 601 0

atan 625 0

cos 624 0

de�ne the pointers towards the Scilab primitives abs, atan, cos.Running the program bin/newfun or more easily the Make�le allows then the gener-

ation of the �le funtab.f.

3.3.2 Interface routine

When a Scilab function is called, the system calls the corresponding interface programafter having de�ned the variables fin, lhs and rhs in the common /com/ and con�guredthe database: common/STACK/ and /VSTK/.

The variables lhs and rhs

These variables indicate the numbers of left-parameters (lhs) and right-parameters (rhs)used for the call of the function.Example: [x,y]=foo(a,b,c) gives lhs = 2 and rhs = 3.

The interface must check if these parameters numbers agree with the allowed valuesfor the functions. Di�erent variants can be de�ned for a unique function by using thepossibility of a variable length for the parameters list.

In case of incompatibility for the parameter list, the interface program calls the errorhandling program (error) with the code 41 (lhs) or 42 (rhs) and return the prompt.

3.4 A working example

We de�ne here a fortran routine and then we interface it with Scilab. We are in the caseof the Scilab data types are not simple fortran types. The code of this routine is thefollowing :

subroutine dmptr(pm,d,n,tr,dt)

c!purpose

c Computes the trace of a square polynomial matrix pm

c!Calling sequence

c

c subroutine dmptr(pm,d,n,tr,dt)

c double precision pm(*),tr(*)

c integer d(*),dt

c

c pm : array of polynomial matrix coefficients:

c pm=[coeff(pm(1,1)),coeff(pm(2,1)),...coeff(pm(n,n))]

c d : array of pointer on the first coefficient of pm(i,j)

c d=[1,1+degree(pm(1,1)),1+degree(pm(1,1))+degree(pm(2,1)),...1+...+degree(pm(n,n))]

c n : size of pm matrix

c tr : array of trace polynomial coefficients

Page 33: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

CHAPTER 3. SCILAB INSIDE 28

c dt : degre of trace polynomial

c!

double precision pm(*),tr(*)

integer d(*),dt,n

c

integer deg

c computes trace degree

dt=0

do 01 i=1,n

ii=i+(i-1)*n

dt=max(dt,d(ii+1)-d(ii))

01 continue

c initialize tr coefficients to 0.0d0

call dset(dt,0.0d0,tr,1)

c sum of the diagonal elements of pm

do 10 i=1,n

ii=i+(i-1)*n

deg=d(ii+1)-d(ii)

write(*,*) i,ii,deg

if(deg.gt.0) then

do 05 k=1,deg

tr(k)=tr(k)+pm(d(ii)-1+k)

05 continue

endif

10 continue

return

end

We now write the code newint corresponding to the new interface i.e. de�ning theScilab command tr=trace(mp)

subroutine newint

include 'routines/stack.h'

double precision sr,si

integer iadr, sadr, id(nsiz)

iadr(l)=l+l-1

sadr(l)=(l/2)+1

rhs = max(0,rhs)

lw = lstk(top+1)

l0 = lstk(top+1-rhs)

C+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

if (fin .eq. 1) then

if (rhs .ne. 1) then

call error(39)

return

endif

if (lhs .ne. 1) then

Page 34: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

CHAPTER 3. SCILAB INSIDE 29

call error(41)

return

endif

il1 = iadr(lstk(top-rhs+1))

if (istk(il1) .eq. 1) then

if (istk(il1+1) .ne. istk(il1+2)) then

err=1

call error(20)

return

endif

n1 = istk(il1+1)

l1 = sadr(il1+4)

it1 = istk(il1+3)

sr = 0.0d0

si = 0.0d0

do 10 i = 1,n1

sr = sr+stk(l1+(i-1)+(i-1)*n1)

10 continue

if(it1 .eq. 1) then

do 11 i = 1,n1

si = si+stk(l1+n1*n1+(i-1)+(i-1)*n1)

11 continue

endif

stk(l1) = sr

if(it1 .eq. 1) stk(l1+1) = si

istk(il1+1) = 1

istk(il1+2) = 1

if (si .eq. 0.0d0) istk(il1+3) = 0

lstk(top+1) = l1+1+it1

elseif (istk(il1) .eq. 2) then

if (istk(il1+1) .ne. istk(il1+2)) then

err = 1

call error(20)

return

endif

n1 = istk(il1+1)

id1 = il1+8

l1 = sadr(id1+n1*n1+1)

it1 = istk(il1+3)

idt=0

do 20 i=1,n1

ii=i+(i-1)*n1

idt=max(idt,istk(id1+ii)-istk(id1-1+ii))

20 continue

lr = lw

err = lr+idt*(it1+1) - lstk(bot)

if (err .gt. 0) then

call error(17)

Page 35: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

CHAPTER 3. SCILAB INSIDE 30

return

endif

call dmptr(stk(l1),istk(id1),n1,stk(lr),idt)

if (it1 .ne. 0) then

l1i = l1+istk(id1+n1*n1)-1

lri = lr+idt

call dmptr(stk(l1i),istk(id1),n1,stk(lr+idt),idt0)

endif

istk(il1+1) = 1

istk(il1+2) = 1

istk(il1+3) = it1

istk(il1+8)=1

istk(il1+9)=idt+1

l1 = sadr(il1+10)

call dcopy((idt+1)*(it1+1),stk(lr),1,stk(l1),1)

lstk(top+1)=l1+(idt+1)*(it1+1)

else

buf='First argument is nor a matrix nor a polynomial matrix'

call error(999)

return

endif

endif

end

We now reconsider the previous code with the comments for the di�erent steps of theprocedure

subroutine newint

C INCLUDING THE DATABASE PARAMETERS

C REPLACE SCIDIR BY THE SCILAB PATH

include 'SCIDIR/routines/stack.h'

double precision sr,si

integer iadr, sadr, id(nsiz)

C DEFINITION OF THE ADDRESS CONVERTERS

iadr(l)=l+l-1

sadr(l)=(l/2)+1

rhs = max(0,rhs)

C ADDRESSES OF THE BOUNDS OF THE LOCATIONS OF THE RIGHT HAND SIDE PARAMETERS

l0 = lstk(top+1-rhs)

C+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

if (fin .eq. 1) then

C BEGINNING OF THE CODE TO BE ADDED

C SCILAB tr=trace(mp)

C ==================

C CHECK NUMBER OF CALLING RIGHT HAND SIDE (rhs) ARGUMENTS

if (rhs .ne. 1) then

call error(39)

Page 36: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

CHAPTER 3. SCILAB INSIDE 31

return

endif

C CHECK NUMBER OF CALLING LEFT HAND SIDE (lhs) ARGUMENTS

if (lhs .ne. 1) then

call error(41)

return

endif

C CHECK NOW VARIABLE mp (NUMBER 1)

il1 = iadr(lstk(top-rhs+1))

if (istk(il1) .eq. 1) then

C il1 IS THE TYPE OF THE VARIABLE (SEE FIG. 3.6)

C++++++++++++++ STANDARD MATRIX CASE

if (istk(il1+1) .ne. istk(il1+2)) then

C . Non square matrix

err=1

call error(20)

return

endif

n1 = istk(il1+1)

C l1 ADDRESS OF MATRIX ELEMENTS (REAL PART)

l1 = sadr(il1+4)

C it1 REAL/COMPLEX FLAG (0 or 1)

it1 = istk(il1+3)

C INLINE PROCEDURE TO COMPUTE THE MATRIX TRACE

sr = 0.0d0

si = 0.0d0

do 10 i = 1,n1

sr = sr+stk(l1+(i-1)+(i-1)*n1)

10 continue

if(it1 .eq. 1) then

C . if complex computes imaginary part

do 11 i = 1,n1

si = si+stk(l1+n1*n1+(i-1)+(i-1)*n1)

11 continue

endif

C STORE RESULT IN PLACE OF mp

stk(l1) = sr

if(it1 .eq. 1) stk(l1+1) = si

C SET RESULT SIZES FOR STACK HANDLING

istk(il1+1) = 1

istk(il1+2) = 1

if (si .eq. 0.0d0) istk(il1+3) = 0

C RETURN ADDRESS OF THE FIRST FREE POSITION IN THE STACK

lstk(top+1) = l1+1+it1

C END OF STANDARD MATRIX CASE

elseif (istk(il1) .eq. 2) then

C++++++++++++++POLYNOMIAL MATRIX CASE (SEE FIG. 3.8)

if (istk(il1+1) .ne. istk(il1+2)) then

Page 37: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

CHAPTER 3. SCILAB INSIDE 32

C . non square matrix

err = 1

call error(20)

return

endif

n1 = istk(il1+1)

C id1 STARTING ADDRES OF POINTERS

id1 = il1+8

C l1 STARTING ADDRESS OF THE COEFFICIENTS

l1 = sadr(id1+n1*n1+1)

C it1 REAL/COMPLEX FLAG (0/1)

it1 = istk(il1+3)

C COMPUTING THE SIZE OF THE RESULT

idt=0

do 20 i=1,n1

ii=i+(i-1)*n1

idt=max(idt,istk(id1+ii)-istk(id1-1+ii))

20 continue

C CKECKING AVAILABLE MEMORY

C SET RESULT POINTER TO THE FIRST FREE STACK ADDRESS

lr = lw

C SET ERR TO THE NEGATIVE OF THE FREE SPACE

err = lr+idt*(it1+1) - lstk(bot)

if (err .gt. 0) then

C . Not enough memory

call error(17)

return

endif

C CALLING THE PROCEDURE TO COMPUTE THE MATRIX TRACE

C . Real part

call dmptr(stk(l1),istk(id1),n1,stk(lr),idt)

if (it1 .ne. 0) then

C . Imaginary part

l1i = l1+istk(id1+n1*n1)-1

lri = lr+idt

call dmptr(stk(l1i),istk(id1),n1,stk(lr+idt),idt0)

endif

C DEFINITION OF THE RETURN VARIABLE

C SET THE RESULT HEADER (ISTK PART OF THE STACK)

C . row size

istk(il1+1) = 1

C . column size

istk(il1+2) = 1

C . real/complex flag

istk(il1+3) = it1

C . pointers

istk(il1+8)=1

istk(il1+9)=idt+1

Page 38: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

CHAPTER 3. SCILAB INSIDE 33

C MOVE COMPUTED VALUE IN ITS FINAL PLACE

l1 = sadr(il1+10)

call dcopy((idt+1)*(it1+1),stk(lr),1,stk(l1),1)

C RETURN ADDRESS OF THE FIRST FREE POSITION IN THE STACK

lstk(top+1)=l1+(idt+1)*(it1+1)

C . End of polynomial matrix case

else

C++++++++++++++INVALID ARGUMENT TYPE CASE

buf='First argument is nor a matrix nor a polynomial matrix'

call error(999)

return

endif

C END OF TRACE FUNCTION

endif

end

After that we have to compile the routines dmptr.f and newint.f. Then we use theScilab function addinter to link the new called fortran routine dmptr.o and the newinterface calling fortran routine newint.o with Scilab. The last argument of addinter isthe calling name for Scilab (for a complete description see the on-line help of addinter).During all the Scilab session, mytrace remains de�ned and can be used as prede�nedfunction.

getf('SCI/macros/util/addinter.sci')

addinter('dmptr.o newint.o','newint','mytrace')

a=diag([%s+1,2,3,4])

mytrace(a)

The result is :

-->getf('SCI/macros/util/addinter.sci')

-->addinter('dmptr.o newint.o','newint','mytrace')

linking newint_ defined in dmptr.o newint.o with Scilab

-->a=diag([%s+1,2,3,4])

a =

! 1 + s 0 0 0 !

! !

! 0 2 0 0 !

! !

! 0 0 3 0 !

! !

! 0 0 0 4 !

Page 39: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

CHAPTER 3. SCILAB INSIDE 34

-->mytrace(a)

ans =

10 + s

It may be preferable to add de�nitively a new interface to Scilab. In this case the easiestway for a user is to give the name matusr to the interface entry point and to replace thestandard <scilab dir>/routines/default/matusr.f �le by his own �le. Then the userhas to add the new function names in <scilab dir>/routines/default/fundef andexecute make.

In the directory SCIDIR/routines/interf one can �nd metane.f which is a very largeinterface program automatically generated by intersci and corresponding to the Metanetprograms.

The beginning of the �le description is :

inimet datanet

datanet string l

window scalar

inimet window datanet l

window integer

datanet char

l integer

out sequence window

************************************************

netwindow window

window scalar

netwindow window

window integer

out empty

************************************************

netwindows

scrs row nscrs

netwindows scrs nscrs

scrs cintf

nscrs integer

out sequence scrs

************************************************

loadg name

name string ln

directed scalar

n scalar

Page 40: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

CHAPTER 3. SCILAB INSIDE 35

tail row madim

head row madim

node_name stringmat 1 ndim

node_type row ndim

node_x row ndim

node_y row ndim

node_color row ndim

node_diam row ndim

node_border row ndim

node_font_size row ndim

node_demand row ndim

edge_name stringmat 1 madim

edge_color row madim

edge_width row madim

edge_hi_width row madim

edge_font_size row madim

edge_length row madim

edge_cost row madim

edge_min_cap row madim

edge_max_cap row madim

edge_q_weight row madim

edge_q_orig row madim

edge_weight row madim

default_node_diam scalar

default_node_border scalar

default_edge_width scalar

default_edge_hi_width scalar

default_font_size scalar

loadg name ln directed n tail head node_name node_typ

e node_x node_y node_color node_diam node_border node_font_size node_dem

and edge_name edge_color edge_width edge_hi_width edge_font_size edge_len

gth edge_cost edge_min_cap edge_max_cap edge_q_weight edge_q_orig edge_weight

default_node_diam default_node_border default_edge_width default_

edge_hi_width default_font_size ndim madim

sup integer

name char

ln integer

directed integer

n integerslides_root.dvi

tail cintf

head cintf

node_name Cstringv

node_type cintf

node_x cintf

node_y cintf

node_color cintf

node_diam cintf

Page 41: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

CHAPTER 3. SCILAB INSIDE 36

node_border cintf

node_font_size cintf

...................................

When we apply intersci to this �le we get metane.f which is the interface routine andmetane.fundef which is the list of all the new interfaced routines (see below). This list isthen added to the global list fundef of Scilab (see this �le in the directory SCIDIR/util.The same manipulation can be done with our previous example : newint can be added atthe end of an existing interface program and the �le fundef completed.

The �le metane.fundef:

inimet 1501 3

netwindow 1502 3

netwindows 1503 3

loadg 1504 3

createg 1505 3

showns 1506 3

showp 1507 3

prevn2p 1508 3

ns2p 1509 3

p2ns 1510 3

edge2st 1511 3

prevn2st 1512 3

compc 1513 3

concom 1514 3

compfc 1515 3

sconcom 1516 3

pcchna 1517 3

ford 1518 3

johns 1519 3

dijkst 1520 3

frang 1521 3

chcm 1522 3

transc 1523 3

dfs 1524 3

umtree 1525 3

umtree1 1526 3

dmtree 1527 3

tconex 1528 3

flomax 1529 3

kilter 1530 3

busack 1531 3

floqua 1532 3

relax 1533 3

findiso 1534 3

ta2lpd 1535 3

ta2lpu 1536 3

lp2tad 1537 3

lp2tau 1538 3

Page 42: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

List of Figures

3.1 Internal structure of Scilab : : : : : : : : : : : : : : : : : : : : : : : : : : : 133.2 The 3 arrays of the database : : : : : : : : : : : : : : : : : : : : : : : : : : 153.3 Description of the stack : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 163.4 STK to ISTK conversion : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 163.5 Correspondance of the arrays : : : : : : : : : : : : : : : : : : : : : : : : : : 173.6 Description of a variable location : : : : : : : : : : : : : : : : : : : : : : : : 183.7 Real or complex matrix : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 203.8 Character string matrix. m : number of lines ,n : number of columns : : : 213.9 Polynomial matrix. m : number of lines , n : number of columns : : : : : : 223.10 List. n : number of the elements of the list : : : : : : : : : : : : : : : : : : 23

37

Page 43: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

List of Tables

2.1 Description of a pair of Scilab function and Fortran subroutine : : : : : : : 32.2 Description of a variable element of a list : : : : : : : : : : : : : : : : : : : 7

3.1 Scilab codes for known characters : : : : : : : : : : : : : : : : : : : : : : : : 25

38

Page 44: rectangl e - Klaus Betzler's Homepage - Startseite · Chapter 1 In tro duction W e will describ e the di eren t pro cedures for adding routines to Scilab. The simplest w a y is the

Index

intersci, 2

39