From 1f668f6f26aade28aaa4be637341ff8fb151faa1 Mon Sep 17 00:00:00 2001 From: Omarnaeem Date: Wed, 19 Aug 2015 23:21:48 +0200 Subject: [PATCH 1/3] assignment 2 code added --- cachematrix.R | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..404e3e5c182 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -4,6 +4,17 @@ ## Write a short comment describing this function makeCacheMatrix <- function(x = matrix()) { + m <- NULL + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setinverse <- function(inv) m <<- inv + getinverse <- function() m + list(set = set, get = get, + setinverse = setinverse, + getinverse = getinverse) } @@ -12,4 +23,13 @@ makeCacheMatrix <- function(x = matrix()) { cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + m <- x$getinverse() + if(!is.null(m)) { + message("getting cached data") + return(m) + } + data <- x$get() + m <- solve(data) + x$setinverse(x) + m } From 5d3e8963177b9caab17de58e71eb66d826043ae2 Mon Sep 17 00:00:00 2001 From: Omarnaeem Date: Fri, 21 Aug 2015 13:15:58 +0200 Subject: [PATCH 2/3] Comments Added --- cachematrix.R | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 404e3e5c182..0e2057df3b3 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,17 +1,29 @@ -## Put comments here that give an overall description of what your -## functions do +## The following two functions uses R scoping rules to cache inverse of a matrix so if the content does +## not change then the result of inverse matrix is retured from cache rather than computing the value again +## in time consumping computations. <<- operator in R is used to assign a value to an object +## in an environment that is different from the current environment. -## Write a short comment describing this function + +## This fucntion creates a list containing functions to +## set the value of the matrix +## Get the value of the matrix +## set the value of the inverse of the matrix +## get the value of the inverse of the matrix makeCacheMatrix <- function(x = matrix()) { m <- NULL + ## setter funtion set <- function(y) { x <<- y m <<- NULL } + ## get function to return the matrix get <- function() x + ## set the inverse to varialbe m setinverse <- function(inv) m <<- inv + ## ger the inverse from varialbe m getinverse <- function() m + list(set = set, get = get, setinverse = setinverse, getinverse = getinverse) @@ -19,17 +31,23 @@ makeCacheMatrix <- function(x = matrix()) { } -## Write a short comment describing this function +## Return a matrix that is the inverse of 'x' either from already computed value from Cache or +## Calculates the inverse and return the result. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + + m <- x$getinverse() if(!is.null(m)) { message("getting cached data") return(m) } + data <- x$get() + + ##calucutes the inverse of the matrix by using R built in Funtion Solve if result is not in Cache m <- solve(data) x$setinverse(x) - m + + return(m) } From 635a89076d281476a18f911f1f871086e7cd1134 Mon Sep 17 00:00:00 2001 From: Omarnaeem Date: Fri, 21 Aug 2015 14:53:14 +0200 Subject: [PATCH 3/3] Comments updated + Fix applied --- cachematrix.R | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 0e2057df3b3..6e6392aa14e 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -44,10 +44,9 @@ cacheSolve <- function(x, ...) { } data <- x$get() - ##calucutes the inverse of the matrix by using R built in Funtion Solve if result is not in Cache m <- solve(data) - x$setinverse(x) - + x$setinverse(m) return(m) } +