Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1 Help
Browse help interactively
matlab doc
Octave help -i % browse with Info
Scilab help
R help.start()
Python help()
gnuplot help or ?
idl ?
Axiom )hd
Help on using help
matlab help help or doc doc
R help()
Python help
idl ?help
Axiom )help ?
Help for a function
matlab help plot
R help(plot) or ?plot
Python help(plot) or ?plot
gnuplot help plot or ?plot
idl ?plot or man,plot
Maxima describe(keyword)$
Maple ?keyword
Mathematica ?keyword
MuPAD ?keyword
Help for a toolbox/library package
matlab help splines or doc splines
R help(package=splines)
Python help(pylab)
Demonstration examples
matlab demo
Scilab demoplay();
R demo()
idl demo
Example using a function
R example(plot)
Maxima example(factor);
References: Hankin, Robin. R for Octave users (), available from http://cran.r-project.org/doc/contrib/R-and-octave-.txt
(accessed ..); Martelli, Alex. Python in a Nutshell (OReilly, ); Oliphant, Travis. Guide to NumPy (Trelgol, ); Hunter,
John. The Matplotlib Users Guide (), available from http://matplotlib.sf.net/ (accessed ..); Langtangen, Hans Petter. Python
Scripting for Computational Science (Springer, ); Ascher et al.: Numeric Python manual (), available from
http://numeric.scipy.org/numpy.pdf (accessed ..); Moler, Cleve. Numerical Computing with MATLAB (MathWorks, ),
available from http://www.mathworks.com/moler/ (accessed ..); Eaton, John W. Octave Quick Reference (); Merrit, Ethan.
Demo scripts for gnuplot version 4.0 (), available from http://gnuplot.sourceforge.net/demo/ (accessed ..); Woo, Alex.
Gnuplot Quick Reference (), available from http://www.gnuplot.info/docs/gpcard.pdf (accessed ..); Venables & Smith: An
Introduction to R (), available from http://cran.r-project.org/doc/manuals/R-intro.pdf (accessed ..); Short, Tom. R reference
card (), available from http://www.rpad.org/Rpad/R-refcard.pdf (accessed ..); Greenfield, Jedrzejewski & Laidler. Using
Python for Interactive Data Analysis (), pp., available from http://stsdas.stsci.edu/perry/pydatatut.pdf (accessed ..);
Brisson, Eric. Using IDL to Manipulate and Visualize Scientific Data, available from http://scv.bu.edu/Tutorials/IDL/ (accessed
..); Wester, Michael (ed). Computer Algebra Systems: A Practical Guide (), available from
http://www.math.unm.edu/wester/cas review.html (accessed ..).
MATLAB commands in numerical Python 2
Vidar Bronken Gundersen /mathesaurus.sf.net
2 Operators
Help on operator syntax
matlab help -
Scilab help symbols
R help(Syntax)
Addition
Generic a + b
matlab a + b
R a + b
Python a + b or add(a,b)
gnuplot a + b
idl a + b
bc a + b
Subtraction
Generic a - b
matlab a - b
R a - b
Python a - b or subtract(a,b)
gnuplot a - b
idl a - b
bc a - b
Multiplication
Generic a * b
matlab a * b
R a * b
Python a * b or multiply(a,b)
gnuplot a * b
idl a * b
bc a * b
Division
Generic a / b
matlab a / b
R a / b
Python a / b or divide(a,b)
gnuplot a / b
idl a / b
Power, ab
matlab a .^ b
R a ^ b
Python a ** b
power(a,b)
pow(a,b)
gnuplot a ** b
idl a ^ b
Axiom a**b
Maxima a^b or a**b
bc a ^ b
Remainder
matlab rem(a,b)
Scilab modulo(a,b)
R a %% b
Python a % b
remainder(a,b)
fmod(a,b)
gnuplot a % b
idl a MOD b
Axiom rem(a,b)
Maxima mod(a,b)
Maple a mod b
Mathematica Mod[a,b]
MuPAD a mod b
Derive MOD(a,b)
bc a % b
Integer division
R a %/% b
bc a / b
Increment, return new value
Octave ++a
idl ++a or a+=1
bc ++a
Increment, return old value
Octave a++
idl a++
bc a++
In place operation to save array creation overhead
Python a+=b or add(a,b,a)
Octave a+=1
idl a+=1
bc a+=b
Factorial, n!
matlab factorial(a)
R factorial(a)
Axiom factorial(a)
Maxima a!
Maple a!
MATLAB commands in numerical Python 4
Vidar Bronken Gundersen /mathesaurus.sf.net
Imaginary part
Generic imag(z)
matlab imag(z)
R Im(3+4i)
Python z.imag
idl imaginary(z)
gnuplot imag({3,4})
Maxima imagpart(z)
Argument
matlab arg(z)
R Arg(3+4i)
gnuplot arg({3,4})
Complex conjugate
Generic conj(z)
matlab conj(z)
R Conj(3+4i)
Python z.conj(); z.conjugate()
idl conj(z)
2.8 Trigonometry
Sine
Generic sin(a)
Cosine
Generic cos(a)
Tangent
Generic tan(a)
Arcsine
Generic asin(a) or arcsin(a)
Arccosine
Generic acos(a) or arccos(a)
Arctangent
Generic atan(a) or arctan(a)
Arctangent, arctan(b/a)
matlab atan(a,b)
R atan2(b,a)
Python atan2(b,a)
Hyperbolic sine
Generic sinh(a)
Hyperbolic cosine
Generic cosh(a)
Hyperbolic tangent
Generic tanh(a)
Hypotenus; Euclidean distance
p
x2 + y 2
Python hypot(x,y)
idl randomu(seed,[6,6])
Normal distribution
matlab randn(1,10)
Scilab rand(1,10,normal)
R rnorm(10)
Python random.standard_normal((10,))
3 Vectors
Row vector, 1 n-matrix
matlab a=[2 3 4 5];
R a <- c(2,3,4,5)
Python a=array([2,3,4,5])
idl a = [2, 3, 4, 5]
Column vector, m 1-matrix
matlab adash=[2 3 4 5];
R adash <- t(c(2,3,4,5))
Python array([2,3,4,5])[:,NewAxis]
array([2,3,4,5]).reshape(-1,1)
r_[1:10,c]
idl transpose([2,3,4,5])
3.1 Sequences
,,, ... ,
matlab 1:10
R seq(10) or 1:10
Python arange(1,11, dtype=Float)
range(1,11)
idl indgen(10)+1
dindgen(10)+1
.,.,., ... ,.
matlab 0:9
R seq(0,length=10)
Python arange(10.)
idl dindgen(10)
,,,
matlab 1:3:10
R seq(1,10,by=3)
Python arange(1,11,3)
idl indgen(4)*3+1
,,, ... ,
matlab 10:-1:1
R seq(10,1) or 10:1
Python arange(10,0,-1)
,,,
matlab 10:-3:1
R seq(from=10,to=1,by=-3)
Python arange(10,0,-3)
Linearly spaced vector of n= points
matlab linspace(1,10,7)
R seq(1,10,length=7)
Python linspace(1,10,7)
Reverse
matlab reverse(a)
Scilab a($:-1:1)
R rev(a)
Python a[::-1] or
idl reverse(a)
Set all values to same scalar value
matlab a(:) = 3
Python a.fill(3), a[:] = 3
matlab [1:4 a]
R c(1:4,a)
Python concatenate((range(1,5),a), axis=1)
idl [indgen(3)+1,a]
3.3 Repeating
,
matlab [a a]
R rep(a,times=2)
Python concatenate((a,a))
, ,
R rep(a,each=3)
Python a.repeat(3) or
MATLAB commands in numerical Python 9
Vidar Bronken Gundersen /mathesaurus.sf.net
, ,
R rep(a,a)
Python a.repeat(a) or
4 Matrices
Define a matrix
matlab a = [2 3;4 5]
R rbind(c(2,3),c(4,5))
array(c(2,3,4,5), dim=c(2,2))
Python a = array([[2,3],[4,5]]) h i
2 3
idl a = [[2,3],[4,5]]
4 5
Axiom a := matrix [[2,3],[4,5]]
Maxima matrix([2,3],[4,5])
Maple matrix([[2,3],[4,5]])
Mathematica {{2,3},{4,5}}
Derive [[2,3],[4,5]]
Diagonal
a11 a22 a33 a44
Python a.diagonal(offset=0)
4.6 Assignment
matlab a(:,1) = 99
R a[,1] <- 99
Python a[:,0] = 99
MATLAB commands in numerical Python 12
Vidar Bronken Gundersen /mathesaurus.sf.net
idl a>90
Clip upper and lower values
Python a.clip(min=2, max=5)
idl transpose(a)
Maxima transpose(a);
Non-conjugate transpose
matlab a. or transpose(a)
Python a.transpose()
Determinant
matlab det(a)
R det(a)
Python linalg.det(a) or determinant(a)
idl determ(a)
Axiom determinant a
Maxima determinant(a);
Inverse
matlab inv(a)
R solve(a)
Python linalg.inv(a) or inverse(a)
idl invert(a)
Axiom inverse a
Maxima invert(a),detout;
Pseudo-inverse
matlab pinv(a)
R ginv(a)
Python linalg.pinv(a)
Norms
matlab norm(a)
Python norm(a)
Eigenvalues
matlab eig(a)
Scilab spec(a)
R eigen(a)$values
Python linalg.eig(a)[0]
eigenvalues(a)
idl hqr(elmhes(a))
Mathematica Eigenvalues[matrix]
Axiom eigenvalues a
Singular values
matlab svd(a)
R svd(a)$d
Python linalg.svd(a)
singular_value_decomposition(a)
idl svdc,A,w,U,V
Mathematica SingularValueDecomposition[m]
Cholesky factorization
matlab chol(a)
Python linalg.cholesky(a)
Eigenvectors
matlab [v,l] = eig(a)
Scilab [v,l] = spec(a)
R eigen(a)$vectors
Python linalg.eig(a)[1]
eigenvectors(a)
Axiom eigenvectors a
Rank
matlab rank(a)
R rank(a)
Python rank(a)
MATLAB commands in numerical Python 13
Vidar Bronken Gundersen /mathesaurus.sf.net
4.8 Sum
Sum of each column
matlab sum(a)
Scilab sum(a,c)
R apply(a,2,sum)
Python a.sum(axis=0)
idl total(a,2)
Sum of each row
matlab sum(a)
Scilab sum(a,r)
R apply(a,1,sum)
Python a.sum(axis=1)
idl total(a,1)
Sum of all elements
matlab sum(sum(a))
Scilab sum(a)
R sum(a)
Python a.sum()
idl total(a)
Sum along diagonal
Python a.trace(offset=0)
Cumulative sum (columns)
matlab cumsum(a)
R apply(a,2,cumsum)
Python a.cumsum(axis=0)
4.9 Sorting
Example data 4 3 2
matlab a = [ 4 3 2 ; 2 8 6 ; 1 4 7 ] 2 8 6
Python a = array([[4,3,2],[2,8,6],[1,4,7]]) 1 4 7
Flat and sorted
matlab sort(a(:)) 1 2 2
Scilab s=sort(a(:)); s($:-1:1) 3 4 4
R t(sort(a)) 6 7 8
Python a.ravel().sort() or
Sort each column
matlab sort(a)
1 3 2
Scilab s=sort(a,r); s($:-1:1,:)
2 4 6
R apply(a,2,sort)
4 8 7
Python a.sort(axis=0) or msort(a)
idl sort(a)
Sort each row
matlab sort(a) 2 3 4
Scilab s=sort(a,c); s(:,$:-1:1) 2 6 8
R t(apply(a,1,sort)) 1 4 7
Python a.sort(axis=1)
Sort rows (by first row) 1 4 7
matlab sortrows(a,1) 2 8 6
Python a[a[:,0].argsort(),] 4 3 2
Sort, return indices
R order(a)
Python a.ravel().argsort()
Sort each column, return indices
Python a.argsort(axis=0)
Sort each row, return indices
Python a.argsort(axis=1)
return indices, i
matlab [v i] = max(a)
Scilab [v,i] = max(a,c)
R i <- apply(a,1,which.max)
pairwise max
matlab max(b,c)
Python maximum(b,c)
R pmax(b,c)
matlab cummax(a)
R apply(a,2,cummax)
max-to-min range
Python a.ptp(); a.ptp(0)
Kronecker product
1 2 2 4
" #
matlab kron(a,b)
3 4 6 8
Scilab kron(a,b) or a .*. b
3 6 4 8
R kronecker(a,b)
9 12 12 16
Python kron(a,b)
Matrix division, ba1
matlab a / b
Left matrix division, b1 a
(solve linear equations)
matlab a \ b
Scilab linsolve(a,b)
Ax = b
R solve(a,b)
Python linalg.solve(a,b)
solve_linear_equations(a,b)
idl cramer(a,b)
Vector dot product
Python vdot(a,b)
Cross product
Python cross(a,b)
Replace values
Python a.put(2,indices)
5 Multi-way arrays
Define a -way array
matlab a = cat(3, [1 2; 1 2],[3 4; 3 4]);
Python a = array([[[1,2],[1,2]], [[3,4],[3,4]]])
matlab a(1,:,:)
Python a[0,...]
7 Plotting
7.1 Basic x-y plots
4
d line plot 2
matlab plot(a) 1
R plot(a, type="l") 0
Python plot(a) -1
-2
idl plot, a
-3
-4
0 20 40 60 80 100
4.5
R plot(x[,1],x[,2])
3.0
Python plot(x[:,0],x[:,1],o)
idl plot, x(1,*), x(2,*) 2.5
2.0
4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0
1
4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0
MATLAB commands in numerical Python 17
Vidar Bronken Gundersen /mathesaurus.sf.net
7.1.4 Functions
Defining functions
matlab f = inline(sin(x/3) - cos(x/5)) x
x
f (x) = sin 3 cos 5
Scilab deff(y = f(x),y = sin(x/3) - cos(x/5))
R f <- function(x) sin(x/3) - cos(x/5)
Plot a function for given range
matlab ezplot(f,[0,40])
fplot(sin(x/3) - cos(x/5),[0,40])
1.0
Octave
% no ezplot
0.5
Scilab
fplot2d([0:.5:40],f)
0.0
f (x)
0.5
Python
x = arrayrange(0,40,.5)
1.0
y = sin(x/3) - cos(x/5)
1.5
plot(x,y, o)
2.0
x
plot sin(x/3) - cos(x/5) with points
Axiom draw( sin(x/3) - cos(x/5), x=0..40 )
135 45
270
matlab hist(randn(1000,1))
R hist(rnorm(1000))
idl plot, histogram(randomn(5,1000))
matlab plot(sort(a))
R plot(apply(a,1,sort),type="l")
MATLAB commands in numerical Python 19
Vidar Bronken Gundersen /mathesaurus.sf.net
7.4 3d data
7.4.1 Contour and image plots
Contour plot
matlab contour(z) 2
0.0
R contour(z) 1
0.2
Python
0.8
levels, colls = contour(Z, V, 6
0.4
0.
0.6
-0.4
-0.2
-0.6
0
0.8
1.0
origin=lower, extent=(-3,3,-3,3))
-1
clabel(colls, levels, inline=1, -0.2
fmt=%1.1f, fontsize=10) -2
idl contour, z -2 -1 0 1 2
nlevels=7, color=gray.colors) 1
Python contourf(Z, V, 0
cmap=cm.gray,
-1
origin=lower,
extent=(-3,3,-3,3)) -2
0.0
1
0.2
Image with contours
0.8
6
0.4
0.
0.6
-0.4
-0.2
-0.6
0
0.8
1.0
Python # imshow() and contour() as above
-1
-0.2
-2
-2 -1 0 1 2
matlab n=-2:.1:2;
[x,y] = meshgrid(n,n);
z=x.*exp(-x.^2-y.^2);
Python n=arrayrange(-2,2,.1) 2 y 2
f (x, y) = xex
[x,y] = meshgrid(n,n)
z = x*power(math.e,-x**2-y**2)
R f <- function(x,y) x*exp(-x^2-y^2)
n <- seq(-2,2, length=40)
z <- outer(n,n,f)
Mesh plot
0.4
matlab mesh(z)
0.2
R persp(x,y,z,
0.0
z
ticktype=detailed) 0.4
1
2
Python 0
y
idl surface, z x
0 1
2
2
Surface plot
matlab surf(x,y,z) or surfl(x,y,z)
Octave % no surfl() 0.4
R persp(x,y,z, 0.2
0.0
z
1
col=lightblue, shade=0.75, ltheta=120, 0.4
2
0
ticktype=detailed)
y
0
idl shade_surf, z x
1
1
loadct,3 2
2
MATLAB commands in numerical Python 20
Vidar Bronken Gundersen /mathesaurus.sf.net
icc-gamut.csv
d scatter plot
matlab plot3(x,y,z,k+) 80
60
40
20
R cloud(z~x*y) 0
-20
-40
-60 90
100
-80 80
gnuplot splot icc-gamut.csv 80 60
70
60 50
40 40
20 30
0
20
-20
-40 10
-60 0
8 Data analysis
8.1 Set membership operators
Create sets
matlab a = [ 1 2 2 5 2 ];
b = [ 2 3 4 ];
R a <- c(1,2,2,5,2)
b <- c(2,3,4)
Python a = array([1,2,2,5,2])
b = array([2,3,4])
a = set([1,2,2,5,2])
b = set([2,3,4])
Set unique
matlab unique(a)
R unique(a)
Python unique1d(a) 1 2 5
unique(a)
set(a)
Maxima setify(a)
Set union
matlab union(a,b)
R union(a,b)
Python union1d(a,b)
a.union(b)
Maxima union(a,b)
MATLAB commands in numerical Python 21
Vidar Bronken Gundersen /mathesaurus.sf.net
Set intersection
matlab intersect(a,b)
R intersect(a,b)
Python intersect1d(a)
a.intersection(b)
Maxima intersect(a,b)
Set difference
matlab setdiff(a,b)
R setdiff(a,b)
Python setdiff1d(a,b)
a.difference(b)
Maxima setdifference(a,b)
complement(b,a)
Set exclusion
matlab setxor(a,b)
R setdiff(union(a,b),intersect(a,b))
Python setxor1d(a,b)
a.symmetric_difference(b)
True for set member
matlab ismember(2,a)
R is.element(2,a) or 2 %in% a
Python 2 in a
setmember1d(2,a)
contains(a,2)
8.2 Statistics
Average
matlab mean(a)
R apply(a,2,mean)
Python a.mean(axis=0)
mean(a [,axis=0])
idl mean(a)
Axiom mean a
Median
matlab median(a)
R apply(a,2,median)
Python median(a) or median(a [,axis=0])
idl median(a)
Axiom median(a)
Standard deviation
matlab std(a)
R apply(a,2,sd)
Python a.std(axis=0) or std(a [,axis=0])
idl stddev(a)
Variance
matlab var(a)
R apply(a,2,var)
Python a.var(axis=0) or var(a)
idl variance(a)
Correlation coefficient
matlab corr(x,y)
R cor(x,y)
Python correlate(x,y) or corrcoef(x,y)
idl correlate(x,y)
Covariance
matlab cov(x,y)
R cov(x,y)
Python cov(x,y)
Laplace transform
Axiom laplace(e,t,s)
10 Programming
Script file extension
matlab .m
Scilab .sce
R .R
Python .py
gnuplot .gp or .plt
idl .idlbatch
Maxima .mc or .mac
bc bc
Comment symbol (rest of line)
matlab %
Octave % or #
Scilab //
R #
Python #
gnuplot #
idl ;
Axiom --
Mathematica (* .. *)
Maple #
Maxima /* .. */
MuPAD //
/* .. */
# .. #
Derive ".."
reduce %
bc /* .. */
Import library functions
matlab % must be in MATLABPATH
Octave % must be in LOADPATH
Scilab getf(foo.sci)
R library(RSvgDevice)
Python from pylab import *
Maxima load(SET);
Eval
matlab string=a=234;
eval(string)
Scilab eval()
evstr()
execstr()
R string <- "a <- 234"
eval(parse(text=string))
Python string="a=234"
eval(string)
10.1 Loops
for-statement
matlab for i=1:5; disp(i); end
R for(i in 1:5) print(i)
Python for i in range(1,6): print(i)
idl for k=1,5 do print,k
Multiline for statements
matlab for i=1:5
disp(i)
disp(i*2)
end
R for(i in 1:5) {
print(i)
print(i*2)
}
Python for i in range(1,6):
print(i)
print(i*2)
idl for k=1,5 do begin $
print, i &$
print, i*2 &$
end
10.2 Conditionals
if-statement
matlab if 1>0 a=100; end
R if (1>0) a <- 100
Python if 1>0: a=100
idl if 1 gt 0 then a=100
if-else-statement
matlab if 1>0 a=100; else a=0; end
idl if 1 gt 0 then a=100 else a=0
MATLAB commands in numerical Python 24
Vidar Bronken Gundersen /mathesaurus.sf.net
10.3 Debugging
Most recent evaluated expression
matlab ans
R .Last.value
Axiom %
Maxima %
List variables loaded into memory
matlab whos or who
R objects()
idl help
Clear variable x from memory
matlab clear x or clear [all]
R rm(x)
Axiom )clear properties x
Print
matlab disp(a)
R print(a)
Python print a
idl print, a
This document is still draft quality. Most d plot examples are made using Matplotlib, and d plots using R or Gnuplot.
Version number and download url for software used: Python .., http://www.python.org/; NumPy .., http://numeric.scipy.org/;
Matplotlib ., http://matplotlib.sf.net/; IPython .., http://ipython.scipy.org/; R .., http://www.r-project.org/; Octave ..,
http://www.octave.org/; Scilab ., http://www.scilab.org/; Gnuplot ., http://www.gnuplot.info/; Maxima .., http://maxima.sf.net/.
For referencing: Gundersen, Vidar Bronken. MATLAB commands in numerical Python (Oslo/Norway, ), available from:
http://mathesaurus.sf.net/
Contributions are appreciated: The best way to do this is to edit the xml and submit patches to our tracker or forums.