Slides by: Diana Inkpen and Alan Williams

Slides:



Advertisements
Similar presentations
EXAMPLES (Arrays). Example Many engineering and scientific applications represent data as a 2-dimensional grid of values; say brightness of pixels in.
Advertisements

What is shape function ? shape function is a function that will give the displacements inside an element if its displacement at all the node locations.
Chapter Matrices Matrix Arithmetic
1 CS 162 Introduction to Computer Science Chapter 7 Matrix Manipulation Herbert G. Mayer, PSU Status 9/21/2014.
MF-852 Financial Econometrics
1 ICS103 Programming in C Lecture 16: 2-Dimensional Arrays.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Chapter 2 Section 3 Arithmetic Operations on Matrices.
CE 311 K - Introduction to Computer Methods Daene C. McKinney
Table of Contents Solving Systems of Linear Equations - Gaussian Elimination The method of solving a linear system of equations by Gaussian Elimination.
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 13 – Having Fun with Arrays in Java Webpage:
Matrices Jordi Cortadella Department of Computer Science.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Multi-Dimensional Arrays Arrays that have more than one index: Example of differences between basic data types and arrays using integers: Basic integer:
ITI 1120 Lab #9 Slides by: Diana Inkpen and Alan Williams.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Two-Dimensional Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot.
Meeting 18 Matrix Operations. Matrix If A is an m x n matrix - that is, a matrix with m rows and n columns – then the scalar entry in the i th row and.
Slide Copyright © 2009 Pearson Education, Inc. 7.3 Matrices.
Section 4Chapter 4. 1 Copyright © 2012, 2008, 2004 Pearson Education, Inc. Objectives Solving Systems of Linear Equations by Matrix Methods Define.
Matrices: Simplifying Algebraic Expressions Combining Like Terms & Distributive Property.
Introduction to Programming (in C++) Multi-dimensional vectors Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
1 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
UNIT 10 Multidimensional Arrays.
Two-Dimensional Arrays and Matrices ELEC 206 Computer Applications for Electrical Engineers.
Multidimensional Arrays Computer and Programming.
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
CS 450: COMPUTER GRAPHICS TRANSFORMATIONS SPRING 2015 DR. MICHAEL J. REALE.
Table of Contents Matrices - Definition and Notation A matrix is a rectangular array of numbers. Consider the following matrix: Matrix B has 3 rows and.
Precalculus Section 14.1 Add and subtract matrices Often a set of data is arranged in a table form A matrix is a rectangular.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Matrices. Variety of engineering problems lead to the need to solve systems of linear equations matrixcolumn vectors.
MATRICES A rectangular arrangement of elements is called matrix. Types of matrices: Null matrix: A matrix whose all elements are zero is called a null.
Matrices. Matrix A matrix is an ordered rectangular array of numbers. The entry in the i th row and j th column is denoted by a ij. Ex. 4 Columns 3 Rows.
Introduction to Programming
MTH108 Business Math I Lecture 20.
Lecture 18: Nested Loops and Two-Dimensional Arrays
3.8 Matrices L Al-zaid Math1101.
Properties and Applications of Matrices
Matrices Rules & Operations.
Linear Algebra review (optional)
Two-Dimensional Arrays
1.5 Matricies.
Programming application CC213
ECE Application Programming
Introduction To Matrices
Matrix Operations SpringSemester 2017.
Computer Graphics Matrix
Two Dimensional Arrays
7.3 Matrices.
Nested Loop Review and Two-Dimensional Arrays
Section 2.4 Matrices.
2.2 Introduction to Matrices
The Hanoi Tower Problem
Matrices Introduction.
CS100: Discrete structures
Lecture 13: Two-Dimensional Arrays
Multidimensional array
Presented By Farheen Sultana Ist Year I SEM
Lets Play with arrays Singh Tripty
Question 1a) What is printed by the following Java program? int s;
Matrix Operations SpringSemester 2017.
Matrix A matrix is a rectangular arrangement of numbers in rows and columns Each number in a matrix is called an Element. The dimensions of a matrix are.
Matrices are identified by their size.
EECE.2160 ECE Application Programming
Arrays and Matrices Prof. Abdul Hameed.
Slides by: Diana Inkpen and Alan Williams
3.8 Matrices L Al-zaid Math1101.
Applied Discrete Mathematics Week 4: Functions
Presentation transcript:

Slides by: Diana Inkpen and Alan Williams ITI 1120 Lab #10 Slides by: Diana Inkpen and Alan Williams

