forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
48 lines (39 loc) · 1.07 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
## Functions below allow computing of the matrix's inverse
## and caching the result for subsequent calls
## Function takes a matrix as an input and returns a special
## cached matrix structure (list)
makeCacheMatrix <- function(x = matrix()) {
i <- NULL
## setter of matrix
set <- function(y){
x <<- y
i <<- NULL
}
## getter of matrix
get <- function() x
## setter of inverted
setInv <- function(inverted) i <<- inverted
## getter of inverted
getInv <- function() i
## returned structure
list(set = set, get = get,
setInv = setInv,
getInv = getInv)
}
## Function computes an inverse of the matrix
## It takes cached matrix as an put
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
## try get from cache
i <- x$getInv()
if (!is.null(i)){
message("getting from cache")
return (i)
}
## else get and save in cache for a later use
message("computing inverse")
originalMatrix <- x$get()
i <- solve(originalMatrix)
x$setInv(i)
i
}