This repository was archived by the owner on Aug 8, 2026. It is now read-only.
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
94 lines (78 loc) · 2.78 KB
/
Copy pathcachematrix.R
File metadata and controls
94 lines (78 loc) · 2.78 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
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
91
92
93
94
## Put comments here that give an overall description of what your
## functions do
## Write a short comment describing this function
makeCacheMatrix <- function(x = matrix()) {
##Holds the cached value or NULL
inverseCache <- NULL
##Setting and storing the matrix. Y is the new value.
setMatrix <- function(y){
x <<- y
##Flushing the cache
inverseCache <<- NULL
}
##Get and return the stored matrix
getMatrix <- function() {
x
}
##Setting and caching the argument
setInverse <- function(solveMatrix) {
inverseCache <<- solveMatrix
}
##Get and return the cached value
getInverse <- function() {
inverseCache
}
##Return a list
list(setMatrix = setMatrix, getMatrix = getMatrix, setInverse = setInverse, getInverse = getInverse)
}
## Write a short comment describing this function
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
##Check if there is a cached inversion
inverseCache <- x$getInverse()
##If there is a cached version, return it!
if(!is.null(inverseCache)) {
message("Getting cached data")
return(inverseCache)
}
##If there is no cached version, calculate new one
data <- x$getMatrix()
inverseCache <- solve(data, ...)
##And set it into the cache
x$setInverse(inverseCache)
inverseCache
}
##-----Debuging the program-----
## > debug(cacheSolve)
## > cacheSolve(m1)
## debugging in: cacheSolve(m1)
## debug at ~/Documents/GitHub/ProgrammingAssignment2/cachematrix.R#40: {
## inverseCache <- x$getInverse()
## if (!is.null(inverseCache)) {
## message("Getting the cached data")
## return(inverseCache)
## }
## data <- x$getMatrix()
## inverseCache <- solve(data, ...)
## x$setInverse(inverseCache)
## inverseCache
##}
## Browse[2]>
## debug at ## ~/Documents/GitHub/ProgrammingAssignment2/cachematrix.R#44: inverseCache <- x$getInverse()
## Browse[2]>
## debug at ~/Documents/GitHub/ProgrammingAssignment2/cachematrix.R#47: if (!is.null(inverseCache)) {
## message("Getting the cached data")
## return(inverseCache)
## }
## Browse[2]>
## debug at ~/Documents/GitHub/ProgrammingAssignment2/cachematrix.R#48: message("Getting the cached data")
## Browse[2]>
## Getting the cached data
## debug at ~/Documents/GitHub/ProgrammingAssignment2/cachematrix.R#49: return(inverseCache)
## Browse[2]>
## exiting from: cacheSolve(m1)
## [,1] [,2] [,3] [,4]
## [1,] -1.103614 0.4619003 -0.8468213 0.1603883
## [2,] 4.953622 -4.1491442 3.1038958 1.6461716
## [3,] -8.249267 6.7118825 -6.1928336 -2.7668901
## [4,] 4.932770 -4.8247051 3.8629930 1.8299655