CS100A Lecture 19 - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

CS100A Lecture 19

Description:

... (a piece of MatLab) in Java. Get an appreciation for ... A complex number with a zero imaginary part and an integral real part is displayed as an integer. ... – PowerPoint PPT presentation

Number of Views:12
Avg rating:3.0/5.0
Slides: 17
Provided by: Mill1
Category:
Tags: cs100a | an | imaginary | in | lecture

less

Transcript and Presenter's Notes

Title: CS100A Lecture 19


1
CS100A Lecture 19
  • Previous Lecture
  • Representation Rules of Thumb.
  • Transform problems to simpler equivalent problems
  • Maintain duplicate representations if helpful
  • Choose representations that limit search spaces
  • Find representations that yield uniform
    algorithms.
  • The Next Several Lectures
  • 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

2
MatLab
  • Each "primitive" arithmetic value is a complex
    number, i.e., a pair of real numbers, known
    respectively as the real part and the imaginary
    part.
  • gtgt sqrt(-2)
  • ans
  • 0 1.4142i
  • A complex number with a zero imaginary part is
    displayed as a real number.
  • gtgt sqrt(2)
  • ans
  • 1.4142
  • A complex number with a zero imaginary part and
    an integral real part is displayed as an integer.
  • gtgt sqrt(4)
  • ans
  • 2
  • Operators - . ./ . lt lt gt gt
  • Example
  • gtgt sqrt(-2) sqrt(2)

3
Class ml Representation
  • Development strategy Represent primitive MatLab
    arithmetic values as objects of class ml.
  • Development strategy Dont get hung up doing all
    operations now. Just implement a few and trust
    that the rest will be similar.
  • // Primitive MatLab arithmetic values.
  • class ml
  • double re // Real part
  • double im // Imaginary part
  • // Constructors
  • . . .
  • // Methods
  • . . .

4
Class ml Constructors
  • ml(double re, double im)
  • this.re re
  • this.im im
  • ml(double re)
  • this( re, 0.0 )
  • ml(int re)
  • this( (double)re, 0.0 )
  • ml(int re, double im)
  • this( (double)re, im )

5
Overloading in Java
  • Signature . The name of a constructor or method
    and the types of its parameters is called its
    signature.
  • Overloading. If two constructors or methods of a
    class have the same name, but different
    signatures, then the method name is said to be
    overloaded.
  • Invocation. When a method is invoked, the number
    of arguments and their types are used to
    determine the signature of the method that will
    be invoked.
  • Sample invocations
  • // Each of the following constructs 1.0 0.0i
  • ml( 1.0, 0.0 ) // Invokes first constructor.
  • ml( 1.0 ) // Invokes second constructor.
  • ml( 1 ) // Invokes third constructor.
  • ml( 1, 0.0 ) // Invokes fourth constructor.
  • ml( 1.0, 0 ) // Invokes fifth constructor.
  • this() can be used in a constructor definition
    to invoke another constructor of the same class.

6
Class ml Method toString
  • // Convert MatLab real to String.
  • private String toStringReal(double x)
  • if ( (x - Math.floor(x)) 0 )
  • return (int)x ""
  • else
  • return x ""
  • // Convert MatLab complex value to String.
  • public String toString()
  • if ( im 0 )
  • return toStringReal(re)
  • else
  • return toStringReal(re) " "
  • toStringReal(im) "i"

7
Class ml Arithmetic Methods
  • public static ml add(ml x, ml y)
  • return new ml(x.re y.re, x.im y.im)
  • public static ml sub(ml x, ml y)
  • return new ml(x.re - y.re, x.im - y.im)
  • / other operators
  • . ./ lt lt gt gt unary- etc.
  • /

8
Matrix
  • A matrix is a rectangular table of values.
  • gtgt ones(2,3)
  • ans
  • 1 1 1
  • 1 1 1
  • The elements of two matrices with the same
    dimensions can be added (), subtracted (-),
    multiplied (.), and divided(./).
  • gtgt ones(2,3) ones(2,3)
  • ans
  • 2 2 2
  • 2 2 2
  • Other operators . lt lt gt gt
  • The empty matrix, written , has height and
    width 0. The empty matrix results from requesting
    a non-positive height or width.
  • gtgt ones(2,-1)

9
Scalars
  • A scalar is just a 1-by-1 matrix.
  • gtgt ones(1,1)
  • ans
  • 1
  • Scalar operations are just special cases of
    matrix operations, i.e., operations on 1-by-1
    matrices.
  • gtgt 1 2
  • ans
  • 3
  • A scalar operand s is treated as a matrix with
    the same dimensions as the other operand and the
    value s everywhere.
  • gtgt 5 ones(2,3)
  • ans
  • 6 6 6
  • 6 6 6

10
class ML
  • Class ML implements MatLab matrix values.
  • Development strategy Dont get hung up doing all
    matrix operations now. Just implement a few, and
    trust that the rest will be similar.
  • Development strategy Dont get hung up about
    efficiency now.

11
class ML Representation
  • // MatLab Matrix
  • public class ML
  • private int h // height of matrix
  • private int w // width of matrix
  • private ml values // elements of matrix
  • // Constructors.
  • . . .
  • // Methods.
  • . . .

12
class ML Constructors
  • // Construct h-by-w uninitialized matrix.
  • private ML(int h, int w)
  • this.h h
  • this.w w
  • values new ml hw
  • // Construct h-by-w matrix initialized with v.
  • public ML(int h, int w, ml v)
  • this(h,w)
  • for (int r0 rlth r)
  • for (int c 0 c lt w c)
  • valuesrc v
  • // Construct scalar matrix v.
  • public ML(ml v)
  • this(1,1)

13
class ML Methods ones, zeros, toString
  • // Construct matrix of all ones.
  • public static ML ones(int h, int w)
  • return new ML( h, w, new ml(1.0))
  • // Construct matrix of all zeros.
  • public static ML zeros(int h, int w)
  • return new ML( h, w, new ml(0.0))
  • // Convert this matrix to String.
  • public String toString()
  • String result ""
  • for (int r 0 r lt h r)
  • for (int c 0 c lt w c)
  • result result valuesrc " "

14
class ML Utility methods
  • // this is empty if either height or width is 0.
  • private boolean isEmpty()
  • return h0 w 0
  • // this is scalar if both height and width are 1.
  • private boolean isScalar()
  • return h1 w 1

15
class ML Method add
  • // x y
  • public static ML add(ML x, ML y)
  • if ( x.isEmpty() y.isEmpty() )
  • return new ML(0,0)
  • else if ( x.isScalar() )
  • ML result new ML(y.h, y.w)
  • ml scalarValue x.values00
  • for (int r 0 r lt y.h r)
  • for (int c 0 c lt y.w c)
  • result.valuesrc
  • ml.add( scalarValue,
  • y.valuesrc )
  • return result
  • else if ( y.isScalar() )
  • ML result new ML(x.h, x.w)

16
class ML Method add, continued
  • else if (x.h ! y.h x.w ! y.w)
  • throw new RuntimeException(
  • matrix dimensions do not match")
  • else
  • ML result new ML(x.h, x.w)
  • for (int r 0 r lt x.h r)
  • for (int c 0 c lt x.w c)
  • result.valuesrc
  • ml.add( x.valuesrc,
  • y.valuesrc)
  • return result
  • // end of method add
Write a Comment
User Comments (0)
About PowerShow.com