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
68 lines (62 loc) · 2.12 KB
/
cachematrix.R
File metadata and controls
68 lines (62 loc) · 2.12 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
68
## The following functions are intended for calculating and storing the inverse
## of a matrix. Their main goal is to cache this data in a way that can be
## easily accessible after it has been computed once, and in doing so saving in
## computing time and resources.
## This function creates a matrix object that can cache its inverse.
makeCacheMatrix <- function(x = matrix()) {
# This function creates a special "matrix" object that can cache its inverse.
#
# Args:
# x: A matrix object whose inverse is going to be calculated and cached
#
# Returns:
# A list of functions to access and store the inverse of the matrix
s <- NULL
set <- function(y) {
x <<- y
s <<- NULL
}
#get <- function() x
# Alternative writing for easier reading
get <- function(){
x
}
#setsolve <- function(solve) s <<- solve
#Alternative writing for easier reading
setsolve <- function(solve){
s <<- solve
}
#getsolve <- function() s
#Alternative writing for easier reading
getsolve <- function(){
s
}
# Return a list of the functions available
list(set = set, get = get,
setsolve = setsolve,
getsolve = getsolve)
}
## This function computes the inverse of a matrix returned by `makeCacheMatrix`.
## If the inverse has already been calculated (and the matrix has not changed),
## then `cacheSolve` should retrieve the inverse from the cache.
cacheSolve <- function(x, ...) {
# This function computes the inverse of a matrix returned by `makeCacheMatrix`.
# If the inverse has already been calculated (and the matrix has not changed),
# then `cacheSolve` should retrieve the inverse from the cache.
#
# Args:
# x: A list of functions that enable access to the inverse of a matrix
#
# Returns:
# Return a matrix that is the inverse of a given matrix stored by
# makeCacheMatrix
s <- x$getsolve()
if(!is.null(s)) {
message("getting cached data")
return(s)
}
data <- x$get()
s <- solve(data, ...)
x$setsolve(s)
s
}