From bdf17f96da8620f6ce0321f41cc19e7e86617668 Mon Sep 17 00:00:00 2001 From: Kazbek Date: Sun, 20 Apr 2014 11:25:18 +0400 Subject: [PATCH 1/8] empty tests file added. --- test_cachematrix.R | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 test_cachematrix.R diff --git a/test_cachematrix.R b/test_cachematrix.R new file mode 100644 index 00000000000..a4499c82ce8 --- /dev/null +++ b/test_cachematrix.R @@ -0,0 +1,8 @@ +# Test for cachematrix.R +# uses testthat framework +# running tests: +# load R +# > require("test_that") +# > test_file("test_cachematrix.R") + +source("cachematrix.R"); From 15712e106d6e0d93e0e2fbf28452b1506c42ea8a Mon Sep 17 00:00:00 2001 From: Kazbek Date: Sun, 20 Apr 2014 11:27:39 +0400 Subject: [PATCH 2/8] makeCacheMatrix implemented. --- cachematrix.R | 18 +++++++++++++----- test_cachematrix.R | 30 ++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..3bae3565a6c 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,10 +1,18 @@ -## Put comments here that give an overall description of what your -## functions do -## Write a short comment describing this function - -makeCacheMatrix <- function(x = matrix()) { +# returns matrix object that stores its inverse +makeCacheMatrix <- function(mtx = matrix()) { + cachedInverse <- NULL + set <- function(y) { + mtx <<- y + cachedInverse <<- NULL + } + get <- function() mtx + setInverse <- function(inv) cachedInverse <<- inv + getInverse <- function() cachedInverse + list(set = set, get = get, + setInverse = setInverse, + getInverse = getInverse) } diff --git a/test_cachematrix.R b/test_cachematrix.R index a4499c82ce8..7874fe8f780 100644 --- a/test_cachematrix.R +++ b/test_cachematrix.R @@ -6,3 +6,33 @@ # > test_file("test_cachematrix.R") source("cachematrix.R"); + +context("makeCacheMatrixTest"); + +test_matrix <- matrix(c(1, 2, 3, 4), 2, 2) +test_matrix_inverse <- solve(test_matrix) + +test_that("Can be initialized with matrix", { + cm <- makeCacheMatrix(test_matrix) + expect_that(cm$get(), equals(test_matrix)); +}); + +test_that("Inverse of the cached matrix without matrix is null", { + cm <- makeCacheMatrix() + expect_that(cm$getInverse(), equals(NULL)); +}); + +test_that("Setting matrix should empty inverse", { + cm <- makeCacheMatrix() + cm$set(test_matrix) + cm$setInverse(test_matrix_inverse) + cm$set(test_matrix) + expect_that(cm$getInverse(), equals(NULL)); +}); + +test_that("Getting inverse should return cached inverse", { + cm <- makeCacheMatrix() + cm$set(test_matrix) + cm$setInverse(test_matrix_inverse) + expect_that(cm$getInverse(), equals(test_matrix_inverse)); +}); From 46713cfeb80e8a3b645eee0ca1d8323862e5768f Mon Sep 17 00:00:00 2001 From: Kazbek Date: Sun, 20 Apr 2014 11:28:58 +0400 Subject: [PATCH 3/8] cacheSolve implemented. --- cachematrix.R | 17 ++++++++++++++--- test_cachematrix.R | 15 +++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 3bae3565a6c..97d7404cdc2 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,4 +1,4 @@ - +# makeCacheMatrix # returns matrix object that stores its inverse makeCacheMatrix <- function(mtx = matrix()) { @@ -16,8 +16,19 @@ makeCacheMatrix <- function(mtx = matrix()) { } -## Write a short comment describing this function + +# cacheSolve +# looks for inverse of matrix in the cache, if not found, computes inverse and sets cache. Returns +# inverse of the matrix. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + inv <- x$getInverse() + if(!is.null(inv)) { + # message("getting cached data") + return(inv) + } + mtx <- x$get() + inv <- solve(mtx, ...) + x$setInverse(inv) + inv } diff --git a/test_cachematrix.R b/test_cachematrix.R index 7874fe8f780..bfda2f91a3c 100644 --- a/test_cachematrix.R +++ b/test_cachematrix.R @@ -36,3 +36,18 @@ test_that("Getting inverse should return cached inverse", { cm$setInverse(test_matrix_inverse) expect_that(cm$getInverse(), equals(test_matrix_inverse)); }); + + +context("cacheSolveTest"); + +test_that("Returns inverse from cached matrix", { + cm <- makeCacheMatrix(mtx=test_matrix) + cm$setInverse(test_matrix_inverse) + expect_that(cacheSolve(cm), equals(test_matrix_inverse)) +}); + +test_that("Computes and caches inverse", { + cm <- makeCacheMatrix(mtx=test_matrix) + expect_that(cm$getInverse(), equals(NULL)) + expect_that(cacheSolve(cm), equals(test_matrix_inverse)) +}); From 3a0c600f16e92798c3707e2836b03eb251093a2e Mon Sep 17 00:00:00 2001 From: Kazbek Date: Sun, 20 Apr 2014 11:30:03 +0400 Subject: [PATCH 4/8] module documentation added. --- cachematrix.R | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/cachematrix.R b/cachematrix.R index 97d7404cdc2..8ffc6ace5a4 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,3 +1,41 @@ +## Matrix inversion is usually a costly computation and their may be some +## benefit to caching the inverse of a matrix rather than compute it repeatedly. +## This two functions do exactly that. Create matrix that stores its inverse (using makeCacheMatrix) +## then get its inverse (using cacheSolve) +## +## Minimal example: +## +## > mc <- makeCacheMatrix(mtx=matrix(c(1, 2, 3, 4), 2, 2)) +## +## > cacheSolve(mc) # first call, computing and set to cache +## [,1] [,2] +## [1,] -2 1.5 +## [2,] 1 -0.5 +## +## > cacheSolve(mc) # second call, getting from cache +## [,1] [,2] +## [1,] -2 1.5 +## [2,] 1 -0.5 +## Matrix inversion is usually a costly computation and their may be some +## benefit to caching the inverse of a matrix rather than compute it repeatedly. +## This two functions do exactly that. Create matrix that stores its inverse (using makeCacheMatrix) +## then get its inverse (using cacheSolve) +## +## Minimal example: +## +## > mc <- makeCacheMatrix(mtx=matrix(c(1, 2, 3, 4), 2, 2)) +## +## > cacheSolve(mc) # first call, computing and set to cache +## [,1] [,2] +## [1,] -2 1.5 +## [2,] 1 -0.5 +## +## > cacheSolve(mc) # second call, getting from cache +## [,1] [,2] +## [1,] -2 1.5 +## [2,] 1 -0.5 + + # makeCacheMatrix # returns matrix object that stores its inverse From 3bf0cff0854bc1e8dcd4131bcf6e591d339d7186 Mon Sep 17 00:00:00 2001 From: Kazbek Date: Sun, 20 Apr 2014 11:37:01 +0400 Subject: [PATCH 5/8] documentation extra copy removed. --- cachematrix.R | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 8ffc6ace5a4..129eb315913 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -16,24 +16,6 @@ ## [,1] [,2] ## [1,] -2 1.5 ## [2,] 1 -0.5 -## Matrix inversion is usually a costly computation and their may be some -## benefit to caching the inverse of a matrix rather than compute it repeatedly. -## This two functions do exactly that. Create matrix that stores its inverse (using makeCacheMatrix) -## then get its inverse (using cacheSolve) -## -## Minimal example: -## -## > mc <- makeCacheMatrix(mtx=matrix(c(1, 2, 3, 4), 2, 2)) -## -## > cacheSolve(mc) # first call, computing and set to cache -## [,1] [,2] -## [1,] -2 1.5 -## [2,] 1 -0.5 -## -## > cacheSolve(mc) # second call, getting from cache -## [,1] [,2] -## [1,] -2 1.5 -## [2,] 1 -0.5 # makeCacheMatrix From aaa7673bfdb15496929ba662318866c75ddb81ef Mon Sep 17 00:00:00 2001 From: Kazbek Date: Sun, 20 Apr 2014 11:38:41 +0400 Subject: [PATCH 6/8] code cleaned up. --- cachematrix.R | 2 -- 1 file changed, 2 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 129eb315913..3daf416b6f7 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -36,7 +36,6 @@ makeCacheMatrix <- function(mtx = matrix()) { } - # cacheSolve # looks for inverse of matrix in the cache, if not found, computes inverse and sets cache. Returns # inverse of the matrix. @@ -44,7 +43,6 @@ makeCacheMatrix <- function(mtx = matrix()) { cacheSolve <- function(x, ...) { inv <- x$getInverse() if(!is.null(inv)) { - # message("getting cached data") return(inv) } mtx <- x$get() From 20df2c9e3f8944e0d4a74b3051d86e584d08b215 Mon Sep 17 00:00:00 2001 From: Kazbek Date: Wed, 23 Apr 2014 09:34:10 +0400 Subject: [PATCH 7/8] wrong instruction to run tests fixed. --- test_cachematrix.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_cachematrix.R b/test_cachematrix.R index bfda2f91a3c..4275468794c 100644 --- a/test_cachematrix.R +++ b/test_cachematrix.R @@ -2,7 +2,7 @@ # uses testthat framework # running tests: # load R -# > require("test_that") +# > require("testthat") # > test_file("test_cachematrix.R") source("cachematrix.R"); From 09048bc3391e07d368f24511b3095d02fabf6e93 Mon Sep 17 00:00:00 2001 From: Kazbek Date: Sat, 26 Apr 2014 10:33:50 +0400 Subject: [PATCH 8/8] more comments. --- cachematrix.R | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 3daf416b6f7..5e5c1cebe14 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,4 +1,4 @@ -## Matrix inversion is usually a costly computation and their may be some +## Matrix inversion is usually a costly computation and there may be some ## benefit to caching the inverse of a matrix rather than compute it repeatedly. ## This two functions do exactly that. Create matrix that stores its inverse (using makeCacheMatrix) ## then get its inverse (using cacheSolve) @@ -24,15 +24,22 @@ makeCacheMatrix <- function(mtx = matrix()) { cachedInverse <- NULL set <- function(y) { + # changes initial matrix and removes old matrix cache mtx <<- y cachedInverse <<- NULL } get <- function() mtx - setInverse <- function(inv) cachedInverse <<- inv - getInverse <- function() cachedInverse - list(set = set, get = get, - setInverse = setInverse, - getInverse = getInverse) + setInverse <- function(inv) { + # sets inverse version of the matrix + cachedInverse <<- inv + } + getInverse <- function() { + # returns inverse of the matrix + cachedInverse + } + list(set=set, get=get, + setInverse=setInverse, + getInverse=getInverse) } @@ -43,8 +50,10 @@ makeCacheMatrix <- function(mtx = matrix()) { cacheSolve <- function(x, ...) { inv <- x$getInverse() if(!is.null(inv)) { + # cached invers exists, return it return(inv) } + # inverse is not cached yet, cache it and return mtx <- x$get() inv <- solve(mtx, ...) x$setInverse(inv)