From 4fc25fd84c05df8e85e9dd2123f8040d83129b14 Mon Sep 17 00:00:00 2001 From: ericmayor Date: Mon, 21 Apr 2014 15:11:06 +0200 Subject: [PATCH] file edited for Prog assignment 2 --- cachematrix.R | 47 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..6a683362d2e 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,38 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function - -makeCacheMatrix <- function(x = matrix()) { +# This function returns a list composed of a matrix of NAs called Inverse and functions that +# get:() returns the content of the matrix x submitted as argument of makeCacheMatrix +# (set() of the original piece of code was not included because it was useless) +# set_inverse() assigns the inverse of the matrix to the Inverse matrix using the special operator <<- +# because Inverse is outside the environemnt of the function +# get_inverse() returns the matrix Inverse +makeCacheMatrix <- function(x) { + Inverse <- matrix (nrow = nrow(x), ncol = ncol(x)) + get <- function() x + set_inverse <- function(Invs) { + Inverse <<- Invs + } + get_inverse <- function() { + Inverse + } + list(get = get, set_inverse = set_inverse, + get_inverse = get_inverse) } - -## Write a short comment describing this function - -cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' -} +# This function returns the inverse of a matrix which was previously submitted as argument to the makeCacheMatrix function. +# The argument for this function is a list returned by makeCacheMatrix(). +# The function first retrieves the matrix called Inverse from the list returned with makeCacheMatrix() using the function get_inverse of that list +# It then checks if the first element is NA and if not prints 'Getting inverse of matrix' and returns the matrix (and the following code is not executed) +# In case the first element of the matrix is actually an NA (meaning that the inverse of the matrix has not yet been computed), the function retrieves +# the content of the matrix originally submitted to makeCacheMatrix() and assigns it to object M. It then assigns the inverse of M to Inverse. +# It then assigns the object Inverse (in the scope of the function) to the Inverse object of the list submitted as argument, and finally returns the Inverse object. +cacheSolve <- function(x) { + Inverse <- x$get_inverse() + if(!is.na(Inverse[1,1])) { + print("Getting inverse of matrix") + return(Inverse) + } + M <- x$get() + Inverse <- solve(M) + x$set_inverse(Inverse) + Inverse +} \ No newline at end of file