forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
36 lines (28 loc) · 1.69 KB
/
cachematrix.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
## These functions creates a special matrix object and computes and cache its inverse
## to prevent unnessary costly computations. This function assumes that the matrix
## provided is inversible and will return an error if it's not.
## Creates a special matrix object that can cache it's inverse.
makeCacheMatrix <- function(x = matrix()) {
m<-NULL
}
get <-function() x #function returns the matrix x stored in the local environment
setinv <- function(inv) m<<-inv #function set the value of m to the inverse of the matrix to be stored in cache of the parent environment
getinv <- function() m #returns the value of the inverse
list(get=get,
setinv=setinv,
getinv=getinv) # returns the matrix object that's
}
## Takes the matrix object created with the makeCacheMatrix function and returns the inverse
## of the matrix by retrieving from cache if it's been calculated
## or return calculate it and store it in cache if it has not been previously calculated.
cacheSolve <- function(x, ...) {
inv<-x$getinv() # gets the inverse from the matrix object created with makeCacheMatrix
if(!is.null(inv)){ # if the inverse of the matrix is available the inverse is returned
message("getting cached data")
return(inv) # return the inverse of the matrix
}
data<-x$get() # if it's not available get the matrix
inv<-solve(data,...) # calculate the inverse
x$setinv(inv) # store the inverse in cache of the local environment of makeCacheMatrix
inv # Return the inverse of the matrix
}