LIFE MEBI 531
Popular in Course
Popular in Medical Education And Biomedical Informatics
This 42 page Class Notes was uploaded by Marguerite Quigley on Wednesday September 9, 2015. The Class Notes belongs to MEBI 531 at University of Washington taught by Ira Kalet in Fall. Since its upload, it has received 22 views. For similar materials see /class/192264/mebi-531-university-of-washington in Medical Education And Biomedical Informatics at University of Washington.
Reviews for LIFE
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 09/09/15
More on setf setf can assign a value to a symbol gt setf foo 6 6 gt foo 6 For this setq is more efficient But setf can do much more It can assign a value to a place gt setq foo a very short list A VERY SHORT LIST gt setf third foo long LONG gt foo A VERY LONG LIST This is a destructive operation the list is changed and therefore the value of foo is changed indirectly Valid place names for use with setf first aref svref rest nth fill pointer second elt symbol value third get symbol function fourth gethash documentat ion and so on up to tenth as well as combinations of f i rst and rest car and cdr and their relatives up to four of any combination of a or d like caddr or cddadr Arrays In Common Lisp the array data type provides effi ciency and clarity when dealing with regular arrays of data A medical image is a good example of an array Arrays usually have fixed dimensions specified by a list of dimensions make array creates an array object gt setf image 6 make array 512 512 initial element 0 2AO O O O J Array access is done with aref The aref function is the accessor for array elements gt aref image 6 234 167 1079 and setf works with aref to update array entries gt setf aref image 6 234 167 334 334 gt aref image 6 234 167 334 Arrays can provide efficiency setq foo make array 512 512 dotimes i 512 dotimes j 512 setf aref foo i j 00 dotimes i 512 dotimes j 512 if aref foo i j 00 setf aref foo i j list ira i j setf aref foo j i foo An array element can be any lisp object Like lists different elements can have values that are different types defvar pdp cells make array 15 9 initial contents button quotDelete Panelquot nil button quotComputequot nil nil button type momentary nil nil label quotBeamsquot nil nil nil nil nil nil left arrow nil nil nil fg color slzred label quotquot label quotquot label quotquot label quotquot right arrow nil nil nil fg color slred the monitor units row label quotPointsquot nil button quotALL FRACquot nil nil button type momentary label quotMUquot number nil 00 100000 number nil 00 100000 number nil 00 100000 number nil 00 100000 nil zup arrow nil nil nil fg color slzred nil label quotDosequot nil nil nil nil nil nil ten rows of point doses zlabel quotquot nil number nil 00 100000 nil number nil 00 100000 number nil 00 100000 number nil 00 100000 number nil 00 100000 nil AAAAM AAAA A Array dimensions can be determined at runtime To find out the dimensions of an array object there are two related functions array dimension gives a specified dimension gt array dimension image 6 O 512 gt array dimension image 6 1 512 array dimensions returns a list of all the dimen sions of the array gt array dimensions image 6 512 512 But setf can not be used with either of these func tions Vectors One dimensional arrays are called vectors The vector function makes a vector with its argu ments as initial contents gt vector foo 5 6 ira FOO 5 6 IRA For vectors there is a faster accessor function svref gt setf test vector foo 5 6 ira FOO 5 6 IRA gt svref test 3 IRA An image processing example defun map image map image ampoptional result quotmap image map image ampoptional result returns an array of pixels from image by composing image array with the gray map map must be an array specifying a pixel value to be output for each possible image data value If the result array is provided it is reused otherwise a new array is createdquot declare type simple array clxpixel 1 map type simple array clxpixel 2 result type simple array unsigned byte l6 2 image let x dim array dimension image 1 y dim array dimension image 0 temparray or result make array list y dim x dim element type case image bits per pixel 8 unsigned byte 8 l6 unsigned byte 16 32 unsigned byte 32 declare type fixnum x dim y dim dotimes j y dim declare type fixnum j dotimes i x dim declare type fixnum i setf aref temparray j i aref map aref image j i temparray Structures are an extension of the type hierarchy The defstruct macro adds a new data type to the environment Example defstruct point x y 2 defines a data type called point having three named fields x y and 2 Also defstruct creates accessor functions point x point y and point z and a constructor function make point defstruct also automatically makes a copy function copy point and a predicate function point p that returns t if its argument is a point Details about structures The constructor function accepts keyword arguments to initialize the fields Accessor functions return the values stored there gt setf pt make point x 34 y 26 2 00 SPOINT X 34 Y 26 Z 00 point X pt point z pt OVUJV 4 O The setf macro can be used with a structure to update a field setf point z pt 51 l point z pt 1 U IVU39IV Examples using points To compute the distance between two points defun distance ptl pt2 let dx abs point x pt2 point x ptl dy abs point y pt2 point y ptl dz abs point z pt2 point z ptl sqrt dx dx dy dy dz dz Considering points as vectors we can do dotproducts and cross products defun dot product ptl pt2 point x ptl point x pt2 point y ptl point y pt2 point z ptl point z pt2 defun cross product ptl pt2 make point X point y ptl point z pt2 point z ptl point y pt2 y point z ptl point x pt2 point x ptl point z pt2 z point x ptl point y pt2 point y ptl point x pt2 The drug interaction system with structures To use structures for drug interaction records change only a small amount of code the accessor functions after providing a structure definition defstruct drug entry name effects contras defun name d drug entry name d defun effects d drug entry effects d defun contras d drug entry contras d These definitions replace the original ones that treat drug entries as lists Constructing the drug interaction data To construct the drug data list call make drug entry with the appropriate arguments instead ofjust writing a quoted list defvar CCU drugs list make drug entry name aspirin effects analgesia anti inflammatory blood thinner platelet inhibition heart attack contras gastritis bleeding tendency platelet inhibition nausea make drug entry name procaineamide effects cardiac arrhythmia digoxin toxicity contras renal toxicity Slow heart rate cardiac arrhythmia chf nausea platelet inhibition make drug entry name digoxin effects chf slow heart rate contras Slow heart rate cardiac arrhythmia nausea Initial values for fields in structures You can provide default initial values for fields in the defstruct form defstruct point x 00 y 00 2 00 Then the constructor will use them if initial values are not explicitly pro vided gt setf pt make point x 34 y 26 SPOINT X 34 Y 26 Z 00 point X pt point z pt OVUJV 4 0 Initial values can be arbitrary forms An initial value form can contain any expression and will be evaluated each time an instance is constructed example from Graham ANSI Common Lisp defstruct polemic type progn format t quotWhat type of polemic was it quot read effect nil In this case using make polemic results in a query on the command line gt make polemic What type of polemic was it scathing SPOLEMIC TYPE SCATHING EFFECT NIL Providing inputs to the constructor will override the defaults gt make polemic type humorous effect laughter SPOLEMIC TYPE HUMOROUS EFFECT LAUGHTER Structures can provide a kind of inheritance From Winston and Horn Lisp we see that a structure type can be a subtype of another defstruct employee length of service 0 payment salary defstruct hacker include employee preferred language lisp It can provide defaults that override parent defaults defstruct salesperson include employee payment commission preferred car mercedes More examples of structure types A structure called course that includes the course name the quarter the instructor s name a list of student names and the meeting room defstruct course name quarter instructor students room defvar courses nil defun add new course name qtr inst room push make course name name quarter qtr instructor inst room room courses defun update room course list name new room setf course room find name course list key course name new room Using the course structure The schedule function computes the student s schedule defun schedule course list student name if null course list nil let result nil dolist course course list result if find student name course students course push course result Limitations of structure types Structures provide data abstraction while retaining efficiency But the nam ing conventions are restricted An accessor name must be unique to a type and its subtypes defstruct rectangle height width Accessors rectangle height and rectangle width not just height and width defstruct building height owner cost has accessors named building height and so on Data driven procedures Structures like any other types can have datadriven functions but it is awkward defstruct rectangle height width defstruct circle radius defun area x cond rectangle p x rectangle height x rectangle width x circle p x pi expt circle radius x 2 Generic functions Generic functions provide a better way to organize the datadriven call and are a next level of procedural abstraction defmethod area x rectangle rectangle height x rectangle width X defmethod area x circle pi expt circle radius x 2 When the area function is used in a program the builtin generic function dispatch finds the applicable method and evaluates its body Generic functions and ordinary functions 0 An ordinary function cannot have methods added to it Only a generic function can be built up in such parts 0 If a symbol already has an ordinary function or macro definition using defmethod with it is an error 0 If a symbol already has a generic function definition using defun with it is also an error 80 we cannot define methods for the arithmetic functions for example Properties of generic functions Generic functions 0 can be used everywhere ordinary functions are used including with funcall and apply 0 are always associated with names 0 can have names that correspond to the use of setf eg if a generic function refers to a place Multimethods or multiple dispatch Which method is used can depend on the type of any number of arguments The draw function in Prism takes two inputs a thing to draw and a view in which to draw m defmethod draw pstr pstruct dolist con contours pstr when poly nearly equal 2 con pos display epsilon draw transverse vertices con prim x0 y0 scale tv transverse view defmethod draw pstr pstruct sv sagittal view dolist con contours pstr let rects nil mapl lambda verts when rest verts let Xl first first verts yl second first verts append vertices con list first vertices con More on multimethods Sometimes default methods are useful in incremental prototyping defmethod draw obj t V t format t quotNo DRAW method for class A in A quot class name class of obj class name class of V but stub methods may still be needed defmethod draw b beam V View draw portal dashed prim portal collimator b bt sad V draw isocenter solid prim bt scale x orig y orig when display axis b draw central axis solid prim bt sad scale x orig y orig unless zerop id wdg draw wedge solid prim beam transform b V t sad rotation wdg scale x orig y orig slzwidth pic slzheight pic defmethod draw b beam V room View quotstub to prevent crashes just don t draw it until we figure out the 3D representation of a beamquot Augmenting generic functions When a new data type is added to the system you just define another method for each generic function that is relevant You don t modify existing code defstruct triangle base height defmethod area x triangle 05 triangle base x triangle height x but we cannotjust write height x Classes The class generalizes the idea of a structure defclass rectangle height width defclass circle radius but we do not get all the defaults that defstruct provides Instead we must specify names for accessors etc defclass rectangle height accessor height width accessor width defclass circle radius accessor radius Constructors and initialization The generic function make instance serves as the constructor for all classes gt setf foo make instance circle ltCIRCLE X7l4df702gt gt setf radius foo 5 radius foo U39IVU39I Default slot values and keyword initialization can be arranged defclass circle radius accessor radius initarg radius initform 4000 Use of defaults and initargs Examples using default slot values and initialization arguments gt setf earth make instance circle ltCIRCLE X7l4df702gt gt radius earth 4000 gt setf mercury make instance circle radius 1700 ltCIRCLE X7l4df702gt gt radius mercury 1700 Accessors are generic functions Here are the methods for area written with accessors defmethod area x rectangle height x width x defmethod area x circle pi expt radius x 2 Accessors are also generic functions so the names can be the same in different classes defclass triangle base accessor base height accessor height defmethod area x triangle 05 base x height x Classes extend the Common Lisp type system There is a mapping of types to classes The object system supports three kinds of classes builtin classes correspond to the predefined types and types derived from them eg number symbol cons array etc structure classes are defined by defstruct standard classes are defined by defclass And the whole thing is extensible since it is built on its own machinery The extension scheme is called the Metaobject Protocol MOP Inheritance comes with specialization A subclass inherits slots and methods from all its parent classes defclass molecule name initarg name accessor name initform nil formula initarg formula accessor formula type initarg type accessor type defclass protein molecule function initarg function accessor function initform kinase regulates initarg regulates accessor regulates initform nil defclass t rna molecule amino acid initarg amino acid accessor amino acid More on inheritance Slots and defaults are inherited gt setq ml make instance molecule name ethanol formula C h 3 c h 2 o h type alcohol gt setq m2 make instance protein name insulin formula gly ala tyr tyr ala type polypeptide function Sugar metabolism gt setq t5 make instance t rna amino acid tyr formula g c c a t a t Methods can be inherited or superceded The count atoms function returns an association list of atom symbols and the number in the molecule defmethod count atoms m molecule count Simple molecule formula m defmethod count atoms m protein combine counts mapcar count atoms formula m defmethod count atoms m symbol count atoms gethash m molecule table defclass amino acid molecule Inheritance and class precedence In class precedence with single inheritance method selection goes from most specific to least specific With multiple inheritance it is more complex Precedence order goes from 1 most specific to least specific 2 for each argument from left to right 3 stop when you reach a node with another subclass not visited 4 applied recursively at each node in the class hierarchy An example from Graham ANSI Common Lisp defclass sculpture height accessor height initarg height width accessor width initarg width depth accessor depth initarg depth defclass statue sculpture subject accessor subject initarg subject defclass metalwork metal type accessor metal type initarg metal type defclass casting metalwork defclass cast statue statue casting 0 What is the class precedence list Method selection Another example from Graham defmethod combine x y list x y defclass Stuff name accessor name initarg name defclass ice cream Stuff defclass topping Stuff defmethod combine ic ice cream top topping format nil quotNA ice cream with A toppingquot name ic name top defmethod combine ic ice cream x format nil quotNA ice cream with A name ic x Methods can specialize on any types Generic functions like combine can have methods that specialize on any type and even particular instances defmethod combine x number y number xy defmethod combine x eql powder y eql Spark boom defmethod combine vl point v2 point make instance point x x v1 x v2 y y v1 y v2 2 2 v1 2 v2 Auxiliary methods and method combination The unqualified methods we considered are called primary methods It is also possible to have auxiliary methods that supplement or combine with the most specific primary method They are o before methods executed in most specific first order o after methods executed in least specific first order o around methods that wrap around the others The sculpture example from Graham again defclass sculpture height accessor height initarg height width accessor width initarg width depth accessor depth initarg depth defclass statue sculpture subject accessor subject initarg subject defclass metalwork metal type accessor metal type initarg metal type initform bronze defclass casting metalwork O defclass cast statue statue casting O Auxiliary methods and method combination An auxiliary method adds to the action of the primary method defmethod catalog s sculpture format t quotSculpture A high A wide and A deep quot height s width s depth s defmethod catalog after s statue format t quotIt is a statue of A quot subject s defmethod catalog after m metalwork format t quotIt is made of A quot metal type m
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'