1
- # # Put comments here that give an overall description of what your
2
- # # functions do
3
-
4
- # # Write a short comment describing this function
1
+ # ' Creates a special list with keys "get", "set", "setsolve" and "getsolve"
2
+ # ' that will cache the result of inversing a matrix
3
+ # '
4
+ # ' You can use this object directly, but to be able to correctly cache
5
+ # ' the inverse of the matrix you should always use the `cacheSolve` function
6
+ # ' on it.
7
+ # '
8
+ # ' "get" will read give you the currently wrapped matrix
9
+ # ' "set" will change the currently wrapped matrix and clear the inverse cache if there is one
10
+ # ' "setsolve" will save the cached inverse, should not be directly called
11
+ # ' "getsolve" will get the cached inverse, should not be directly called
12
+ # '
13
+ # ' @param x the matrix that will have it's inverse cached
14
+ # ' @return a list wrapping the given matrix
15
+ # ' @examples
16
+ # ' m <- makeCacheMatrix(matrix( rnorm(N*M,mean=0,sd=1), 3, 3))
5
17
6
18
makeCacheMatrix <- function (x = matrix ()) {
7
-
19
+ m <- NULL
20
+ set <- function (y ) {
21
+ x <<- y
22
+ m <<- NULL
23
+ }
24
+ get <- function () x
25
+ setsolve <- function (solved ) m <<- solved
26
+ getsolve <- function () m
27
+ list (set = set , get = get ,
28
+ setsolve = setsolve ,
29
+ getsolve = getsolve )
8
30
}
9
31
10
32
11
- # # Write a short comment describing this function
33
+ # ' Calculates and caches or returns the cached result of inversing the given matrix
34
+ # '
35
+ # ' This function will either return the available inverse directly if it isn't cached
36
+ # ' or will calculate the inverse, save it to the cache and then return this cached result.
37
+ # '
38
+ # ' @param x the special list created by `makeCacheMatrix`
39
+ # ' @param ... other parameters you would usually give to a call to `solve`
40
+ # ' @return the inverse of the currently wrapped matrix
41
+ # ' @examples
42
+ # ' m <- makeCacheMatrix(matrix( rnorm(N*M,mean=0,sd=1), 3, 3))
43
+ # ' cacheSolve(m)
12
44
13
- cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
15
- }
45
+ cacheSolve <- function (x , ... ) {
46
+ m <- x $ getsolve()
47
+ if (! is.null(m )) {
48
+ message(" getting cached data" )
49
+ return (m )
50
+ }
51
+ data <- x $ get()
52
+ m <- solve(data , ... )
53
+ x $ setsolve(m )
54
+ m
55
+ }
0 commit comments