Skip to content

Commit 4e1e61b

Browse files
committed
initial version
1 parent 7f657dd commit 4e1e61b

File tree

1 file changed

+44
-9
lines changed

1 file changed

+44
-9
lines changed

cachematrix.R

Lines changed: 44 additions & 9 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
3-
4-
## Write a short comment describing this function
5-
6-
makeCacheMatrix <- function(x = matrix()) {
1+
## A pair of functions that combined can create an object wrapping a square
2+
## matrix that can cache its inverse to avoid the expense of this operation
3+
## executing more than once.
74

5+
## 'matrix_val' is a square matrix to be contained in the cache enabled object.
6+
##
7+
## Return an object that contains the input martix_val with the ability to
8+
## cache its inverse.
9+
##
10+
makeCacheMatrix <- function(matrix_val = matrix()) {
11+
inverse_val <- NULL
12+
set <- function(new_matrix) {
13+
matrix_val <<- new_matrix
14+
inverse_val <<- NULL
15+
}
16+
get <- function() {
17+
matrix_val
18+
}
19+
setInverse <- function(updated) {
20+
inverse_val <<- updated
21+
}
22+
getInverse <- function() {
23+
inverse_val
24+
}
25+
list(set = set,
26+
get = get,
27+
setInverse = setInverse,
28+
getInverse = getInverse)
829
}
930

1031

11-
## Write a short comment describing this function
12-
32+
## 'x' the cache matrix object created by a call to makeCacheMatrix.
33+
## '...' any optional arguments that should be passed to the call to solve()
34+
## used to invert the contained matrix.
35+
##
36+
## Return the inverted matrix value, this will be calculated on the first call
37+
## but the result will be stored in the matrix object for later use.
38+
##
1339
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
40+
## Return a matrix that is the inverse of 'x'
41+
m <- x$getInverse()
42+
if(is.null(m)) {
43+
data <- x$get()
44+
m <- solve(data, ...)
45+
x$setInverse(m)
46+
} else {
47+
message("getting cached data")
48+
}
49+
m
1550
}

0 commit comments

Comments
 (0)