From 9afdd026e7550663d312f0ca03366ad87b2f3a38 Mon Sep 17 00:00:00 2001 From: Simone Busoli Date: Fri, 20 Feb 2015 23:03:46 +0100 Subject: [PATCH] initial import --- .gitignore | 3 +++ ProgrammingAssignment2.Rproj | 13 +++++++++++ cachematrix.R | 42 ++++++++++++++++++++++++++---------- 3 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 .gitignore create mode 100644 ProgrammingAssignment2.Rproj diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000000..807ea251739 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.Rproj.user +.Rhistory +.RData diff --git a/ProgrammingAssignment2.Rproj b/ProgrammingAssignment2.Rproj new file mode 100644 index 00000000000..8e3c2ebc99e --- /dev/null +++ b/ProgrammingAssignment2.Rproj @@ -0,0 +1,13 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..835f85375dc 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,35 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function - makeCacheMatrix <- function(x = matrix()) { - + inverse <- NULL + + get <- function() x + + set <- function(y) { + x <<- y + inverse <<- NULL + } + + getInverse <- function() inverse + + setInverse <- function(inv) inverse <<- inv + + list(get = get, + set = set, + getInverse = getInverse, + setInverse = setInverse) } - -## Write a short comment describing this function - cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' -} + inverse <- x$getInverse() + + if(!is.null(inverse)) { + message("reading from cache") + return (inverse) + } + + message("computing inverse") + + inverse <- solve(x$get()) + x$setInverse(inverse) + + inverse +} \ No newline at end of file