From ff68fe3fecdd1ed5abf5e90e9dc71a31a7501885 Mon Sep 17 00:00:00 2001 From: Kamsi Dan Date: Thu, 4 Jun 2026 22:05:15 +0300 Subject: [PATCH 1/3] Caching the inverse of a matrix - successfully completed! --- cachematrix.R | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..bc15e0ddaa2 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,36 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function +## This function creates a matrix that caches the value of its inverse makeCacheMatrix <- function(x = matrix()) { + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setmatrix <- function(solve) m <<- solve + getmatrix <- function() m + list(set = set, get = get, + setmatrix = setmatrix, + getmatrix = getmatrix) } -## Write a short comment describing this function +## to solve for the inverse, and retrieve cached inverse when called if it has already been calculated cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + m <- x$getmatrix() + if(!is.null(m)) { + message("getting cached data") + return(m) + } + data <- x$get() + m <- solve(data, ...) + x$setmatrix(m) + m } + +my_matrix <- makeCacheMatrix(matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2)) #to initialize matrix for testing +cacheSolve(my_matrix) #first function run to calculate matrix inverse +cacheSolve(my_matrix) #to see if inversion was gotten from cache this time and not recalculated +my_matrix$getmatrix() #to see if cache was stored in 'getmatrix()' From ec859b927eb82fb957e72c1621d0e612f8cb5cc6 Mon Sep 17 00:00:00 2001 From: Kamsi Dan Date: Thu, 4 Jun 2026 22:12:21 +0300 Subject: [PATCH 2/3] new comment added for cacheSolve() --- .gitignore | 5 +++++ ProgrammingAssignment2.Rproj | 13 +++++++++++++ cachematrix.R | 2 +- 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 ProgrammingAssignment2.Rproj diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000000..7ef3ead157a --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.Rproj.user +.Rhistory +.RData +.Ruserdata +.positai diff --git a/ProgrammingAssignment2.Rproj b/ProgrammingAssignment2.Rproj new file mode 100644 index 00000000000..8e3c2ebc99e --- /dev/null +++ b/ProgrammingAssignment2.Rproj @@ -0,0 +1,13 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX diff --git a/cachematrix.R b/cachematrix.R index bc15e0ddaa2..f0fbb361a9b 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -16,7 +16,7 @@ makeCacheMatrix <- function(x = matrix()) { } -## to solve for the inverse, and retrieve cached inverse when called if it has already been calculated +## to solve for the inverse, and retrieve cached inverse from 'makeCacheMatrix()' when called if it has already been calculated cacheSolve <- function(x, ...) { m <- x$getmatrix() From 3f68a71b056c17c3a7b0ef62b7c88ced7ba67f44 Mon Sep 17 00:00:00 2001 From: Kamsi Dan Date: Thu, 4 Jun 2026 22:44:00 +0300 Subject: [PATCH 3/3] added more comments for better explanation --- cachematrix.R | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index f0fbb361a9b..360a138b5f5 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,14 +1,17 @@ ## This function creates a matrix that caches the value of its inverse makeCacheMatrix <- function(x = matrix()) { - m <- NULL + matrix_inverse <- NULL #sets inverse to be null until calculated + + #to set the matrix set <- function(y) { x <<- y - m <<- NULL + matrix_inverse <<- NULL } + #to get the matrix inverse from this function get <- function() x - setmatrix <- function(solve) m <<- solve - getmatrix <- function() m + setmatrix <- function(solve) matrix_inverse <<- solve + getmatrix <- function() matrix_inverse list(set = set, get = get, setmatrix = setmatrix, getmatrix = getmatrix) @@ -19,15 +22,15 @@ makeCacheMatrix <- function(x = matrix()) { ## to solve for the inverse, and retrieve cached inverse from 'makeCacheMatrix()' when called if it has already been calculated cacheSolve <- function(x, ...) { - m <- x$getmatrix() - if(!is.null(m)) { - message("getting cached data") - return(m) + matrix_inverse <- x$getmatrix() #to get the matrix inverse from the first function + if(!is.null(matrix_inverse)) { + message("getting cached data") #to print this message when retrieving cached data if already calculated + return(matrix_inverse) #retrieve cached data if already calculated } - data <- x$get() - m <- solve(data, ...) - x$setmatrix(m) - m + data <- x$get() #to get initialized matrix + matrix_inverse <- solve(data, ...) #solve for inverse + x$setmatrix(matrix_inverse) #set inverse as new output value + matrix_inverse #return the inverse } my_matrix <- makeCacheMatrix(matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2)) #to initialize matrix for testing