forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
51 lines (46 loc) · 1.35 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
## There are only two function. first you may initalize a matrix with makeCacheMatrix function. It stores the matrix data and inverse of the data
## when you call cacheSolve function It checks the object which have already created with makeCacheMatrix, if it includes inverse data it returns that value
## if the inverse data is NULL it computes its value.
#initilizes CacheMatrix instance
makeCacheMatrix <- function(x = matrix()) {
I <- NULL
set <- function(y){
x <<- y
I <<- NULL
}
get <- function() x
setInverse <- function(solve) I <<- solve
getInverse <- function() I
list(set = set, get = get, setInverse = setInverse, getInverse = getInverse)
}
## tries to compute inverse of matrices created with makeCacheMatrix function.
## if the instance has inverse value then returns it else it computes and sets it with setInverse
cacheSolve <- function(x, ...) {
I <- x$getInverse()
if(!is.null(I)){
message("GET FROM CACHE!")
return(I)
}
message("CALCULATED")
data <- x$get()
I <- solve(data, ...)
x$setInverse(I)
I
}
##TEST##
##initialize object
M <- makeCacheMatrix();
## create a matrix
test <- matrix(1:4, 2, 2);
## set the matrix
M$set(test);
## solve it
cacheSolve(M);
## get solution
M$getInverse();
## solve againt (it returns from cache!)
cacheSolve(M);
## set as NULL
M$setInverse(NULL);
## solve it again (computes again)
cacheSolve(M);