forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
39 lines (31 loc) · 1.6 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
37
38
39
## This contains twp functions, one of which takes a non-singular matrix as an input and returns a list of 4 functions;
## the second one takes the output of the first function, either calculates and returns the inverse of that matrix or displays the cached value.
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL #Initializing the inverse value as NULL
set <- function(y) { # function to change the input matrix
x <<- y
inv <<- NULL
}
get <- function() { # function to return the input matrix of makeCacheMatrix()
x
}
setinv <- function(inverse) { # function to set the calculated inverse value from cacheSolve into inv
inv <<- inverse #superassignment operator
}
getinv <- function() { # function to get the inverse value stored in inv
inv
}
list( set = set, get = get, setinv = setinv, getinv = getinv) # output list returned by makeCacheMatrix()
}
cacheSolve <- function(x, ...) {
inv <- x$getinv() # Setting in inv the inverse value from the above function
if(!is.null(inv)) { # checks if inv has stored value of inverse of x i.e. the inverse of x has already been calculated
message("getting cached data")
return(inv) # Returns inv and exits the function cacheSolve
}
# when inv is NULL :
mat <- x$get() # store original input matrix in a variable mat
inv <- solve(mat) # solve() finds inverse of x and stores it in inv
x$setinv(inv) # setting value of inv (still NULL in upper function) equal to calculated inverse value
inv ## Return a matrix that is the inverse of 'x'
}