Ch.6 Efficient calculations. 6.1 Vectorized computations  Replacing numbers with ‘for’ expression tmp <- Sys.time() x <- rnorm(15000) for (i in 1:length(x)){

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Parallel R Andrew Jaffe Computing Club 4/5/2015. Overview Introduction multicore Array jobs The rest.
The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a.
February 26, 2015Applied Discrete Mathematics Week 5: Mathematical Reasoning 1 Addition of Integers Example: Add a = (1110) 2 and b = (1011) 2. a 0 + b.
Section 4.2 – Multiplying Matrices Day 2
Matrix Multiplication To Multiply matrix A by matrix B: Multiply corresponding entries and then add the resulting products (1)(-1)+ (2)(3) Multiply each.
Maths for Computer Graphics
Table of Contents Matrices - Multiplication Assume that matrix A is of order m  n and matrix B is of order p  q. To determine whether or not A can be.
Chapter 7 Matrix Mathematics Matrix Operations Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
1.3 Matrices and Matrix Operations.
Algebra 2: Lesson 5 Using Matrices to Organize Data and Solve Problems.
Chapter 5. Loops are common in most programming languages Plus side: Are very fast (in other languages) & easy to understand Negative side: Require a.
Matlab tutorial course Lesson 2: Arrays and data types
Basic Operations MultiplicationDeterminants Cramer’s RuleIdentityInverses Solving Systems of equations APPENDIX.
1 Week 3: Vectors and Matrices (Part III) READING: 2.2 – 2.4 EECS Introduction to Computing for the Physical Sciences.
Ch 1.3 – Order of Operations
1.3 Matrices and Matrix Operations. Definition A matrix is a rectangular array of numbers. The numbers in the array are called the entries in the matrix.
Programming in R coding, debugging and optimizing Katia Oleinik Scientific Computing and Visualization Boston University
Introduction to MATLAB Session 3 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2011.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Introduction to R. Why use R Its FREE!!! And powerful, fairly widely used, lots of online posts about it Uses S -> an object oriented programing language.
Co. Chapter 3 Determinants Linear Algebra. Ch03_2 Let A be an n  n matrix and c be a nonzero scalar. (a)If then |B| = …….. (b)If then |B| = …..... (c)If.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Inverse of a Matrix Multiplicative Inverse of a Matrix For a square matrix A, the inverse is written A -1. When A is multiplied by A -1 the result is the.
ME 142 Engineering Computation I Matrix Operations in Excel.
4.1 Using Matrices Warm-up (IN) Learning Objective: to represent mathematical and real-world data in a matrix and to find sums, differences and scalar.
Matrices: Simplifying Algebraic Expressions Combining Like Terms & Distributive Property.
Digital Image Processing Lecture 12: Image Topology (Suppl)
ME 142 Engineering Computation I Matrix Operations in Excel.
General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.
Chapter 1 Section 1.5 Matrix Operations. Matrices A matrix (despite the glamour of the movie) is a collection of numbers arranged in a rectangle or an.
1.7 Linear Independence. in R n is said to be linearly independent if has only the trivial solution. in R n is said to be linearly dependent if there.
Loops CS 103 February 23, Review FOR is a control construct that provides a loop for ii = 1:10 fprintf(‘%d \n’, ii) end FOR is a control construct.
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
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.
Notes Over 4.2 Finding the Product of Two Matrices Find the product. If it is not defined, state the reason. To multiply matrices, the number of columns.
Lecture 4 Speeding up your code Trevor A. Branch FISH 553 Advanced R School of Aquatic and Fishery Sciences University of Washington.
Matrix Algebra Definitions Operations Matrix algebra is a means of making calculations upon arrays of numbers (or data). Most data sets are matrix-type.
FUNCTIONS The parts of a function In order to work correctly, a function must be written a specific way, which is called the syntax. The basic syntax for.
Working with R Marcin Krzystanek PhD student at Cancer Systems Biology group, CBS Technical University of Denmark.
Control Structures Hara URL:
Review > plot(x=primates$Bodywt, y=primates$Brainwt, xlim=c(0,300), ylim=c(0,1400), cex=2, pch=21, col="black", bg="salmon", xlab = "Body weight (kg)",
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
An Introduction to Matrix Algebra Math 2240 Appalachian State University Dr. Ginn.
Notes Over 1.2.
13.4 Product of Two Matrices
Programming in R coding, debugging and optimizing Katia Oleinik Scientific Computing and Visualization Boston University
Chapter 7 Matrix Mathematics
The R Book Chapter 2: Essentials of the R Language Session 6.
Ch.4 Data Manipulation.
Conditional Statements if/else
Selection—Making Decisions
Matrix Multiplication
Factoring if/else code
Ch.5 Writing functions.
JavaScript Selection Statement Creating Array
Recoding III: Introducing apply()
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Vectorized Code, Logical Indexing
Recoding III: Introducing apply()
Matrices and Matrix Operations
CSCI N317 Computation for Scientific Applications Unit R
CSCI N207 Data Analysis Using Spreadsheet
Linear Algebra Lecture 24.
Simplifying Numerical Expressions
Chapter 5 R programming Instructor: Li, Han.
Calculate 81 ÷ 3 = 27 3 x 3 x 3 3 x 3 x 3 x 3 ÷ 3 = This could be written as
Presentation transcript:

Ch.6 Efficient calculations

6.1 Vectorized computations  Replacing numbers with ‘for’ expression tmp <- Sys.time() x <- rnorm(15000) for (i in 1:length(x)){ if(x[i] > 1){ x[i] <- 1 } Sys.time - tmp Time difference of secs

6.1 Vectorized computations  Replacing numbers with a ‘vectorized’ function tmp <- Sys.time() x <- rnorm(15000) x[x>1] <- 1 Sys.time() - tmp Time difference of secs

6.1 Vectorized computations  The if-else expression tmp <- Sys.time() x <- rnorm(15000) for (i in 1:length(x)){ if(x[i] > 1){ x[i] <- 1 } else { x[i] <- -1 } } Sys.time() - tmp Time difference of secs

6.1 Vectorized computations  a vectorized function ‘ifelse’ tmp <- Sys.time() x <- rnorm(15000) x 1,1,-1) tmp - Sys.time() Time difference of secs

6.1 Vectorized computations  The cumsum function x <- 1:10 y <- cumsum(x) y [1]

6.1 Vectorized computations  Matrix multiplication C <- A%*%B  If we choose the elements of the matrices A and B ‘cleverly' explicit for-loops could be avoided. For example: A <- matrix(rnorm(1000),ncol=10) n <- dim(A)[1] mat.means <- t(A)%*%rep(1/n,n)

6.2 The apply and outer functions  To calculate the means of all columns in a matrix, use the following syntax: M <- matrix(rnorm(10000),ncol=100) apply(M,1,mean) colMeans(M) rowMeans(M) the apply function

6.2 The apply and outer functions  The function apply can also be used with a function that you have written yourself.  Extra arguments to your function must now be passed trough the apply function.  The following construction calculates the number of entries that is larger than a threshold d for each column in a matrix the apply function

6.2 The apply and outer functions tresh d) } M <- matrix(rnorm(10000),ncol=100) apply(M,1,tresh,d=0.6) [1] [17] [33] [49] [65] [81] [97] ?lapply ?sapply the apply function

