From 56bb446a57b6a71dd7de24888370d4764e113015 Mon Sep 17 00:00:00 2001 From: Naman Garg Date: Fri, 24 Oct 2025 05:20:22 +0530 Subject: [PATCH 1/2] defined the functions makeCacheMatrix and cacheSolve --- cachematrix.R | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa4..174e109a09 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -4,7 +4,17 @@ ## Write a short comment describing this function makeCacheMatrix <- function(x = matrix()) { - + i <- NULL + set <- function(y) { + x <<- y + i <<- NULL + } + get <- function() x + setinverse <- function() { + i <<- solve(x) + } + getinverse <- function() i + list(set=set,get=get,setinverse=setinverse,getinverse=getinverse) } @@ -12,4 +22,11 @@ makeCacheMatrix <- function(x = matrix()) { cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + i <- x$getinverse() + if (!is.null(i)){ + message("from the cache") + return(i) + } + i <- x$setinverse() + i } From ee4787b53e24cd5e92cede3c697679e9895b1041 Mon Sep 17 00:00:00 2001 From: Naman Garg Date: Fri, 24 Oct 2025 05:54:10 +0530 Subject: [PATCH 2/2] added comments to the function --- cachematrix.R | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 174e109a09..13c66194f6 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,7 +1,10 @@ ## Put comments here that give an overall description of what your ## functions do -## Write a short comment describing this function +## this function returns a list of functions. +## the functions within the list all have their scope set within the parent function +## the functions within the list allow us to cache a matrix, retrieve a cached matrix, +## cache inverse of the cached matrix, retrieve cached inverse of the a cached matrix makeCacheMatrix <- function(x = matrix()) { i <- NULL @@ -18,7 +21,9 @@ makeCacheMatrix <- function(x = matrix()) { } -## Write a short comment describing this function +## this function returns the inverse of the cached matrix within the variable +## the functions first checks if the inverse is cached and sends the cached results back if condition is true +## if the inverse of the matrix isn't cached the the function solves for the inverse, caches it, and shares back the result cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x'