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
76 lines (65 loc) · 2.77 KB
/
cachematrix.R
File metadata and controls
76 lines (65 loc) · 2.77 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
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
## Put comments here that give an overall description of what your
## functions do
## Function makeCacheMatrix take a matrix as an argument and return a list
## containing all getters and setters. Along with functions, returned list also
## have access to all the elements which are free variables for functions but
## defined in parent environment.
## Reason:
## getters and setters are defined inside function makeCacheMatrix and when
## list is returned, it contains pointer to functions and thus the environment
## in which functions are declared not cleaned from memory, and thus variables
## x and m, though free variables for functions like get, still can access it.
## This behaviour is direct consequence of Lexical Scoping.
## Write a short comment describing this function
## This function returns R object, a list which contain functions to get matrix
## itself and its inverse, and functions to set matrix and its inverse.
## Assignment operator '<<-' is used here to assign variable in parent scope.
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(y) {
x <<- y
inv <<- NULL
}
get <- function() x
getInv <- function() inv
setInv <- function(x) inv <<- x
list(set = set, get = get, getInv = getInv, setInv = setInv)
}
## Write a short comment describing this function
## This function takes list returned by makeCacheMatrix as argument and compute
## the inverse of matrix passed to makeCacheMatrix (default is empty matrix), and
## check if its is already present in cache.
## if TRUE, then it simply return the inverse with a message that it has returned
## from value present in cache.
## otherwise, it get the matrix by 'get' method, compute its inverse, set that
## value to cache and return the inverse.
## method 'set' can change the matrix, it takes a matrix as an argument
## See 'Example Run:' below to see how it works.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inv <- x$getInv()
if(!is.null(inv)) {
message("From Cache")
return(inv)
}
data <- x$get()
inv <- solve(data)
x$setInv(inv)
inv
}
## Example run:
## > source("cachematrix.R")
## > mat <- matrix(rnorm(16), nrow = 4, ncol = 4)
## > mat_inv <- makeCacheMatrix(mat)
## mat_inv is a R object which is input to cacheSolve()
## > mat_inv$get()
## returns 'mat'
## > mat_inv$getInv()
## returns NULL because matrix inverse has not been calculated for the first time
## > mat_inv$setInv()
## set matrix inverse calculated first time
## > mat_inv$set(mat2)
## set matrix 'mat2' replaced with 'mat'
## cacheSolve(mat_inv)
## return the required inverse, if already calculated before then returned from
## cache, else calculated and set by method 'setInv'