Skip to content

Commit 864729b

Browse files
committed
method implementations
1 parent 7f657dd commit 864729b

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

cachematrix.R

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,38 @@
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+
## makeCacheMatrix - create a matrix wrapper whose inverse is cacheable
2+
## cacheSolve - invert a matrix; use cached result if available
53

4+
## This method returns a wrapper for a matrix for which the inverse is
5+
## cacheable
66
makeCacheMatrix <- function(x = matrix()) {
7-
7+
inverse <- NULL
8+
set <- function( y )
9+
{
10+
x <<- y
11+
inverse <<- NULL
12+
}
13+
get <- function()
14+
{
15+
x
16+
}
17+
setInverse <- function( i )
18+
{
19+
inverse <<- i
20+
}
21+
getInverse <- function()
22+
{
23+
inverse
24+
}
25+
list( set=set, get=get, setInverse=setInverse, getInverse=getInverse )
826
}
927

10-
11-
## Write a short comment describing this function
12-
28+
## This method calculates the inverse of matrix x
1329
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
30+
candidate <- x$getInverse()
31+
if( !is.null( candidate ) ) {
32+
candidate
33+
} else {
34+
inverse <- solve( x$get() )
35+
x$setInverse( inverse )
36+
inverse
37+
}
1538
}

0 commit comments

Comments
 (0)