1
- # # These functions can calculate the inverse matrix and
2
- # # cache the results so it does not need to be recalculated
3
- # # when re-used.
1
+ # # These functions can calculate the inverse of a matrix and cache
2
+ # # the results, so it does not need to be recalculated when re-used.
4
3
5
- # # This function returns a closure that contains
6
- # # both the original matrix passed as argument x
7
- # # and its inverse
4
+ # # This function returns a closure that holds both the original
5
+ # # matrix passed as argument x and its inverse
8
6
makeCacheMatrix <- function (x = matrix ()) {
9
7
xi <- NULL
10
8
11
- # # Sets the enclosed matrix and
12
- # # resets any cached inverse
9
+ # # Sets the enclosed matrix and resets any cached inverse
13
10
set <- function (y ) {
14
11
x <<- y
15
12
xi <<- NULL
@@ -18,8 +15,7 @@ makeCacheMatrix <- function(x = matrix()) {
18
15
# # Returns the enclosed matrix
19
16
get <- function () x
20
17
21
- # # Caches the given solution to the
22
- # # a inverse matrix
18
+ # # Caches the given solution to the inverse matrix
23
19
setSolve <- function (solve ) xi <<- solve
24
20
25
21
# # Gets the cached inverse of the matrix
@@ -31,10 +27,9 @@ makeCacheMatrix <- function(x = matrix()) {
31
27
}
32
28
33
29
34
- # # This function expects a closure returned
35
- # # by makeCacheMatrix above and leverages its
36
- # # caching capabilities to return the solution
37
- # # for the inverse matrix
30
+ # # This function expects a closure returned by makeCacheMatrix above
31
+ # # and uses its caching capabilities to return the solution for
32
+ # # the inverse matrix
38
33
cacheSolve <- function (x , ... ) {
39
34
# # Return a matrix that is the inverse of 'x'
40
35
xi <- x $ getSolve()
@@ -61,8 +56,7 @@ print(cm$get())
61
56
xi <- cacheSolve(cm )
62
57
print(cm $ getSolve())
63
58
64
- # # Calculates the inverse again, it should return
65
- # # the cached value
59
+ # # Calculates the inverse again, it should return the cached value
66
60
xi <- cacheSolve(cm )
67
61
68
62
# # Is this really the inverse matrix?
@@ -73,7 +67,7 @@ if(isTRUE(all.equal(xi, mi))) {
73
67
}
74
68
75
69
# Test #2: With a 3x3 matrix
76
- message(" Test 2: with a 3x3 matrix" )
70
+ message(" Test # 2: with a 3x3 matrix" )
77
71
m <- matrix (c(3 , 2 , 0 , 0 , 0 , 1 , 2 , - 2 , 1 ), 3 , 3 )
78
72
79
73
cm <- makeCacheMatrix(m )
@@ -82,8 +76,7 @@ print(cm$get())
82
76
xi <- cacheSolve(cm )
83
77
print(cm $ getSolve())
84
78
85
- # # Calculates the inverse again, it should return
86
- # # the cached value
79
+ # # Calculates the inverse again, it should return the cached value
87
80
xi <- cacheSolve(cm )
88
81
89
82
if (isTRUE(all.equal(m %*% xi , diag(3 )))) {
0 commit comments