From c8820d1013fbf61b1617da58ae9a7799dfd5eba6 Mon Sep 17 00:00:00 2001 From: Maheswara77 Date: Mon, 14 Jul 2025 14:32:39 +0530 Subject: [PATCH] Update cachematrix.R --- cachematrix.R | 59 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 11 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..e1afa15df04 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,52 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function - -makeCacheMatrix <- function(x = matrix()) { - +# Creates a special matrix object that can cache its inverse +# Argument parameter: Matrix, Output: List +makeCacheMatrix <- function(x = matrix()){ + # Initialize the inverse value + m <- NULL + + # Method to set the matrix + set <- function(y){ + x <<- y + m <<- NULL + } + + # Method to get the matrix + get <- function() x + + # Method to set the inverse of the matrix + setinverse <- function(inverse) m <<- inverse + + # Method to get the inverse of the matrix + getinverse <- function() m + + # Output list + 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' +# To compute the inverse of the special matrix +# Argument parameter: Matrix, Output: Inverse Matrix +cacheSolve <- function(x, ...){ + # Initialize a matrix that is the inverse of x matrix + m <- x$getinverse() + + # Return a matrix if it is the inverse of x matrix + if(!is.null(m)) { + message("getting cached data") + return(m) + } + + # Get the matrix from the object + data <- x$get() + + # Method to solve the inverse using matrix multiplication + m <- solve(data, ...) + + # Set the inverse of inverse matrix + x$setinverse(m) + + # Return the matrix + m }