-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
## This function creates a matrix object that can cache its inverse | ||
makeCacheMatrix <- function(x=matrix()){ | ||
## Initializing the inverse | ||
inv <- NULL | ||
## Setting the matrix | ||
set <- function(y) | ||
{ | ||
matrix <<- y | ||
inv <<- NULL | ||
} | ||
## Method to get the matrix | ||
get <- function() | ||
{ | ||
matrix | ||
} | ||
## Method to set the inverse of the matrix | ||
setInverse <- function(inverse) | ||
{ | ||
inv <<- inverse | ||
} | ||
## Method to get the inverse of the matrix | ||
getInverse <- function() | ||
{ | ||
inv | ||
} | ||
## Returning the list of methods | ||
list(set = set, get = get, setInverse = setInverse, getInverse = getInverse) | ||
} | ||
## This function computes the inverse of the special matrix returned by the "makeCacheMatrix" function. | ||
## - If the inverse has already been calculated (and the matrix has not changed), then the "cachesolve" should retrieve the inverse from the cache. | ||
cacheSolve <- function(x, ...) | ||
{ | ||
## getting a matrix that is the inverse of 'x' | ||
inv <- x$getInverse() | ||
## returns if the inverse has already been calculated (i.e. if !is.null(m)==TRUE) | ||
if(!is.null(inv)) { | ||
message("getting cached data") | ||
return(inv) | ||
} | ||
## If the inverse wasn't yet been calculated ## | ||
## getting the matrix from our object | ||
data <- x$get() | ||
## calculating the inverse by using matrix multiplication | ||
m <- solve(data) %*% data | ||
## storing the inverse to the object to future usage | ||
x$setInverse(m) | ||
## returning a matrix that is the inverse of 'x' | ||
m | ||
} |