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
52 lines (45 loc) · 1.32 KB
/
Copy pathcachematrix.R
File metadata and controls
52 lines (45 loc) · 1.32 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
49
50
51
52
## Coursera: Data Science Specialization
## Module: R Programing
## Assignment: 2
## makeCacheMatrix
## This function creates a "Special" version of matrix, which stores its inverted
## version in cache. It provides easy interface to work with cache
makeCacheMatrix <- function(x = matrix()) {
matrix_cache <- NULL
setMatrix <- function(mat) {
x <<- mat
matrix_cache <<- NULL
}
getMatrix <- function() {
x
}
setInverted <- function(mat) {
matrix_cache <<- mat
}
getInverted <- function() {
matrix_cache
}
list(
getInverted = getInverted,
getMatrix = getMatrix,
setInverted = setInverted,
setMatrix = setMatrix
)
}
## cacheSolve
## This function is an facade to solve function, which uses cached matrix
## object and returns inverse matrix. If the invers doesn't exists this
## function calculates it in traditional way.
cacheSolve <- function(x, ...) {
inverted <- x$getInverted()
# Check if inverse matrix was cached otherwise calculate one.
if ( is.null(inverted) ) {
data <- x$getMatrix()
if ( nrow(data) != ncol(data) ) {
stop("Can not calculate inversion for a not square Matrix")
}
inverted <- solve(data, ...)
x$setInverted(inverted)
}
inverted
}