Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lexical Scoping #5766

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Lexical Scoping...R
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(y) {
x <<- y
inv <<- NULL # Reset the cache if the matrix is changed
}
get <- function() x
setInverse <- function(inverse) inv <<- inverse
getInverse <- function() inv
list(set = set, get = get, setInverse = setInverse, getInverse = getInverse)
}
cacheSolve <- function(x, ...) {
# Check if the inverse is already cached
inv <- x$getInverse()

if(!is.null(inv)) {
# If cached, return the cached inverse
message("getting cached data")
return(inv)
}

# Otherwise, compute the inverse
mat <- x$get() # Get the matrix
inv <- solve(mat, ...) # Compute the inverse

# Cache the computed inverse
x$setInverse(inv)

# Return the computed inverse
inv
}
39 changes: 39 additions & 0 deletions Lexical Scoping.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,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

32 changes: 32 additions & 0 deletions Lexical_Scoping.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
makeCacheMatrix <- function(x = matrix()) { ## define the argument with default mode of "matrix"
inv <- NULL ## initialize inv as NULL; will hold value of matrix inverse
set <- function(y) { ## define the set function to assign new
x <<- y ## value of matrix in parent environment
inv <<- NULL ## if there is a new matrix, reset inv to NULL
}
get <- function() x ## define the get fucntion - returns value of the matrix argument

setinverse <- function(inverse) inv <<- inverse ## assigns value of inv in parent environment
getinverse <- function() inv ## gets the value of inv where called
list(set = set, get = get, setinverse = setinverse, getinverse = getinverse) ## you need this in order to refer
## to the functions with the $ operator
}


## Write a short comment describing this function
## This function computes the inverse of the special "matrix" returned by makeCacheMatrix above.
## If the inverse has already been calculated (and the matrix has not changed),
## then cacheSolve will retrieve the inverse from the cache

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
}