forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachematrix.R
More file actions
86 lines (70 loc) · 2.46 KB
/
cachematrix.R
File metadata and controls
86 lines (70 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
## Matrix inversion is usually a costly computation and there may be some
## benefit to caching the inverse of a matrix rather than compute it repeatedly.
## These functions provide the functionality for caching the inverse of a matrix.
## This function creates a special "matrix" object that can cache its inverse.
## makeVector used as a model.
## m is the matrix
## i is the inverse matrix
makeCacheMatrix <- function( m = matrix() )
{
## initialize starting values to NULL
i <- NULL
## set will assign the matrix and reset the inverse to NULL
set <- function( y )
{
m <<- y
i <<- NULL
}
## get returns the current matrix
get <- function() m
## setinverse will assign the inverse value
setinverse <- function( inverse ) i <<- inverse
## getinverse will return the inverse value
getinverse <- function() i
## create the vector for the functions.
list( set = set, get = get,
setinverse = setinverse,
getinverse = getinverse )
}
## This function computes the inverse of the special "matrix" returned by
## makeCacheMatrix above. If the inverse has already been calculated (and the
## matrix has not changed), then the cachesolve should retrieve the inverse from
## the cache.
## m is the matrix we want to provide the inverse for
## i is the inverse solution
cacheSolve <- function(m, ...)
{
## first, check to see if we have it already
i <- m$getinverse()
## if i isn't null, then we have it cached, so just return it
if( !is.null( i ) )
{
message( "getting cached data" )
return( i )
}
# we don't have it, so call setInverse and it will return 'i' for us
m$setinverse( solve( m$get(), ... ) )
}
## Test function, builds two simple 2x2 invertible matrices. Outputs the
## starting matrices then computes their inverses. Then checks the caching
## by outputting the inverses again, and then validates the inverse by
## multiplying the starting and inverse matrices to get an identity matrix.
doTest <- function()
{
cm1 <- makeCacheMatrix();
m1 <- matrix( c( 4, 2, 7, 6 ), nrow = 2 )
cm1$set( m1 )
cm2 <- makeCacheMatrix();
m2 <- matrix( c( 4, 3, 3, 2 ), nrow = 2 )
cm2$set( m2 )
print(m1)
print(m2)
print( cacheSolve( cm1 ) )
print( cacheSolve( cm2 ) )
print( "check for caching" )
print( cacheSolve( cm1 ) )
print( m1 %*% cacheSolve( cm1 ) )
print( cacheSolve( cm2 ) )
print( m2 %*% cacheSolve( cm2 ) )
}
# doTest()