-
Notifications
You must be signed in to change notification settings - Fork 144k
/
Copy pathLexical Scoping.R
39 lines (26 loc) · 982 Bytes
/
Lexical Scoping.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL # Initialize the inverse as NULL
set <- function(y) {
x <<- y # Assign new matrix value
inv <<- NULL # Reset inverse cache
}
get <- function() x # Retrieve the matrix
setInverse <- function(inverse) inv <<- inverse # Store the inverse
getInverse <- function() inv # Retrieve the inverse
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'
inv <- x$getInverse()
if (!is.null(inv)) {
message("Getting cached inverse")
return(inv)
}
data <- x$get()
if (nrow(data) != ncol(data)) {
stop("The matrix must be square to compute its inverse.")
}
inv <- solve(data, ...) # Compute inverse
x$setInverse(inv) # Cache inverse
inv # Return inverse