Skip to content

Commit ed27278

Browse files
committed
added test code to comments
1 parent 07026f8 commit ed27278

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

cachematrix.R

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,28 @@
33
## cacheSolve(x) returns the inverse of the matrix actually setted on x
44
## if inverse cached, then just returns cached value, else calculates and caches the inverse first
55
## 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+
628

729

830
## params
@@ -13,6 +35,9 @@
1335
## -> get: returns the actual matrix
1436
## -> setinverse(inverse): caches the inverse of the matrix
1537
## -> getinverse: returns the cached matrix inverse
38+
##
39+
## example
40+
## > X <- makeCacheMatrix(matrix(c(1,2,2,3), 2, 2))
1641
makeCacheMatrix <- function(x = matrix()) {
1742
i <- NULL
1843
set <- function(y) {
@@ -34,6 +59,11 @@ makeCacheMatrix <- function(x = matrix()) {
3459
##
3560
## returns the inverse of the matrix.
3661
## 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
3767
cacheSolve <- function(x, ...) {
3868
## Return a matrix that is the inverse of 'x'
3969
i <- x$getinverse()

0 commit comments

Comments
 (0)