forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
71 lines (52 loc) · 2.52 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
60
61
62
63
64
65
66
67
68
69
70
71
## Put comments here that give an overall description of what your
## functions do
## Write a short comment describing this function
# Function to create a special type of matrix, a cacheable matrix
makeCacheMatrix <- function(x = matrix()) {
inverse_matrix <- NULL #This variable will hold the inverse matrix,
#but for now is being set to null,
#since the inverse of the matrix as not been calculated yet
# Setting the value of Vector
set_vector <- function(y){
#updating old matrix with new matrix
x <<- y
#reset inverse_matrix
inverse_matrix <<- NULL
}
# getting actual matrix
get_matrix <- function() x
#set the value of inverse_matrix
set_inverse_matrix <- function(solve) inverse_matrix <- solve
#get inverse_matrix
get_inverse_matrix <- function() inverse_matrix
# list with the available functions
list(set_vector = set_vector, get_matrix=get_matrix, set_inverse_matrix=set_inverse_matrix, get_inverse_matrix=get_inverse_matrix)
}
## Working of above function :
# The following function calculates the inverse of the special "matrix" created with the above function (makeCacheMatrix).
# However, it first checks to see if the inverse of the matrix has already been calculated.
# If so, it gets the inverse of the matrix from the cache and skips the computation.
# Otherwise, it calculates the inverse of the matrix of the data and sets the value
# of the inverse of the matrix in the cache via the setinverse function.
## Write a short comment describing this function
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
# get the inverse of the matrix
inverse_matrix <- x$get_inverse_matrix()
# check if this inverse of the matrix has been calculated
if(!is.null(inverse_matrix)) {
# if so, prints this message "getting cached data" and
# returns the inverse of the matrix and skips the computation.
message("getting cached data")
return(inverse_matrix)
}
#If the inverse has not been calculated:
# get the actual matrix
data <- x$get_matrix()
# calculating the inverse of the matrix using solve
inverse_matrix <- solve(data, ...)
# update the variable that holds the inverse of the matrix
x$set_inverse_matrix(inverse_matrix)
# return inverse_matrix
inverse_matrix
}