Skip to content

Commit c97454d

Browse files
committed
rprog-013 assignment 2 ffirst commit
1 parent 7f657dd commit c97454d

File tree

2 files changed

+70
-105
lines changed

2 files changed

+70
-105
lines changed

README.md

Lines changed: 20 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,28 @@
11
### Introduction
22

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.
3+
This is programming assignment 2 of the Data Science Specialisation
4+
course module "R Programming" https://class.coursera.org/rprog-013
145

15-
### Example: Caching the Mean of a Vector
6+
### makeCacheMatrix
167

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.
8+
This function creates a "special" matrix vector x and exposes it
9+
through 4 functions that are returned as a list:
10+
1. get() returns the input matrix x
11+
2. set(y) assigns y, the matrix value argument to x
12+
3. getminv() returns inv, the cached inverse of matrix x
13+
4. setminv(minv) assigns the matrix inverse calculation minv to inv
2114

22-
The first function, `makeVector` creates a special "vector", which is
23-
really a list containing a function to
15+
Essentially makeCacheMatrix is a wrapper object for
16+
x - the input matrix (assumed to be invertible)
17+
y - the inverse of matrix x
18+
associated getter and setter methods
2419

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
20+
### cacheSolve
2921

30-
<!-- -->
22+
This function calculates the inverse of the matrix
23+
returned by the makeCacheMatrix$get function.
24+
It also caches the inverse result so that it can be returned
25+
directly on subsequent calls without having to recalculate it.
3126

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.
27+
This function depends on an instance of the makeCacheMatrix function,
28+
passed in as mandatory argument makem.

cachematrix.R

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,57 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
5-
1+
##
2+
## This is programming assignment 2 of the Data Science Specialisation
3+
## course module "R Programming" https://class.coursera.org/rprog-013
4+
##-----------------------------------------------------------------------
5+
## makeCacheMatrix
6+
##
7+
## This function creates a "special" matrix vector x and exposes it
8+
## through 4 functions that are returned as a list:
9+
## 1. get() returns the input matrix x
10+
## 2. set(y) assigns y, the matrix value argument to x
11+
## 3. getminv() returns inv, the cached inverse of matrix x
12+
## 4. setminv(minv) assigns the matrix inverse calculation minv to inv
13+
##
14+
## Essentially makeCacheMatrix is a wrapper object for
15+
## x - the input matrix (assumed to be invertible)
16+
## y - the inverse of matrix x
17+
## associated getter and setter methods
18+
##
619
makeCacheMatrix <- function(x = matrix()) {
720

21+
inv <- NULL
22+
set <- function(y) {
23+
x <<- y
24+
inv <<- NULL
25+
}
26+
get <- function() x
27+
setminv <- function(minv) inv <<- minv
28+
getminv <- function() inv
29+
list(set = set, get = get,
30+
setminv = setminv,
31+
getminv = getminv)
832
}
933

1034

11-
## Write a short comment describing this function
35+
##
36+
## cacheSolve
37+
##
38+
## This function calculates the inverse of the matrix
39+
## returned by the makeCacheMatrix$get function.
40+
## It also caches the inverse result so that it can be returned
41+
## directly on subsequent calls without having to recalculate it.
42+
##
43+
## This function depends on an instance of the makeCacheMatrix function,
44+
## passed in as mandatory argument makem.
45+
##
46+
cacheSolve <- function(makem, ...) {
1247

13-
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
48+
minv <- makem$getminv()
49+
if(!is.null(minv)) {
50+
message("getting cached inverted matrix data")
51+
return(minv)
52+
}
53+
data <- makem$get()
54+
minv <- solve(data, ...)
55+
makem$setminv(minv)
56+
minv
1557
}

0 commit comments

Comments
 (0)