3
3
# # cacheSolve(x) returns the inverse of the matrix actually setted on x
4
4
# # if inverse cached, then just returns cached value, else calculates and caches the inverse first
5
5
# # x is the list of functions return by makeCacheMatrix
6
+ # #
7
+ # # Test Code
8
+ # # 1) create a 2x2 invertible matrix
9
+ # # > A <- matrix(c(1,2,2,3), 2, 2)
10
+ # # [,1] [,2]
11
+ # # [1,] 1 2
12
+ # # [2,] 2 3
13
+ # #
14
+ # # 2) call makeCacheMatrix to get the special matrix
15
+ # # > X <- makeCacheMatrix(A)
16
+ # #
17
+ # # 3) call cacheSolve to get the inverse of the matrix
18
+ # # > inverse <- cacheSolve(X)
19
+ # # [,1] [,2]
20
+ # # [1,] -3 2
21
+ # # [2,] 2 -1
22
+ # #
23
+ # # 4) Test that the inverse is correct: A * inverse = I
24
+ # # > A %*% inverse
25
+ # #
26
+ # # 5) Call cacheSolve again should print the message "getting cached data"
27
+
6
28
7
29
8
30
# # params
13
35
# # -> get: returns the actual matrix
14
36
# # -> setinverse(inverse): caches the inverse of the matrix
15
37
# # -> getinverse: returns the cached matrix inverse
38
+ # #
39
+ # # example
40
+ # # > X <- makeCacheMatrix(matrix(c(1,2,2,3), 2, 2))
16
41
makeCacheMatrix <- function (x = matrix ()) {
17
42
i <- NULL
18
43
set <- function (y ) {
@@ -34,6 +59,11 @@ makeCacheMatrix <- function(x = matrix()) {
34
59
# #
35
60
# # returns the inverse of the matrix.
36
61
# # If inverse was already calculated, then it returns the cached copy, and prints a notice message "getting cached data"
62
+ # #
63
+ # # example
64
+ # # > X <- makeCacheMatrix(matrix(c(1,2,2,3), 2, 2))
65
+ # # > inverse <- cacheSolve(X)
66
+ # # > matrix(c(1,2,2,3), 2, 2) %*% inverse # should return the 2x2 identity matrix
37
67
cacheSolve <- function (x , ... ) {
38
68
# # Return a matrix that is the inverse of 'x'
39
69
i <- x $ getinverse()
0 commit comments