Skip to content

Commit 57efb88

Browse files
committed
Task Done
The answers are provided.
1 parent 7f657dd commit 57efb88

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

cachematrix.R

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,82 @@
11
## Put comments here that give an overall description of what your
22
## functions do
33

4+
# Answer:
5+
#The provided code defines two functions, makeCacheMatrix and cacheSolve,
6+
#which work together to create a matrix object
7+
#capable of caching its inverse and efficiently retrieving it when needed.
8+
9+
###########################################################################
10+
11+
# Create a matrix object that can cache its inverse
12+
makeCacheMatrix <- function(x = matrix()) {
13+
# Initialize an empty variable to store the cached inverse
14+
cached_inverse <- NULL
15+
16+
# Function to set the matrix and invalidate the cached inverse
17+
setMatrix <- function(matrix) {
18+
x <<- matrix
19+
cached_inverse <<- NULL
20+
}
21+
22+
# Function to retrieve the matrix
23+
getMatrix <- function() {
24+
x
25+
}
26+
27+
# Function to compute the inverse and cache it
28+
cacheInverse <- function() {
29+
if (!is.null(cached_inverse)) {
30+
# If the inverse is already cached, return it
31+
message("Retrieving cached inverse.")
32+
return(cached_inverse)
33+
} else {
34+
# If the inverse is not cached, compute it using solve function
35+
message("Calculating inverse.")
36+
cached_inverse <<- solve(x)
37+
return(cached_inverse)
38+
}
39+
}
40+
41+
# Return a list of functions to be used as the special matrix object
42+
list(setMatrix = setMatrix, getMatrix = getMatrix, cacheInverse = cacheInverse)
43+
}
44+
45+
# Function to compute the inverse of the matrix using caching
46+
cacheSolve <- function(cacheMatrix) {
47+
# Call the cacheInverse function from the cacheMatrix object
48+
cacheMatrix$cacheInverse()
49+
}
50+
51+
# An example of how the function works!
52+
matrix1 <- makeCacheMatrix(matrix(c(1, 2, 3, 4), nrow = 2))
53+
54+
# Compute the inverse
55+
cacheSolve(matrix1) # This will calculate and cache the inverse
56+
57+
# Compute the inverse again, and it will retrieve the cached value without re-computing
58+
cacheSolve(matrix1)
59+
60+
#########################################################
61+
462
## Write a short comment describing this function
563

664
makeCacheMatrix <- function(x = matrix()) {
765

866
}
967

68+
## Answer
69+
#[This function, "makeCacheMatrix," appears to be a constructor for a cache-enabled matrix.
70+
#The function takes one parameter, 'x,' which is a matrix with
71+
#a default value of an empty matrix if not provided.]
72+
1073

1174
## Write a short comment describing this function
1275

1376
cacheSolve <- function(x, ...) {
1477
## Return a matrix that is the inverse of 'x'
1578
}
79+
80+
## Answer
81+
#This function is designed to
82+
#calculate and return the inverse of a given matrix 'x'.

0 commit comments

Comments
 (0)