E177, Reference/Assignment for Classes: Intro February 12, 2008 http://jagger.me.berkeley.edu/~pack/e177 Copyright 2005-2008, Andy Packard. This work.

Slides:



Advertisements
Similar presentations
Solving ODEs UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Advertisements

Functions UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Numerical Integration UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the.
Time Complexity UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Simulink SubSystems and Masking April 22, Copyright , Andy Packard. This work is licensed under the.
Cell Arrays UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Recursion and Induction UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
Sorting UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Script Files UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Objects and Classes UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Numerical Differentiation UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under.
IEEE Arithmetic UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Searching UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Struct Arrays UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Polynomials UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Basics of Matlab UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Intro to Simulink April 15, Copyright , Andy Packard. This work is licensed under the Creative Commons.
Inheritance in Matlab Classes E177 February 14, Copyright , Andy Packard. This work is licensed.
Root Finding UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Get/Set Methods UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Intro to Simulink Modified by Gary Balas 20 Feb 2011 Copyright , Andy Packard. This work is licensed under.
Introduction to CMex E177 April 25, Copyright 2005, Andy Packard. This work is licensed under the Creative.
Object-Oriented Programming (OOP) Lecture No. 16
Operator Overloading Introduction
OOP: Creating a Class Topics More OOP concepts
User-Written Functions
APPENDIX a WRITING SUBROUTINES IN C
Introduction to Java E177 April 29, me. berkeley
Announcements Midterm2 Grades to be announced THIS WEEK
Announcements Final Exam will be on August 19 at 16:00
CS 326 Programming Languages, Concepts and Implementation
EGR 2261 Unit 4 Control Structures I: Selection
EGR 115 Introduction to Computing for Engineers
Assignment and Arithmetic expressions
For/Switch/While/Try UC Berkeley Fall 2004, E77 me
Introduction to Programming for Mechanical Engineers (ME 319)
Lists in Lisp and Scheme
Object-Oriented Programming (OOP) Lecture No. 16
The dirty secrets of objects
Reusable Graphics Objects UC Berkeley Fall 2004, E77 me
More Selections BIS1523 – Lecture 9.
Class Info E177 January 22, me. berkeley
Bases and Representations, Memory, Pointers, Arrays, For-Loops
Conditions and Ifs BIS1523 – Lecture 8.
PROGRAMMING IN HASKELL
Lecture 22 Inheritance Richard Gesick.
Matlab tutorial course
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
E177, Graphics Objects in Matlab February 26, me
The switch statement: an alternative to writing a lot of conditionals
@ReferAssignGetSet February 21, me. berkeley
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Class Intro/TDD Intro 8/23/2005
Programming in C# CHAPTER - 7
CISC/CMPE320 - Prof. McLeod
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Object-Oriented Programming Using C++ Second Edition
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Announcements Final will be NEXT WEEK on August 17
Compiler Construction
Function Handles UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Data Structures and Algorithms Introduction to Pointers
Java Programming Language
Arrays in Matlab UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Announcements Program 1 due noon Lab 1 due noon
EE 194/BIO 196: Modeling biological systems
Compiler Construction
Basics of Matlab UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Presentation transcript:

E177, Reference/Assignment for Classes: Intro February 12, 2008 http://jagger.me.berkeley.edu/~pack/e177 Copyright 2005-2008, Andy Packard. This work is licensed under the Creative Commons Attribution-ShareAlike License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

References If M is a Matlab object, then LHS = expr involving M.NameReference first calls a method named subsref in the methods folder for class(M)to evaluate M.NameReference If no such method exists, then the built-in subsref is called, which works as expected if M is a struct generates an error if M is a user-defined class, a double, a cell, a char, etc… Generally, for user-defined classes, a subsref method must be written. In the case above, the execution is actually L.type = ’.’ % scalar char L.subs = ’NameReference’ LHS = expr involving subsref(M,L)

