Sciscipy: A Scilab API for Python


Table of Content

How to use sciscipy

Basic access to scilab from python with sciscipy

The low level module to access scilab functionalities is called sciscipy. It contains three methods that allow you to

  • read the value of an existing variable in scilab
  • write the value of an existing python variable to a scilab variable
  • eval a string in scilab

With those three commands, everything happens as if scilab was just running in the background (which in fact, it does). You can type commands with the eval method as you would type on the scilab prompt and copy/paste values from scilab to python using the read/write methods.

A simple example:

>>> import sciscipy              
>>> sciscipy.eval("x=[1,2,3]")       # create 'x' in scilab
>>> x ="x")           # read 'x' from scilab
>>> x
[1.0, 2.0, 3.0]
>>> x[0] = 4
>>> sciscipy.write("y", x)           # create 'y' in scilab
>>> y ="y")
>>> y
[4.0, 2.0, 3.0]

It is already possible to go quite a long way with this method. As long as the type conversion is supported from scilab to python and back, every scilab command can be issued with eval and the result can be read with the read command. The supported types and how they are translated from python to scilab is described in SupportedTypes

A more integrated approach : the scilab module

Although the three methods of the sciscipy module can be used to get any result from scilab, any code that makes an extensive use of scilab tools would soon become difficult to read and maintain as most important commands would be encapsulated in strings passed to the eval command. The scilab modules solves this problem. It works as follows:

>>> from scilab import scilab as sci
>>> sci.inv([[1,2],[3,4]])
[[-1.9999999999999996, 0.99999999999999978], [1.4999999999999998, -0.49999999999999989]]

The line

>>> from scilab import scilab as sci

imports the variable sci which is an instance of a Scilab class defined in the scilab module. This instance can be used to invoke any scilab function. The statement y = sci.inv(x) is equivalent to

sciscipy.write("tmp1", x)
sciscipy.eval("tmp2 = inv(tmp1)")
y ="tmp2")

Python matrices as list of list or as numpy array

If numpy is not available, scilab matrices are represented in Python as list of list. This is convenient for typing a matrix easily on the command line but operator such as multiplication do not work directly has shown here:

>>> def mult(a, b):
...     sciscipy.write("a",a)
...     sciscipy.write("b",b)
...     sciscipy.eval("res=a*b")
...     return"res")
>>> mult(sci.eye(3,3), [[1,2,3],[4,5,6],[7,8,9]])
[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]

If numpy is available, matrices will be represented in Python as numpy array. The code above becomes:

>>> import numpy
>>>,3), [[1,2,3],[4,5,6],[7,8,9]])
array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.],
       [ 7.,  8.,  9.]])
>>> type(sci.eye(3,3))
<type 'numpy.ndarray'>

Defining scilab function on the fly

With this approach, it is also possible to define Scilab function on the fly:

>>> sciscipy.eval("deff('[x,y]=myfct(a,b)',['x=a+b'; 'y=a-b'])")
>>> [x, y] = sci.myfct(1,2)
>>> (x,y)
(array([ 3.]), array([-1.]))

Some examples

Using the Scilab number generator

>>> import timeit
>>> timeit.Timer('sci.grand(1000,100,"nor",1, 0.5)', 
                 setup = 'from scilab import scilab as sci ;'\
                 gc.enable()').timeit(number = 100)
>>> timeit.Timer('numpy.random.normal(1, 0.5, (1000, 100))', 
                 setup = 'import numpy ;'\
                 'gc.enable()').timeit(number = 100)

Using ploting functions