Interface to Scilab

Scilab is a scientific software package for numerical computations providing a powerful open computing environment for engineering and scientific applications. Scilab includes hundreds of mathematical functions with the possibility to add interactively programs from various languages (C, C++, Fortran…). It has sophisticated data structures (including lists, polynomials, rational functions, linear systems…), an interpreter and a high level programming language.

The commands in this section only work if you have the “scilab” interpreter installed and available in your PATH. It’s not necessary to install any special Sage packages.

EXAMPLES:

sage: scilab.eval('2+2')                 # optional - scilab
'ans  =\n \n    4.'
sage: scilab('2+2')                      # optional - scilab
4.
sage: a = scilab(10)                     # optional - scilab
sage: a**10                              # optional - scilab
1.000D+10

Tutorial based the MATLAB interface tutorial:

EXAMPLES:

sage: scilab('4+10')                     # optional - scilab
14.
sage: scilab('date')                     # optional - scilab; random output
15-Feb-2010
sage: scilab('5*10 + 6')                 # optional - scilab
56.
sage: scilab('(6+6)/3')                  # optional - scilab
4.
sage: scilab('9')^2                      # optional - scilab
81.
sage: a = scilab(10); b = scilab(20); c = scilab(30)    # optional - scilab
sage: avg = (a+b+c)/3                    # optional - scilab
sage: avg                                # optional - scilab
20.
sage: parent(avg)                        # optional - scilab
Scilab

sage: my_scalar = scilab('3.1415')       # optional - scilab
sage: my_scalar                          # optional - scilab
3.1415
sage: my_vector1 = scilab('[1,5,7]')     # optional - scilab
sage: my_vector1                         # optional - scilab
1.    5.    7.
sage: my_vector2 = scilab('[1;5;7]')     # optional - scilab
sage: my_vector2                         # optional - scilab
1.
5.
7.
sage: my_vector1 * my_vector2            # optional - scilab
75.

sage: row_vector1 = scilab('[1 2 3]')             # optional - scilab
sage: row_vector2 = scilab('[3 2 1]')             # optional - scilab
sage: matrix_from_row_vec = scilab('[%s; %s]'%(row_vector1.name(), row_vector2.name()))     # optional - scilab
sage: matrix_from_row_vec                            # optional - scilab
1.    2.    3.
3.    2.    1.

sage: column_vector1 = scilab('[1;3]')               # optional - scilab
sage: column_vector2 = scilab('[2;8]')               # optional - scilab
sage: matrix_from_col_vec = scilab('[%s %s]'%(column_vector1.name(), column_vector2.name()))                                    # optional - scilab
sage: matrix_from_col_vec                            # optional - scilab
1.    2.
3.    8.

sage: my_matrix = scilab('[8, 12, 19; 7, 3, 2; 12, 4, 23; 8, 1, 1]')    # optional - scilab
sage: my_matrix                                      # optional - scilab
    8.     12.    19.
    7.     3.     2.
    12.    4.     23.
    8.     1.     1.

sage: combined_matrix = scilab('[%s, %s]'%(my_matrix.name(), my_matrix.name()))                                        # optional - scilab
sage: combined_matrix                               # optional - scilab
8.     12.    19.    8.     12.    19.
7.     3.     2.     7.     3.     2.
12.    4.     23.    12.    4.     23.
8.     1.     1.     8.     1.     1.

sage: tm = scilab('0.5:2:10')                       # optional - scilab
sage: tm                                            # optional - scilab
0.5    2.5    4.5    6.5    8.5

sage: my_vector1 = scilab('[1,5,7]')                # optional - scilab
sage: my_vector1(1)                                 # optional - scilab
1.
sage: my_vector1(2)                                 # optional - scilab
5.
sage: my_vector1(3)                                 # optional - scilab
7.

Matrix indexing works as follows:

sage: my_matrix = scilab('[8, 12, 19; 7, 3, 2; 12, 4, 23; 8, 1, 1]')     # optional - scilab
sage: my_matrix(3,2)                                # optional - scilab
4.

One can also use square brackets:

sage: my_matrix[3,2]                                # optional - scilab
4.

Setting using parenthesis cannot work (because of how the Python language works). Use square brackets or the set function:

sage: my_matrix = scilab('[8, 12, 19; 7, 3, 2; 12, 4, 23; 8, 1, 1]')    # optional - scilab
sage: my_matrix.set(2,3, 1999)                          # optional - scilab
sage: my_matrix                                         # optional - scilab
       8.         12.         19.
       7.          3.       1999.
      12.          4.         23.
       8.          1.          1.
