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
51 lines (41 loc) · 2.02 KB
/
cachematrix.R
File metadata and controls
51 lines (41 loc) · 2.02 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
## Put comments here that give an overall description of what your
## functions do
## The function takes a square invertible matrix and has getters and setters for that matrix and inverse if any.
## The function returns a list object which gives access to all the sub-funtions defined within this.
makeCacheMatrix <- function(x = matrix()) {
## intializing the inverse value to NULL
y <- NULL
## sets the value of x as matrix passed as an argument
setMatrix <- function(y){
x<<-y
y<<-NULL
}
## returns the value of the x
getmatrix <- function() x
## sets the inverse value of a matrix
setInverse <- function(inverse) y <<- inverse
## returns the inverse value of matrix if already stored else returns null.
getInverse <- function() y
## The function returns a list object on which the above functions can be called
list(setMatrix = setMatrix, getmatrix = getmatrix,
setInverse = setInverse,
getInverse = getInverse)
}
## This function is responsible for returning the inverse of a matrix. It calculates the inverse of a matrix the first
## time and thereafter on any request for the inverse it returns the cached copy instead of computing it again.
cacheSolve <- function(x, ...) {
## Calls the getInverse() from above function on a list object returned by the later.
inverse <- x$getInverse()
## if the value for inverse is no null i.e. the inverse has already been cached from the earlier request
## then it simply returns the cached copy.
if(!is.null(inverse)){
print("Inverse returned from cache")
return(inverse)
}
## If this is the first request for the inverse of a particular matrix then it computes the inverse of that
## matrix and first caches the inverse and then returns the value.
data <- x$getmatrix()
inverse <- solve(data)
x$setInverse(inverse)
inverse
}