Basics Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Slides:



Advertisements
Similar presentations
More Flow Control Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
Advertisements

Basic Algorithms on Arrays. Learning Objectives Arrays are useful for storing data in a linear structure We learn how to process data stored in an array.
Lists Introduction to Computing Science and Programming I.
Arrays. Memory organization Table at right shows 16 bytes, each consisting of 8 bits Each byte has an address, shown in the column to the left
Multidimensional Arrays in Java Vidhu S. Kapadia.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
1 11/8/06CS150 Introduction to Computer Science 1 Arrays Chapter 8 page 477 November 13, 2006.
Lecture 7 Sept 19, 11 Goals: two-dimensional arrays (continued) matrix operations circuit analysis using Matlab image processing – simple examples Chapter.
Lecture 6 Sept 15, 09 Goals: two-dimensional arrays matrix operations circuit analysis using Matlab image processing – simple examples.
Arrays Chapter 8 page /24/07CS150 Introduction to Computer Science 1 Arrays (8.1)  One variable that can store a group of values of the same.
Structured Data Types and Encapsulation Mechanisms to create new data types: –Structured data Homogeneous: arrays, lists, sets, Non-homogeneous: records.
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
Python Crash Course Numpy Bachelors V1.0 dd Hour 1.
Introduction Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
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.
1 © 2012 The MathWorks, Inc. Speeding up MATLAB Applications.
A Level Computing#BristolMet Session Objectives U2#S6 MUST identify different data types used in programming aka variable types SHOULD describe each data.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
I Power Int 2 Computing Software Development High Level Language Constructs.
1 Data Structures CSCI 132, Spring 2014 Lecture 32 Tables I.
Python Crash Course Numpy 3 rd year Bachelors V1.0 dd Hour 5.
Linear Algebra Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Python Arrays. An array is a variable that stores a collection of things, like a list. For example a list of peoples names. We can access the different.
Working with Arrays in MATLAB
Defining a 2d Array A 2d array implements a MATRIX. Example: #define NUMROWS 5 #define NUMCOLS 10 int arr[NUMROWS][NUMCOLS];
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Python Mini-Course University of Oklahoma Department of Psychology Lesson 21 NumPy 6/11/09 Python Mini-Course: Lesson 21 1.
I Power Higher Computing Software Development High Level Language Constructs.
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Linear Algebra Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Introduction Copyright © Software Carpentry This work is licensed under the Creative Commons Attribution License See
Basic Flow Control Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
Matlab Programming for Engineers
Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
Python Crash Course Numpy & MatplotLib Sterrenkundig Practicum 2 V1.0 dd Hour 3.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers.
Array Declarations Arrays contain a fixed number of variables of identical type Array declaration and allocation are separate operations Declaration examples:
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
Data Structure and Algorithm: CIT231 Lecture 3: Arrays and ADT DeSiaMore DeSiaMorewww.desiamore.com/ifm1.
Arrays in MIPS Assembly Computer Organization and Assembly Language: Module 6.
Programming in C Arrays, Structs and Strings. 7/28/092 Arrays An array is a collection of individual data elements that is:An array is a collection of.
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.
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.
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.
Two dimensional arrays A two dimensional m x n array A is a collection of m. n elements such that each element is specified by a pair of integers (such.
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.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
CSE 1301 Lecture 14 2D Arrays Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
Indexing Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Python Aliasing Copyright © Software Carpentry 2010
PH2150 Scientific Computing Skills
CSCI N207 Data Analysis Using Spreadsheet
MATLAB Programming Indexing Copyright © Software Carpentry 2011
Topics Sequences Introduction to Lists List Slicing
Multidimensional array
MATLAB Programming Basics Copyright © Software Carpentry 2011
Dr. Sampath Jayarathna Cal Poly Pomona
Topics Sequences Introduction to Lists List Slicing
Dr. Sampath Jayarathna Old Dominion University
Dynamic Array: Implementation of Stack
Presentation transcript:

Basics Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See for more information. Matrix Programming

MatricesBasics NumPy ( Provides MATLAB-style arrays for Python And many other things A data parallel programming model –Write x*A*x.T to calculate xAx T –The computer takes care of the loops All encapsulated in special objects called arrays

MatricesBasics Create an array from a list >>> import numpy >>> vals = [1, 2, 3] >>> arr = numpy.array(vals) >>> arr array([1, 2, 3])

MatricesBasics Arrays are homogeneous –I.e., all values have the same type Allows values to be packed together –Saves memory –Faster to process valsarr int1×3

MatricesBasics So what does this do? >>> arr = numpy.array([1, 2.3]) >>> arr array([1., 2.3]) A float, not an int

MatricesBasics You can specify at creation time: >>> array([1, 2, 3, 4], dtype=float32) array([ 1., 2., 3., 4.])

MatricesBasics You can specify at creation time: >>> array([1, 2, 3, 4], dtype=float32) array([ 1., 2., 3., 4.]) Why would you want to specify a type?

MatricesBasics You can also specify the data type later >>> a = array([1, 2, 3, 4], dtype=float32) >>> a.astype(int) array([ 1, 2, 3, 4]) >>> a.dtype = int >>> a array([ , , , ])

MatricesBasics booluint[8,16,32,64 ] intfloat int8float[32,64,128 ] int16complex int32complex[64,126] int64 Basic data types (in increasing order) are:

MatricesBasics Many other ways to create arrays >>> z = numpy.zeros((2, 3)) >>> z array([[0., 0., 0.], [0., 0., 0.]]) Type is float unless something else specified

MatricesBasics Many other ways to create arrays >>> z = numpy.zeros((2, 3)) >>> z array([[0., 0., 0.], [0., 0., 0.]]) Type is float unless something else specified What do these do? >>> block = numpy.ones((4, 5)) >>> mystery = numpy.identity(4)

MatricesBasics Can create arrays without filling in values >>> x = numpy.empty((2, 2)) >>> x array([[ e-297, e+173], [ e-309, e+000]]) "Values" will be whatever bits were in memory Should not be used without being initialized

MatricesBasics Can create arrays without filling in values >>> x = numpy.empty((2, 2)) >>> x array([[ e-297, e+173], [ e-309, e+000]]) "Values" will be whatever bits were in memory Should not be used without being initialized When is this useful?

MatricesBasics Assigning creates alias: does not copy data >>> first = numpy.ones((2, 2)) >>> first array([[1., 1.], [1., 1.]]) >>> second = first >>> second[0, 0] = 9 >>> first array([[9., 1.], [1., 1.]])

MatricesBasics Assigning creates alias: does not copy data >>> first = numpy.ones((2, 2)) >>> first array([[1., 1.], [1., 1.]]) >>> second = first >>> second[0, 0] = 9 >>> first array([[9., 1.], [1., 1.]]) Not second[0][0]

MatricesBasics Use the array.copy method >>> first array([[1., 1.], [1., 1.]]) >>> second = first.copy() >>> second[0, 0] = 9 >>> first array([[1., 1.], [1., 1.]])

MatricesBasics Arrays also have properties >>> first array([[1., 1.], [1., 1.]]) >>> first.shape (2, 2) >>> block = numpy.zeros((4, 7, 3)) >>> block.shape (4, 7, 3)

MatricesBasics Arrays also have properties >>> first array([[1., 1.], [1., 1.]]) >>> first.shape (2, 2) >>> block = numpy.zeros((4, 7, 3)) >>> block.shape (4, 7, 3) Not a method

MatricesBasics Arrays also have properties >>> first array([[1., 1.], [1., 1.]]) >>> first.shape (2, 2) >>> block = numpy.zeros((4, 7, 3)) >>> block.shape (4, 7, 3) Consistent

MatricesBasics array.size is the total number of elements >>> first.size 4 >>> block.size 84 2×2 4×7×3

MatricesBasics Reverse on all axes with array.transpose >>> first = numpy.array([[1, 2, 3], [4, 5, 6]]) >>> first.transpose() array([[1, 4], [2, 5], [3, 6]]) >>> first array([[1, 2, 3], [4, 5, 6]])

MatricesBasics Flatten arrays using array.ravel >>> first = numpy.zeros((2, 2, 2)) >>> second = first.ravel() >>> second.shape (8,)

MatricesBasics Think about the 2×4 array A: >>> A array([[1, 2, 3, 4], [5, 6, 7, 8]]) A looks 2-dimensional But computer memory is 1-dimensional Must decide how to lay out values

MatricesBasics Logical Physical Row-major order concatenates the rows Used by C and Python

MatricesBasics Logical Physical Column-major order concatenates the columns Used by Fortran and MATLAB

MatricesBasics No difference in usability or performance… …but causes headaches when passing data from one language to another (Just like 0-based vs. 1-based indexing)

MatricesBasics No difference in usability or performance… …but causes headaches when passing data from one language to another (Just like 0-based vs. 1-based indexing) What order are 3-dimensional arrays stored in?

MatricesBasics Can reshape arrays in many other ways >>> first = numpy.array([1, 2, 3, 4, 5, 6]) >>> first.shape (6,) >>> second = first.reshape(2, 3) >>> second array([[1, 2, 3], [4, 5, 6]]) Also aliases the data Tuple with 1 element Not packed into a tuple

MatricesBasics New shape must have same size as old >>> first = numpy.zeros((2, 2)) >>> first.reshape(3, 3) ValueError: total size of new array must be unchanged Cannot possibly work because it is just creating an alias for the existing data

MatricesBasics Change physical size using array.resize >>> block array([[ 10, 20, 30], [110, 120, 130], [210, 220, 230]]) >>> block.resize(2, 2) >>> block array([[ 10, 20], [110, 120]])

MatricesBasics Change physical size using array.resize >>> block array([[ 10, 20, 30], [110, 120, 130], [210, 220, 230]]) >>> block.resize(2, 2) >>> block array([[ 10, 20], [110, 120]]) What happens when the array grows?

MatricesBasics Review: –Arrays are blocks of homogeneous data –Most operations create aliases –Can be reshaped (size remains the same) –Or resized

November 2010 created by Richard T. Guy Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See for more information.