2
2
3
3
# # Build the matrix wrapper
4
4
makeCacheMatrix <- function (x = matrix ()) {
5
- # # by default we do not have an inverse.
6
- inv <- NULL
7
-
8
- # # Allow the caller to set a new matrix in place of the use used at initialisation.
9
- set <- function ( y ) {
10
- x <<- y
11
- # # make sure an eventual cached inverse is cleared
12
- inv <<- NULL
13
- }
14
-
15
- # # Return the matrix
16
- get <- function () {
17
- x
18
- }
19
-
20
- # # cache the inverse
21
- setinverse <- function ( inverse ) {
22
- inv <<- inverse
23
- }
24
-
25
- # # return the inverse
26
- getinverse <- function () {
27
- inv
28
- }
5
+ # # by default we do not have an inverse.
6
+ inv <- NULL
7
+
8
+ # # Allow the caller to set a new matrix in place of the use used at initialisation.
9
+ set <- function ( y ) {
10
+ x <<- y
11
+ # # make sure an eventual cached inverse is cleared
12
+ inv <<- NULL
13
+ }
14
+
15
+ # # Return the matrix
16
+ get <- function () {
17
+ x
18
+ }
19
+
20
+ # # cache the inverse
21
+ setinverse <- function ( inverse ) {
22
+ inv <<- inverse
23
+ }
24
+
25
+ # # return the inverse
26
+ getinverse <- function () {
27
+ inv
28
+ }
29
+ list ( set = set , get = get , setinverse = setinverse , getinverse = getinverse )
29
30
}
30
31
31
32
32
33
# # Given an object returned by makeCacheMatrix, calculate the inverse and cache it for further calls.
33
34
# # If a cached inverse has already been calculated on the wrapped matrix, the cached version is returned.
34
35
cacheSolve <- function (x , ... ) {
35
- # # Return a matrix that is the inverse of 'x'
36
-
37
- inv <- x $ getinverse
38
-
39
- if ( ! is.null( inv ) ) {
40
- # # The wrapped matrix has a non NULL inverse
41
- message( " cache hit" )
42
- return inv
43
- }
44
-
45
- # # Calculate the inverse using solve
46
- # # TODO: handle special cases more carefully, not all matrices can be inverted.
47
- inv <- solve( x $ get() )
48
-
49
- # # Cache the result of solve()
50
- x $ setinverse( inv )
36
+ # # Return a matrix that is the inverse of 'x'
37
+
38
+ inv <- x $ getinverse()
39
+
40
+ if ( ! is.null( inv ) ) {
41
+ # # The wrapped matrix has a non NULL inverse
42
+ message( " cache hit" )
43
+ return ( inv )
44
+ }
45
+
46
+ # # Calculate the inverse using solve
47
+ # # TODO: handle special cases more carefully, not all matrices can be inverted.
48
+ inv <- solve( x $ get() )
49
+
50
+ # # Cache the result of solve()
51
+ x $ setinverse( inv )
52
+
53
+ # # return the inverse
54
+ inv
55
+ }
51
56
52
- # # return the inverse
53
- inv
54
- }
57
+ # # Tested using:
58
+ # #
59
+ # # > m <- matrix(rnorm( 9 ), 3, 3)
60
+ # # >
61
+ # # >
62
+ # # > cm <- makeCacheMatrix( m )
63
+ # # > cacheSolve( cm )
64
+ # # [,1] [,2] [,3]
65
+ # # [1,] 2.371877 0.8502974 2.338579
66
+ # # [2,] 0.960316 -1.2965751 1.011746
67
+ # # [3,] -3.353513 -1.6878236 -5.258276
68
+ # # > cacheSolve( cm )
69
+ # # cache hit
70
+ # # [,1] [,2] [,3]
71
+ # # [1,] 2.371877 0.8502974 2.338579
72
+ # # [2,] 0.960316 -1.2965751 1.011746
73
+ # # [3,] -3.353513 -1.6878236 -5.258276
0 commit comments