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

Slides:



Advertisements
Similar presentations
2.3 Modeling Real World Data with Matrices
Advertisements

4.1 Introduction to Matrices
Matrices A matrix is a rectangular array of quantities (numbers, expressions or function), arranged in m rows and n columns x 3y.
Visualization Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
Maths for Computer Graphics
Part 3 Chapter 8 Linear Algebraic Equations and Matrices PowerPoints organized by Dr. Michael R. Gustafson II, Duke University All images copyright © The.
100’s of free ppt’s from library
CE 311 K - Introduction to Computer Methods Daene C. McKinney
Chapter 7 Matrix Mathematics Matrix Operations Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Introduction Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
Chapter 2 Systems of Linear Equations and Matrices Section 2.4 Multiplication of Matrices.
CSEP 590tv: Quantum Computing Dave Bacon June 29, 2005 Today’s Menu Administrivia Complex Numbers Bra’s Ket’s and All That Quantum Circuits.
Recommendations Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Algebra 2: Lesson 5 Using Matrices to Organize Data and Solve Problems.
Little Linear Algebra Contents: Linear vector spaces Matrices Special Matrices Matrix & vector Norms.
1.1.2 INTRODUCTION TO SYSTEMS OF LINEAR EQUATIONS Chapter 1: Systems of Linear Equations and Matrices SWBAT: Redefine algebraic operations as Elementary.
Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall.
Review of Matrices Or A Fast Introduction.
Learner’s Guide to MATLAB® Chapter 2 : Working with Arrays.
Lecture 28: Mathematical Insight and Engineering.
Algebra 3: Section 5.5 Objectives of this Section Find the Sum and Difference of Two Matrices Find Scalar Multiples of a Matrix Find the Product of Two.
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.
Lesson 11-1 Matrix Basics and Augmented Matrices Objective: To learn to solve systems of linear equation using matrices.
MATLAB 程式設計 Learning Linear Algebra 方煒 台大生機系. MATLAB 程式設計 Vector Products, Dot and Cross a=[1,2,3];b=[3,2,1]; C=a*b D=a.*b E=dot(a,b) F=cross(a,b)
2009/9 1 Matrices(§3.8)  A matrix is a rectangular array of objects (usually numbers).  An m  n (“m by n”) matrix has exactly m horizontal rows, and.
Linear Algebra Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Working with Arrays in MATLAB
The goal is to give an introduction to the mathematical operations with matrices. A matrix is a 2-dimensional arrangement of (real valued) data. The data.
Discrete Mathematics 1 Kemal Akkaya DISCRETE MATHEMATICS Lecture 16 Dr. Kemal Akkaya Department of Computer Science.
Introduction Copyright © Software Carpentry 2010 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
Copyright 2013, 2010, 2007, Pearson, Education, Inc. Section 7.3 Matrices.
Slide Copyright © 2009 Pearson Education, Inc. 7.3 Matrices.
Copyright © 2011 Pearson Education, Inc. Solving Linear Systems Using Matrices Section 6.1 Matrices and Determinants.
Section 4Chapter 4. 1 Copyright © 2012, 2008, 2004 Pearson Education, Inc. Objectives Solving Systems of Linear Equations by Matrix Methods Define.
10.3 Systems of Linear Equations: Matrices. A matrix is defined as a rectangular array of numbers, Column 1Column 2 Column jColumn n Row 1 Row 2 Row 3.
3.6 Solving Systems Using Matrices You can use a matrix to represent and solve a system of equations without writing the variables. A matrix is a rectangular.
Basics Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
Sec 4.1 Matrices.
Unit 3 Matrix Arithmetic IT Disicipline ITD 1111 Discrete Mathematics & Statistics STDTLP 1 Unit 3 Matrix Arithmetic.
Section 9-1 An Introduction to Matrices Objective: To perform scalar multiplication on a matrix. To solve matrices for variables. To solve problems using.
Algebra Matrix Operations. Definition Matrix-A rectangular arrangement of numbers in rows and columns Dimensions- number of rows then columns Entries-
STA302: Regression Analysis. Statistics Objective: To draw reasonable conclusions from noisy numerical data Entry point: Study relationships between variables.
Finishing up Chapter 5. Will this code enter the if statement? G=[30,55,10] if G
Indexing Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
4.1 An Introduction to Matrices Katie Montella Mod. 6 5/25/07.
Chapter 5: Matrices and Determinants Section 5.1: Matrix Addition.
Chapter 1 Computing Tools Variables, Scalars, and Arrays Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Matrices. Variety of engineering problems lead to the need to solve systems of linear equations matrixcolumn vectors.
Matrices and systems of Equations. Definition of a Matrix * Rectangular array of real numbers m rows by n columns * Named using capital letters * First.
A rectangular array of numeric or algebraic quantities subject to mathematical operations. The regular formation of elements into columns and rows.
13.3 Product of a Scalar and a Matrix.  In matrix algebra, a real number is often called a.  To multiply a matrix by a scalar, you multiply each entry.
1 Matrix Math ©Anthony Steed Overview n To revise Vectors Matrices.
An Introduction to Matrix Algebra Math 2240 Appalachian State University Dr. Ginn.
Lesson 43: Working with Matrices: Multiplication
12-1 Organizing Data Using Matrices
Matrix Operations Free powerpoints at
Matrix Operations.
Matrix Addition and Scalar Multiplication
Matrix Multiplication
Matrix Operations.
Matrix Operations Free powerpoints at
Matrix Operations.
Matrix Operations Free powerpoints at
7.3 Matrices.
MATLAB Programming Indexing Copyright © Software Carpentry 2011
3.5 Perform Basic Matrix Operations
Chapter 4 Matrices & Determinants
Presentation transcript:

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

