Skip to content

Commit eab8c20

Browse files
committed
caching and calling functions
1 parent 7f657dd commit eab8c20

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

cachematrix.R

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,50 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## Function to make a special matrix object that can cache its inverse when
2+
## computer second function to compute inverses but calling for a cache first
33

4-
## Write a short comment describing this function
4+
## This function creates a special "matrix" object that can cache its
5+
## inverse.
56

67
makeCacheMatrix <- function(x = matrix()) {
7-
8+
inverse <- NULL
9+
set <- function(y) {
10+
# setting function
11+
x <<- y
12+
inverse <<- NULL
13+
}
14+
get <- function() {
15+
# getting function
16+
x
17+
}
18+
setinverse <- function(value) {
19+
# set inverse within scope
20+
inverse <<- value
21+
}
22+
getinverse <- function() {
23+
inverse
24+
}
25+
26+
list(
27+
set = set, get = get,
28+
setinverse = setinverse,
29+
getinverse = getinverse
30+
)
831
}
932

10-
11-
## Write a short comment describing this function
33+
## This function computes the inverse of the special "matrix" returned by
34+
## makeCacheMatrix above. If the inverse has already been calculated (and the
35+
## matrix has not changed), then cacheSolve should retrieve the inverse from
36+
## the cache.
1237

1338
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
39+
## Return a matrix that is the inverse of 'x'
40+
## assumption matrix is always invertable
41+
inverse <- x$getinverse()
42+
if(!is.null(inverse)) {
43+
message("getting cached data")
44+
return(inverse)
45+
}
46+
data <- x$get()
47+
inverse <- solve(data, ...)
48+
x$setinverse(inverse)
49+
inverse
1550
}

0 commit comments

Comments
 (0)