forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
55 lines (49 loc) · 2.15 KB
/
cachematrix.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
## Put comments here that give an overall description of what your
## functions do
## These functions are designed to create a Matrix object which caches the Inverse
## of the Matrix for resuse with out recomputing the Inverse
## Write a short comment describing this function
## This function creates a list for the Matrix which contains member functions for
## 1 - Seting the value of the matrix
## 2 - Returning the matrix
## 3 - Computing the Inverse
## 4 - Returning the Inverse
makeCacheMatrix <- function(x = matrix()) {
# Set the inverse to Null to begin
inverse <- NULL
# The Set function will set the matrix within the CacheMatrix object to
# the matrix but it will not compute the inverse
set <- function(y) {
y <<- x
inverse <<- NULL
}
# The Get function returns the matrix
get <- function() x
# The setInverse function sets the value to computed
# This works because the super assignment operator <<- will go up in
# scope to the inervse variable defined by the makeCacheMatrix
# environment. It exploits the lexical scoping rules of R.
setInverse <- function(inverseComputed) inverse <<- inverseComputed
# The getInverse function returns the current value of the Inverse
getInverse <- function() inverse
# Finally we return the list with the functions
list(set = set, get = get, setInverse = setInverse, getInverse = getInverse)
}
## Write a short comment describing this function
## This function returns the inverse of the matrix, but only calls solve(X) function
## if the inervse has not been calculated before
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inverse <- x$getInverse()
# if the inverse has been cached use it
if (!is.null(inverse)) {
message("Using cached inverse")
return(inverse)
}
#otherwise we have to compute it
myMatrix <- x$get()
inverseComputed <- solve(myMatrix)
x$setInverse(inverseComputed)
# and then return it
inverseComputed
}