forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
52 lines (48 loc) · 1.64 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
## Put comments here that give an overall description of what your
## functions do
## Write a short comment describing this function
##
## As far as I understand it, the logic of this program is as follows.
## “Caching” refers to using a value that is already defined rather than
## re-calculating it. This can be done by using the scoping rules and in
## particular the <<- operator.
##
## First, makeCacheMatrix() creates a list of functions, which can be used to
## set the value of the matrix and retrieve them, as well as get the result
## of solve() (and provide one?).
##
## makeCacheMatrix returns a list.
makeCacheMatrix <- function(x = matrix()) {
s <- NULL
set <- function(y) {
x <<- y
s <<- NULL
}
get <- function() x
setsolution <- function(solution) s <<- solution
getsolution <- function() s
list (set = set, get = get,
setsolution = setsolution,
getsolution = getsolution)
}
## Write a short comment describing this function
##
## cacheSolve takes an argument of the type of the output of makeCacheMatrix
## as an argument and tries to see whether that list's getsolution() function
## is null or not. If it is not, it will not calculate the inverse but simply
## return the pre-set (cached) value.
##
## If it is null (e.g. is.null(s) is TRUE), it will solve the matrix using
## the makeCacheMatrix function defined above.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
s <- x$getsolution()
if (!is.null(s)) {
message("Getting cached data.")
return(s)
}
data <- x$get()
s <- solve(data, ...)
x$setsolution(s)
s
}