Deeper Reference If M is a Matlab object, then LHS = expr involving M.Name1.Name2 generates a call to subsref equivalent to L(1).type = ’.’ L(1).subs = ’Name1’ L(2).type = ’.’ L(2).subs = ’Name2’ LHS = expr involving subsref(M,L) Note: The second argument is, in general, a non-scalar struct array. The length of the array indicates the depth of the reference.

Conventions for reference behavior If M is a Matlab object, then a reference of the form M.Name1 is the same as get(M,’Name1’) If M is a Matlab variable, then a reference of the form M.Name1.Name2 should be equivalent to breaking it into 2 steps, first tmp = M.Name1 followed by a reference of the form tmp.Name2 More generally, a reference of the form M.Name1.Name2.Name3.Name4.Name5 tmp.Name2.Name3.Name4.Name5 recursive

Psuedo-code for 1st subsref function B = subsref(A,L) switch L(1).type case ’.’ B = get(A,L(1).subs); % like get otherwise % some other cases we will learn about end % Now do the remaining references recursively if length(L)>1 B = subsref(B,L(2:end));

type: Other references If M is a Matlab object, then LHS = expr M(IdxA,IdxB).Name2{IdxC,IdxD} The complex reference of M generates a call to subsref equivalent to L(1).type = ’()’ L(1).subs = [{IdxA} {IdxB}] % 1x2 cell L(2).type = ’.’ L(2).subs = ’Name2’ L(3).type = ’{}’ L(3).subs = [{IdxC} {IdxD}] % 1x2 cell subsref(M,L)

Psuedo-code for 1st subsref function B = subsref(A,L) switch L(1).type case ’.’ B = get(A,L(1).subs); case ’()’ % code to handle () case ’{}’ % code to handle {} end if length(L)>1 B = subsref(B,L(2:end));

1st Exception/rule Consider a class @userclass. Suppose the methods folder is as shown. Inside method1.m, the behavior for subsref-like operations is as follows: userclass.m subsref.m method1.m ... @userclass Suppose A is an object of class userclass. Then any expression of the form A.name, A(idx), etc, will treat A as a struct (calling the @struct/subsref method which we know about). Note that the existing userclass/subsref will not be called. The only manner in which to call the existing userclass/subsref from within method1 is a direct call, for example L.type = ’()’; L.subs = {[1 3 5] [2 3 6]}; refA = subsref(A,L)

Concatenation If A and B are Matlab variables, then [A B] calls the method horzcat(A,B) in the methods folder for the higher precedence class of A and B If no such method exists, then the built-in horzcat is called. In the 2nd case, if A and B are objects of the same user-defined class, then the result is of the same class, with the underlying struct having grown via the concatenation. A = e177poly([2 4 6]); size(A) B = e177poly([8 10]); size(B) C = [A B]; size(C), class(C) struct(C), size(struct(C)) If subsref has not been defined for the class, then the built-in subsref does array referencing as expected isequal(C(1),A) If subsref has been defined (to handle a .Name reference, say) then it needs to explicitly handle () reference, but that can be easy…

Psuedo-code for 2nd subsref function B = subsref(A,L) switch L(1).type case ’.’ B = get(A,L(1).subs); case ’()’ B = builtin('subsref',A,L(1)); otherwise % must still handle ’{}’ case end if length(L)>1 B = subsref(B,L(2:end)); Let builtin handle array reference

Concatenation, e177poly Since concatenation (ie., stacking) automatically works (by default), it is easy to create arrays of polynomials. Consequently, The methods (eg., plus.m, mtimes.m, etc) need to be written in a manner to handle these. Natural to treat as matrices of polynomials, and define the appropriate matrix operations. Some work to do… In the methods, the e177poly object is a struct array, with fields coeff and symbol. As usual, need to deal with the fact that symbols might not be the same. Just make simple conventions, and follow them. At this point, note that most of the methods don’t properly account for arrays of polynomials…

Concatenation/Reference For some classes, the built-in array concatenation and the built-in array referencing will not be the desired behavior. In those cases, there needs to be tight coordination between the horzcat, vertcat, and cat subsref and subsasgn get and set methods of the class. We will address this in next few lecture and homeworks.

