forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
56 lines (53 loc) · 1.95 KB
/
cachematrix.R
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
# creates a special purpose object (extended matrix) from an R matrix
# this "extended matrix" can hold a second matrix in a scoped cache variable called "mym"
makeCacheMatrix<-function(m=matrix()) {
# myInverse and m below are "scoped" variables, not directly accessible from calling environment
myInverse <- NULL
# set is used to set a NEW matrix; potentially existing inverse are reset
set <- function (y) {
m<<-y
myInverse <<- NULL
}
get <- function () m
setinverse <- function (invmatrix) myInverse <<-invmatrix
getinverse <- function () myInverse
list (set=set, get=get,setinverse=setinverse,getinverse=getinverse)
}
# cacheSolve returns the inverse of a matrix
# it needs a special purpose object, an "extended matrix" created by function makeCacheMatrix
# cacheSolve will compute the inverse only if the extended matrix does not hold it internally
cacheSolve <- function(matrixEx,...) {
inv <- matrixEx$getinverse()
if(!is.null(inv)){
message ("[getting cached data!]")
return (inv)
}
originalRealMatrix <- matrixEx$get()
inv <- solve(originalRealMatrix,...)
matrixEx$setinverse(inv)
inv
}
#Extra fct to test if the process works with above defined function
testCachingOfInv <- function () {
message ("creating sample matrix ...")
somedata<-rbind(1:3,0:2,c(0,0,5))
m<-matrix(data=somedata,nrow=3)
print(m)
message ("creating special object ...")
mEx <- makeCacheMatrix (m)
message ("retrieving inverse (handling cache) 1 ...")
print (cacheSolve(mEx))
message ("retrieving inverse (handling cache) 2 ...")
print (cacheSolve(mEx))
print("")
message ("creating another sample matrix ...")
somedata<-rbind(1:3,0:2,c(0,0,17))
m<-matrix(data=somedata,nrow=3)
print(m)
message ("creating special object ...")
mEx <- makeCacheMatrix (m)
message ("retrieving inverse (handling cache) 1 ...")
print (cacheSolve(mEx))
message ("retrieving inverse (handling cache) 2 ...")
print (cacheSolve(mEx))
}