Skip to content

Commit c5a14a3

Browse files
committed
Add existing file
1 parent 7f657dd commit c5a14a3

File tree

1 file changed

+53
-7
lines changed

1 file changed

+53
-7
lines changed

cachematrix.R

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,61 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
# R Programming Week 2 Assignment: Lexical Scoping
32

4-
## Write a short comment describing this function
3+
# Validation:
4+
# m <- matrix(rnorm(4),nrow=2,ncol=2)
5+
# x <- makeCacheMatrix(m)
6+
# cacheSolve(x)
7+
# cacheSolve(x)
58

6-
makeCacheMatrix <- function(x = matrix()) {
9+
# Overall these functions create matrices and subsequently move the created matrix to and from
10+
# the cache memory and perform operations on the data
711

8-
}
12+
m <- matrix(rnorm(4),nrow=2,ncol=2) # creates the 2D square matrix to be inverted in this test
13+
print(m) # prints the created 2D square matrix to the console
14+
15+
# In principle, makeCacheMatrix creates the list of functions that move the data matrix and the
16+
# inverted data matrix to and from the cache memory
917

18+
makeCacheMatrix <- function(x = numeric()) {
19+
invm <- NULL ## initialises a NULL variable for the inverted matrix "invm" in the local memory
20+
set <- function(y) { ## creates the set function to set the data matrix and invm to the cache memory
21+
x <<- y ## sets the passed data matrix to cache memory
22+
invm <<- NULL ## initialises NULL in cache memory
23+
}
24+
get <- function() x ## function to get variable x from cache memory
25+
setinv <- function(solve) invm <<- solve ## function to set the value of invm in cache memory
26+
getinv <- function() invm ## get the value of invm from cache memory
27+
list(set = set, get = get, ## returns list of functions
28+
setinv = setinv,
29+
getinv = getinv)
30+
}
1031

11-
## Write a short comment describing this function
32+
# In pricniple, cacheSolve receives the data matrix (created above in x <- makeCacheMatrix(m)) and checks the cache to see if the
33+
# inverse of the matrix (invm) has already been calculated. If so, the value of invm stored in cache is displayed in the console.
34+
# If a NULL value is stored in the cache, the data matrix x (stored in cache) will be retrieved and the inverse matrix is calculated.
35+
# This will then be stored in cache memory and finally be displayed in the console.
1236

1337
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
38+
invm <- x$getinv() ## retrives the value of invm into local memory from cache memory
39+
if(!is.null(invm)) { ## NOT logical operator to determine whether inverse has already been calculated
40+
message("getting cached data") ## if NOT null, will display the message "getting cached data" ...
41+
return(invm) ## ... and print to console
42+
}
43+
data <- x$get() ## if a NULL value of invm is obtained, the data matrix is loaded to local memory
44+
invm <- solve(data, ...) ## and the inverse calculated using the solve() function
45+
x$setinv(invm) ## setinv saves the invm to cache memory
46+
invm ## ... and print to console
1547
}
48+
49+
# Example:
50+
51+
# m <- matrix(rnorm(4),nrow=2,ncol=2)
52+
# > x <- makeCacheMatrix(m)
53+
# > cacheSolve(x)
54+
# [,1] [,2]
55+
# [1,] 1.0593015 1.6706482
56+
# [2,] -0.1026943 0.9169256
57+
# > cacheSolve(x)
58+
# getting cached data
59+
# [,1] [,2]
60+
# [1,] 1.0593015 1.6706482
61+
# [2,] -0.1026943 0.9169256

0 commit comments

Comments
 (0)