Linear Algebra NumPy arrays make operations on rectangular data easy But they are not quite mathematical matrices >>> a = array([[1, 2], [3, 4]]) >>> a * a array([[ 1, 4], [ 9, 16]]) Operators act elementwise

Matrix ProgrammingLinear Algebra So this does what you think >>> a + a array([[ 2, 4], [ 6, 8]]) And NumPy is sensible about scalar values >>> a + 1 array([[ 2, 3], [ 4, 5]])

Matrix ProgrammingLinear Algebra Lots of useful utilities >>> sum(a) 10 >>> sum(a, 0) array([4, 6]) >>> sum(a, 1) array([3, 7])

Matrix ProgrammingLinear Algebra Lots of useful utilities >>> sum(a) 10 >>> sum(a, 0) array([4, 6]) >>> sum(a, 1) array([3, 7]) What does sum(a, 2) do?

Matrix ProgrammingLinear Algebra Example: disease statistics –One row per patient –Columns are hourly responsive T cell counts >>> data[:, 0] # t 0 count for all patients array([1, 0, 0, 2, 1]) >>> data[0, :] # all samples for patient 0 array([1, 3, 3, 5, 12, 10, 9])

Matrix ProgrammingLinear Algebra Example: disease statistics –One row per patient –Columns are hourly responsive T cell counts >>> data[:, 0] # t 0 count for all patients array([1, 0, 0, 2, 1]) >>> data[0, :] # all samples for patient 0 array([1, 3, 3, 5, 12, 10, 9]) Why are these 1D rather than 2D?

Matrix ProgrammingLinear Algebra >>> mean(data) Intriguing, but not particularly meaningful >>> mean(data, 0) # over time array([ 0.8, 2.6, 4.4, 6.4, 10.8, 11., 12.2]) >>> mean(data, 1) # per patient array([ 6.14, 4.28, 16.57, 2.14, 5.29])

Matrix ProgrammingLinear Algebra Select the data for people who started with a responsive T cell count of 0 >>> data[:, 0] array([1., 0., 0., 2., 1.]) >>> data[:, 0] == 0. array([False, True, True, False, False], dtype=bool) >>> data[ data[:, 0] == 0 ] array([[ 0., 1., 2., 4., 8., 7., 8.], [ 0., 4., 11., 15., 21., 28., 37.]])

Matrix ProgrammingLinear Algebra Find the mean T cell count over time for people who started with a count of 0 >>> data[:, 0] Column 0

Matrix ProgrammingLinear Algebra Find the mean T cell count over time for people who started with a count of 0 >>> data[:, 0] == 0 Column 0 is 0

Matrix ProgrammingLinear Algebra Find the mean T cell count over time for people who started with a count of 0 >>> data[ data[:, 0] == 0 ] Rows where column 0 is 0

Matrix ProgrammingLinear Algebra Find the mean T cell count over time for people who started with a count of 0 >>> mean(data[ data[:, 0] == 0 ], 0) Mean along axis 0 of rows where column 0 is 0

Matrix ProgrammingLinear Algebra Find the mean T cell count over time for people who started with a count of 0 >>> mean(data[ data[:, 0] == 0 ], 0) array([ 0., 2.5, 6.5, 9.5, 14.5, 17.5, 22.5])

Matrix ProgrammingLinear Algebra Find the mean T cell count over time for people who started with a count of 0 >>> mean(data[ data[:, 0] == 0 ], 0) array([ 0., 2.5, 6.5, 9.5, 14.5, 17.5, 22.5]) Key to good array programming: no loops! Just as true for MATLAB or R as for NumPy

Matrix ProgrammingLinear Algebra What about "real" matrix multiplication? >>> a = array([[1, 2], [3, 4]]) >>> dot(a, a) array([[ 7, 10], [15, 22]]) >>> v = arange(3) # [0, 1, 2] >>> dot(v, v) # 0*0 + 1*1 + 2*2 5

Matrix ProgrammingLinear Algebra Dot product only works for sensible shapes >>> dot(ones((2, 3)), ones((2, 3))) ValueError: objects are not aligned NumPy does not distinguish row/column vectors >>> v = array([1, 2]) >>> a = array([[1, 2], [3, 4]]) >>> dot(v, a) array([ 7, 10]) >>> dot(a, v) array([ 5, 11]) = =

Matrix ProgrammingLinear Algebra Can also use the matrix subclass of array >>> m = matrix([[1, 2], [3, 4]]) >>> m matrix([[ 1, 2], [ 3, 4]]) >>> m*m matrix([[ 7, 10], [15, 22]]) Use matrix(a) or array(m) to convert

Matrix ProgrammingLinear Algebra Which should you use? If your problem is linear algebra, matrix will probably be more convenient –Treats vectors as N×1 matrices Otherwise, use array –Especially if you're representing grids, rather than mathematical matrices

Matrix ProgrammingLinear Algebra Always look at ith_Doc before writing any functions of your own conjugate convolve correlate diagonal fft gradient histogram lstsq npv roots solve svd Fast… …and someone else has debugged them

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.