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
48 lines (42 loc) · 1.91 KB
/
cachematrix.R
File metadata and controls
48 lines (42 loc) · 1.91 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
## We are trying to cache an expensive matrix operation viz. inversion of matrix.
## We will be given a square matrix, and we are supposed to return the inverse of it.
## we need to make sure that matrices which has already been inverted,
## should not be inverted again, instead we should return the previously computed result.
## To achieve this, we will use two functions 1. makeCacheMatrix 2. cacheSolve
## The functions are described below individually
## Function makeCacheMatrix() can be viewed as a container which holds the user provided matrix,
## it's invert and few functions to retrieve and set both the quantities.
## makeCacheMatrix function take a square matrix as input (assumption as per the assignment statement),
## and returns a list which has the 4 functions.
## im is a variable local to the makeCacheMatrix Frame, it stores the result and is initialized to NULL
## set is function that takes a matrix and over-writes the formal argument variable x
## get is a function that returns the user provided matrix
## setSolution is a function which stores a given inversion result
## getSolution is a function that returns the cached solution
makeCacheMatrix <- function(x = matrix()) {
im <- NULL
set <- function(y) {
x <<- y
im <<- NULL
}
get <- function() x
setSolution <- function(solution) im <<- solution
getSolution <- function() im
list(set = set, get = get,
setSolution = setSolution,
getSolution = getSolution)
}
## cacheSolve is the function which takes the list object returned by makeCacheMatrix as input and gives the inverse of that matrix as output.
## First we fetch the cached result and if it's null then we compute the inverted matrix and store it for future use
## else we simply return the cached result.
cacheSolve <- function(x, ...) {
im <- x$getSolution()
if(!is.null(im)) {
message("getting cached data")
return(im)
}
data <- x$get()
im <- solve(data, ...)
x$setSolution(im)
im
}