From 7f31024af24eb23479f8def3ba0613d6bab6030f Mon Sep 17 00:00:00 2001 From: McCompexpert Date: Sun, 20 Apr 2014 11:30:17 +0200 Subject: [PATCH 1/6] My first commit --- cachematrix.R | 1 + 1 file changed, 1 insertion(+) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..3971b2a60ba 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,3 +1,4 @@ +## My first change and commit ## Put comments here that give an overall description of what your ## functions do From 6a9855b1f310e53c23f309489f7bd7aee40c2a82 Mon Sep 17 00:00:00 2001 From: McCompexpert Date: Sun, 20 Apr 2014 11:36:53 +0200 Subject: [PATCH 2/6] Added .gitignore --- .gitignore | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000000..b9b615a3300 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +# History files +.Rhistory + +# Example code in package build process +*-Ex.R + +# R data files from past sessions +.Rdata \ No newline at end of file From e4341be3dfbfda371a9ab6dc79e8dd9df0ade759 Mon Sep 17 00:00:00 2001 From: McCompexpert Date: Sun, 20 Apr 2014 12:10:13 +0200 Subject: [PATCH 3/6] Added the first version of the makeCacheMatrix --- cachematrix.R | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 3971b2a60ba..1f0b34517ee 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,39 @@ -## My first change and commit -## Put comments here that give an overall description of what your -## functions do +## Function makeCacheMatrix creates a "special" matrix an function to set and +## and get values for the matrix and the inverse +## -## Write a short comment describing this function +## Function makeCacheMatrix contains a function to +## 1. set the value of the matrix +## 2. get the value of the matrix +## 3. set the value of the inverse +## 4. get the value of the inverse -makeCacheMatrix <- function(x = matrix()) { +makeCacheMatrix <- function(x = matrix()) { + #set the inverse value to NULL + inv <- NULL + # 1. set the value of the matrix + set <- function(y) { + x <<- y + # set inv back to NULL because the matrix changed + inv <<- NULL + } + # 2. get the value of the matrix + get <- function() x + # 3. set the inverse + setinv <- function(inverse) inv <<- inverse + # 4. get the inverse + getinv <- function() inverse + + # asseble a list of all the functions defined above + list(set = set, get = get, + setinv = setinv, + getinv = getinv) } -## Write a short comment describing this function +## Function cacheSolve returns chached inverse if the original +## matrix has not changed cacheSolve <- function(x, ...) { ## Return a matrix that is the inverse of 'x' From 41ce3503e4d7c1495dd338ab50ac9a95ddb16b40 Mon Sep 17 00:00:00 2001 From: McCompexpert Date: Sun, 20 Apr 2014 12:22:04 +0200 Subject: [PATCH 4/6] Corrected some comments --- cachematrix.R | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 1f0b34517ee..6edbb65b23f 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,6 +1,9 @@ -## Function makeCacheMatrix creates a "special" matrix an function to set and -## and get values for the matrix and the inverse -## +## makeCacheMatrix - creates a "special" matrix which can cache its +## inverse. Remark: matrix supplied needs to be invertible. +## +## cacheSolve - computes the inverse of the special matrix generated with +## makeCacheMatrix. If the cache is calculated and the special matrix +## has not been changes, the cacheSolve retrieves the cached inverse value. ## Function makeCacheMatrix contains a function to ## 1. set the value of the matrix @@ -25,7 +28,7 @@ makeCacheMatrix <- function(x = matrix()) { # 4. get the inverse getinv <- function() inverse - # asseble a list of all the functions defined above + # assemble a list of all the functions defined above list(set = set, get = get, setinv = setinv, getinv = getinv) From d17fb28383a6560645fe4a1a95bc6cd2b6f550fc Mon Sep 17 00:00:00 2001 From: McCompexpert Date: Sun, 20 Apr 2014 12:51:46 +0200 Subject: [PATCH 5/6] Added cacheSolve fun, tested the output with test cases, enhanced some comments --- cachematrix.R | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 6edbb65b23f..bd89e2b1c4b 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -2,7 +2,7 @@ ## inverse. Remark: matrix supplied needs to be invertible. ## ## cacheSolve - computes the inverse of the special matrix generated with -## makeCacheMatrix. If the cache is calculated and the special matrix +## makeCacheMatrix. If the cache has already been calculated and the special matrix ## has not been changes, the cacheSolve retrieves the cached inverse value. ## Function makeCacheMatrix contains a function to @@ -26,7 +26,7 @@ makeCacheMatrix <- function(x = matrix()) { # 3. set the inverse setinv <- function(inverse) inv <<- inverse # 4. get the inverse - getinv <- function() inverse + getinv <- function() inv # assemble a list of all the functions defined above list(set = set, get = get, @@ -35,9 +35,28 @@ makeCacheMatrix <- function(x = matrix()) { } -## Function cacheSolve returns chached inverse if the original -## matrix has not changed +## cacheSolve - calculates the inverse of the special +## matrix supplied from the makeCacheMatrix function. +## 1. check if the inverse has already been calculated. +## If true, get the inverse from the cache and skip the +## calculation. +## 2. If false, 3. calculates the inverse of the +## matrix and 4. sets the value of the inverse in the cache with +## the setinv function. 5. returns the newly calculated value. cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' -} + # 1. check if the inverse is calculated and cached + inv <- x$getinv() + if(!is.null(inv)) { + message("getting cached data") + return(inv) + } + # 2. not cached, place the matrix into data + data <- x$get() + # 3. calculate the inverse + inv <- solve(data, ...) + # 4. afterwards, cache the inverse + x$setinv(inv) + # finally, return the newly calculated inverse + inv +} \ No newline at end of file From 83fc6d565746145ff0f4dc3b18d5781676b426cb Mon Sep 17 00:00:00 2001 From: McCompexpert Date: Sun, 20 Apr 2014 13:06:25 +0200 Subject: [PATCH 6/6] Minor comment enhancements --- cachematrix.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index bd89e2b1c4b..ad7e272b675 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -10,7 +10,7 @@ ## 2. get the value of the matrix ## 3. set the value of the inverse ## 4. get the value of the inverse - +## 5. assemble a list of all the functions defined above makeCacheMatrix <- function(x = matrix()) { #set the inverse value to NULL @@ -28,7 +28,7 @@ makeCacheMatrix <- function(x = matrix()) { # 4. get the inverse getinv <- function() inv - # assemble a list of all the functions defined above + # 5. assemble a list of all the functions defined above list(set = set, get = get, setinv = setinv, getinv = getinv) @@ -57,6 +57,6 @@ cacheSolve <- function(x, ...) { inv <- solve(data, ...) # 4. afterwards, cache the inverse x$setinv(inv) - # finally, return the newly calculated inverse + # 5. finally, return the newly calculated inverse inv } \ No newline at end of file