Simple Example MATLAB function

Download Report

Transcript Simple Example MATLAB function

2
• 45% say scientists spend more time programming
than 5 years ago.
• 38% of scientists spend at least 1/5th of their time
programming.
• Codes often buggy, sometimes leading to papers
being retracted. Self-taught programmers.
• Monster codes, poorly documented, poorly tested,
and often used inappropriately.
3
FORTRAN
C/C++
Java
AspectJ
MATLAB
PERL
Python
Domain-specific
4
• started as an interface to standard FORTRAN
libraries for use by students.... but now
• 1 million MATLAB programmers in 2004, number
doubling every 1.5 to 2 years.
• over 1200 MATLAB/Simulink books
• used in many sciences and engineering
disciplines
5
6
MATLAB
FORTRAN
Why do scientists choose MATLAB?
7
8
9
No Types and “Flexible” Syntax ....
10
11
What if we extend
the language to
include typing
aspects?
12
1
2
3
4
5
6
7
function [ r ] = Ex1( n )
% Ex1(n) creates a vector of n values containing
% the values [ sin (1), sin (2), ..., sin(n)]
for i=1:n
r ( i ) = sin( i );
end
end
>> Ex1( 3)
ans = 0. 8415
0. 9093
>> Ex1( 2. 3)
ans = 0. 8415
0. 9093
0. 1411
13
>> Ex1( i nt 32( 3) )
??? Undef i ned f unct i on or met hod ' si n' f or i nput
ar gument s of t ype ' i nt 32' .
Er r or i n ==> Ex1 at 5
r ( i ) = si n( i ) ;
>> Ex1( ' c' )
??? For col on oper at or wi t h char oper ands, f i r st
and l ast oper ands must be char .
Er r or i n ==> Ex1 at 4
f or i =1: n
>> Ex1( @si n)
??? Undef i ned f unct i on or met hod ' _col onobj ' f or
i nput ar gument s of t ype ' f unct i on_handl e' .
Er r or i n ==> Ex1 at 4
f or i =1: n
14
>> Ex1( compl ex( 1, 2) )
War ni ng: Col on oper ands must be r eal scal ar s.
> I n Ex1 at 4
ans = 0. 8415
>> Ex1( t r ue)
War ni ng: Col on oper ands shoul d not be l ogi cal .
> I n Ex1 at 4
ans = 0. 8415
>> Ex1( [ 3, 4, 5] )
ans = 0. 8415
0. 9093
0. 1411
15
1
2
3
4
5
6
7
8
9
funct ion y = st ur m( X ,B C,F,G ,R )
% ST U R M Solve t he St ur m¡ L i ouvi l le equat i on:
% d( F ¤dY / dX ) / dX ¡ G ¤Y = R usi ng li near ¯ ni t e elem ent s.
% I N PU T :
% X ¡ a one¡ di m ensi onal gr i d¡ poi nt ar r ay of lengt h N .
% B C ¡ i s a 2 by 3 m at r i x [A 1, B 1, C1 ; A n, B n, Cn]
...
% A lex P let zer : plet zer @pppl.gov ( A ug. 97/ J uly 99) .
...
16
1
2
3
4
5
6
7
8
9
function [ r ] = Ex1( n )
% Ex1(n) creates a vector of n values containing
% the values [ sin (1), sin (2), ..., sin(n)]
atype(' n' , ' scalar of Float' );
for i=1:n
r ( i ) = sin( i );
end
atype(' r ' , ' array [n.value] of n.basetype' );
end
>> Ex1( 3)
ans = 0. 8415
0. 9093
0. 1411
>> Ex1( ' c' )
Type er r or i n Ex1. m, Li ne 4: Expect i ng ' n' t o have
t ype ' scal ar of f l oat ' , but got t he t ype
' scal ar of char ' .
17
any
dat a
fnhandle
ar r ay cellar r ay st r uct
18
ch ar
l ogi cal
i nt 8
si gn ed
i nt 32
i nt 64
i nt
*
u n si gn ed
r eal
i nt 16
u i nt 8
u i nt 16
u i nt 32
u i nt 64
° oat
si n gl e
d ou b l e
i nt 8:com p
nu m er i c
si gn ed :com p
i nt 32:com p
i nt 64:com p
i nt :com p
u i nt 8:com p
u n si gn :com p
com p l ex
i nt 16:com p
u i nt 16:com p
u i nt 32:com p
u i nt 64:com p
° oat :com p
si n gl e:com p
d ou b l e:com p
19
hatype-stmt i ::= at y p e `( ' `' ' hvarnamei `' ' `, ' `' ' htype-speci `' ' `) '
hvarnamei := = hidenti¯er i
j hidenti¯er-patterni
hidenti¯er-patterni := = `* '
j `* ' `[ a- zA- Z0- 9] +'
j `[ a- zA- Z] [ a- zA- Z0- 9] * ' `* '
htype-speci ::= any
j hdatai
j fnhandle
j hidenti¯er i . t y p e
hdatai ::= dat a
j harray-typei
j hcellarray-typei
j hstruct-typei
20
harray-typei ::= scal ar [of hbase-typei ]
j ar r ay [hdimsi ] [of hbase-typei ]
hbase-typei ::= `* '
j char
j l ogi cal
j hnumerici
j hidenti¯er i `. ' baset y p e
hdimsi ::= `[ ' hdim-list i `] '
j `[ ' `. . . ' `] '
j `[ ' `. . . ' `, ' hdim-list i `] '
j `[ ' hdim-list i `, ' `. . . ' `] '
j `[ ' hdim-list i `, ' `. . . ' `, ' hdim-list i `] '
j `[ ' hidenti¯er i `. ' dim s `] '
hdimi ::= `* '
j hinteger-literal i
j `<' hidenti¯er i `>'
j hidenti¯er i `. ' value
j hidenti¯er i `. ' hidenti¯er i
21
1
2
3
4
5
6
7
8
9
10
function [ r ] = foo( a, b, c, d )
atype(' a' , ' array [...] of int ' );
atype(' b' , ' array[¤,¤] ' );
atype(' c' , ' array [¤,¤,...]) of complex');
atype(' d' , ' scalar of uint32' );
% ...
% body of foo
% ...
atype(' r ' , ' array[a.dims] of int ' );
end
22
ch ar
l ogi cal
i nt 8
si gn ed
i nt 32
i nt 64
i nt
*
u n si gn ed
r eal
i nt 16
u i nt 8
u i nt 16
u i nt 32
u i nt 64
° oat
si n gl e
d ou b l e
i nt 8:com p
nu m er i c
si gn ed :com p
i nt 32:com p
i nt 64:com p
i nt :com p
u i nt 8:com p
u n si gn :com p
com p l ex
i nt 16:com p
u i nt 16:com p
u i nt 32:com p
u i nt 64:com p
° oat :com p
si n gl e:com p
d ou b l e:com p
23
1
2
3
4
5
6
7
function [ r ] = foo( a )
atype(' a' , ' any' );
% ...
% body of foo
% ...
atype(' r ' , ' a.type' );
end
• a.type
• a.value
• a.dims
• a.basetype
24
1
2
3
4
5
6
7
8
function [ r ] = foo( a, b )
atype(' a' , ' array[< n> ,< m> ] of real');
atype(' b' , ' array[a.m,< p> ] of a.basetype' );
% ...
% body of foo
% ...
atype(' r ' , ' array[a.m,b.p] of a.basetype' );
end
• <n> can be used as a dimension spec
• value of n is instantiated from the runtime
dimension
• repeated use in same atype statement
implies equality
25
ch ar
l ogi cal
i nt 8
si gn ed
i nt 32
i nt 64
i nt
*
u n si gn ed
r eal
i nt 16
u i nt 8
u i nt 16
u i nt 32
u i nt 64
° oat
si n gl e
d ou b l e
i nt 8:com p
nu m er i c
si gn ed :com p
i nt 32:com p
i nt 64:com p
i nt :com p
u i nt 8:com p
u n si gn :com p
com p l ex
i nt 16:com p
u i nt 16:com p
u i nt 32:com p
u i nt 64:com p
° oat :com p
si n gl e:com p
d ou b l e:com p
26
1
2
3
4
5
6
7
8
9
function [ r ] = foo( ca, f )
atype(' ca' , ' cellarray [< n> ,< m> ] of
struct with f x: double, y: double g' );
atype(' f ' , ' fnhandle' );
% ...
% body of foo
% ...
atype(' r ' , ' cellarray [ca.m,ca.n] of ca.basetype' );
end
27
28
29
Aspect Ideas
MATLAB-style
programming
30
31
32
• Complete implementation of typing aspects, both as a
library and via a weaver – hopefully summer 2011. (…
convince my students they are a good idea …)
• Release of three main tools: front-end/analysis
framework, McVM (Virtual Machine) and McFor (MATLAB
to FORTRAN). PLDI 2011 tutorial.
• Refactoring tools for MATLAB. How to help programmers
convert their programs to better structured, and more
efficient codes?
33
• MATLAB is an
important language.
• Many challenges,
types a key challenge.
• Types are dynamic, but
important for program
understanding and
performance.
• Typing aspects one
approach.
www.sable.mcgill.ca/mclab
34