From a66c8542a0b376a67ebb06970d80a89ad8830ba0 Mon Sep 17 00:00:00 2001 From: entmoot Date: Sat, 12 Apr 2014 20:39:05 +0200 Subject: [PATCH 1/2] solution --- cachematrix.R | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..88ce27cb629 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,42 @@ -## Put comments here that give an overall description of what your -## functions do +## Functions for cached matrix inverse operation -## Write a short comment describing this function -makeCacheMatrix <- function(x = matrix()) { +## makeCacheMatrix creates special form of matrix that +## enables caching of matrix inverse calculations +makeCacheMatrix <- function(x = matrix()) { + inv<-NULL + set <- function(y) { + x <<- y + inv <<- NULL + } + get <- function() x + setinv <- function(inverse) inv <<- inverse + getinv <- function() inv + list(set = set, get = get, + setinv = setinv, + getinv = getinv) } -## Write a short comment describing this function +## cacheSolve uses special matrix object x created by makeCachedMatrix +## in order to calculate the inverse of the matrix x. +## When the matrix inversion result from previous calculation +## is available in the cache, it is just reported back +## without the new recalculation. Otherwise, the inverse is +## calculated and stored in the cache for future reuse cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' + inv <- x$getinv() + if(!is.null(inv)) { + message("getting cached data") + return(inv) + } + data <- x$get() + inv <- solve(data, ...) + x$setinv(inv) + inv } + + From ef0b9b0131f534e536977e32b4d100e4a0a63117 Mon Sep 17 00:00:00 2001 From: entmoot Date: Fri, 25 Apr 2014 17:40:58 +0200 Subject: [PATCH 2/2] improvements --- cachematrix.R | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 88ce27cb629..94fe7f10152 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,5 +1,16 @@ ## Functions for cached matrix inverse operation +## makeCacheMatrix is used for creation of special +## matrix object that caches the calculated inverse +## until the matrix itself changes. +## cacheSolve uses special matrix object x created by +## makeCachedMatrix in order to calculate the inverse +## of the matrix x. When the matrix inversion result +## from previous calculation is available in the cache, +## it is just reported back without the new recalculation. +## Otherwise, the inverse is calculated and stored in +## the cache for future reuse + ## makeCacheMatrix creates special form of matrix that ## enables caching of matrix inverse calculations @@ -7,8 +18,11 @@ makeCacheMatrix <- function(x = matrix()) { inv<-NULL set <- function(y) { - x <<- y - inv <<- NULL + if (!identical(x,y)) + { + x <<- y + inv <<- NULL + } } get <- function() x setinv <- function(inverse) inv <<- inverse @@ -19,12 +33,9 @@ makeCacheMatrix <- function(x = matrix()) { } -## cacheSolve uses special matrix object x created by makeCachedMatrix -## in order to calculate the inverse of the matrix x. -## When the matrix inversion result from previous calculation -## is available in the cache, it is just reported back -## without the new recalculation. Otherwise, the inverse is -## calculated and stored in the cache for future reuse +## cacheSolve takes special form of matrix as input +## and returns its inverse, either by reporting the cached +## or calculated version cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x'