diff --git a/Lexical Scoping...R b/Lexical Scoping...R new file mode 100644 index 00000000000..d0ef4c0cbfc --- /dev/null +++ b/Lexical Scoping...R @@ -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 +} \ No newline at end of file diff --git a/Lexical Scoping.R b/Lexical Scoping.R new file mode 100644 index 00000000000..75dfa5d57f2 --- /dev/null +++ b/Lexical Scoping.R @@ -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 + \ No newline at end of file diff --git a/Lexical_Scoping.R b/Lexical_Scoping.R new file mode 100644 index 00000000000..11d9d1fea7e --- /dev/null +++ b/Lexical_Scoping.R @@ -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 +} \ No newline at end of file