Skip to content

Commit 962f489

Browse files
committed
Added functions per Assignment requirements.
1 parent 7f657dd commit 962f489

File tree

1 file changed

+125
-3
lines changed

1 file changed

+125
-3
lines changed

cachematrix.R

Lines changed: 125 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,137 @@
11
## Put comments here that give an overall description of what your
22
## functions do
33

4-
## Write a short comment describing this function
4+
5+
6+
## This function creates a special "matrix" object
7+
## that can cache its inverse.
58

69
makeCacheMatrix <- function(x = matrix()) {
710

11+
# define mxi as NULL
12+
mxi <- NULL
13+
# push original data to global environment to cache.
14+
# pls note that calling function won't use the set method
15+
set <- function(y) {
16+
x <<- y
17+
mxi <<- NULL
18+
}
19+
# return the cached original matrix
20+
get <- function() x
21+
22+
# obtains inversed matrix data from the calling function
23+
# pushes to 'mxi' in the global environment
24+
setmxinverse <- function(mxinverse) mxi <<- mxinverse
25+
getmxinverse <- function() mxi
26+
27+
# return a list of method names for calling function to refer to
28+
list(set = set, get = get,
29+
setmxinverse = setmxinverse,
30+
getmxinverse = getmxinverse)
831
}
932

1033

11-
## Write a short comment describing this function
34+
35+
36+
37+
## This function computes the inverse of the special "matrix"
38+
## returned by makeCacheMatrix.
39+
##
40+
## If the inverse has already been calculated (and the matrix
41+
## has not changed), then this function should retrieve the
42+
## inverse from the cache.
1243

1344
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
45+
46+
# try x from the global environment
47+
# the method of x is $getmxinverse in cached data
48+
mxi <- x$getmxinverse()
49+
50+
# if not cached mix is NULL
51+
# but !is.null == true indicates that cached data exists
52+
if(!is.null(mxi)) {
53+
message("... CACHED matrix-inverse")
54+
# return and exit the function.
55+
return(mxi)
56+
}
57+
58+
# when cached data is NULL proceed with calculation of inverse
59+
# request data from the local scope of the makeCacheMatrix.R
60+
data <- x$get()
61+
# calculate the inverse
62+
im <- solve(data, ...)
63+
# and save it to global environment to cache
64+
x$setmxinverse(im)
65+
66+
# set the message regarding initial calculation, not cached yet
67+
message("... NEW instance of matrix-inverse")
68+
print(im) #return inversed matrix by printing to terminal (test purposes)
1569
}
70+
71+
72+
73+
74+
75+
76+
77+
## ================================================================
78+
## FOLLOWING IS A TEST OF BOTH CACHE AND INVERSE (SOLVE) OPERATIONS
79+
80+
81+
m=matrix(c(1,5,7,8,9,4,6,2,3), nrow=3, ncol=3)
82+
message("... ORIGINAL test matrix")
83+
print(m)
84+
85+
nm = makeCacheMatrix(m)
86+
o1 <- cacheSolve(nm) # initial: should be new instance of inverse
87+
im <- cacheSolve(nm) # second call : should be the cached version
88+
print(im)
89+
90+
## another test: return to the original by inversing the inversed
91+
om <- makeCacheMatrix(im)
92+
## and inverse the inversed matrix => original matrix
93+
om1 <- cacheSolve(om) # initial: should be new instance of inverse of inverse = original
94+
om2 <- cacheSolve(om) # second call :should be the cached version of the original
95+
print(om2)
96+
97+
98+
## ======================================
99+
## TEST OUTPUT IS AS FOLLOWS
100+
101+
102+
103+
# ... ORIGINAL test matrix
104+
# [,1] [,2] [,3]
105+
# [1,] 1 8 6
106+
# [2,] 5 9 2
107+
# [3,] 7 4 3
108+
109+
# ... NEW instance of matrix-inverse
110+
# [,1] [,2] [,3]
111+
# [1,] -0.076923077 0.0000000 0.1538462
112+
# [2,] 0.004048583 0.1578947 -0.1133603
113+
# [3,] 0.174089069 -0.2105263 0.1255061
114+
115+
# ... CACHED matrix-inverse
116+
# [,1] [,2] [,3]
117+
# [1,] -0.076923077 0.0000000 0.1538462
118+
# [2,] 0.004048583 0.1578947 -0.1133603
119+
# [3,] 0.174089069 -0.2105263 0.1255061
120+
121+
122+
# RETURN to ORIGINAL MATRIX
123+
124+
# ... NEW instance of matrix-inverse
125+
# [,1] [,2] [,3]
126+
# [1,] 1 8 6
127+
# [2,] 5 9 2
128+
# [3,] 7 4 3
129+
130+
# ... CACHED matrix-inverse
131+
# [,1] [,2] [,3]
132+
# [1,] 1 8 6
133+
# [2,] 5 9 2
134+
# [3,] 7 4 3
135+
136+
## ======================================
137+

0 commit comments

Comments
 (0)