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
62 lines (50 loc) · 1.86 KB
/
cachematrix.R
File metadata and controls
62 lines (50 loc) · 1.86 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
## Implementation of matrix objects which cach their own inverse
## makeCacheMatrix creates an object which represent a matrix which caches it's
## own inverse.
## Parameters:
## x - An R matrix to be represented by the returned object.
## Return value:
## An object representing a matrix which cahces it's own object.
makeCacheMatrix <- function(x = matrix()) {
# cache value placeholder
inverse <- NULL
# matrix value setter, resetting the cache
set <- function(y) {
x <<- y
inverse <<- NULL
}
# matrix value getter
get <- function() x
# cache setter
setsolve <- function(solution) inverse <<- solution
# cache getter
getsolve <- function() inverse
#return the object as a list of the object functions
list(set = set, get = get, setsolve = setsolve, getsolve = getsolve)
}
## cacheSolve retrieves the inverse of a matrix, represented by an object
## created by the makeCacheMatrix function, by retrieving the cached value
## if it exists, or by calculating and caching the value otherwise.
## Parameters:
## x - an object created by makeCacheMatrix
## Return value:
## A matrix that is the inverse of 'x'
cacheSolve <- function(x, ...) {
# get the cached value
inverse <- x$getsolve()
if(!is.null(inverse)) {
# the cached value exists, return it
message("getting cached data")
return(inverse)
}
# the cache wasn't calculated yet, calculate it
message("calculating data")
# get the matrix
m <- x$get()
# calaculate the inverse
inverse <- solve(m, ...)
# cache the value
x$setsolve(inverse)
# return the caclulated value
inverse
}