forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
51 lines (47 loc) · 1.94 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
## The following two functions provide a mechanism to cache the results
## of the R "solve" function (calculating the inverse of a matrix),
## which makes the code, where the inverse of matrix is used more than once,
## more efficient
## This function creates a special object, which stores the inversed matrix
## as a cache (in object called "inv"), and also provides methods to set and get
## the original matrix (methods"set" and "get" accordingly), and set and get the
## inverse matrix (methods "setsolve" and "getsolve" accrodingly).
## Example of usage:
## To create a cached matrix execute the following:
## > cachedMatrix <-makeCacheMatrix(m)
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(y) {
x <<- y
inv <<- NULL
}
get <- function() x
setsolve <- function(solve) inv <<- solve
getsolve <- function() inv
list(set = set, get = get,
setsolve = setsolve,
getsolve = getsolve)
}
## This function is used to get the inverse of a matrix using R method "solve", but
## should be applied to a special matrix object created by previous function. When the
## method is executed, it is first checked whether the cached value of the inverse matrix
## exists, and this value is returned directly (no calculations perfored). Otherwise, if the
## value is not found, it is calculated (using "solve" function), stored for future
## accesses, and returned.
## Example of usage:
## For the matrix object created previously, to get the inverse the following is executed:
## > cacheSolve(cachedMatrix)
## If the above command is repeated, we'll se the "getting cached data" message, which means that
## the value has been taken from the cache, and is not calculated again
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inv <- x$getsolve()
if(!is.null(inv)) {
message("getting cached data")
return(inv)
}
data <- x$get()
inv <- solve(data, ...)
x$setsolve(inv)
inv
}