Matlab - Object-Oriented Programming: Paul Schrimpf
Matlab - Object-Oriented Programming: Paul Schrimpf
Matlab - Object-Oriented Programming: Paul Schrimpf
Paul Schrimpf
Procedural
I Do this, then do that ...
I Most of what we have seen so far
Functional
I Operate on functions
I e.g. LISP, Scheme, Haskell
Object-oriented
I Focus on code reuse and reliability
I An object is data and methods to manipulate it
I Take components that are used repeatedly and share characteristics
and implement as a class
others ...
1 classdef seriesFn
2 properties % the data
3 order % order of series
4 coeff % coefficients
5 dim % dimension
6 powers % powers that go with coefficients
7 end
8 methods (Abstract=true) % these are methods that are only
9 % implemented in child classes
10 y = sval(f,x) % evaluate series
11 d = derivative(f) % create derivative
12 F = integral(f,lo,hi) % evaluate integral
13 c = mtimes(a,b) % multiplication
14 s = approxFn(s,fn,tol) % make s approximate fn
15 end
Paul Schrimpf () Matlab – Object-Oriented Programming January 14, 2009 6 / 15
Example: function series class – constructor
Constructors create a new object
Child classes can redefine their constructors (or any other method) if
they do not, they inherit their parent’s constructor
The following code would go inside a methods block inside classdef
seriesFn
1 function f = seriesFn(order,coeff,tol,dim)
2 f.order = order;
3 f.dim = dim;
4 f.powers = intVecsLessThan(f.order,f.dim)';
5 if (length(coeff)<size(f.powers,1))
6 warning('order=%d, but only %d coeff\n',order, ...
7 length(coeff));
8 end
9 f.coeff = coeff;
10 f.coeff(end+1:size(f.powers,2)) = 0;
11 f.coeff = reshape(f.coeff,1,numel(f.coeff));
12 end
1 classdef autodiff
2 properties
3 val % value
4 deriv % derivative, deriv(:,:,i) is dval / dx i
5 end % properties
6 methods
7 function dx = autodiff(x) % Constructor
8 % ... body omitted ...
9 end % function autodiff
10 % Accessors
11 function v = value(x)
12 v = x.val;
13 end
14 function d = diff(x)
15 d = x.deriv;
16 end
1 function c=mtimes(a,b)
2 c = autodiff([]);
3 if (isa(a,'autodiff'))
4 if (isa(b,'autodiff'))
5 c.val = a.val*b.val;
6 c.deriv = zeros([size(c.val) size(a.deriv,3)]);
7 for i=1:size(a.deriv,3)
8 c.deriv(:,:,i) = a.deriv(:,:,i) *b.val + ...
9 a.val*b.deriv(:,:,i);
10 end
11 else
12 c.val = a.val * b;
13 c.deriv = zeros([size(c.val) size(a.deriv,3)]);
14 for i=1:size(a.deriv,3)
15 c.deriv(:,:,i) = a.deriv(:,:,i)*b;
16 end
17 end
18 else
19 % ... other cases omitted ...
20 end % mtimes()
Paul Schrimpf () Matlab – Object-Oriented Programming January 14, 2009 13 / 15
Example: Forward AD – usage
1 Incorporate objects into the dynamic programming example from earlier. You might begin by making it use the
serisFn class described above.
2 Add to the autodiff class. It is incomplete. Many methods that work for double matrices have not been implemented.
Particularly important and easy methods that need to be implemented include size, ndims, length, and numel. For
motivation, you could try making the autodiff class work with some other code that you have written.
3 If the autodiff class was well designed, it would allow doing something like
x = autodiff(autodiff(x)) to compute 2nd and higher order derivatives. Does this work? If not,
try to make it work.
4 Design a class hierarchy for datasets. Every econometric program needs to deal with data. A well designed class should
make dealing with data easier.
5 Think about the patterns in the type of programs that you most often write, or expect to write. Design classes that will
help organize your programs.