CS100J Lecture 22 - PowerPoint PPT Presentation

1 / 8
About This Presentation
Title:

CS100J Lecture 22

Description:

Implement (a piece of MatLab) in Java. Get an appreciation for what MatLab ... Grotesque duplication of code. Loss of 'single point of change'. No abstraction. ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 9
Provided by: Mill1
Category:

less

Transcript and Presenter's Notes

Title: CS100J Lecture 22


1
CS100J Lecture 22
  • Previous Lecture
  • MatLab
  • Learn MatLab
  • Implement (a piece of MatLab) in Java
  • Get an appreciation for what MatLab does for you
  • Learn to implement a class of some complexity
  • Handout The CS100 MatLab Syllabus
  • Java Concepts
  • Overloading
  • this as a constructor
  • The Next Several Lectures
  • MatLab and its implementation, continued.

2
Sample Client Code
  • // ones(2,3) ones(2,3)
  • ML.add( ML.ones(2,3), ML.ones(2,3) )
  • // 5 ones(2,3)
  • ML.add( new ML(new ml(5)), ML.ones(2,3) )
  • The second example suggest that it might be
    useful to add an additional overloaded
    constructor for ML
  • // Construct scalar matrix v.
  • public ML(int v)
  • this(1,1)
  • values00 new ml(v)
  • so the example could be
  • // 5 ones(2,3)
  • ML.add( new ML(5), ML.ones(2,3) )

3
Critique of ML Method add
  • Most of the code (all but the 3 invocations of
    ml.add) has nothing to do with the fact that this
    is the method for addition.
  • To implement subtraction, almost all code (except
    for the 3 invocations of ml.add) would be
    identical.
  • We could implement each of the other
    element-by-element matrix operations in about 30
    seconds each
  • Copy the definition of ML.add
  • Replace add with sub, or mult, or div,
    etc.
  • Write ml.add, ml.mult, ml.div, etc.
  • Terrible
  • Grotesque duplication of code.
  • Loss of single point of change.
  • No abstraction.
  • Surely, there must be a way to avoid duplicating
    the common code.
  • Coming later.

4
MatLab
  • Matrices with the same number of rows can be
    concatenated horizontally (comma optional).
  • gtgt 1 2 3
  • ans
  • 1 2 3
  • gtgt ones(2,3) , (2 . ones(2,2))
  • ans
  • 1 1 1 2 2
  • 1 1 1 2 2
  • Matrices with the same number of columns can be
    concatenated vertically.
  • gtgt 1 2 3
  • ans
  • 1
  • 2
  • 3
  • gtgt ones(2,3) (2 . ones(1,3))
  • ans

5
MatLab
  • A constant matrix can be written with a
    combination of horizontal and vertical matrix
    concatenations.
  • gtgt 1, 2, 3 4, 5, 6 7, 8, 9
  • ans
  • 1 2 3
  • 4 5 6
  • 7 8 9

6
Implementation Strategy
  • ML.row(x,y) implements binary row concatenation,
    i.e., x, y
  • ML.col(x,y) implements binary column
    concatenation, i.e., x y
  • Defer concatenation of more than 2 rows or
    columns at a time. Until then
  • / a, b, . . ., y, z /
  • ML.row(a, ML.row(b, ... ML.row(y, z)) ... )
  • / a b . . . y z /
  • ML.col(a, ML.col(b, ... ML.col(y, z)) ... )
  • (Actually ML.row and ML.col are associative, so
    the grouping is arbitrary.)

7
class ML Method row
  • // x, y
  • static ML row(ML x, ML y)
  • if (x.h ! y.h)
  • throw new RuntimeException
  • ("operands must have same height")
  • else
  • ML result new ML(x.h, x.wy.w)
  • for (int r 0 r lt x.h r)
  • for (int c 0 c lt x.w c)
  • result.valuesrc x.valuesrc
  • for (int c 0 c lt y.w c)
  • result.valuesrx.wc
  • y.valuesrc
  • return result

8
class ML Method col
  • // x y
  • static ML col(ML x, ML y)
  • if (x.w ! y.w)
  • throw new RuntimeException
  • ("operands must have same width")
  • else
  • ML result new ML(x.hy.h, x.w)
  • for (int c 0 c lt x.w c)
  • for (int r 0 r lt x.h r)
  • result.valuesrc x.valuesrc
  • for (int r 0 r lt y.h r)
  • result.valuesx.hrc
  • y.valuesrc
  • return result
Write a Comment
User Comments (0)
About PowerShow.com