|
1 | | -## Put comments here that give an overall description of what your |
2 | | -## functions do |
| 1 | +## The two functions here make use of environments to cache results that |
| 2 | +## are expensive to compute and are used many times. |
| 3 | +## Here the inversion of a matrix is acheived using caching. |
| 4 | +## note that the matrix is considered invertible. |
3 | 5 |
|
4 | | -## Write a short comment describing this function |
5 | 6 |
|
| 7 | +## function makeCacheMatrix, takes a regular matrix |
| 8 | +## and creates a special type of matrix, that includes setters and getters |
| 9 | +## and a cache variable that stores the inverse |
6 | 10 | makeCacheMatrix <- function(x = matrix()) { |
| 11 | +minv <- NULL |
| 12 | +set <- function(y){ |
| 13 | + x <<- y |
| 14 | + m <<- NULL |
| 15 | +} |
| 16 | +get <- function() x |
| 17 | +setinv <- function(solve) minv <<-solve |
| 18 | +getinv <- function() minv |
| 19 | +list(set=set,get=get,setinv=setinv,getinv=getinv) |
7 | 20 |
|
8 | 21 | } |
9 | 22 |
|
10 | 23 |
|
11 | | -## Write a short comment describing this function |
| 24 | +## function cacheSolve checks if the inverse for this matrix is already |
| 25 | +## available in the cache, if so, it returns the cached inverse |
| 26 | +## if its not available it computes the inverse, stores it in the cache |
| 27 | +## and then returns the inverse |
12 | 28 |
|
13 | 29 | cacheSolve <- function(x, ...) { |
14 | 30 | ## Return a matrix that is the inverse of 'x' |
| 31 | + minv <- x$getinv() |
| 32 | + if(!is.null(minv)) { |
| 33 | + message("getting cached data") |
| 34 | + return(minv) |
| 35 | + } |
| 36 | + data <- x$get() |
| 37 | + minv <- solve(data, ...) |
| 38 | + x$setinv(minv) |
| 39 | + minv |
| 40 | + |
15 | 41 | } |
0 commit comments