forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
90 lines (69 loc) · 2.68 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#################################################################
## Functions to take a invertible matrix, compute inverse matrix
## check if inverse matrix is already cached for the given matrix
## else generate the inverse matrix, cache it & return it.
## makeCacheMatrix function
## ------------------------
## this function is first called to create a list of functions
## to generate and return given matrix & its inverse matrix
## it returns the list of functions
## cacheSolve function
## --------------------
## this function is called next to generate inverse matrix
## it takes list object and options to 'solve' function.
## it checks if inverse matrix exists,
## else generate one with solve function and returns the inverse matrix.
#################################################################
## makeCacheMatrix function
##--------------------------
## This function is used to create a list object with functions
## to create & return given matrix and its inverse matrix
## takes one argument and returns a list object with functions
## to set or get given matrix and its inverse matrix
makeCacheMatrix <- function(x = matrix()) {
# creates a variable S
s <- NULL
# function to set the values of matrix
set <- function(y) {
x <<- y
s <<- NULL
}
# function to return the matrix
get <- function() x
# function to set the values of inverse matrix
setsolve <- function(solve) s <<- solve
# fucntion to return inverse matrix
getsolve <- function() s
# returns list of functions to set , get matrix and its inverse matrix
list(set = set, get = get,
setsolve = setsolve,
getsolve = getsolve)
}
## cacheSolve function
## -------------------
## cacheSolve function takes list object and required solve funtion options if
## necessary, checks if a inverse matrix is already cached and returns it.
## If inverse matrix does not exist,
## gets the original matrix through list object get function,
## computes the inverse using solve function
## caches the inverse matrix using setsolve function
## and returns the inverse matrix
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
# function call to get inverse matrix associated with 'x' and stores as 's'
s <- x$getsolve()
# returns 's'(inverse matrix) if 's' is not null
if(!is.null(s)) {
message("getting cached data")
return(s)
}
# get the matrix for which inverse matrix should be calculated and store
#it as data
data <- x$get()
# computes inverse of matrix and stores as 's'
s <- solve(data, ...)
# caches inverse matrix 's'
x$setsolve(s)
# returns inverse matrix 's'
s
}