Skip to content

Commit cf775bc

Browse files
committed
Comments for the entire file. A usage section with short comments for each of the two functions: makeCacheMatrix and cacheSolve
1 parent 2f815bf commit cf775bc

File tree

1 file changed

+43
-17
lines changed

1 file changed

+43
-17
lines changed

cachematrix.R

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,60 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## Couple of functions to optimize inversing matrixes as it could be fairly
2+
## computational work to find the inverse of a large matrix. Usage below:
3+
##
4+
## [Inspired by Johanna Tikanmäki COMMUNITY TA usage for cachemean on
5+
## https://class.coursera.org/rprog-014/forum/thread?thread_id=105]
6+
##
7+
## cMatrix <- makeCacheMatrix(m) #first a cache aware matrix where 'm' is the
8+
## #matrix you would like to use as input
9+
## cacheSolve(cMatrix) #calculate Inverse (solve) of the matrix
10+
## cacheSolve(cMatrix) #this will return the cached matrix inverse
11+
## cMatrix$set(new_m) #cMatrix can be set to a new input matrix ('new_m')
12+
## cacheSolve(cMatrix) #calculate Inverse of new_m for the first time
13+
## cacheSolve(cMatrix) #this will return the cached inverse
14+
## cacheSolve(cMatrix) #same result as above; still cached
315

4-
## Write a short comment describing this function
516

6-
makeCacheMatrix <- function(x = matrix()) {
7-
cachedMatrixInverse <- NULL
17+
## Create a cached aware matrix that that be reused with new matrixes as well
18+
19+
makeCacheMatrix <- function(m = matrix()) {
20+
cachedMatrixInverse <- NULL #cached matrix inverse value if set
821
set <- function(y) {
9-
x <<- y
10-
cachedMatrixInverse <<- NULL
22+
m <<- y #set new value for raw matrix 'm' set
23+
#at makeCacheMatrix function level using
24+
#<<-
25+
cachedMatrixInverse <<- NULL #clear cached as 'm' has changed at the
26+
#parent frame of this function
1127
}
12-
get <- function() x
28+
get <- function() m #get raw matrix 'm'
29+
30+
#functions below: set & get cached matrix value for makeCacheMatrix's
31+
#cacheMatrixInverse variable
1332
setMatrixInverse <- function(inv) cachedMatrixInverse <<- inv
1433
getMatrixInverse <- function() cachedMatrixInverse
34+
35+
#return a list containing various functions that can be used to:
36+
#get raw matrix 'm'
37+
#set raw matrix that will clear matrix inverse as well
38+
#getMatrixInverse to get cachedMatrixInverse
39+
#setMatrixInverse to set cachedMatrixInverse
1540
list(set = set, get = get,
1641
setMatrixInverse = setMatrixInverse,
1742
getMatrixInverse = getMatrixInverse)
1843
}
1944

2045

21-
## Write a short comment describing this function
46+
## Solve (Inverse) and cache matrix. Has to be used as shown in the usage
47+
## section. 'x' has to be an object returned by makeCacheMatrix function
2248

2349
cacheSolve <- function(x, ...) {
24-
## Return a matrix that is the inverse of 'x'
25-
matrixInverse <- x$getMatrixInverse()
26-
if(!is.null(matrixInverse)) {
27-
message("found cached matrix inverse")
28-
return(matrixInverse)
50+
matrixInverse <- x$getMatrixInverse() #try retrieving cached value
51+
if(!is.null(matrixInverse)) { #if we find the value
52+
message("found cached matrix inverse") #message we have cached value
53+
return(matrixInverse) #return from this function
2954
}
30-
matrix <- x$get()
31-
calculatedMatrixInverse <- solve(matrix)
32-
x$setMatrixInverse(calculatedMatrixInverse)
55+
matrix <- x$get() #get actual matrix data
56+
calculatedMatrixInverse <- solve(matrix) #use solve from {base} package
57+
x$setMatrixInverse(calculatedMatrixInverse) #set cached value for x
58+
## Return a matrix that is the inverse of 'x'
3359
calculatedMatrixInverse
3460
}

0 commit comments

Comments
 (0)