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
62 lines (47 loc) · 1.84 KB
/
cachematrix.R
File metadata and controls
62 lines (47 loc) · 1.84 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
## This program is implemented by M.M. Mahbubul Syeed
## email: [email protected]
##Sample input for testing (just to assist the examinar :)
# mat <- matrix(1:4,2,2)
# x <- makeCacheMatrix(mat)
# cacheSolve(x)
# run the last command again to verify the caching
## This function receives a matrix for inversion and also cacheses the result.
## This function creates a special "vector", which is really a list containing a function to
#set and get the value of the given matrix for inversion. and
#set and get the inversion matrix.
makeCacheMatrix <- function(x = matrix()) {
#used to cache the inverse matrix. initialized to null if no inversion available
inverseMatrix <- NULL
#set the given input matrix
set <- function(y) {
x <<- y
inverseMatrix <<- NULL
}
#returns the input matrix
get <- function() x
# sets the inverse of the matrix.
setInverseMatrix <- function(inverse) inverseMatrix <<- inverse
#return the inverse matrix (returns null if not cashed before)
getInverseMatrix <- function() inverseMatrix
#creates the list containg the functions
list(set = set, get = get, setInverseMatrix = setInverseMatrix, getInverseMatrix = getInverseMatrix)
}
## This funciton calculates the inverse of a matrix if not already cerated and cached by the
# the function ablove.
cacheSolve <- function(x, ...) {
# gets the inverse matrix from cache
# returns null if the inverse not alrealy cached
chachedMatrix <- x$getInverseMatrix()
#check if the inverse is available or not
#if yes then return the result from cache
if(!is.null(chachedMatrix)) {
message("getting cached data")
return(chachedMatrix)
}
#otherwise, calculate the inverse matrix and cache it
data <- x$get()
inverse <- solve(data)
x$setInverseMatrix(inverse)
#print the inverse matrix and return
inverse
}