From b178240505e16df1db930fb29dde96e62d2e9c20 Mon Sep 17 00:00:00 2001 From: mao-quanteco Date: Wed, 16 Apr 2014 17:10:58 +0200 Subject: [PATCH 1/3] First commit of the hw, tested --- cachematrix.R | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..bccd602ab05 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,42 @@ ## Put comments here that give an overall description of what your ## functions do -## Write a short comment describing this function - -makeCacheMatrix <- function(x = matrix()) { +## makeCacheMatrix creates a matrix object with a cacheable inverse +## resembling a class instance in a OO Programming language. +## It returns a list of functions to set and get the matrix itself +## and its inverse +makeCacheMatrix <- function(X = matrix()) { + invX <- NULL + set <- function(Y) { + X <<- Y + invX <<- NULL + } + get <- function(){ + X + } + setinv <- function(inv){ + invX <<- inv + } + getinv <- function(){ + invX + } + list(set=set, get=get, setinv=setinv, getinv=getinv) } -## Write a short comment describing this function +## cacheSolve finds the inverse of a matrix object created by makeCacheMatrix, +## if the value of the inverse has already been computed it returns its cached value. +## The matrix is assumed to be invertible. -cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' +cacheSolve <- function(X, ...) { + ## Return a matrix that is the inverse of 'X' + invX <- X$getinv() + if(!is.null(invX)) { # if invX != NULL it has been computed before, thus return cached invX + message("getting cached data") + return(invX) + } + invX <- solve(X$get(), ...) # else solve + X$setinv(invX) + invX } From b79fcff75a6d7656fd540e553a1d932c5fa215c5 Mon Sep 17 00:00:00 2001 From: mao-quanteco Date: Thu, 17 Apr 2014 15:00:30 +0200 Subject: [PATCH 2/3] Added an example script --- cachematrix.R | 17 ++++++++--------- simple_usage.R | 8 ++++++++ 2 files changed, 16 insertions(+), 9 deletions(-) create mode 100644 simple_usage.R diff --git a/cachematrix.R b/cachematrix.R index bccd602ab05..f70adf7b0d2 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,16 +1,14 @@ -## Put comments here that give an overall description of what your -## functions do - ## makeCacheMatrix creates a matrix object with a cacheable inverse -## resembling a class instance in a OO Programming language. +## in a way that mimcs a class instantiation in a OO Programming language. ## It returns a list of functions to set and get the matrix itself -## and its inverse +## and its inverse. makeCacheMatrix <- function(X = matrix()) { invX <- NULL set <- function(Y) { X <<- Y - invX <<- NULL + invX <<- NULL ## changing X's content we set X^-1 to NULL, thus X^-1 must be computed + ## from scratch by cacheSolve } get <- function(){ X @@ -27,16 +25,17 @@ makeCacheMatrix <- function(X = matrix()) { ## cacheSolve finds the inverse of a matrix object created by makeCacheMatrix, ## if the value of the inverse has already been computed it returns its cached value. -## The matrix is assumed to be invertible. +## The matrix X is assumed to be invertible. cacheSolve <- function(X, ...) { ## Return a matrix that is the inverse of 'X' invX <- X$getinv() - if(!is.null(invX)) { # if invX != NULL it has been computed before, thus return cached invX + if(!is.null(invX)) { ## if invX != NULL it has been computed before, + ## thus return cached invX message("getting cached data") return(invX) } - invX <- solve(X$get(), ...) # else solve + invX <- solve(X$get(), ...) ## else compute X^-1 by R's solve X$setinv(invX) invX } diff --git a/simple_usage.R b/simple_usage.R new file mode 100644 index 00000000000..c8c321ebe3a --- /dev/null +++ b/simple_usage.R @@ -0,0 +1,8 @@ +## A simple example showing the usage of cacheMatrix.R + +source("cachematrix.R") +A <- makeCacheMatrix(matrix(c(1/3,0,0,2/3),2,2)) #create matrix object A +A$get() #display A +cacheSolve(A) #compute A^-1 (1st time) +A$getinv() #display A^-1 +cacheSolve(A) #compute A^-1 again, (no computation cached inverse is used) \ No newline at end of file From 850ce570f02020e686147996c9a15a5702dbd348 Mon Sep 17 00:00:00 2001 From: mao-quanteco Date: Thu, 17 Apr 2014 16:53:51 +0200 Subject: [PATCH 3/3] Modified example script --- cachematrix.R | 4 ++-- simple_usage.R | 11 +++++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index f70adf7b0d2..fba906cced8 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -28,14 +28,14 @@ makeCacheMatrix <- function(X = matrix()) { ## The matrix X is assumed to be invertible. cacheSolve <- function(X, ...) { - ## Return a matrix that is the inverse of 'X' + ## Return a matrix that is the inverse of 'X' invX <- X$getinv() if(!is.null(invX)) { ## if invX != NULL it has been computed before, ## thus return cached invX message("getting cached data") return(invX) } - invX <- solve(X$get(), ...) ## else compute X^-1 by R's solve + invX <- solve(X$get(), ...) ## else compute X^-1 by standard solve function. X$setinv(invX) invX } diff --git a/simple_usage.R b/simple_usage.R index c8c321ebe3a..3452de2efbd 100644 --- a/simple_usage.R +++ b/simple_usage.R @@ -1,8 +1,11 @@ ## A simple example showing the usage of cacheMatrix.R source("cachematrix.R") + A <- makeCacheMatrix(matrix(c(1/3,0,0,2/3),2,2)) #create matrix object A -A$get() #display A -cacheSolve(A) #compute A^-1 (1st time) -A$getinv() #display A^-1 -cacheSolve(A) #compute A^-1 again, (no computation cached inverse is used) \ No newline at end of file + +print(A$get()) #display A + +print(cacheSolve(A)) #compute A^-1 (1st time) + +print(cacheSolve(A)) #compute A^-1 again, (no computation cached inverse is used) \ No newline at end of file