Scilab Bag Of Tricks: The Scilab-2.5 IAQ (Infrequently Asked Questions) | ||
---|---|---|
Prev | Chapter 7. Scilab Core | Next |
The interface to Scilab's core is widely undocumented. To save the reader frequent lookups in the defining file, SCI/routines/interf/stack1.f, we have compiled the most important ones in the following sections: query, access and creation of objects.
The functions in this group allow for retrieval of information about the parameters a function has been called with, and about the properties of objects on the stack.
function CheckRhs (FunctionName : in String; MinNumParameter : in Integer; MaxNumParameter : in Integer) return Boolean;
Check the number of actual parameters on the right-hand side to be in the range MinNumParameter : MaxNumParameter. Return true if it is in the range, otherwise raise error 77 associated with FunctionName.
Note that a function that is called without any parameters, i.e. Foo(), gets an Rhs of -1.
The semantics of CheckRhs are slightly goofy. If the number of actual input parameters is in the specified range, CheckRhs returns True, but it never returns False as it raises an error in this case.
Ensure that at least 2, but not more than 5 parameters are passed to the function:
if (.not. checkrhs(fname, 2, 5)) return
We have assumed that fname is set to the function's name.
CheckLhs, Rhs, Lhs
function CheckLhs (FunctionName : in String; MinNumParameter : in Integer; MaxNumParameter : in Integer) return Boolean;
Check the number of output variables, i.e. arguments on the right-hand side to be in the range MinNumParameter : MaxNumParameter. Return true if it is in the range, otherwise raise error 78 associated with FunctionName.
Note that it is no error to supply less output parameters than the function actually yields. The extra values are silently discarded. This is true for the case of zero output values, too; then ans gets the first output value. So, a function called without any output parameters gets an Lhs of 1.
The semantics of CheckLhs are slightly goofy. If the number of actual output parameters is in the specified range, CheckLhs returns True, but it never returns False as it raises an error in this case.
Ensure that there are not more than 2 output parameters, when the function is called:
if (.not. checklhs(fname, 1, 2)) return
We have assumed that fname is set to the function's name.
CheckRhs, Rhs, Lhs
Lhs : Integer
The number of actual output parameters (i.e. those on the left-hand side of the assignment operator) is stored in the global variable Lhs.
CheckLhs, CheckRhs, Rhs
Rhs : Integer
The number of actual input parameters (i.e. those on the right-hand side of the assignment operator) is stored in the global variable Rhs.
CheckLhs, CheckRhs, Lhs
The functions in this section grant the programmer access to parameters that are stored on the Scilab stack. The general working is always the same: An index to the current (i.e. as on entry of the function) top of the parameter stack, "BasePointer", and an index to the desired argument, "StackPointer", are passed to the API. On return the user gets all necessary information about the argument like sub-type and dimension and as important indices, "FooIndex", into the Scilab heap which act like pointers to the actual contents. This way only meta-data is passed, saving time-consuming copy operations.
function GetMat (FunctionName : in String; BasePointer : in ParameterStackAddress; StackPointer : in ParameterStackAddress; IsComplex : out ComplexFlag; Rows : out Natural; Columns : out Natural; RealIndex : out DataStackIndex; ImaginaryIndex : out DataStackIndex) return Boolean;
Retrieve the address(es) and dimensions of a real or complex matrix from the parameter stack. The BasePointer must be set to the parameter stack pointer's value on entry of the calling function. StackPointer points to the desired parameter on the parameter stack. If successful, GetMat returns True, and IsComplex, Rows, Columns, and RealIndex have valid values. If IsComplex = ComplexVariable then ImaginaryIndex is valid, too. If the parameter indexed by StackPointer is not a matrix then GetMat returns False.
The output parameter IsComplex indicates whether the matrix on the data stack is purely real or complex. In the first case RealIndex points to the matrix, in the second case RealIndex points to the real part of matrix, and ImaginaryIndex points to the imaginary part. In any case Rows and Columns are the number of rows and columns in the matrix.
Fetch the addresses of a possible complex m times n matrix from position top of the parameter stack.
if (.not. getmat(fname, topk, top, iscmpx, m, n, are, aim)) return
It is assumed that fname has been set to the function's name, and topk carries the position of the stack on entry to the calling function.
GetRMat, GetRVect, GetVect
function GetRMat (FunctionName : in String; BasePointer : in ParameterStackAddress; StackPointer : in ParameterStackAddress; Rows : out Natural; Columns : out Natural; RealIndex : out DataStackIndex) return Boolean;
function GetRVect (FunctionName : in String; BasePointer : in ParameterStackAddress; StackPointer : in ParameterStackAddress; Rows : out Natural; Columns : out Natural; RealIndex : out DataStackIndex) return Boolean;
function GetVect (FunctionName : in String; BasePointer : in ParameterStackAddress; StackPointer : in ParameterStackAddress; IsComplex : out ComplexFlag; Rows : out Natural; Columns : out Natural; RealIndex : out DataStackIndex; ImaginaryIndex : out DataStackIndex) return Boolean;
function GetScalar (FunctionName : in String; BasePointer : in ParameterStackAddress; StackPointer : in ParameterStackAddress; Index : out DataStackIndex) return Boolean;
The object creation functions are mainly used to setup temporary variables for the current procedure or the procedures to be called; they bear a lot of resemblance with the object access functions (see also the section called Access Object). The difference is that a new object is created and therefore stack space is used.
function CreMat (FunctionName : in String; StackPointer : in ParameterStackAddress; WantComplex : in ComplexFlag; Rows : in Natural; Columns : in Natural; RealIndex : out DataStackIndex; ImaginaryIndex : out DataStackIndex) return Boolean;