sage: my_matrix[2,3] = -126                             # optional - scilab
sage: my_matrix                                         # optional - scilab
       8.         12.         19.
       7.          3.      - 126.
      12.          4.         23.
       8.          1.          1.

AUTHORS:

– Ronan Paixao (2008-11-26), based on the MATLAB tutorial by
William Stein (2006-10-11)
class sage.interfaces.scilab.Scilab(maxread=None, script_subdirectory=None, logfile=None, server=None, server_tmpdir=None, seed=None)

Bases: sage.interfaces.expect.Expect

Interface to the Scilab interpreter.

EXAMPLES:

sage: a = scilab('[ 1, 1, 2; 3, 5, 8; 13, 21, 33 ]')    # optional - scilab
sage: b = scilab('[ 1; 3; 13]')                         # optional - scilab
sage: c = a * b                                         # optional - scilab
sage: print(c)                                          # optional - scilab
  30.
  122.
  505.
console()

Starts Scilab console.

EXAMPLES:

sage: scilab.console()          # optional - scilab; not tested
eval(command, *args, **kwds)

Evaluates commands.

EXAMPLES:

sage: scilab.eval("5")                      # optional - scilab
'ans  =
5.’
sage: scilab.eval(“d=44”) # optional - scilab ‘d =

44.’

get(var)

Get the value of the variable var.

EXAMPLES:

sage: scilab.eval('b=124;')                 # optional - scilab
''
sage: scilab.get('b')                       # optional - scilab
'

124.’

sage2scilab_matrix_string(A)

Return a Scilab matrix from a Sage matrix.

INPUT:
A Sage matrix with entries in the rationals or reals.
OUTPUT:
A string that evaluates to an Scilab matrix.

EXAMPLES:

sage: M33 = MatrixSpace(QQ,3,3)             # optional - scilab
sage: A = M33([1,2,3,4,5,6,7,8,0])          # optional - scilab
sage: scilab.sage2scilab_matrix_string(A)   # optional - scilab
'[1, 2, 3; 4, 5, 6; 7, 8, 0]'
set(var, value)

Set the variable var to the given value.

EXAMPLES:

sage: scilab.set('a', 123)        # optional - scilab
sage: scilab.get('a')               # optional - scilab
'

123.’

set_seed(seed=None)

Set the seed for gp interpreter.

The seed should be an integer.

EXAMPLES:

sage: from sage.interfaces.scilab import Scilab # optional - scilab
sage: s = Scilab() # optional - scilab
sage: s.set_seed(1) # optional - scilab
1
sage: [s.rand() for i in range(5)] # optional - scilab
[

     0.6040239,

     0.0079647,

     0.6643966,

     0.9832111,

     0.5321420]
version()

Returns the version of the Scilab software used.

EXAMPLES:

sage: scilab.version()                      # optional - scilab
'scilab-...'
whos(name=None, typ=None)

Returns information about current objects. Arguments: nam: first characters of selected names typ: name of selected Scilab variable type

EXAMPLES:

sage: scilab.whos("core")                   # optional - scilab
'Name                     Type           Size           Bytes...'
sage: scilab.whos(typ='function')           # optional - scilab
'Name                     Type           Size           Bytes...'
class sage.interfaces.scilab.ScilabElement(parent, value, is_name=False, name=None)

Bases: sage.interfaces.expect.ExpectElement

set(i, j, x)

Set the variable var to the given value.

EXAMPLES:

sage: scilab.set('c', 125)          # optional - scilab
sage: scilab.get('c')               # optional - scilab
'

125.’

sage.interfaces.scilab.scilab_console()

This requires that the optional Scilab program be installed and in your PATH, but no optional Sage packages need to be installed.

EXAMPLES:

sage: from sage.interfaces.scilab import scilab_console # optional - scilab
sage: scilab_console()                               # optional - scilab; not tested
        ___________________________________________
                       scilab-5.0.3

                 Consortium Scilab (DIGITEO)
               Copyright (c) 1989-2008 (INRIA)
               Copyright (c) 1989-2007 (ENPC)
        ___________________________________________


Startup execution:
  loading initial environment

-->2+3
ans  =

   5.

-->quit

Typing quit exits the Scilab console and returns you to Sage. Scilab, like Sage, remembers its history from one session to another.

sage.interfaces.scilab.scilab_version()

Return the version of Scilab installed.

EXAMPLES:

sage: from sage.interfaces.scilab import scilab_version # optional - scilab
sage: scilab_version()    # optional - scilab
'scilab-...'