forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
70 lines (56 loc) · 1.65 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
makeCacheMatrix <- function(x = matrix()) {
## Sets the matrix, required to have an initial matrix for calculation
mat <- x
invm <-NULL ##Place holder for inverse of m
set <- function(y) {
##Set the matrix (x) to a new matrix (y)
#Compare existing x with new y, if the same then inverse will be already set and used as such
#If not same then we have to recalculate the inverse
if(!matequal(mat,y))
{
invm <<- NULL
}
mat <<- y
}
#Function that will test if 2 matrices are equal
matequal <- function(a,b) {is.matrix(a) && is.matrix(b) && dim(a) == dim(b) && all(a==b)}
get <- function() {
mat
}
## Function that will set the inverse, only if the matrix has changed
setinverse <- function(inverse) {
invm <<- inverse
}
##Function that will get the inverse
getinverse <- function() {
invm
}
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse
)
}
cacheSolve <- function(x, ...) {
#Calculate the Inverse.
#Example to use Function.
# 1. Setup the first matrix (Only NON singular) to calculate: mymatrix <- matrix(c(1:16),2,2)
# 2. Setup the Function to do the Matrix caching: cm <- makeCacheMatrix(mymatrix)
# 3. Solve the Inverse (and cache if not exist): cacheSolve(cm)
# 4. If other matrix are to be calculated, use set() function: cm$(newmatrix)
ivm <- x$getinverse()
if(!is.null(ivm)) {
message("Getting cached data")
return(ivm)
}
ma <- (x$get())
mat <-ma
dd <- det(ma)
dd
if(dd!=0)#Test the determinant
{
ivm <- solve(ma)
x$setinverse(ivm)
}
#return the calculted Inverse
ivm
}