forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachematrix.R
More file actions
48 lines (39 loc) · 1.43 KB
/
cachematrix.R
File metadata and controls
48 lines (39 loc) · 1.43 KB
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
## Functions are used to cache inversion of matrix operation
## instead of realculation for identical matrix
## Function constructs a "matrix wrapper" as list of functions
## for accessing and setting underlying data.
## 'x' is a matrix to be inverted
## Return list of functions:
# get() - returns original matrix
# set(y) - replaces original matrinx with new one;
# clears cached value of previous inversion
# setInverse(inverted) - saves inverted matrix to cache
# getInverse() - returns inverted matrix from cache
makeCacheMatrix <- function(x = matrix()) {
cachedInv <- NULL
get <- function() x
set <- function(y) {
x <<- y
cachedInv <<- NULL
}
setInverse <- function(inverted) cachedInv <<- inverted
getInverse <- function() cachedInv
list(set = set, get = get,
setInverse = setInverse,
getInverse = getInverse)
}
## Function solves matrix wrapped by makeCacheMatrix() if there is no cached
## value and saves result to cache. Otherwise return cached result.
## 'x' - "matrix wrapper" returned by makeCacheMatrix().
## Return a matrix that is the inverse of 'x'
cacheSolve <- function(x, ...) {
solvedMatrix <- x$getInverse()
if(!is.null(solvedMatrix)) {
message("getting cached data")
return(solvedMatrix)
}
originalMatrix <- x$get()
solvedMatrix <- solve(originalMatrix, ...)
x$setInverse(solvedMatrix)
solvedMatrix
}