forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
62 lines (58 loc) · 1.89 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
54
55
56
57
58
59
60
61
62
# Programmong Assignment 2
# This function creates a special "matrix" object
# that can cache its inverse.
# Mostly based on code from example "Caching the Mean of a Vector"
## Arguments:
## x - something numeric, empty by default
## (matrix is a vector with extra dimension)
makeCacheMatrix <- function(x = numeric()) {
m <- NULL
# actions to set matrix
set <- function(y) {
x <<- y
m <<- NULL
}
# actions to get matrix
get <- function() x
# actions to set inverse matrix
setsolve <- function(solve) m <<- solve
# actions to get inverse matrix
getsolve <- function() m
list(set = set, get = get,
setsolve = setsolve,
getsolve = getsolve)
}
# This function computes the inverse of the special "matrix"
# returned by makeCacheMatrix()
# Timer was added just because I'm curious
## Arguments:
## x - cashed matrix object,
## created by makeCacheMatrix()
## ... - any detailed settings
cacheSolve <- function(x, ...) {
# start timer
start <- strftime(Sys.time(), format = "%Y-%m-%d %H:%M:%S")
m <- x$getsolve()
if(!is.null(m)) {
# say user that inverse matrix was already calculated
print(message("getting cached data"))
# stop timer
print(paste(as.numeric(difftime(strftime(Sys.time(),
format = "%Y-%m-%d %H:%M:%S"),
t)),
"seconds"))
# return result
return(m)
}
# calculate for the first time
data <- x$get()
m <- solve(data, ...)
x$setsolve(m)
# stop timer
print(paste(as.numeric(difftime(strftime(Sys.time(),
format = "%Y-%m-%d %H:%M:%S"),
t)),
"seconds"))
# return a matrix that is the inverse of 'x'
m
}