forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
executable file
·78 lines (74 loc) · 1.69 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
## Below functions allows to create a cache-enabled version of matrix and
## cache the matrix' inverse (result of solve function). This allows to cache
## a time consuming operation. For more details refer to functions descriptions.
##
## Usage Example:
## > cm <- makeCacheMatrix(matrix(1:4, ncol=2))
##
## > cm$get()
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
##
## > cacheSolve(cm)
## [,1] [,2]
## [1,] -2 1.5
## [2,] 1 -0.5
##
## > cacheSolve(cm)
## getting cached data....
## [,1] [,2]
## [1,] -2 1.5
## [2,] 1 -0.5
##
## > cm$getSolve()
## [,1] [,2]
## [1,] -2 1.5
## [2,] 1 -0.5
makeCacheMatrix <- function(x = matrix()) {
# Creates cache-enabled matrix.
#
# Args:
# x: a matrix object
#
# Returns:
# An object with 4 functions:
# - get() returns data
# - set(y) changes data
# - getSolve() returns matrix' inverse
# - setSolve() sets the matrix' inverse
s <- NULL
set <- function(y) {
x <<- y
s <<- NULL
}
get <- function() x
setSolve <- function(solve) s <<- solve
getSolve <- function() s
list(set = set,
get = get,
setSolve = setSolve,
getSolve = getSolve)
}
cacheSolve <- function(x) {
# Caches the inverse of a matrix created with makeCacheMatrix function.
#
# Notes:
# Function doesn't take /.../ argument as /solve/ shouldn't be passed
# more than the data to inverse the matrix.
#
# Args:
# x: matrix created with makeCacheMatrix function.
#
# Returns:
# /x/ matrix' inverse
s <- x$getSolve()
if(!is.null(s)) {
message("getting cached data....")
return(s)
}
data <- x$get()
s <- solve(data)
x$setSolve(s)
s
}