1
- # # Put comments here that give an overall description of what your
2
- # # functions do
3
-
4
- # # Write a short comment describing this function
5
-
6
- makeCacheMatrix <- function (x = matrix ()) {
1
+ # # This R script will expose functions that allow a caller to compute
2
+ # # the inverse of a matrix while speeding up the calculation by pulling
3
+ # # previously computed values from the cache.
7
4
5
+ # # Takes an invertible matrix as input and exposes
6
+ # # a set of functions that enables the caller to
7
+ # # cache the inverse of a matrix
8
+ # # Input: An invertible matrix
9
+ # # Output: A special vector that contains the following functions
10
+ # # set: stores the supplied matrix and resets the previously computed inverse
11
+ # # get: returns the matrix
12
+ # # setinverse: takes as parameter the inverse matrix to be stored in cache
13
+ # # getinverse: Returns the stored inverse matrix
14
+ makeCacheMatrix <- function (m = matrix ())
15
+ {
16
+ i <- NULL
17
+ set <- function (y ) {
18
+ m <<- y
19
+ i <<- NULL
20
+ }
21
+ get <- function () m
22
+ setinverse <- function (inverse ) i <<- inverse
23
+ getinverse <- function () i
24
+ list (set = set , get = get ,
25
+ setinverse = setinverse ,
26
+ getinverse = getinverse )
8
27
}
9
28
10
-
11
- # # Write a short comment describing this function
12
-
13
- cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
15
- }
29
+ # # Takes a special matrix vector and returns its inverse. If the inverse has already
30
+ # # been computed - and the matrix has not changed - the inverse is retrieved from cache
31
+ # # Input : A special matrix vector created from a makeCacheMatrix call
32
+ # # Output: Inverse of the matrix.
33
+ cacheSolve <- function (m , ... )
34
+ {
35
+ i <- m $ getinverse()
36
+
37
+ if (! is.null(i ))
38
+ {
39
+ message(" Printing cached inverse" )
40
+ return (i )
41
+ }
42
+
43
+ data <- m $ get()
44
+ i <- solve(data , ... )
45
+ m $ setinverse(i )
46
+ i
47
+
48
+ }
0 commit comments