Skip to content

Commit 99f14b6

Browse files
committed
Initial commit of cachematrix.R, still need to verify functionality, but initial tests seem to be working properly
1 parent 7f657dd commit 99f14b6

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

cachematrix.R

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,38 @@
22
## functions do
33

44
## Write a short comment describing this function
5-
5+
## makeCacheMatrix creates a matrix object which allows caching of its inverse
6+
## parameter/argument x is an invertible matrix, defaulting to an empty matrix if x is not defined
7+
## A list is returned with operations for matrix setting/getting, and inverse matrix setting/getting
68
makeCacheMatrix <- function(x = matrix()) {
7-
9+
10+
inv <- NULL
11+
set <- function(y) {
12+
x <<- y
13+
inv <<- NULL
14+
}
15+
16+
get <- function() x
17+
setinverse <- function(inverse) inv <<- inverse
18+
getinverse <- function() inv
19+
20+
list(set = set, get = get, setinverse = setinverse, getinverse = getinverse)
821
}
922

1023

1124
## Write a short comment describing this function
12-
25+
## cacheSolve returns the inverse of the matrix supplied to makeCacheMatrix
26+
## if the inverse has already been calculated, the cached version is used and returned without
27+
## having to calculated/add extra overhead. If it has not been calculated prior, it is calculated, and then cached for
28+
## future retrievals
1329
cacheSolve <- function(x, ...) {
1430
## Return a matrix that is the inverse of 'x'
31+
inv <- x$getinverse()
32+
if (!is.null(inv)) {
33+
return(inv)
34+
}
35+
data <- x$get()
36+
inv <- solve(data, ...)
37+
x$setinverse(inv)
38+
return(inv)
1539
}

0 commit comments

Comments
 (0)