Lab 3. Why Compressed Row Storage –A sparse matrix has a lot of elements of value zero. –Using a two dimensional array to store a sparse matrix wastes.

Slides:



Advertisements
Similar presentations
ArrayList Difference of Array and ArrayList [Sample code] TestArrayList.java.
Advertisements

1 Convert this problem into our standard form: Minimize 3 x x 2 – 6 x 3 subject to 1 x x 2 – 3 x 3 ≥ x x 2 – 6 x 3 ≤ 5 7 x 1.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013.
Arrays. 1D Array Representation In C 1-dimensional array x = [a, b, c, d] map into contiguous memory locations Memory abcd start location(x[i]) = start.
CSCI 317 Mike Heroux1 Sparse Matrix Computations CSCI 317 Mike Heroux.
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
1 2-D Arrays Overview l Why do we need Multi-dimensional array l 2-D array declaration l Accessing elements of a 2-D array l Declaration using Initializer.
CMPUT 101 Lab #6 October 29, :00 – 17:00. Array in C/C++ Array is a structure type variable. One dimension of array int: int num[3]; There are.
Java: How to Program Arrays and ArrayLists Summary Yingcai Xiao.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
CS1101X: Programming Methodology Recitation 9 Recursion II.
Multi-Dimensional Arrays Rectangular & Jagged Plus: More 1D traversal.
Multi-Dimensional Arrays in Java "If debugging is the process of removing software bugs, then programming must be the process of putting them in." -- Edsger.
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
Arrays and Matrices 황승원 Fall 2010 CSE, POSTECH. 2 2 Any real-life matrix you know?
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
1 Lecture # 4. * An array is a group of contiguous or related data items that share a common name. * Each value is stored at a specific position * Position.
Arrays- Part 2 Spring 2013Programming and Data Structure1.
Matrix Arithmetic. A matrix M is an array of cell entries (m row,column ) and it must have rectangular dimensions (Rows x Columns). Example: 3x x.
13.1 Matrices and Their Sums
1 Chapter 8 Multi-Dimensional Arrays. 2 1-Dimentional and 2-Dimentional Arrays In the previous chapter we used 1-dimensional arrays to model linear collections.
First Data Structure Definition A data structure is a data type whose components are smaller data structures and/or simple data types.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
2D-Arrays Quratulain. Learning Objectives Two-dimensional arrays Declaration Initialization Applications.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Mixing integer and floating point numbers in an arithmetic operation.
ITI 1120 Lab #9 Slides by: Diana Inkpen and Alan Williams.
Object-Oriented Programming Simple Stack Implementation.
Data Structure CS 322. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays LAB#1 : Arrays.
Sparse Vectors & Matrices Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7.
Get Longest Run Index (FR) public int getLongestRunIndex(int []values) { int maxRunStart = -1, maxRunLength = 1; int runStart = 0, runLength = 1; for(int.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
1. 2  Introduction  Array Operations  Number of Elements in an array One-dimensional array Two dimensional array Multi-dimensional arrays Representation.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
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.
Lecture 16 Linked Lists. In this lecture Fundamentals Applications Memory Allocation Creating a List Inserting Nodes.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
Array and String.
Multi-dimensional Array 1 Multi-dimensional array refers to an array with more than one index. It is a logical representation. On physical storage, the.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
L20: Sparse Matrix Algorithms, SIMD review November 15, 2012.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Two-Dimensional Data Class of 5 students Each student has 3 test scores Store this information in a two- dimensional array First dimension: which student.
1 ARRAYS AND STRUCTURES. 2 Arrays Array: a set of index and value data structure For each index, there is a value associated with that index. representation.
A rectangular array of numeric or algebraic quantities subject to mathematical operations. The regular formation of elements into columns and rows.
Arrays. 1D Array Representation In C++ 1-dimensional array x = [a, b, c, d] map into contiguous memory locations Memory abcd start location(x[i]) = start.
1 2-d Arrays. 2 Two Dimensional Arrays We have seen that an array variable can store a list of values Many applications require us to store a table of.
1 Why do we need arrays? Problem - Input 5 test scores => int test1,test2,test3,test4,test5 100 test scores? 10,000 employees? A structured data type is.
L EC. 05: A RRAYS 0. C ONTENT  Features of arrays  Declaring, creating and using arrays  Enhanced for -construct  Variable length parameter-list 1.
Lecture 13 Sparse Matrix-Vector Multiplication and CUDA Libraries
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. System.out.println(“Please enter grade.
Matrix Operations.
Matrix Operations.
Case Study 2 – Marking a Multiple-choice Test
Decision statements. - They can use logic to arrive at desired results
Matrices Elements, Adding and Subtracting
Arrays of Objects // The following does NOT create memory for
class PrintOnetoTen { public static void main(String args[]) {
Multidimensional array
Arrays in Java.
Chapter 8 Multidimensional Arrays
22C:21 Problem 2.3 Solution Outline.
Presentation transcript:

Lab 3

Why Compressed Row Storage –A sparse matrix has a lot of elements of value zero. –Using a two dimensional array to store a sparse matrix wastes a lot of memory. –Compressed Row Storage (CRS) format only stores the nonzero elements. –Using CRS format to store a sparse matrix will save a lot of memory.

Compressed Row Storage – val array stores the values of the nonzero elements in a row-wise fashion. – col_ind array stores the corresponding column indices of the elements in the val array. E.g. col_ind[5] stores the column index of val[5]. – row_ptr array stores the locations in the val array and col_ind array that start a row.

val …42 col_ind … row_ptr The number of nonzero elements of row i = row_ptr[i+1] - row_ptr[ i ] The value of nonzero elements of row i: val[ row_ptr[ i ] ],..., val[ row_ptr[ i+1 ] -1 ] the number of rows +1 the number of nonzero elements in a matrix

//Compressed Row Storage format //for a sparse square (mSize X mSize) matrix public class CRS{ //the values of the nonzero elements of the matrix float[] val; //the column indices of the elements in val array int[] col_idx; //locations in the val and col_idx array that start a row int[] row_ptr; //the size of the matrix: the number of rows int mSize=0; //constructor that takes a sparse matrix and convert it to a CRS object CRS( float[][] matrix){... } //print the matrix in CRS format. public void printCRS(){... } //test the program public static void main(String[] args){... }

CRS( float[][] matrix){ int i, j, index; //the total number of nonzero in the matrix int totalNonZeros; //get the number of rows and columns mSize = matrix.length; //get the total number of nonzero entries in the matrix totalNonZeros = 0; for( i=0; i<mSize; i++){ for( j=0; j<mSize; j++){ if(matrix[i][j] != 0) totalNonZeros++; } //allocate memory for val and col_idx array val = new float[ totalNonZeros ]; col_idx = new int[ totalNonZeros ]; //allocate memory for row_ptr row_ptr = new int[ mSize+1]; row_ptr[ 0 ] = 0;

//store the matrix in CRS format index = 0;// point to the next position to store the value for( i=0; i<mSize; i++ ){//each row for( j=0; j<mSize; j++ ){//each column if( matrix[i][j] != 0 ){ //add the value to val val[ index ] = matrix[ i ][ j ]; //record the column index in col_idx col_idx[ index ] = j; index++; } //update row_ptr row_ptr[ i+1 ] = index;} }//end of CRS( float[][] matrix) xx... index val

//test the program public static void main(String[] args){ float[][] matrix = {{10, 0, 0, 0, -2, 0}, {3, 9, 0, 0, 0, 3}, {0, 7, 8, 7, 0, 0}, {3, 0, 8, 7, 5, 0}, {0, 8, 0, 9, 9, 13}, {0, 4, 0, 0, 2, -1}}; System.out.println("the original sparse matrix"); for(int i=0; i<6; i++){ for(int j=0; j<6; j++){ System.out.print(matrix[i][j]+", "); } System.out.println(); } System.out.println(); CRS crs = new CRS(matrix); crs.printMatrix(); crs.printCRS(); }