Skip to content

Commit c168989

Browse files
committed
done first pass of cacheSolve function + some testing in R.
seems to work.
1 parent f2042f8 commit c168989

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

cachematrix.R

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## Functions allowing the creation of a special 'matrix' type that can cache
2+
## its inverse in order to save computation resources.
33

4-
## Using a function call to create space to cache a matrix's inverse.
5-
## Functions to access that space are created: (get, set, getinv, setinv)
4+
## We use a function call to create space to cache a matrix's inverse.
5+
## Functions to access that environment are created: (get, set, getinv, setinv)
66

77
makeCacheMatrix <- function(x = matrix()) {
88
i <- NULL #initialize variable to store inverse
99
set <- function(y) {
10-
#assign a matrix to this CacheMatrix
10+
#assigns a matrix to this CacheMatrix
1111
x <<- y
1212
m <<- NULL
1313
}
14-
get <- function() x #get the matrix currently in this CacheMatrix
15-
setinv <- function(inv) i <<- inv #assign an inverse to this CacheMatrix
16-
getinv <- function() i #get the inverse currently in this CacheMatrix
17-
list(set = set, get = get, setinv = setinv, getinv = getinv)
14+
get <- function() x #gets the matrix currently in this CacheMatrix
15+
setinv <- function(inv) i <<- inv #assigns an inverse to this CacheMatrix
16+
getinv <- function() i #gets the inverse currently in this CacheMatrix
17+
list(set = set, get = get, setinv = setinv, getinv = getinv) #return funcs
1818
}
1919

2020

21-
## Computes the inverse of a makeCacheMatrix 'matrix'. If the inverse has been
21+
## Computes the inverse of a CacheMatrix. If the inverse has been
2222
##cached, it retrieves it.
2323

2424
cacheSolve <- function(x, ...) {
2525
## Return a matrix that is the inverse of 'x'
26+
i <- x$getinv() #get the inverse of a given CacheMatrix
27+
if(!is.null(i)) {
28+
message('getting cached data')
29+
return(i)
30+
}
31+
#unless it's null, in which case:
32+
data <- x$get() #get the matrix
33+
i <- solve(data) #solve for the inverse
34+
x$setinv(i) #store the inverse in the CacheMatrix
35+
i
2636
}

0 commit comments

Comments
 (0)