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
64 lines (50 loc) · 1.93 KB
/
cachematrix.R
File metadata and controls
64 lines (50 loc) · 1.93 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
# Programming assignment 2: My exercise
## This program is an example of R functions that, taking advantage of R scoping rules,
## are potentially able to cash data to save time computation.
## The last lines of this prograsm include a specific section to verify the proper execution
## of the developpecd functions and the calculation based on a matrix sample M(nrow=3,ncol=3)
## This function, makeCacheMatrix creates a special "matrix", which is really a list
## containing three functions to:
## - get the matrix
## - set the matrix inverse
## - get the matrix inverse
makeCacheMatrix <- function(x = matrix()) {
invm <- NULL
get <- function() x
setinvm <- function(mat) invm <<- mat
getinvm <- function() invm
list(get = get,
setinvm = setinvm,
getinvm = getinvm)
}
## The following function calculates the matrix inverse of the special "matrix" created
## with the functions included in the list defined by the above function makeCacheMatrix.
## However, it first checks to see if the matrix inverse has already been calculated.
## If so, it gets the matrix inverse from the cache and skips the computation.
## Otherwise, it calculates the matrix inverse from the data stored in makeCacheMatrix
## function environmernt and sets the matrix inverse in the cache via the setinvm function.
cacheSolve <- function(x, ...) {
invm <- x$getinvm()
if(!is.null(invm)) {
message("getting cached matrix inverse")
return(invm)
}
mat <- x$get()
invm <- solve(mat, ...)
x$setinvm(invm)
invm
}
# Verification of proper function execution
# M = example of invertible matrix
M <- matrix(c(1,0,0,1,1,0,1,1,1) , nrow = 3, ncol = 3)
M
# First case of a new inverse matrix calculation
CM <- makeCacheMatrix(M)
Minv <- cacheSolve(CM)
Minv
# Second case regarding the retrival of a cached matrix
Minv <- cacheSolve(CM)
Minv
# Computation verification: Mat_Ver shall be the identity matrix
Mat_Ver <- M %*% Minv
Mat_Ver