From cee60178a2f209744e1af1016d4d0d6b638dd52a Mon Sep 17 00:00:00 2001 From: daymos Date: Tue, 22 Apr 2014 01:04:09 +0200 Subject: [PATCH 1/3] Update cachematrix.R --- cachematrix.R | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..e8095c7f1e0 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -3,13 +3,41 @@ ## Write a short comment describing this function -makeCacheMatrix <- function(x = matrix()) { +## makeCacheMatrix takes a matrix and returns and "object" I I may call it so, with one attributeand some methods. +## the attribute is the variable m wich is first initialised to 0. +## method set replace the value of the matrix with a new one + +makeCacheMatrix <- function(x = numeric()) { + m <- NULL + z <- x + set <- function(y) { + x <<- y + m <<- NULL + } + get <- function() x + setinverse <- function(solve) m <<- solve + getinverse <- function() m + list(set = set, get = get, + setinverse = setinverse, + getinverse = getinverse) } + ## Write a short comment describing this function 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(m) + m } + From fbf9c06cd24e4dc2bc259f4918694c83cea0b2b4 Mon Sep 17 00:00:00 2001 From: daymos Date: Tue, 22 Apr 2014 11:27:45 +0200 Subject: [PATCH 2/3] Update cachematrix.R --- cachematrix.R | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index e8095c7f1e0..81e7e1023c5 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,16 +1,30 @@ -## Put comments here that give an overall description of what your -## functions do +##OVERALL DESCRIPTION +##makeCacheMatrix takes a matrix and create an object with one attribute and 4 methods, +##which is rappresented in R as a list. -## Write a short comment describing this function +##cacheSolve takes in the object created by the previous function as a list, and if the attribute m is +##equal to 0 it calculates the inverse matrix. Othervise it returns the value found in m + + +## DESCRIPTION OF makeCacheMatrix + +## makeCacheMatrix takes a matrix and returns and "object" if I may call it so, with one attribute and some methods. +## the object is rappresented by a list in R. +## In other languages this would be called a constructor -## makeCacheMatrix takes a matrix and returns and "object" I I may call it so, with one attributeand some methods. ## the attribute is the variable m wich is first initialised to 0. -## method set replace the value of the matrix with a new one +## method 'set' replace the value of the matrix with a new one within the closure of the object +## method 'get' return the value of the matrix within the closure +## method 'setinverse' solve the the matrix and store the inverse in the variable m +## method 'getinverse' returns the value of the inverse matrix + +## list is the form in which the output is returned makeCacheMatrix <- function(x = numeric()) { + m <- NULL - z <- x + set <- function(y) { x <<- y m <<- NULL @@ -25,13 +39,15 @@ makeCacheMatrix <- function(x = numeric()) { -## Write a short comment describing this function +## DESCRIPTION of cacheSolve + +## this function takes an object described as before, in the form of a list +## it calls the method getinverse and if it returns zero it then calculate the inverse of the matrix. +## otherwise it returns the inverse which is stored in m cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' - m <- x$getinverse() - if(!is.null(m)&& ) { + if(!is.null(m)) { message("getting cached data") return(m) } @@ -40,4 +56,3 @@ cacheSolve <- function(x, ...) { x$setinverse(m) m } - From 7fcbdcb5dede361eb1ceee19a38f8289cfbb03d8 Mon Sep 17 00:00:00 2001 From: daymos Date: Tue, 22 Apr 2014 11:41:17 +0200 Subject: [PATCH 3/3] Update cachematrix.R --- cachematrix.R | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 81e7e1023c5..c132ab33ced 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -10,10 +10,13 @@ ## makeCacheMatrix takes a matrix and returns and "object" if I may call it so, with one attribute and some methods. ## the object is rappresented by a list in R. -## In other languages this would be called a constructor +## In other languages this would be called a constructor although it seems there are no classes in R so I dont know if +## this is correct ## the attribute is the variable m wich is first initialised to 0. -## method 'set' replace the value of the matrix with a new one within the closure of the object +## method 'set' replace the value of the matrix with a new one within the closure of the object. +## 'set' also reinitialises the value of m, so if this method is used the cached valued will be overwritten. + ## method 'get' return the value of the matrix within the closure ## method 'setinverse' solve the the matrix and store the inverse in the variable m ## method 'getinverse' returns the value of the inverse matrix