diff --git a/cachematrix.R b/cachematrix.R index a50be65aa44..b7e666dd6ef 100644 --- a/cachematrix.R +++ b/cachematrix.R @@ -1,15 +1,56 @@ -## Put comments here that give an overall description of what your -## functions do +## 'makeCacheMatrix (matrix x)' stores a given matrix x and allows the caching of its inverse +## calculated and returned with 'cachesolve (makeCacheMatrix x)': cached inverse matrix returned when +## available, else computed by 'cacheSolve' and stored. Cached value is reset when matrix values are set. -## Write a short comment describing this function +## 'makeCacheMatrix (matrix)' +## .. creates a list type matrix which stores the matrix inverse +## .. $set() sets matrix, sets cached value to NULL +## .. $get() returns matrix +## .. $setinverse() sets 'inv' cached matrix inverse +## .. $getinverse() returns 'inv' cached matrix inverse makeCacheMatrix <- function(x = matrix()) { - + + # inv .. inverse holder + inv <- NULL + + # set() .. store matrix, inverse to NULL + set <- function(y) { + x <<- y + inv <<- NULL + } + + # get() .. return matrix + get <- function() x + + # setinv() .. set inverse + setinverse <- function(inv_) inv <<- inv_ + + # getinv() .. return inverse + getinverse <- function() inv + + # list + list(set = set, get = get, + setinverse = setinverse, + getinverse = getinverse) } -## Write a short comment describing this function +## function 'cacheSolve (makeCacheMatrix x)' +## .. returns a matrix that is the inverse of 'x' +## .. returns cached value inv of x if existing, else solves for matrix x, +## .. stores output in inv of x and returns inverse cacheSolve <- function(x, ...) { - ## Return a matrix that is the inverse of 'x' + + inv <- x$getinverse() + + if(!is.null(inv)) { + message("getting cached data") + } else { + data <- x$get() + inv <- solve(data, ...) + x$setinverse(inv) + } + return(inv) } diff --git a/cachematrix.html b/cachematrix.html new file mode 100644 index 00000000000..690f6c5de4a --- /dev/null +++ b/cachematrix.html @@ -0,0 +1,239 @@ + + + + + + + +cachematrix.R + + + + + + + + + + + + + + + + + +

cachematrix.R

+ +

Jean-Pierre — Apr 14, 2014, 1:52 PM

+ +
## Put comments here that give an overall description of what your
+## functions do
+
+## Write a short comment describing this function
+
+makeCacheMatrix <- function(x = matrix()) {
+
+  # inv .. inverse holder
+  inv <- NULL
+
+  # set() .. store matrix, inverse to NULL
+  set <- function(y) {
+    x <<- y
+    inv <<- NULL
+  }
+
+  # get() .. return matrix
+  get <- function() x
+
+  # setinv() .. set inverse
+  setinverse <- function(inv_) inv <<- inv_
+
+  # getinv() .. return inverse
+  getinverse <- function() inv
+
+  # list
+  list(set = set, get = get,
+       setinverse = setinverse,
+       getinverse = getinverse)
+}
+
+
+## Write a short comment describing this function
+
+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)
+  }
+  data <- x$get()
+  inv <- solve(data, ...)
+  x$setinverse(inv)
+  inv
+}
+
+ + + + +