forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
53 lines (37 loc) · 1.62 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
## Two functions in this file implement persistance through caching inverted matrices
## This function caches inverted matrix
makeCacheMatrix <- function(x = matrix()) {
## initialize result
invMatrix <- NULL
## set matrix
set <- function (mx){
x <<- mx
invMatrix <- NULL
}
## get original matrix
get<- function() x
## set inverted matrix
setInverted <- function(solve) invMatrix <- solve
## get inverted matrix
getInverted <- function() invMatrix
## make functions available as part of makeCacheMatrix
list(set = set, get = get, invertMatrix = setInverted, getInvertedMatrix = getInverted)
}
## This function computes inverse of the matrix. If such inversion exists already, return cached value
## otherwise force the calculation and store in the cache
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
## check if inverted matrix exists, if so return its value
invMatrix <- x$getInvertedMatrix()
if (!is.null(invMatrix)){
message("getting inverted matrix")
return (invMatrix)
}
## if inverted matrix doesn't exist yet, invert it and set cache to new value
## solve function is called with only one parameter forcing use of identity matrix
data <- x$get()
invMatrix <- solve(data)
x$invertMatrix(invMatrix)
# return newly computed value
invMatrix
}