forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
47 lines (43 loc) · 1.82 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
40
41
42
43
44
45
46
47
## R Programming - Week 3 - Programming Assignment 2
##
## This code file contains 2 functions, makeCacheMatrix and cacheSolve,
## which are meant to be used the following way :
##
## 1. create a matrix object, for example : x <- matrix(rnorm(100, 1), 10, 10)
## 2. list <- makeCacheMatrix(x)
## 3. cacheSolve(list)
## The makeCacheMatrix function creates & returns a special list containing 4 functions
##
## 1. set : sets the content of the matrix which we'll calculate the inverse of
## 2. get : gets the content of the matrix
## 3. setInv : sets the cached value of the matrix inverse
## 4. getInv : gets the cached value of the matrix inverse
makeCacheMatrix <- function(x = matrix()) {
i <- NULL # inverse variable
set <- function(y) {
x <<- y
i <<- NULL
}
get <- function() x
setInv <- function(inv) i <<- inv
getInv <- function() i
list(set = set, get = get,
setInv = setInv,
getInv = getInv)
}
## The cacheSolve function computes and returns a matrix that is the inverse of 'x'
## but only if the inverse has not already been calculated (i.e. i = NULL)
## or if the matrix has changed since the last inverse computation (which sets i as NULL)
## otherwise the function retrieves the cached inverse value using the get function
cacheSolve <- function(x, ...) {
i <- x$getInv()
if(!is.null(i)) { # this tests if the inverse i has already been computed and if matrix has changed
message("Cached data available and the matrix has not changed. Getting cached inverse.")
return(i)
}
else message("No cached data available or the matrix has changed. Calculating the inverse and caching it.")
data <- x$get()
i <- solve(data, ...)
x$setInv(i)
i
}