6.2 The apply and outer functions  This function is used to run another function on the cells of a so called ragged array. A ragged array is a pair of two vectors of the same size. One of them contains data and the other contains grouping information. The following data vector x en grouping vector y form an example of a ragged array. x <- rnorm(50) y <- as.factor(sample(c("A","B","C","D"), size=50, replace=T)) tapply(x, y, mean, trim = 0.3) A B C D The tapply function

6.2 The apply and outer functions  For every combination of the vector elements of x and y this function is evaluated. Some examples are given by the code below. x <- 1:3 y <- 1:3 z <- outer(x,y,FUN="-") z [,1] [,2] [,3] [1,] [2,] [3,] The outer function

6.2 The apply and outer functions x <- c("A", "B", "C", "D") y <- 1:9 z <- outer(x, y, paste, sep = "") z # function with grid combination x <- seq(-4,4,l=50) y <- x myf <- function(x,y){ sin(x)+cos(y)} z <- outer(x,y, FUN = myf) persp(x,y,z, theta=45, phi=45, shade = 0.45) The outer function

6.2 The apply and outer functions The outer function

6.2 Other vectorized functions colSums (x, na.rm = FALSE, dims = 1) rowSums (x, na.rm = FALSE, dims = 1) colMeans(x, na.rm = FALSE, dims = 1) rowMeans(x, na.rm = FALSE, dims = 1) rowsum(x, group, reorder = TRUE,...) aggregate(x, by, FUN,..., simplify = TRUE) sweep(x, MARGIN, STATS, FUN="-",...) scale(x, center = TRUE, scale = TRUE) …

“The quiet statisticians have changed our world -not by discovering new facts or technical developments but by changing the ways that we reason, experiment and form our opinions... “ - by Ian Hacking Thank you !