From 6394b84fb1e5f80ff071475efb4e7542e91665b9 Mon Sep 17 00:00:00 2001 From: Chanda Ambreen <51892342+Ambreen-png@users.noreply.github.com> Date: Tue, 10 Jun 2025 21:33:47 +0500 Subject: [PATCH 1/2] Update cachematrix.R --- cachematrix.R | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..1148ffde2c9 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -3,10 +3,27 @@ ## Write a short comment describing this function -makeCacheMatrix <- function(x = matrix()) { +makeCacheMatrix <- function() { + mat <- NULL + inv_cache <- NULL + setMatrix <- function(x) { + mat <<- x + inv_cache <<- NULL +} + getInverse <- function() { + if(is.null(inv_cache)) { + inv_cache <<- solve(mat) +} + inv_cache } + list(setMatrix = setMatrix, getInverse = getInverse) +} + cacheSolve <- function(x, ...) { + inv_cache <- x$getInverse() + inv_cache +} ## Write a short comment describing this function From 7ee440cd604921d840f5c08c5ff4d85b194b68e3 Mon Sep 17 00:00:00 2001 From: Chanda Ambreen <51892342+Ambreen-png@users.noreply.github.com> Date: Tue, 17 Jun 2025 13:04:33 +0500 Subject: [PATCH 2/2] Update cachematrix.R --- cachematrix.R | 64 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 1148ffde2c9..17a70d54ee9 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -3,30 +3,46 @@ ## Write a short comment describing this function -makeCacheMatrix <- function() { - mat <- NULL - inv_cache <- NULL - - setMatrix <- function(x) { - mat <<- x - inv_cache <<- NULL -} - getInverse <- function() { - if(is.null(inv_cache)) { - inv_cache <<- solve(mat) -} - inv_cache -} - - list(setMatrix = setMatrix, getInverse = getInverse) -} - cacheSolve <- function(x, ...) { - inv_cache <- x$getInverse() - inv_cache +# Function to create a special "matrix" object that can cache its inverse +makeCacheMatrix <- function(x = matrix()) { + inv <- NULL # Initialize the inverse as NULL + + # Function to set a new matrix + set <- function(y) { + x <<- y + inv <<- NULL # Reset the inverse when a new matrix is set + } + + # Function to get the matrix + get <- function() x + + # Function to set the inverse + setInverse <- function(inverse) inv <<- inverse + + # Function to get the inverse + getInverse <- function() inv + + # Return a list of the functions + list(set = set, + get get, + setInverse = setInverse, + getInverse = getInverse) } -## Write a short comment describing this function - -cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' +# Function to compute or retrieve the cached inverse of the matrix +cachesolve <- function(x, ...) { + inv <- x$getInverse() # check if Inverse is already cached + + # If cached, return it + if (!is.null(inv)) { + message("Getting cached inverse") + return(inv) + } + + # If not cached, compute the inverse + mat <- x$get() + inv <- solve(mat, ...) # Compute the inverse + + x$setInverse(inv) # Cache the inverse + inv # Return the inverse }