forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
34 lines (27 loc) · 908 Bytes
/
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
## Below functions provides inverse of a square invertible matrix
## makeCacheMatrix sets the matrix (user provided) and returns cached inverse matrix (inv)
##if it is already calculated earlier
makeCacheMatrix <- function(x = matrix()) {
inv<-NULL
get=function() x
set=function(a){
x<<-a
inv<<-NULL
}
getinv=function()inv
setinv <- function(invq) inv <<- invq
list(get=get,set=set,getinv=getinv, setinv=setinv)
}
## Return a matrix that is the inverse of 'x'
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inv<-x$getinv()
##check if inverse already calculated in past
if(!is.null(inv)){
print("Getting cached inv")
return (inv)
}
y <- x$get()
invq<-solve(y)
x$setinv(invq)
}