|
1 | | -## Put comments here that give an overall description of what your |
2 | | -## functions do |
3 | | - |
4 | | -## Write a short comment describing this function |
| 1 | +# Matrix Inversion Caching Functions |
| 2 | +# This script implements two functions for caching matrix inversions: |
| 3 | +# 1. makeCacheMatrix: Creates a special matrix object that can cache its inverse |
| 4 | +# 2. cacheSolve: Computes the inverse of the special matrix, using cached values when available |
5 | 5 |
|
| 6 | +#' Create a special matrix object that can cache its inverse |
| 7 | +#' |
| 8 | +#' This function returns a list of functions that can: |
| 9 | +#' - set the matrix value |
| 10 | +#' - get the matrix value |
| 11 | +#' - set the inverse matrix |
| 12 | +#' - get the inverse matrix |
| 13 | +#' |
| 14 | +#' @param x A square invertible matrix |
| 15 | +#' @return A list of functions to interact with the matrix and its cached inverse |
6 | 16 | makeCacheMatrix <- function(x = matrix()) { |
7 | | - |
| 17 | + # Initialize the inverse as NULL |
| 18 | + inv <- NULL |
| 19 | + |
| 20 | + # Function to set the matrix |
| 21 | + set <- function(y) { |
| 22 | + x <<- y # Store the new matrix in the parent environment |
| 23 | + inv <<- NULL # Reset the cached inverse when matrix changes |
| 24 | + } |
| 25 | + |
| 26 | + # Function to get the matrix |
| 27 | + get <- function() x |
| 28 | + |
| 29 | + # Function to set the inverse |
| 30 | + setInverse <- function(inverse) inv <<- inverse |
| 31 | + |
| 32 | + # Function to get the inverse |
| 33 | + getInverse <- function() inv |
| 34 | + |
| 35 | + # Return a list of functions |
| 36 | + list(set = set, |
| 37 | + get = get, |
| 38 | + setInverse = setInverse, |
| 39 | + getInverse = getInverse) |
8 | 40 | } |
9 | 41 |
|
10 | | - |
11 | | -## Write a short comment describing this function |
12 | | - |
| 42 | +#' Compute the inverse of a special matrix created by makeCacheMatrix |
| 43 | +#' |
| 44 | +#' This function computes the inverse of the matrix. However, if the inverse has |
| 45 | +#' already been calculated (and the matrix has not changed), then it retrieves |
| 46 | +#' the inverse from the cache to avoid redundant computation. |
| 47 | +#' |
| 48 | +#' @param x A special matrix object created with makeCacheMatrix |
| 49 | +#' @return The inverse of the matrix |
13 | 50 | cacheSolve <- function(x, ...) { |
14 | | - ## Return a matrix that is the inverse of 'x' |
| 51 | + # Try to get the cached inverse |
| 52 | + inv <- x$getInverse() |
| 53 | + |
| 54 | + # If the inverse is already calculated, return it with a message |
| 55 | + if(!is.null(inv)) { |
| 56 | + message("getting cached data") |
| 57 | + return(inv) |
| 58 | + } |
| 59 | + |
| 60 | + # Otherwise, get the matrix and calculate its inverse |
| 61 | + data <- x$get() |
| 62 | + |
| 63 | + # Calculate the inverse using solve() |
| 64 | + inv <- solve(data, ...) |
| 65 | + |
| 66 | + # Cache the inverse for future use |
| 67 | + x$setInverse(inv) |
| 68 | + |
| 69 | + # Return the inverse |
| 70 | + return(inv) |
15 | 71 | } |
| 72 | + |
| 73 | +# Example usage: |
| 74 | +# m <- matrix(c(1,2,3,4), nrow=2, ncol=2) |
| 75 | +# cm <- makeCacheMatrix(m) |
| 76 | +# cacheSolve(cm) # Calculates and returns inverse |
| 77 | +# cacheSolve(cm) # Returns cached inverse |
0 commit comments