The method to use Scilab function in C++ code
1. Introduction
Scilab© is a free and open source software for numerical computation. Many advanced data structures and numerical methods in scientific computation have been built in Scilab. It also includes a high level programming language similar to the Matlab© language. For Matlab users, this software can be used as a free alternative.
For C++ (and other high level general purpose programming languages) programmers, Scilab provides a quick and easy access to many necessary numerical methods, such as matrix operation, solution to equations, simulation, control, optimization, and signal processing. This document will describes one applicable configuration.
Depends on purposes, it may be necessary to trust only on someone’s own codes sometimes. So use the method in this document with caution. If the compiler and the operation system are different, this document may be useful as a reference.
The material is based on http://help.scilab.org/docs/5.3.3/en_US/call_scilab.html. This experience was gained during my 2012 spring visit to Hokkaido University, Japan.
2. Procedure
Software
• Windows 7 Enterprise 64-bit (English version)
• Visual Studio 2010 Premium 32-bit (English version)
• Scilab 5.3.3 32-bit (English version)
Installation directory: C:\Program Files (x86)\scilab-5.3.3
Remark: Visual studio and Scilab need to be both 32-bit versions or both 64-bit versions.
Tell C++ code to use Scilab
Add #include files and commands to initial call_scilab
These three lines will provide necessary information about scilab related functions for visual studio. They can be added after the other #include” commands.
#include “call_scilab.h” #include “api_scilab.h” #include “stack-c.h” |
Call_scilab functions should be placed between the initialization and termination of Scilab engine.
// Initialization
if ( StartScilab(NULL, NULL, 0) == FALSE ) { printf(“Error : StartScilab\n”); return 0; } |
// Termination
if ( TerminateScilab(NULL) == FALSE ) { printf(“Error : TerminateScilab\n”); cout<}} |
These two parts of codes can be placed in the beginning and ending of the “main” function.
Change the property to let visual studio and windows find Scilab
The property setting of the “project/solution” in visual studio can be found in the right-click menu (Illustration 1).
Add “additional include directories” (Illustration 2). In the author’s case, the added content is C:\Program Files (x86)\scilab-5.3.3\modules\core\includes;C:\Program Files (x86)\scilab-5.3.3\modules\call_scilab\includes;C:\Program Files (x86)\scilab-5.3.3\modules\api_scilab\includes;
Add “additional library directories” (Illustration 3). In the author’s case, the added content is C:\Program Files (x86)\scilab-5.3.3\bin
Add new dependencies to “additional dependencies” (Illustration 4). In the author’s case, the added content is C:\Program Files (x86)\scilab-5.3.3\bin\api_scilab.lib;C:\Program Files (x86)\scilab-5.3.3\bin\call_scilab.lib;
Add “import library” (Illustration 5). In the author’s case, the content is C:\Program Files (x86)\scilab-5.3.3\bin
Add the installation path of Scilab to the system environment (Illustration 6). In the author’s case, the added content is C:\Program Files (x86)\scilab-5.3.3\bin;
C++ programming procedure
- Add the #include files, initialization codes and termination codes, as mentioned in the previous section.
- Transport data between C++’s memory and Scilab’s memory.
To use functions in Scilab, the variables necessary must be already in the Scilab’s memory. The results are also stored in Scilab’s memory, so they need to be exported to the C++ part after running the functions. To create simple variables in the Scilab section, the value can be used to form a string in the format of Scilab command. For example, the command SendScilabJob(“d=2.0;”); creates a variable with the name “d” and the value “2.0”. Note the default type of numeric value in Scilab is double. To create matrices, it is more convenient to create them firstly in C++, and then use certain functions provided by Scilab to transfer them to the Scilab part. Note that in Scilab, the matrices are filled in the column firstly, i.e. the storage of a matrix [1 2;3 4] would look like [1 3 2 4].
These functions related to double-management include:
SciErr getMatrixOfDouble(void* _pvCtx, int* _piAddress, int* _piRows, int* _piCols, double** _pdblReal)
Remark: _pvCtx is Scilab environment pointer, simply pass in “pvApiCtx” pro-vided by api_scilab.h; SciErr getComplexMatrixOfDouble(void* _pvCtx, int* _piAddress, int* _piRows, int* _piCols, double** _pdblReal, double** _pdblImg) SciErr readNamedMatrixOfDouble(void* _pvCtx, char* _pstName, int* _piRows, int* _piCols, double* _pdblReal) SciErr readNamedComplexMatrixOfDouble(void* _pvCtx, char* _pstName, int* _piRows, int* _piCols, double* _pdblReal, double* _pdblImg) |
- Send commands in Scilab format to Scilab engine.
Two functions are provided to do the job. They are “SendScilabJob” and “SendScilabJobs”. The former one sends one command, and the other one can send multiple commands.
3. Where to find help?
a. Scilab help browser
This can be activated by the command “help” in Scilab console. Related topics are listed under the title of “call_scilab API” and “API Scilab”.
b. Example codes
They are located in a sub-directory in the Scilab installation directory. For example, the directory can be “C:\Program Files (x86)\scilab-5.3.3\modules\call_scilab\examples”