Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 54 additions & 8 deletions cachematrix.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,61 @@
## Put comments here that give an overall description of what your
## functions do
# Written by Larry Mannings, 25 November 2025

## Write a short comment describing this function

makeCacheMatrix <- function(x = matrix()) {
# makeCacheMatrix() function leverages the makeVector example from R Programming
# Ass. 2, modified to create a list consisting of cached matrix and
# corresponding inverse.

makeCacheMatrix <- function(x = numeric()) {

# x is numeric matrix object representing an invertible matrix
# i is matrix object representing inverse of matrix object (x)

# Initialize the inverse matrix to NULL
i <- NULL

# Set the value of the matrix
set <- function(y) {
x <<- y
i <<- NULL
}
# Get the value of the matrix
get <- function() x

# Set the value of the inverse matrix
setinv <- function(inv) i <<- inv

# Get the value of the inverse matrix
getinv <- function() i

# Store objects in a list and return
list(set = set, get = get,
setinv = setinv,
getinv = getinv)
}


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

# cacheSolve() function leverages the cachemean() example from R Programming
# Ass. 2, modified to compute the inverse of the returned by
# makeCacheMatrix() function.

cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
}

# x is list object representing constructed from the makeCacheMatrix
# function
# i is matrix object representing inverse of matrix object (x)

# Call the getinv(function) as a list object to get matrix inverse
i <- x$getinv()
# If the inverse has already been calculated retrieve from the cache
if(!is.null(i)) {
message("getting cached data")
return(i)
}
# Else, retrieve the cached matrix and solve for the inverse matrix
data <- x$get()
i <- solve(data, ...)
# Cache the inverse matrix and return value
x$setinv(i)
i
}