From 110451b2adf83663159984c89a98485b3a4f3e43 Mon Sep 17 00:00:00 2001 From: Hemanth Date: Thu, 27 Feb 2020 17:57:47 +0530 Subject: [PATCH 1/3] Create cachematrix1.R --- cachematrix1.R | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 cachematrix1.R diff --git a/cachematrix1.R b/cachematrix1.R new file mode 100644 index 00000000000..8e091052c83 --- /dev/null +++ b/cachematrix1.R @@ -0,0 +1,61 @@ +## Functions that cache the inverse of a matrix + +## This function creates a special "matrix" object that can cache its inverse +makeCacheMatrix <- function( m = matrix() ) { + + ## Initialize the variable for inverse property + inv <- NULL + + ## Method to set the matrix + set <- function( matrix ) { + m <<- matrix + inv <<- NULL + } + + ## Method to get the matrix + get <- function() { + m + } + + ## Set the inverse of the matrix + setInverse <- function(inverse) { + inv <<- inverse + } + + ## Get the inverse of the matrix + getInverse <- function() { + inv + } + + ## Return a list of the methods + list(set = set, get = get, setInverse = setInverse, + getInverse = getInverse) +} + + +## 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 the "cachesolve" should retrieve the inverse from the cache. +cacheSolve <- function(x, ...) { + + ## Return a matrix that is the inverse of 'x' + m <- x$getInverse() + + ## Return the inverse if its already set + if( !is.null(m) ) { + message("Retrieving cached data") + return(m) + } + + ## Retrieve the matrix from our object + var <- x$get() + + ## Calculating the inverse using matrix multiplication + m <- solve(var) %*% var + + ## Set the inverse to the object + x$setInverse(m) + + ## Return the matrix + m +} From 30048c49e80505bb3cc38a560bd1e779bbf007d6 Mon Sep 17 00:00:00 2001 From: Hemanth Date: Thu, 27 Feb 2020 17:59:34 +0530 Subject: [PATCH 2/3] Delete cachematrix.R --- cachematrix.R | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 cachematrix.R diff --git a/cachematrix.R b/cachematrix.R deleted file mode 100644 index a50be65aa44..00000000000 --- a/cachematrix.R +++ /dev/null @@ -1,15 +0,0 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function - -makeCacheMatrix <- function(x = matrix()) { - -} - - -## Write a short comment describing this function - -cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' -} From 71bda573001e27942039109e15f4159e2426f298 Mon Sep 17 00:00:00 2001 From: Hemanth Date: Thu, 27 Feb 2020 18:00:04 +0530 Subject: [PATCH 3/3] Rename cachematrix1.R to cachematrix.R --- cachematrix1.R => cachematrix.R | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename cachematrix1.R => cachematrix.R (100%) diff --git a/cachematrix1.R b/cachematrix.R similarity index 100% rename from cachematrix1.R rename to cachematrix.R