|
1 | | -## Put comments here that give an overall description of what your |
2 | | -## functions do |
| 1 | +# Author: David Esparza Alba |
| 2 | +# Date: 04/18/2014 |
| 3 | +# Decription: |
| 4 | +# |
| 5 | +# This file containts two functions: makeCacheMatrix and |
| 6 | +# cacheSolve. The first has the porpose of creating a special |
| 7 | +# matrix "matrix" object that cache its inverse. The second one |
| 8 | +# computes the inverse of the special matrix returned by makeCacheMatrix. |
| 9 | +# If the inverse has already been caculated, then the cacheSolve function |
| 10 | +# just retrieve the inverse from the cache. |
3 | 11 |
|
4 | | -## Write a short comment describing this function |
| 12 | +######################################################################### |
| 13 | +# Function: makeCacheMatrix |
| 14 | +# |
| 15 | +# Description: Store the matrix x an its inverse in the cache. |
| 16 | +# |
| 17 | +# Input: A matrix x |
| 18 | +# |
| 19 | +# Output: A special "matrix", which is a list with all the functions |
| 20 | +# necessary to retrieve the matrix and its inverse. |
| 21 | +########################################################################## |
5 | 22 |
|
6 | 23 | makeCacheMatrix <- function(x = matrix()) { |
7 | | - |
| 24 | + |
| 25 | + I <- NULL |
| 26 | + |
| 27 | + # Everytime that makeCacheMatrix is called, the inverse matrix I is |
| 28 | + # set to NULL |
| 29 | + |
| 30 | + set <- function(B) |
| 31 | + { |
| 32 | + x <<- B |
| 33 | + I <<- NULL |
| 34 | + } |
| 35 | + |
| 36 | + get <- function() |
| 37 | + { |
| 38 | + x |
| 39 | + } |
| 40 | + |
| 41 | + setInverse <- function(y) |
| 42 | + { |
| 43 | + I <<- y |
| 44 | + } |
| 45 | + |
| 46 | + getInverse <- function() |
| 47 | + { |
| 48 | + I |
| 49 | + } |
| 50 | + |
| 51 | + list(get = get, set = set, |
| 52 | + setInverse = setInverse, |
| 53 | + getInverse = getInverse) |
8 | 54 | } |
9 | 55 |
|
10 | 56 |
|
11 | | -## Write a short comment describing this function |
| 57 | +######################################################################### |
| 58 | +# Function: cacheSolve |
| 59 | +# |
| 60 | +# Description: Returns the inverse of the special "matrix" x. If the |
| 61 | +# inverse has already been computed, it just returns it from the cache, |
| 62 | +# otherwise the inverse is computed and stored in the cache. |
| 63 | +# |
| 64 | +# Input: A special "matrix" x (A list returnded by the makeCacheMatrix |
| 65 | +# function) |
| 66 | +# |
| 67 | +# Output: matrix. The inverse of the special "matrix" x |
| 68 | +######################################################################### |
12 | 69 |
|
13 | 70 | cacheSolve <- function(x, ...) { |
14 | | - ## Return a matrix that is the inverse of 'x' |
| 71 | + |
| 72 | + I <- x$getInverse() |
| 73 | + |
| 74 | + #Check if the inverse is in the cache |
| 75 | + if(!is.null(I)) |
| 76 | + { |
| 77 | + #If it is, just return it |
| 78 | + return(I) |
| 79 | + } |
| 80 | + |
| 81 | + #Otherwise, compute it, store it in the cache and return it |
| 82 | + |
| 83 | + A <- x$get() |
| 84 | + I <- solve(A) |
| 85 | + x$setInverse(I) |
| 86 | + |
| 87 | + I |
15 | 88 | } |
0 commit comments