Matrices - Introduction To Computers & Engineering
Matrices - Introduction To Computers & Engineering
1.00 Lecture 30
Matrices
Matrices
• Matrix is 2-D array of m rows by n columns
a00 a01 a02 a03 a0n-1
a10 a11 a12 a13 a1n-1
a20 a21 a22 a23 a2n-1
am-1,0 am-1,1 am-1,2 am-1,3 am-1,n-1
1
2/11/12
Matrices, p.2
• In this lecture we cover basic matrix
representation and manipulation
– Used most often to prepare matrices for use in solving
linear systems, which we cover in next lecture
• Java has 2-D arrays, declared as, for example
double[][] squareMatrix= new double[5][5];
– But there are no built-in methods for them
• So, its helpful to create a Matrix class:
– Create methods to add, subtract, multiply, form identity
matrix, etc.
– Used for matrices with a few hundred rows or so
• Sparse matrices are handled differently:
– Almost all large matrices (millions of rows or columns)
are extremely sparse (99%+ of entries are zeros)
– Store (i, j, value) in a list or 1-D array or other data
structure
2-D Arrays
double[][] data= new double[5][4];
data[0][0]
No of columns=
data data[0] 8.0 14.0 2.0 3.0
data[0].length
data[1] 5.1 4.2 6.6 0.8
No. of rows=
data.length
A 2-D array is:
a reference to a 1-D array of references to 1-D arrays of data.
This is how well store the matrix data in class Matrix
2
2/11/12
3
2/11/12
MatrixTest
public class MatrixTest {
public static void main(String argv[]) {
Matrix mat1 = new Matrix(3,3);
Matrix mat2 = new Matrix(3,3);
mat1.setIdentity();
mat2.setIdentity();
Matrix res;
res = mat1.add(mat2);
System.out.println("mat1:");
mat1.print();
System.out.println("mat2:");
mat2.print();
System.out.println("mat1 + mat2:" );
res.print();
4
2/11/12
Exercise 1
• Download Matrix and MatrixTest from Web site
• Write an instance method multScalar() to
multiply a matrix by a scalar (double) in class
Matrix
• Invoke your method from MatrixTest main()
• Hints for writing multScalar()
– Use add() as a rough guide
– Find the number of rows and columns in the matrix
– Create a new Matrix object to return as the result
– Loop through all entries (nested for loops) to
multiply by the scalar
– Return the result
• Modify MatrixTest main() method:
– Add a line to use the multScalar() method
– Add another line to print the resulting matrix, using
its print() method
Exercise 2 Introduction
• Implement graphics transforms from last Swing
lecture
• Instead of using Javas rotate() and scale()
methods, youll create matrices to represent
rotation and scaling, multiply them, and apply
them to a shape.
• With some perseverance, your matrix
manipulations will yield the same result as Javas
methods
5
2/11/12
Translation
⎡1 0 t x ⎤ ⎡ x ⎤ ⎡ x + t x ⎤
⎢0 1 t ⎥ ⎢ y ⎥ = ⎢ y + t ⎥
ty ⎢ y⎥⎢ ⎥ ⎢ y⎥
⎢⎣0 0 1 ⎥⎦ ⎢⎣ 1 ⎥⎦ ⎢⎣ 1 ⎥⎦
tx
Rotation
6
2/11/12
Scaling
⎡ sx 0 0 ⎤ ⎡ x ⎤ ⎡ sx ∗ x ⎤
⎢0 sy 0⎥ ⎢ y ⎥ = ⎢ s y ∗ y ⎥
⎢ ⎥⎢ ⎥ ⎢ ⎥
⎢⎣ 0 0 1 ⎥⎦ ⎢⎣ 1 ⎥⎦ ⎢⎣ 1 ⎥⎦
Composing Transformations
• Suppose we want to scale point (x, y) by 2 and
then rotate by 90 degrees.
⎡0 −1 0 ⎤ ⎛ ⎡ 2 0 0 ⎤ ⎡ x ⎤ ⎞
⎢1 0 0 ⎥ ⎜ ⎢ 0 2 0 ⎥ ⎢ y ⎥ ⎟
⎢ ⎥⎜ ⎢ ⎥⎢ ⎥⎟
⎜
⎢⎣0 0 1 ⎥⎦ ⎝ ⎢⎣ 0 0 1 ⎥⎦ ⎢⎣ 1 ⎥⎦ ⎟⎠
rotate scale
7
2/11/12
Composing Transformations, 2
Because matrix multiplication is associative, we can
rewrite this as
⎛ ⎡0 −1 0 ⎤ ⎡ 2 0 0 ⎤ ⎞ ⎡ x ⎤
⎜⎢ ⎥ ⎢0 2 0⎥ ⎟ ⎢ y ⎥
⎜⎢ 1 0 0
⎥⎢ ⎥⎟⎢ ⎥
⎜ ⎢0 0 1 ⎥ ⎢0 0 1 ⎥ ⎟ ⎢ 1 ⎥
⎝⎣ ⎦⎣ ⎦⎠⎣ ⎦
⎡ 0 −2 0 ⎤ ⎡ x ⎤
= ⎢2 0 0⎥ ⎢ y ⎥
⎢ ⎥⎢ ⎥
⎢⎣ 0 0 1 ⎥⎦ ⎢⎣ 1 ⎥⎦
Exercise 2
• Download TransformTest, TransformPanel
– TransformPanel rotates (by 18 º) and scales (by 2) a
rectangle using Java affine transforms
– Run it to see the result. (Dont use this code for exercise.)
• Download TransformTest1, TransformPanel1
– These are skeletons for doing the rotations and scaling
through matrix multiplication yourself. You will:
– Create two matrices (rotate, scale) with your Matrix class
– Scale by 2 in the x and y directions and rotate by 18º (/
10)
• Look at the scaling and rotation matrices in previous slides
– Multiply the 2 matrices, save them in Matrix result. Order
matters in general
• Try it both ways here—its simple enough to give same result
– Pass the values as arguments to AffineTransform() as
shown in TransformPanel1 code on the next slides
– See if your AffineTransform produces the same result
8
2/11/12
Exercise 2: TransformTest
import java.awt.*;
import javax.swing.*;
Exercise 2: TransformPanel
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*; // For 2D classes
g2.setPaint(Color.BLUE);
AffineTransform baseXf = new AffineTransform();
// Scale by 2 in x, y directions, then rotate by 18 degrees
baseXf.rotate(Math.PI/10.0);
baseXf.scale(2.0, 2.0);
g2.transform(baseXf);
g2.draw(rect);
}
}
9
2/11/12
© Oracle. All rights reserved. This content is excluded from our Creative
Commons license. For more information, see http://ocw.mit.edu/fairuse.
10
2/11/12
Exercise 3
• Modify TransfomPanel
– Almost the same as exercise 4, lecture 21
– Initially, rectangle is 50 by 100, at origin
– Apply the following transforms:
• Translate rectangle 50 pixels east, 200 pixels south
• Scale by factor of 1.5, but leave upper left corner of
rectangle in same position
• Rotate by 30 degrees counterclockwise around the origin
– Not around the upper left corner, as in lecture 21, which
would require translating to origin and back again
– Counterclockwise, not clockwise, to stay on the panel
– Draw the original rectangle in red
– Draw the transformed rectangle in blue
– Remember to apply transforms by premultiplying
by each one in order
Exercise 3 cont.
• Mechanics:
– Copy your exercise 2 solution into class
TransformPanel2
– Copy TransformTest into TransformTest2
• Have its main() create a TransformPanel2 object
– Do not use AffineTransform methods rotate(),
scale() or translate()
– Create r, s and t matrices and multiply them
appropriately to create a result matrix holding
the combined transform
– Use the first 6 coefficients of the result matrix
(m00, m10, etc.) in the AffineTransform
constructor, as in exercise 2
11
2/11/12
Exercise 3 output
© Oracle. All rights reserved. This content is excluded from our Creative
Commons license. For more information, see http://ocw.mit.edu/fairuse.
12
MIT OpenCourseWare
http://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.