Skip to content

Commit 3cb6d17

Browse files
committed
Updated makeCacheMatrix and cacheSolve functions so that they calculate and cache correcly as per assignment
1 parent 7f657dd commit 3cb6d17

File tree

1 file changed

+79
-9
lines changed

1 file changed

+79
-9
lines changed

cachematrix.R

Lines changed: 79 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,85 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
5-
6-
makeCacheMatrix <- function(x = matrix()) {
1+
## Assignment 2 of R Programming course
2+
#######################################
73

4+
## Function responsible to 'augment' a matrix by providing a way of storing
5+
## its value and provides means of setting, getting an associated cached
6+
## object (in this exercise we store the inverse of a matrix but other
7+
## results of expensive calculations could be stored).
8+
## Returns:
9+
## List of functions to get set the matrix and get set the cached object.
10+
## How it works:
11+
## The function makeCacheMatrix is defined in the global frame but the list
12+
## that it returns is defined in the local frame of the function call and
13+
## therefore inherits the environment of this call.
14+
## The variable cache.obj and m are also defined within the function call
15+
## frame and therefore also inherit the environment of the call.
16+
## The functions bound to the list returned by makeCacheMatrix all reference
17+
## to free variables, these are variables not defined within theses functions'
18+
## body but, due to the Lexical scoping of R, will be searched in the parent
19+
## environment, that is the environment where the functions are defined.
20+
makeCacheMatrix <- function(m = matrix()) {
21+
22+
23+
# Initialising variable to NULL, this is done in the environment created
24+
# when makeCacheMatrix is called
25+
cached.obj <- NULL
26+
27+
# Stores some cache data into a variable that is defined in the frame
28+
# first call of makeCacheMatrix.
29+
setCachedObj <- function(o) {
30+
# cached.obj is a free variable (not defined within this function)
31+
# Using the <<- operator causes a search to be made through parent
32+
# environments for an existing definition of the variable being
33+
# assigned.
34+
cached.obj <<- o
35+
}
36+
37+
# Function that returns the free variable cached.obj, defined in the
38+
# parent environment of getCachedObj's call environment.
39+
getCachedObj <- function() cached.obj
40+
41+
# Function that returns the initial matrix passed in to makeCacheMatrix
42+
# as an argument.
43+
# m within the body of get is a free variable
44+
get <- function() m
45+
46+
# Function that resets the value of matrix and clears the cached object.
47+
# This is done only if the new matrix is different from the previously
48+
# stored matrix.
49+
set <- function(y) {
50+
51+
if( ! identical( m, y ) ){
52+
m <<- y
53+
cached.obj <<- NULL
54+
}
55+
}
56+
57+
# Returning a list that points to internal functions used to set get
58+
# the initial matrix and the stored cache data.
59+
list(get = get, set = set, setCachedObj = setCachedObj, getCachedObj = getCachedObj)
860
}
961

1062

11-
## Write a short comment describing this function
12-
63+
## Function to calculate the inverse of a matrix and cache the result.
64+
## Returns either a newly calculated inverse or a previously cached result if
65+
## available.
66+
## Takes as arguments an 'augmented' matrix (the result of makeCacheMatrix's call)
67+
## and a variable length of paramters ('...' or ellipses), these latter are passed
68+
## to the inverse function (solve).
1369
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
70+
71+
# Checking if the 'augmented' matrix's cached object is not null, in that case
72+
# skip any calculation and further assignment and return the cached object.
73+
inv <- x$getCachedObj()
74+
75+
if(is.null(inv)) {
76+
# If we are here we have to calculate the inverse, let's pull the original
77+
# matrix out the 'augmented' matrix object, calculate its inverse and store
78+
# it into the 'augmented' matrix
79+
data <- x$get()
80+
inv <- solve(data, ...)
81+
x$setCachedObj(inv)
82+
}
83+
# returning the cached object or the freshly calculated inverse.
84+
inv
1585
}

0 commit comments

Comments
 (0)