forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachematrix.R
More file actions
33 lines (31 loc) · 1.51 KB
/
cachematrix.R
File metadata and controls
33 lines (31 loc) · 1.51 KB
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
makeCacheMatrix <- function(x) { # I assume the user supply an invertible square matrix
i <- NULL # "i" is the inverted matrix, which I set to NULL at the first call
set <- function(y) { # resetting the matrix with new matrix "y"
x <<- y
i <<- NULL # when resetting the matrix, the inverse is reset to NULL
}
get <- function() x # display the current matrix
setinv <- function(inv) i <<- inv #Do not call this function directly, its only purpose is for cachemean to update the list with the newly set inversed matrix
getinv <- function() i # display the current inversed matrix which has been created in the global environment by the previous function and the operator "<<-"
list(set = set, get = get, # This creates the list of function required to cache and access the matrix/inversed matrix
setinv = setinv,
getinv = getinv)
}
cacheinv <- function(x, ...) { # caching function
i <- x$getinv()
if(!is.null(i)) { # If inv has already been calculated
message("getting cached data")
return(i) # return the inversed matrix without calculation
}
data <- x$get() # otherwise, get the original matrix
i <- solve(data, ...) # and solve the inverse
x$setinv(i) # Finally stores the inversed matrix in the corresponding list
i
}
# Use example : m <- matrix(rnorm(9),3)
# mat <- makeCacheMatrix(m)
# cacheinv(mat)
# mat$getinv()
# mat$cacheinv(mat)
# mat$set(matrix(rnorm(9),3))
# etc...