forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
57 lines (47 loc) · 1.51 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
56
57
## This function creates and returns a list of functions that can be
## used to set an inverted matrix in the cache or to get an inverted
## matrix from the cache.
makeCacheMatrix <- function(x = matrix())
{
cacheMatrix <- NULL # for cached value, initialized as null
## to create a matrix in the current environment
set <- function (y)
{
x <<- y
cacheMatrix <<- NULL
}
## to obtain the original matrix
get <- function() {
x
}
## Inverts the matrix and stores it in the cache
setInverse <- function (solvedMatrix) {
cacheMatrix <<- solvedMatrix
}
## Obtains the inverted matrix from the cache
getInverse <- function() {
cacheMatrix
}
## Returns the list of created functions
list (set=set, get=get, setInverse=setInverse, getInverse=getInverse)
}
## This function computers the inverse of the special matrix returned
## by makeCacheMatrix. If the inverse has already been calculated
## (and the matrix has not changed), then cachesolve should retrieve
## the inverse from the cache.
cacheSolve <- function(x, ...)
{
inverse <- x$getInverse()
## Runs if the inverse has already been calculated
## (and the matrix hasn't been changed).
if (!is.null(inverse))
{
message ("Getting cached data")
return (inverse)
}
## If inverse wasn't calculated or matrix was changed
newMatrix <- x$get()
inverse <- solve (newMatrix, ...)
x$setInverse(inverse)
inverse
}