From 3b0447e9f849cc6c9c6b6df78388104b2635de95 Mon Sep 17 00:00:00 2001 From: h-s Date: Mon, 21 Apr 2014 17:52:18 -0400 Subject: [PATCH 1/2] Initial commit for Assignment 2 --- cachematrix.R | 55 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 7 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..73c456159a2 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,56 @@ -## Put comments here that give an overall description of what your -## functions do - -## Write a short comment describing this function +## This function is the constructor for a "special" matrix that is able +## to save its inverse in the cache to avoid repeated calculations. +## It provides four methods for getting and setting the matrix and its inverse. +## +## The get() method will return the matrix to be inverted. +## The set(x) method will set a new matrix to be inverted and clear the cache. +## The getinv() method will return the inverse matrix from the cache +## The setinv(inv) method will save the inverse matrix in the cache makeCacheMatrix <- function(x = matrix()) { - + xinv <- NULL + + get <- function() { + x + } + + set <- function(y) { + x <<- y + xinv <<- NULL + } + + getinv <- function() { + xinv + } + + setinv <- function(inv) { + xinv <<- inv + } + + list(get = get, set = set, getinv = getinv, setinv = setinv) } -## Write a short comment describing this function +## This function returns the inverse matrix of the "special" matrix +## constructed by the above makeCacheMatrix() constructor. +## The special matrix is passed as the argument x. +## If the inverse matrix is available in the cache, a message will +## be printed. +## Otherwise the inverse matrix will be computed and saved in the cache. +## It is assumed that the matrix is always invertible cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + ## Try to retrieve the inverse from cache + inv <- x$getinv() + if (!is.null(inv)) { + message("Getting cache data") + } + else { + ## inverse matrix is not available yet + ## retrieve matrix, compute its inverse, and save it in cache + matrix <- x$get() + inv <- solve(matrix) + x$setinv(inv) + } + inv } From f5b62e9a8ebd4b18307e91d9777c3796aab0976b Mon Sep 17 00:00:00 2001 From: h-s Date: Mon, 21 Apr 2014 18:05:27 -0400 Subject: [PATCH 2/2] Second commit for Assignment 2 --- cachematrix.R | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/cachematrix.R b/cachematrix.R index 73c456159a2..038ca36c80d 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,11 +1,15 @@ ## This function is the constructor for a "special" matrix that is able -## to save its inverse in the cache to avoid repeated calculations. +## to save its inverse in the cache to avoid unnecessary recalculations. ## It provides four methods for getting and setting the matrix and its inverse. +## - get() method will return the matrix to be inverted. +## - set(x) method will set a new matrix to be inverted and clear the cache. +## - getinv() method will return the inverse matrix from the cache +## - setinv(inv) method will save the inverse matrix in the cache ## -## The get() method will return the matrix to be inverted. -## The set(x) method will set a new matrix to be inverted and clear the cache. -## The getinv() method will return the inverse matrix from the cache -## The setinv(inv) method will save the inverse matrix in the cache +## Once an object is created by this constructor, you may change the underlying +## matrix to be inverted by the set() method. This will clear the cache, +## and a new inverse matrix must be computed the next time via cacheSolve(). + makeCacheMatrix <- function(x = matrix()) { xinv <- NULL