Numerical Computing: Scilab
Numerical Computing: Scilab
Using
SCILAB
by
T K Rajan, Govt Victoria College, Palakkad
What is SciLab
• A programming language with a rich collection of numerical algorithms
• An Interpreted Language
• Handles various Data Types and user defined ones
• Dynamically compiles and link other languages like FORTRAN, C etc.
• It is a free software with source code
• It focuses on many areas of scientific computing covering algebra, calculus,
number theory, signal processing, statistic etc.
• It provides many graphic features like 2D, 3D plotting
• In SciLab everything is a matrix
• It is a case sensitive language
Where to get SciLab
Scilab binaries can be downloaded directly from the Scilab
homepage
http://www.scilab.org
http://www.scilab.org/download
Starting and exiting SciLab
Double Click SciLab Shortcut
scilab-5.2.2
Consortium Scilab (DIGITEO)
Copyright (c) 1989-2010 (INRIA)
Copyright (c) 1989-2007 (ENPC)
________________________________________
Startup execution:
loading initial environment
-->help
-->help sqrt
<Tab> key provides intelli sense.
i.e. press Tab key after typing a letter or a word, a window of associated functions will popup
Simple data types
Numbers
Integer, floating-point, complex!
Strings
characters are strings of length 1
Booleans are T or F
Creating Real variables
• No need to declare a variable
• Variables are created when they are set
• The = symbol is used for assigning a value to
a variable
-->x=3
-->y=x*2
-->a=[1 2 3]
-->name=“Rajan”
Simple data types: operators
• + addition
• - substraction
• * multiplication
• / right division i.e. x/y
• \ left division i.e. x\y
• ^ power i.e. xy
• ** power (same as ^)
• ' transpose conjugate
Arrays and Matrices
Array:
-->a = [1, 2, 3, 4, 5]
(creates a one dimensional array and stored in a)
Matrix:
-->b=[1,2,3;2,3,4;3,4,5]
(creates a 3X3 matrix and stored in B)
-->c=[1;2;3]
(creates a column matrix)
Compound data types (2)
Dictionaries:
like an array indexed by a string
d = { "foo" : 1, "bar" : 2 }
print d["bar"] # 2
some_dict = {}
some_dict["foo"] = "yow!"
print some_dict.keys() # ["foo"]
Compound data types (3)
Tuples:
a = (1, 2, 3, 4, 5)
print a[1] # 2
empty_tuple = ()
lists vs. tuples:
lists are mutable; tuples are immutable
lists can expand, tuples can’t
tuples slightly faster
Compound data types (3)
Objects:
class thingy:
# next week’s lecture
t = thingy()
t.method()
print t.field
Built-in data structures (lists, dictionaries)
are also objects
though internal representation is different
Control flow (1)
if, if/else, if/elif/else
if a == 0:
print "zero!"
elif a < 0:
print "negative!"
else:
print "positive!"
Control flow (2)
Notes:
blocks delimited by indentation!
colon (:) used at end of lines
containing control flow keywords
Control flow (3)
while loops
a = 10
while a > 0:
print a
a -= 1
Control flow (4)
for loops
for a in range(10):
print a
a = [3, 1, 4, 1, 5, 9]
for i in range(len(a)):
print a[i]
Control flow (6)
Common while loop idiom:
f = open(filename, "r")
while True:
line = f.readline()
if not line:
break
# do something with line
aside: open() and file()
These are identical:
f = open(filename, "r")
f = file(filename, "r")
The open() version is older
The file() version is the recommended
way to open a file now
uses object constructor syntax (next lecture)
aside 2: file iteration
Instead of using while loop to iterate
through file, can write:
if a == 0:
pass # do nothing
else:
# whatever
Defining functions
def foo(x):
y = 10 * x + 2
return y
All variables are local unless
specified as global
Arguments passed by value
Executing functions
def foo(x):
y = 10 * x + 2
return y
i = 10
d = 3.1415926
s = "I am a string!"
print "%d\t%f\t%s" % (i, d, s)
print "no newline",
Modules (1)
Access other code by importing modules
import math
print math.sqrt(2.0)
or:
from math import sqrt
print sqrt(2.0)
Modules (2)
or:
from math import *
print sqrt(2.0)
Can import multiple modules on one line:
import sys, string, math
Only one "from x import y" per line
Modules (3)
NOTE!
from some_module import *
should be avoided
dumps all names from some_module into
local namespace
easy to get inadvertent name conflicts this way
Modules (4)
f = file("foo", "r")
line = f.readline()
print line,
f.close()
# Can use sys.stdin as input;
# Can use sys.stdout as output.