File tree Expand file tree Collapse file tree 1 file changed +44
-7
lines changed Expand file tree Collapse file tree 1 file changed +44
-7
lines changed Original file line number Diff line number Diff line change 1
- # # Put comments here that give an overall description of what your
2
- # # functions do
1
+ # # These functions cache a given matrix inversion to conserve
2
+ # # repeating the computationally complex inversion calculation.
3
+ # # For the given matrix it will only be calculated once.
3
4
4
- # # Write a short comment describing this function
5
5
6
+ # # makeCacheMatrix provides the functions to get and set the matrix
7
+ # # as well as get and set the inverse
6
8
makeCacheMatrix <- function (x = matrix ()) {
7
-
9
+ m <- NULL
10
+ set <- function (y ) {
11
+ x <<- y
12
+ m <<- NULL
13
+ }
14
+ get <- function () x
15
+ setinverse <- function (inverse ) m <<- inverse
16
+ getinverse <- function () m
17
+ list (set = set , get = get ,
18
+ setinverse = setinverse ,
19
+ getinverse = getinverse )
8
20
}
9
21
10
22
11
- # # Write a short comment describing this function
12
-
13
- cacheSolve <- function (x , ... ) {
23
+ # # cacheinverse calculates the matrix inverse if it is not cached
24
+ # # and reads the cache if it is available intead of calculating
25
+ cacheinverse <- function (x , ... ) {
14
26
# # Return a matrix that is the inverse of 'x'
27
+ m <- x $ getinverse()
28
+ if (! is.null(m )) {
29
+ message(" getting cached data" )
30
+ return (m )
31
+ }
32
+ data <- x $ get()
33
+ m <- solve(data , ... )
34
+ x $ setinverse(m )
35
+ m
15
36
}
37
+
38
+ # # test console session
39
+ # #
40
+ # > x <- rbind(c(4,7),c(2,6))
41
+ # > a <- makeCacheMatrix(x)
42
+ # > cacheinverse(a)
43
+ # [,1] [,2]
44
+ # [1,] 0.6 -0.7
45
+ # [2,] -0.2 0.4
46
+ # > cacheinverse(a)
47
+ # getting cached data
48
+ # [,1] [,2]
49
+ # [1,] 0.6 -0.7
50
+ # [2,] -0.2 0.4
51
+ # >
52
+
You can’t perform that action at this time.
0 commit comments