Exploiting Recent Developments in MATLAB - School of Mathematics

0 downloads 113 Views 694KB Size Report
1997, MATLAB 5: Object oriented (OO) programming. 2008, MATLAB R2008a: enhanced ... Blog post comments: majority in favo
Exploiting Recent Developments Research Matters in MATLAB February 25, 2009 Nick Higham Nick Higham School of Mathematics Director of Research The University of Manchester

School of Mathematics

http://www.maths.manchester.ac.uk/~higham @nhigham, nickhigham.wordpress.com 2017 Joint Mathematics Meetings, Atlanta MAA Session on Innovative and Effective Ways to Teach Linear Algebra, I 1/6

MATLAB and Me Used for research starting with 1984 “Gatlinburg” Fortran version. Used for teaching from around 1986. Still my language of choice for research and teaching. The 6-monthly release cycle has made it hard to keep up with what’s new. For a period in the 2000s there wasn’t much new as regards core math. These slides available at http://bit.ly/2je4nJI.

Nick Higham

Exploiting Recent Developments in MATLAB

2 / 14

MATLAB Guide 3rd Edition, 2017

Nick Higham

Exploiting Recent Developments in MATLAB

3 / 14

New Classes 1997, MATLAB 5: Object oriented (OO) programming. 2008, MATLAB R2008a: enhanced OO programming. Building on the new OO system in last four years: updated graphics system (R2014b), graph and digraph classes (R2015b), table data type and categorical arrays (R2013b), datastores (R2014b), tall arrays (R2016b).

Nick Higham

Exploiting Recent Developments in MATLAB

4 / 14

Scalar Expansion >> A = spiral(2), B = A - 1 A = 1 2 4 3 B = 0 1 3 2

Nick Higham

Exploiting Recent Developments in MATLAB

5 / 14

Scalar Expansion >> A = spiral(2), B = A - 1 A = 1 2 4 3 B = 0 1 3 2 Implicit expansion takes this idea further . . .

Nick Higham

Exploiting Recent Developments in MATLAB

5 / 14

Implicit Expansion (R2016b) >> A = ones(2), B = A + [1 5] A = 1 1 1 1 B = 2 6 2 6 >> A = ones(2) + [1 5]’ A = 2 2 6 6

Nick Higham

Exploiting Recent Developments in MATLAB

6 / 14

Subtracting the Mean R2016b Release Notes mention A - mean(A). >> A = [1 4; 3 2], A - mean(A) A = 1 4 3 2 ans = -1 1 1 -1 Using bsxfun (from R2007a): >> bsxfun(@minus,A,mean(A)) ans = -1 1 1 -1

Nick Higham

Exploiting Recent Developments in MATLAB

7 / 14

Linear Algebra Rules “Matrix plus vector” and “row vector plus column vector” are now legal operations! >> [1 2] + [3 4]’ ans = 4 5 5 6 Concerns: Students may “learn” incorrect linear algebra rules from MATLAB. Codes with bugs may run and be hard to debug.

Nick Higham

Exploiting Recent Developments in MATLAB

8 / 14

Maybe It’s OK? Cleve Moler first proposed it 14 years ago. MathWorks trialled it in internal development builds and in R2016a Prereleases (but not R2016a itself). NumPy had it for > 16 years, and calls it “broadcasting”. GNU Octave has had “broadcasting” since version 3.6.0 (2012). R has it.

Nick Higham

Exploiting Recent Developments in MATLAB

9 / 14

Reaction One blog post by me

, two at Mathworks

,

.

Blog post comments: majority in favor; some wanted different syntax. No reports of broken code, except with try/catch. Little feedback yet from those teaching linear algebra.

Nick Higham

Exploiting Recent Developments in MATLAB

10 / 14

Min Manipulations Form matrix with aij = min(i, j): >> d = 1:4; A = min(d,d’) A = 1 1 1 1 1 2 2 2 1 2 3 3 1 2 3 4

Nick Higham

Exploiting Recent Developments in MATLAB

11 / 14

Multiplication by a Diagonal Matrix(1) >> A = ones(3); d = [1 1e-4 1e-8]; >> A.*d % A*diag(d) ans = 1.0000e+00 1.0000e-04 1.0000e-08 1.0000e+00 1.0000e-04 1.0000e-08 1.0000e+00 1.0000e-04 1.0000e-08 >> A.*d’ % diag(d)*A ans = 1.0000e+00 1.0000e+00 1.0000e-04 1.0000e-04 1.0000e-08 1.0000e-08

Nick Higham

1.0000e+00 1.0000e-04 1.0000e-08

Exploiting Recent Developments in MATLAB

12 / 14

Multiplication by a Diagonal Matrix(2) >> A./d ans =

% A/diag(d) or A*inv(diag(d)) 1 1 1

10000 10000 10000

Nick Higham

100000000 100000000 100000000

Exploiting Recent Developments in MATLAB

13 / 14

Conclusions Implicit expansion allows elegant code and brings improved efficiency without the need for calling bsxfun. No compatibility issues with existing codes. Can it cause problems in linear algebra teaching? Time will tell: no reports of it doing so yet.

Join MathWorks and SIAM to celebrate publication of MATLAB Guide 3ed: SIAM booth (#139), 11am today.

Nick Higham

Exploiting Recent Developments in MATLAB

14 / 14