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
67 lines (51 loc) · 1.63 KB
/
cachematrix.R
File metadata and controls
67 lines (51 loc) · 1.63 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
## A set of functions creating and handling caching of inverse
## matrix of the given one
## Creates an object, which contains matrix data itself, its cached
## inverse matrix and accessors (get/set data, get/set inverse matrix)
makeCacheMatrix <- function(x = matrix())
{
## initializing cached inverse matrix by null
cachedInv <- NULL
## defining set function
set <- function(y)
{
## saving data itself
x <<- y
## resetting cached inverse matrix
cachedInv <<- NULL
}
## defining get function - just returns data
get <- function() x
## defining setinv function - saves inverse matrix
setinv <- function(inv) cachedInv <<- inv
## defining getinv function - returns saved inverse matrix
getinv <- function() cachedInv
## generating list from those functions and returning it
list(set = set, get = get,
setinv = setinv,
getinv = getinv)
}
## calculates inverse matrix for x
## assumes x is always invertible, so no any sanity checks here
cacheSolve <- function(x)
{
## getting cached results
cachedInv <- x$getinv()
## if it is not null - can just return it
if(!is.null(cachedInv))
{
## printing a message about cached data usage and returning it
message("getting cached data")
return(cachedInv)
}
## if cached inverse matrix is null - need to calculate it
# getting data
data <- x$get()
## calculating inverse matrix (solve(a) without b assumes b is identity
## and returns inverse matrix for a)
cachedInv <- solve(data)
## saving calculated inverse matrix to cache
x$setinv(cachedInv)
## and returning it
cachedInv
}