Skip to content

Commit de3414f

Browse files
author
Val Redchenko
committed
R Programming Assignment 2
1 parent 7f657dd commit de3414f

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

cachematrix.R

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,38 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## These two functions work in tandem to enable caching for a square matrix
2+
## inversion calculation.
33

4-
## Write a short comment describing this function
4+
## This function takes a matrix, stores it in a cache variable
5+
## and provides a list of setter and getter functions.
6+
## It acts as a wrapper around the matrix, retaining the inverse in cache
57

68
makeCacheMatrix <- function(x = matrix()) {
7-
9+
inverseX <- NULL
10+
set <- function(y) {
11+
x <<- y
12+
m <<- NULL
13+
}
14+
get <- function() x
15+
setInverse <- function(inverse) inverseX <<- inverse
16+
getInverse <- function() inverseX
17+
list( set = set, get = get, setIntverse = setInverse, getInverse = getInverse )
818
}
919

1020

11-
## Write a short comment describing this function
21+
## This function takes a list returned by makeCacheMatrix and
22+
## returns matrix inverse, getting it from cache if possible.
23+
## If the matrix inverse has not been previously computed (and hence not present in cache)
24+
## then it computes the inverse, stores it in cache and returns the newly computed value
1225

1326
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
27+
## Return a matrix that is the inverse of 'x'
28+
inverseX <- x$getInverse
29+
if ( !is.null(inverseX) ) {
30+
message(inverseX)
31+
message("getting cached data")
32+
return(inverseX)
33+
}
34+
data <- x$get()
35+
inverseX <- solve(X, ...)
36+
x$setInverse(inverseX)
37+
inverseX
1538
}

0 commit comments

Comments
 (0)