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
55 lines (44 loc) · 1.89 KB
/
cachematrix.R
File metadata and controls
55 lines (44 loc) · 1.89 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
## General description of functions in this file:
## cacheSolve returns the solution (inverted matrix) for a provided matrix of
## our special type created by makeCacheMatrix
## A function that creates our special matrix.
## This matrix is actually a list that holds functions for
## accessing and setting the value of the matrix and its inverse.
### set - stores the matrix value and sets the solution to NULL;
### get - retrieves the input matrix;
### setSolved - stores the provided solution;
### retrieves the stored solution.
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
set <- function(y) {
x <<- y
m <<- NULL # reset the solution
}
get <- function() x
setSolved <- function(solved) m <<- solved
getSolved <- function() m
list(set = set, get = get,
setSolved = setSolved,
getSolved = getSolved)
}
## This function returns a matrix that is the inverse of given matrix.
## It will retrieve a cached solution (inverse of a matrix in our case)
## or - if it does not yet exist; calculate it and store it for later.
## The function takes our special CacheMatrix (created by the function above)
## as an argument and tries to retrieve a solution that is stored for it.
## If there is a solution currently stored it is retrieved and returned.
## If there is no solution currently stored (it's a NULL value) we retrive
## the stored matrix, calculate its invert and store the solution for future
## use before returning it.
cacheSolve <- function(x, ...) {
m <- x$getSolved()
if(!is.null(m)) {
message("Getting a previously cached solution")
return(m)
}
# if there is no previous solution cached
data <- x$get()
m <- solve(data, ...)
x$setSolved(m)
m
}