From 808ba9eb1bcab7a1ceb4621c0df93deaf026d70e Mon Sep 17 00:00:00 2001 From: nyario Date: Fri, 20 Feb 2026 13:45:04 +0100 Subject: [PATCH] cachematrix.R --- cachematrix.R | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..d40693d8fe3 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -2,14 +2,55 @@ ## functions do ## Write a short comment describing this function +# makeCacheMatrix creates a special "matrix" object that can cache its inverse. +# It stores a matrix and its inverse in a private environment. +# The returned list contains functions to set and get the matrix, +# as well as to set and get the cached inverse. makeCacheMatrix <- function(x = matrix()) { - +m <- NULL + + set <- function(y) { + x <<- y + m <<- NULL + } + + get <- function() { + x + } + + setinverse <- function(inverse) { + m<<-inverse + } + + getinverse <- function() { + m + } + + list(set = set, + get = get, + setinverse = setinverse, + getinverse = getinverse) } ## Write a short comment describing this function +# cacheSolve computes the inverse of the matrix stored in the special object. +# If the inverse has already been computed, it retrieves the cached value. +# Otherwise, it calculates the inverse using solve(), caches it, +# and then returns the result. cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + inv <- x$getinverse() + + if (!is.null(inv)) { + message("getting cached data") + return(inv) + } + + data <- x$get() + inv <- solve(data, ...) + x$setinverse(inv) + inv }