From d9a4e615d3f3c39d92ed2069dc824ffadd8815e1 Mon Sep 17 00:00:00 2001 From: lareelarr Date: Sat, 21 Sep 2019 22:56:12 -0400 Subject: [PATCH 1/2] Updated the function shells with the proper code. --- cachematrix.R | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..623769b15f4 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,37 @@ -## Put comments here that give an overall description of what your -## functions do +## Write a pair of functions that cache the inverse of a matrix. -## Write a short comment describing this function +## makeCacheMatrix: This function creates a special "matrix" object that test +## can cache its inverse. makeCacheMatrix <- function(x = matrix()) { - + i <- NULL + set <- function(y) { + x <<- y + i <<- NULL + } + get <- function() x + setinv <- function(solve) i <<- solve + getinv <- function() i + list(set = set, get = get, + setinv = setinv, + getinv = getinv) } -## Write a short comment describing this function +## This function computes the inverse of the special "matrix" returned by +## makeCacheMatrix above. If the inverse has already been calculated +## (and the matrix has not changed), then the cachesolve should retrieve +## the inverse from the cache. cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + i <- x$getinv() + if(!is.null(i)) { + message("getting cached data") + return(i) + } + data <- x$get() + i <- solve(data, ...) + x$setinv(i) + i } From 2b6609a9e51798d94fd4d0ac232f46cc50ab3a8d Mon Sep 17 00:00:00 2001 From: Larry Mannings Date: Wed, 26 Nov 2025 00:23:47 -0500 Subject: [PATCH 2/2] Redid the assignment as a refresher --- cachematrix.R | 84 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 30 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 623769b15f4..926b0fc9b6b 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,37 +1,61 @@ -## Write a pair of functions that cache the inverse of a matrix. +# Written by Larry Mannings, 25 November 2025 -## makeCacheMatrix: This function creates a special "matrix" object that test -## can cache its inverse. +# 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 = matrix()) { - i <- NULL - set <- function(y) { - x <<- y - i <<- NULL - } - get <- function() x - setinv <- function(solve) i <<- solve - getinv <- function() i - list(set = set, get = get, - setinv = setinv, - getinv = getinv) +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) } -## This function computes the inverse of the special "matrix" returned by -## makeCacheMatrix above. If the inverse has already been calculated -## (and the matrix has not changed), then the cachesolve should retrieve -## the inverse from the cache. +#--------------------------------------------------------------------------- + +# 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' - i <- x$getinv() - if(!is.null(i)) { - message("getting cached data") - return(i) - } - data <- x$get() - i <- solve(data, ...) - x$setinv(i) - i -} + + # 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 +} \ No newline at end of file