forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cachematrix.R
59 lines (51 loc) · 1.55 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
48
49
50
51
52
53
54
55
56
57
58
59
# ******************************** Start of Programming part **********************
# Following two functions enable setting up a interim matrix and using it to calculate
# inverse by retrieving it from cache as needed
# Function 1 - "makeCacheMatrix " - this function creates a new matrix object and also to
# cache the inverse
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(y) {
x <<- y
inv <<- NULL
}
get <- function() x
setInverse <- function(inverse) inv <<- inverse
getInverse <- function() inv
list(set = set,
get = get,
setInverse = setInverse,
getInverse = getInverse)
}
## Function 2 - This function uses new matrix created using above makeCacheMatrix
# and computes the inverse by retrieving it from cache
cacheSolve <- function(x, ...) {
inv <- x$getInverse()
if (!is.null(inv)) {
message("No new changes. Getting cached data")
return(inv)
}
mat <- x$get()
inv <- solve(mat, ...)
x$setInverse(inv)
inv
}
# ******************************** End of Programming part **********************
# Following section tests the above code
# UnitTest Results
> z <- diag(2, 2)
> z
[,1] [,2]
[1,] 2 0
[2,] 0 2
> new_matrix <- makeCacheMatrix(z)
> cacheSolve(new_matrix)
[,1] [,2]
[1,] 0.5 0.0
[2,] 0.0 0.5
> cacheSolve(new_matrix)
No new changes. Getting cached data
[,1] [,2]
[1,] 0.5 0.0
[2,] 0.0 0.5
>