From 613ee4a80da98094be6c9780d5c3cbd424b04160 Mon Sep 17 00:00:00 2001 From: Przemek Date: Sun, 23 Nov 2014 12:05:01 -0800 Subject: [PATCH 1/2] oslutions for the assignemnt 2 for R programming --- cachematrix.R | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..bb84fed2e93 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,43 @@ ## Put comments here that give an overall description of what your ## functions do +# as stated in the assignment instructions the main purpose of the function is to create an object with the attached functions that allow for calculating an inverse of the matrix, however the object can be accesded multiple times, but it is updated only at the time when input data changes. It helps to save time when executing and accessing data in the loops. + + ## Write a short comment describing this function +## the functions checks if the input matrix is squared, it is assumed that all input matrices are invertable. makeCacheMatrix <- function(x = matrix()) { - + d <- dim(x) + if(d[1] != d[2]) stop("The matrix has no equal dimensios. Provide square matrix.") + i <- NULL + ## contructor of the object in the separate environment using <<- + set <- function(y) { + x <<- y + i <<- NULL + } + ## getters to obtain the data and store the inverse of the input matrix + get <- function() x + setinverse <- function(inv) i <<- inv # storing an inverse in separate environment + getinverse <- function() i + list(set = set, get = get, + setinverse = setinverse, + getinverse = getinverse) } -## Write a short comment describing this function +## The following function help to access the data from the object created by makeCacheMatrix() +## If the output was not created it computes the inverse of the matrix, when already exists it gets the content from the object and updates the output only when the input data changes. cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + m <- x$getinverse() + if(!is.null(m)) { # checking if the object exists + message("getting cached data") # if yes it is jsut returned and no computation is performed + return(m) + } + data <- x$get() # if not calculate the inverse and return value + i <- solve(data) + x$setinverse(i) + i } From 9540ebacc703da505150c901efccf9e8b041189b Mon Sep 17 00:00:00 2001 From: Przemek Date: Sun, 23 Nov 2014 12:08:36 -0800 Subject: [PATCH 2/2] comments updated --- cachematrix.R | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index bb84fed2e93..c863be8001c 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,11 +1,16 @@ ## Put comments here that give an overall description of what your ## functions do -# as stated in the assignment instructions the main purpose of the function is to create an object with the attached functions that allow for calculating an inverse of the matrix, however the object can be accesded multiple times, but it is updated only at the time when input data changes. It helps to save time when executing and accessing data in the loops. +# as stated in the assignment instructions the main purpose of the function is +# to create an object with the attached functions that allow for calculating an +# inverse of the matrix, however the object can be accesded multiple times, but +# it is updated only at the time when input data changes. It helps to save time +# when executing and accessing data in the loops. -## Write a short comment describing this function -## the functions checks if the input matrix is squared, it is assumed that all input matrices are invertable. +## Write a short comment describing this function the functions checks if the +## input matrix is squared, it is assumed that all input matrices are +## invertable. makeCacheMatrix <- function(x = matrix()) { d <- dim(x) @@ -26,8 +31,10 @@ makeCacheMatrix <- function(x = matrix()) { } -## The following function help to access the data from the object created by makeCacheMatrix() -## If the output was not created it computes the inverse of the matrix, when already exists it gets the content from the object and updates the output only when the input data changes. +## The following function help to access the data from the object created by +## makeCacheMatrix() If the output was not created it computes the inverse of +## the matrix, when already exists it gets the content from the object and +## updates the output only when the input data changes. cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x'