Assignments If M is a Matlab object, then M.NameReference = RHS calls a method named subsasgn in the methods folder for class(M). If no such method exists, then the built-in subsasgn is called, which works as expected if M is a struct generates an error if M is a user-defined class, a double, a cell, a char, etc… Generally, for user-defined classes, a subsasgn method must be written. In the case above, the call to subsasgn is equivalent to L.type = ’.’ % scalar char L.subs = ’NameReference’ M = subsasgn(M,L,RHS)

Deeper Assignment A deeper assignment, for example M.Name1.Name2 = RHS generates a call to subsasgn equivalent to L(1).type = ’.’ L(1).subs = ’Name1’ L(2).type = ’.’ L(2).subs = ’Name2’ M = subsasgn(M,L,RHS) The second argument is, in general, a non-scalar struct array. The length of the array indicates the depth of the assignment.

Conventions for assignment behavior If M is a Matlab variable, then an assignment of the form M.Name1 = RS is the same as set(M,’Name1’,RS) Fill in the details for the M.Name1.Name2 = RS case yourself More generally, an assignment of the form M.Name1.Name2.Name3.Name4.Name5 = RS should be equivalent to breaking it into 2 steps, first tmp = M.Name1 % subsref followed by two assignments tmp.Name2.Name3.Name4.Name5 = RS; % subsasgn M.Name1 = tmp; % asgn/set

Psuedo-code for 1st subsasgn function B = subsasgn(A,L,RHS) switch L(1).type case '.' if length(L)==1 B = set(A,L(1).subs,RHS); else tmp = subsref(A,L(1)); tmp = subsasgn(tmp,L(2:end),RHS); B = set(A,L(1).subs,tmp); end otherwise % some other cases still to do

type: Other assignments If M is a Matlab object, then M(IdxA,IdxB).Name2{IdxC,IdxD} = RHS generates a call to subsasgn equivalent to L(1).type = ’()’ L(1).subs = [{IdxA} {IdxB}] % 1x2 cell L(2).type = ’.’ L(2).subs = ’Name2’ L(3).type = ’{}’ L(3).subs = [{IdxC} {IdxD}] % 1x2 cell M = subsasgn(M,L,RHS) Precedence of M and RHS determines which subsasgn gets called, ... Usually...

Additional Exceptions If A is a cell, then A{Idx} = RHS always calls the cell version of subsasgn regardless of the class of RHS. However, if RHS is superior to cell, then a direct call to subsasgn, of the form L.Type = ’{}’; L.subs = [{Idx}]; A = subsasgn(A,L,RHS) will dispatch following the precedence rules directly. Similarly, if B is a struct, then B.fieldname = RHS always calls the builtin version of subsref (ie., for struct array) regardless of the class of RHS

Psuedo-code for 1st subsasgn function B = subsasgn(A,L) switch L(1).type case ’.’ % % as we had two slides ago case ’()’ % code to handle () case ’{}’ % code to handle {} end

Concatenation (recall) If A and B are Matlab variables, then [A B] calls the method horzcat(A,B) in the methods folder for the higher precedence class of A and B If no such method exists, then the built-in horzcat is called. In the 2nd case, if A and B are objects of a user-defined class, then the result is of the same class, with the underlying struct having grown via the concatenation. A = e177poly([2 4 6]); size(A) B = e177poly([8 10]); size(B) C = [A B]; size(C), class(C) struct(C), size(struct(C)) If subsasgn has not been defined for the class, then the built-in version does array assignment as expected C(2) = A*B; isequal(C(2),A*B) If subsasgn has been defined, then it needs to explicitly handle () assignment, but that can be easy…

Psuedo-code for 2nd subsasgn function B = subsasgn(A,L,RHS) switch L(1).type case '.' % code as we had before case '()' if length(L)==1 B = builtin('subsasgn',A,L(1),RHS); else tmp = builtin('subsref',A,L(1)); tmp = subsasgn(tmp,L(2:end),RHS); B = builtin('subsasgn',A,L(1),tmp); end otherwise % must still handle '{}' case