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
61 lines (52 loc) · 1.61 KB
/
cachematrix.R
File metadata and controls
61 lines (52 loc) · 1.61 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
## Functions implement an matrix-object capable of caching its inverse value
## usage:
## create a new object: ematrix<-makeCacheMatrix(numeric_matrix)
## extract numeric matric: ematrix$get()
## change the matrix value: ematrix$set(new_numeric_matrix)
## calculate the numeric inverse matrix: cacheSolve(ematrix)
## Creates a matrix-object enabling to hold its inverse matrix
makeCacheMatrix <- function(x = matrix()) {
if(!is.matrix(x))
{
stop("the argument is not a matrix")
}
invM <- NULL
#the function sets the object value
#usage: obj$set(matrix(...))
set <- function(y) {
if(!is.matrix(y))
{
stop("the argument is not a matrix")
}
x <<- y
invM <<- NULL
}
#the function extracts the numeric matrix
get <- function() x
#the internal function used to store inverse matrix in parent environment
setinv <- function(invm) invM <<- invm
#the internal function used to recall inverse matrix from parent environment
getinv <- function() invM
#the object is represented as a list of handler functions
list(set = set, get = get,
setinv = setinv,
getinv = getinv)
}
## Calcurate the matrix inverse if it hasn't been computed yet or restore it from its cache
cacheSolve <- function(x, ...) {
invM <- x$getinv()
if(!is.null(invM)) {
#return inverse from cache
return(invM)
}
data <- x$get()
ndim=dim(data)
if(ndim[1]!=ndim[2])
{
stop("the matrix is not square")
}
invM <- solve(data,...)
x$setinv(invM)
## Return a matrix that is the inverse of 'x'
invM
}