Introduction to matrices A matrix is a two dimensional rectangular grid of numbers: The dimensions of the matrix are the numbers of rows and columns (in the above case: row dimension 3, column dimension 3). A value within a matrix is referred to by its row and column indices, in that order. Math: number rows and columns from 1, from upper left corner In math notation, M1,2 = 2 For algorithms (and Java), we will use indices starting from 0, to correspond to the array numbering. In algorithm notation, M[0][1] = 2

Matrix element processing To visit every element of an array, we had to use a loop. To visit every element of a matrix, we need to use a loop inside a loop: Outer loop: go through each row of a matrix Inner loop: go through each column within one row of a matrix. Recursion could be used in place of each loop

line_index <= column_index Matrix example Write an algorithm that finds the sum of the upper triangle of a square matrix (i.e. the diagonal and up). 0 1 2 3 4 1 4 5 3 2 6 3 6 4 6 M = 4 3 6 7 2 3 4 2 2 4 2 3 8 3 5 1 2 3 4 How do we know if an element of a square matrix is on or above the main diagonal? line_index <= column_index

Matrix example – cont’d GIVENS: M (the matrix) N (the number of rows and columns in the matrix) RESULT: Sum (the sum of all elements in the upper triangle) INTERMEDIATES: R (row index in the matrix) C column index in the matrix) HEADER: Sum  CalculateUpperTriangle(M,N)

Matrix example – cont’d BODY: SUM  0 R  0 true false R < N ? C  0 C < N ? true false false R  C ? true  SUM  SUM + M[R][C] R  R + 1 C  C + 1

Files needed for this lab WindChill.java For example 1. ITI1120.java For reading in arrays for example 1. MatrixLib.java For reading and printing matrices

Matrix Example 1: Objective Design an algorithm that will create a matrix of wind chill values for various temperatures and wind speeds. The set of temperatures will be stored in an array The set of wind speeds will be stored in a second array An algorithm is already available that calculates the wind chill for a specific temperature and wind speed. Implement the algorithm in Java. For those of you who haven’t yet experienced an Ottawa winter, you may want to keep the results for future use 

Given algorithm: Wind Chill calculation Suppose we are given the following algorithm: Chill  WindChill( Temperature, WindSpeed ) that calculates the wind chill for a given temperature in °C, and wind speed in km/hr. Restrictions: WindSpeed  5.0 km/hr Temperature should be between -50.0°C and +5.0°C. More info: http://www.msc.ec.gc.ca/education/windchill/

Wind Chill Table Design the following algorithm GIVENS: TempArray (an array of temperatures in °C) NT (length of TempArray) SpeedArray (an array of wind speeds in km/hr) NS (length of SpeedArray) RESULTS: ChillTable (a matrix of wind chill values, with NT rows and NS columns) INTERMEDIATES (to be determined) HEADER: ChillTable  WindChillTable( TempArray, NT, SpeedArray, NS)

Implementation in Java Get the following files from the course webpage for lab 10 WindChill.java ITI1120.java The file WindChill.java already has a main( ) method as well as an implementation of the method windChill( temperature, windSpeed ) The file WindChill.java contains the method printTable( temperatureArray, speedArray, chillTable ) used to print the matrix.

Sample output Lowest value during winters 2003 and 2004 in Ottawa. ITI 1120 Fall 2007, Lab 10, Example 1 Name: Diana Inkpen, Student# 81069665 Enter a set of temperatures between -50 and +5: 5 0 -5 -10 -15 -20 -25 -30 -35 -40 Enter a set of wind speeds: 5 10 15 20 25 30 35 40 Wind Chill Table Speed: 5.0 10.0 15.0 20.0 25.0 30.0 35.0 40.0 -------------------------------------------------------- Temp. | 5.0 | 4.1 2.7 1.7 1.1 0.5 0.1 -0.4 -0.7 0.0 | -1.6 -3.3 -4.4 -5.2 -5.9 -6.5 -7.0 -7.4 -5.0 | -7.3 -9.3 -10.6 -11.6 -12.3 -13.0 -13.6 -14.1 -10.0 | -12.9 -15.3 -16.7 -17.9 -18.8 -19.5 -20.2 -20.8 -15.0 | -18.6 -21.2 -22.9 -24.2 -25.2 -26.0 -26.8 -27.4 -20.0 | -24.3 -27.2 -29.1 -30.5 -31.6 -32.6 -33.4 -34.1 -25.0 | -30.0 -33.2 -35.2 -36.8 -38.0 -39.1 -40.0 -40.8 -30.0 | -35.6 -39.2 -41.4 -43.1 -44.5 -45.6 -46.6 -47.5 -35.0 | -41.3 -45.1 -47.6 -49.4 -50.9 -52.1 -53.2 -54.2 -40.0 | -47.0 -51.1 -53.7 -55.7 -57.3 -58.7 -59.8 -60.9 Lowest value during winters 2003 and 2004 in Ottawa. Jan. 1994: this value was achieved in Ottawa! (coldest winter since 1948)

