Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e90c0b9

Browse files
committed
programming assignment 2
1 parent 7f657dd commit e90c0b9

File tree

1 file changed

+38
-8
lines changed

1 file changed

+38
-8
lines changed

cachematrix.R

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,45 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
1+
## I did not follow the pattern establisedh by the makeVector/cachemean
2+
## examples. This implementation simplifies by pushing the cache check
3+
## down into the 'special' matrix object.
4+
##
5+
## Test it ithis way:
6+
##
7+
## x <- matrix(rnorm(100), 10, 10) # creates the original matrix
8+
## xinv <- solve(x) # actual inverse of x.
9+
## xspecial <- makeCacheMatrix(x) # creates the special version of x
10+
## xspecial$isCalculated() # should return FALSE because we haven't calculated the inverse
11+
## xspecialInv <- xspecial$getInverse() # calculates the inverse.
12+
## xspecial$isCalculated() # should return TRUE because we've calculated the inverse.
13+
## identical(xinv, xspecialInv) # should return TRUE because they are both the inverse of x
514

15+
## returns a collection of functions
616
makeCacheMatrix <- function(x = matrix()) {
7-
17+
cachedInverse <- NULL
18+
19+
# handy way to test of the inverse is already calculated.
20+
# used for testing.
21+
isCalculated <- function() {
22+
!is.null(cachedInverse)
23+
}
24+
25+
## do the 'solve' part here. if the cache is NULL compute the cache,
26+
## then return the case.
27+
getInverse <- function() {
28+
if (is.null(cachedInverse)) {
29+
cachedInverse <<- solve(x)
30+
}
31+
return (cachedInverse)
32+
}
33+
34+
# return getInverse and isCalculated
35+
list(getInverse = getInverse,
36+
isCalculated = isCalculated)
837
}
938

1039

11-
## Write a short comment describing this function
12-
40+
## fetch the inverse from the object.
1341
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
42+
## Return a matrix that is the inverse of 'x'
43+
x$getInverse()
1544
}
45+

0 commit comments

Comments
 (0)