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

Skip to content

Commit 7c407dd

Browse files
author
christopher floess
committed
Implement functions capable of working with cached values
1 parent 7f657dd commit 7c407dd

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

cachematrix.R

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,39 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## Implementation of functions exibiting caching of difficult to
2+
## calculate values. This is done by using to <<- operator to save
3+
## values calculated in nested functions to the parent function's
4+
## scope, allowing other nested values to work with these values.
35

4-
## Write a short comment describing this function
56

7+
## This function takes a matrix as an argument. I returns an "object"
8+
## that provides a series of functions for retrieving and setting the
9+
## matrix and it's inverse. It does not however calculate its own
10+
## inverse. The set() function is also responsible for setting the
11+
## inverse of the matrix to null. This is done so that other functions
12+
## working with these "objects" whether to set the inverse.
613
makeCacheMatrix <- function(x = matrix()) {
7-
14+
inverse <- NULL
15+
get <- function(){
16+
x
17+
}
18+
getInverse <- function(){
19+
inverse
20+
}
21+
set <- function(y){
22+
x <<- y
23+
inverse <<- NULL
24+
}
25+
setInverse <- function(m) inverse <<- m
26+
list(get = get, getInverse = getInverse, set = set, setInverse = setInverse)
827
}
928

1029

11-
## Write a short comment describing this function
12-
13-
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
30+
## This function receives a matrix "object" and is responsible
31+
## computing the inverse of the "objects's" matrix. It ONLY does this
32+
## however, if the inverse of the "object's" matrix is null.
33+
cacheSolve <- function(x) {
34+
i <- x$getInverse()
35+
if(is.null(i)) {
36+
i <<- solve(x$get())
37+
}
38+
i
1539
}

0 commit comments

Comments
 (0)