Example 2: Matrix Transpose Write an algorithm that will take a matrix of integers A and transpose the matrix to produce a new matrix AT. The transpose of the matrix is such that element arc in the original matrix will be element aTcr in the transposed matrix. The number of rows in A becomes the number of columns in AT, and the number of columns in A becomes the number of rows in AT. For example:

Translate to Java Translate your matrix transpose algorithm to Java The header of the method should be as follows: public static int[][] transpose( int[][] a ) Get the following file from the virtual campus for lab 10 MatrixLib.java The file MatrixLib.java has the following useful methods: Method to read a matrix of integers, of specified dimension. public static int[][] readIntMatrix( int numRows, int numCols ) Method to print a matrix of integers with nice formatting. public static void printMatrix( int[][] matrix )

Example 3: Matrix Multiplication Suppose that A is an m × n matrix and B is an n × p matrix. The element at row i and column j in A is denoted by aij. Let C = A × B. Then, C will be an m × p matrix, and for 0 ≤ i < m, and 0 ≤ j < p, Write an algorithm to multiply two given matrices.  

Translate to Java Translate your matrix multiplication algorithm to Java The header of the method should be as follows: public static int[][] product( int[][] a, int[][] b ) Use the methods from MatrixLib.java to read and print the matrices.

[solutions and extra information follow this slide]

The Wind Chill calculation Here is the complete wind chill algorithm, for your information. It has already been implemented in WindChill.java. [Source: Environment Canada] GIVENS: Temperature (a temperature in °C between -50 and +5) WindSpeed (a wind speed in km/hr – must be  5 km/hr) RESULTS: Chill (the wind chill value) INTERMEDIATES VFactor (the wind speed raised to the power 0.16) HEADER: Chill  WindChill( Temperature, WindSpeed ) BODY: VFactor  (WindSpeed) 0.16 Chill  13.12 + 0.6215 * Temperature - 11.37 * VFactor + 0.3965 * Temperature * VFactor

Wind Chill Algorithm (page 1) GIVENS: TempArray (an array of temperatures in °C) NT (length of TempArray) SpeedArray (an array of wind speeds in km/hr) NS (length of SpeedArray) RESULTS: ChillTable (a matrix of wind chill values, with NT rows and NS columns) INTERMEDIATES T (index into temperature array) S (index into speed array) HEADER: ChillTable  WindChillTable( TempArray, NT, SpeedArray, NS)

Wind Chill Algorithm (page 2) ChillTable  MakeNewArray(NT) T  0 T < NT true false ChillTable[T]  MakeNewArray(NS) S  0 true false S < NS ChillTable[T][S]  WindChill(TempArray[T], SpeedArray[S]) S  S + 1 T  T + 1

Transpose Algorithm (page 1) GIVENS: A (an array) NRows (number of rows in A) NCols (number of columns in A) RESULTS: AT (the transposed array) INTERMEDIATES TRow (row index in transposed array) TCol (column index in transposed array) HEADER: AT  Transpose( A, NRows, NCols )

Transpose Algorithm (page 2) AT  MakeNewArray(NCols) Row  0 TRow < NCols true false AT[TRow]  MakeNewArray(NRows) TCol  0 false TCol < NRows true AT[TRow][TCol]  A[TCol][TRow] TCol  TCol + 1 TRow  TRow + 1

Multiplication Algorithm (page 1) GIVENS: A (an array of numbers) B (another array of numbers) NARows (number of rows in matrix A) NACols (number of columns in matrix A) NBRows (number of rows in matrix B) NBCols (number of columns in matrix B) RESULTS: C (a matrix that is the product of A and B, with NARows rows and NBCols columns) INTERMEDIATES Row (row index for C) Col (column index for C) Index (summation index for matrix product) HEADER: C  Multiply( A, NARows, NACols, B, NBRows, NBCols)

Multiplication Algorithm (page 2) C  MakeNewArray(NARows) Row  0 Row < NARows true C[Row]  MakeNewArray(NBCols) Col  0 false Col < NBCols true false Sum  0 Index  0 Index < NACols true false C[Row][Col]  Sum Col  Col + 1 Sum  Sum + A[Row][Index]×B[Index][Col] Index  Index + 1 Row  Row + 1