8000 Added files via upload · abdul-git/ProgrammingAssignment2@3a5cb3b · GitHub
[go: up one dir, main page]

Skip to content

Commit 3a5cb3b

Browse files
committed
Added files via upload
1 parent 42b63f2 commit 3a5cb3b

File tree

1 file changed

+105
-62
lines changed

1 file changed

+105
-62
lines changed

README.md

Lines changed: 105 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,105 @@
1-
Introduction
2-
3-
This second programming assignment will require you to write an R function that is able to cache potentially time-consuming computations. For example, taking the mean of a numeric vector is typically a fast operation. However, for a very long vector, it may take too long to compute the mean, especially if it has to be computed repeatedly (e.g. in a loop). If the contents of a vector are not changing, it may make sense to cache the value of the mean so that when we need it again, it can be looked up in the cache rather than recomputed. In this Programming Assignment you will take advantage of the scoping rules of the R language and how they can be manipulated to preserve state inside of an R object.
4-
5-
Example: Caching the Mean of a Vector
6-
7-
In this example we introduce the <<- operator which can be used to assign a value to an object in an environment that is different from the current environment. Below are two functions that are used to create a special object that stores a numeric vector and caches its mean.
8-
9-
The first function, makeVector creates a special "vector", which is really a list containing a function to
10-
11-
set the value of the vector
12-
get the value of the vector
13-
set the value of the mean
14-
get the value of the mean
15-
makeVector <- function(x = numeric()) {
16-
m <- NULL
17-
set <- function(y) {
18-
x <<- y
19-
m <<- NULL
20-
}
21-
get <- function() x
22-
setmean <- function(mean) m <<- mean
23-
getmean <- function() m
24-
list(set = set, get = get,
25-
setmean = setmean,
26-
getmean = getmean)
27-
}
28-
The following function calculates the mean of the special "vector" created with the above function. However, it first checks to see if the mean has already been calculated. If so, it gets the mean from the cache and skips the computation. Otherwise, it calculates the mean of the data and sets the value of the mean in the cache via the setmean function.
29-
30-
cachemean <- function(x, ...) {
31-
m <- x$getmean()
32-
if(!is.null(m)) {
33-
message("getting cached data")
34-
return(m)
35-
}
36-
data <- x$get()
37-
m <- mean(data, ...)
38-
x$setmean(m)
39-
m
40-
}
41-
Assignment: Caching the Inverse of a Matrix
42-
43-
Matrix inversion is usually a costly computation and there may be some benefit to caching the inverse of a matrix rather than computing it repeatedly (there are also alternatives to matrix inversion that we will not discuss here). Your assignment is to write a pair of functions that cache the inverse of a matrix.
44-
45-
Write the following functions:
46-
47-
makeCacheMatrix: This function creates a special "matrix" object that can cache its inverse.
48-
cacheSolve: This function computes the inverse of the special "matrix" returned by makeCacheMatrix above. If the inverse has already been calculated (and the matrix has not changed), then cacheSolve should retrieve the inverse from the cache.
49-
Computing the inverse of a square matrix can be done with the solve function in R. For example, if X is a square invertible matrix, then solve(X) returns its inverse.
50-
51-
For this assignment, assume that the matrix supplied is always invertible.
52-
53-
In order to complete this assignment, you must do the following:
54-
55-
Fork the GitHub repository containing the stub R files at https://github.com/rdpeng/ProgrammingAssignment2 to create a copy under your own account.
56-
Clone your forked GitHub repository to your computer so that you can edit the files locally on your own machine.
57-
Edit the R file contained in the git repository and place your solution in that file (please do not rename the file).
58-
Commit your completed R file into YOUR git repository and push your git branch to the GitHub repository under your account.
59-
Submit to Coursera the URL to your GitHub repository that contains the completed R code for the assignment.
60-
Grading
61-
62-
This assignment will be graded via peer assessment.
1+
### Introduction
2+
3+
This second programming assignment will require you to write an R
4+
function that is able to cache potentially time-consuming computations.
5+
For example, taking the mean of a numeric vector is typically a fast
6+
operation. However, for a very long vector, it may take too long to
7+
compute the mean, especially if it has to be computed repeatedly (e.g.
8+
in a loop). If the contents of a vector are not changing, it may make
9+
sense to cache the value of the mean so that when we need it again, it
10+
can be looked up in the cache rather than recomputed. In this
11+
Programming Assignment you will take advantage of the scoping rules of
12+
the R language and how they can be manipulated to preserve state inside
13+
of an R object.
14+
15+
### Example: Caching the Mean of a Vector
16+
17+
In this example we introduce the `<<-` operator which can be used to
18+
assign a value to an object in an environment that is different from the
19+
current environment. Below are two functions that are used to create a
20+
special object that stores a numeric vector and caches its mean.
21+
22+
The first function, `makeVector` creates a special "vector", which is
23+
really a list containing a function to
24+
25+
1. set the value of the vector
26+
2. get the value of the vector
27+
3. set the value of the mean
28+
4. get the value of the mean
29+
30+
<!-- -->
31+
32+
makeVector <- function(x = numeric()) {
33+
m <- NULL
34+
set <- function(y) {
35+
x <<- y
36+
m <<- NULL
37+
}
38+
get <- function() x
39+
setmean <- function(mean) m <<- mean
40+
getmean <- function() m
41+
list(set = set, get = get,
42+
setmean = setmean,
43+
getmean = getmean)
44+
}
45+
46+
The following function calculates the mean of the special "vector"
47+
created with the above function. However, it first checks to see if the
48+
mean has already been calculated. If so, it `get`s the mean from the
49+
cache and skips the computation. Otherwise, it calculates the mean of
50+
the data and sets the value of the mean in the cache via the `setmean`
51+
function.
52+
53+
cachemean <- function(x, ...) {
54+
m <- x$getmean()
55+
if(!is.null(m)) {
56+
message("getting cached data")
57+
return(m)
58+
}
59+
data <- x$get()
60+
m <- mean(data, ...)
61+
x$setmean(m)
62+
m
63+
}
64+
65+
### Assignment: Caching the Inverse of a Matrix
66+
67+
Matrix inversion is usually a costly computation and there may be some
68+
benefit to caching the inverse of a matrix rather than computing it
69+
repeatedly (there are also alternatives to matrix inversion that we will
70+
not discuss here). Your assignment is to write a pair of functions that
71+
cache the inverse of a matrix.
72+
73+
Write the following functions:
74+
75+
1. `makeCacheMatrix`: This function creates a special "matrix" object
76+
that can cache its inverse.
77+
2. `cacheSolve`: This function computes the inverse of the special
78+
"matrix" returned by `makeCacheMatrix` above. If the inverse has
79+
already been calculated (and the matrix has not changed), then
80+
`cacheSolve` should retrieve the inverse from the cache.
81+
82+
Computing the inverse of a square matrix can be done with the `solve`
83+
function in R. For example, if `X` is a square invertible matrix, then
84+
`solve(X)` returns its inverse.
85+
86+
For this assignment, assume that the matrix supplied is always
87+
invertible.
88+
89+
In order to complete this assignment, you must do the following:
90+
91+
1. Fork the GitHub repository containing the stub R files at
92+
[https://github.com/rdpeng/ProgrammingAssignment2](https://github.com/rdpeng/ProgrammingAssignment2)
93+
to create a copy under your own account.
94+
2. Clone your forked GitHub repository to your computer so that you can
95+
edit the files locally on your own machine.
96+
3. Edit the R file contained in the git repository and place your
97+
solution in that file (please do not rename the file).
98+
4. Commit your completed R file into YOUR git repository and push your
99+
git branch to the GitHub repository under your account.
100+
5. Submit to Coursera the URL to your GitHub repository that contains
101+
the completed R code for the assignment.
102+
103+
### Grading
104+
105+
This assignment will be graded via peer assessment.

0 commit comments

Comments
 (0)
0