forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
65 lines (52 loc) · 1.64 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
## From a given an invertible matrix calculate its inverse and
## cache the inverse of that matrix
## Creates a special "matrix" object that can cache it inverse
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
set <- function(y) {
x <<- y
m <<- NULL
}
# retrieve x from parent environment
get <- function() x
# calculate the inverse
setinverse <- function() m <<- solve(x)
# get the inverse (m) from parent environment
getinverse <- function() m
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## Computes the inverse of the special matrix returned by makeCacheMatrix
## function above. If inverse has already been calculates (and the matrix
## has not changed), then this function bellow should retrive the inverse.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
m <- x$getinverse()
if (!is.null(m)) {
message("getting cached data..")
}
data <- x$get()
m <- solve(data)
x$setinverse()
m
}
## Testing the functions
m1 <- matrix(c(1/2, -1/4, -1, 3/4), nrow = 2, ncol = 2)
m1
I2 <- matrix(c(1,0,0,1), nrow = 2, ncol = 2)
I2
n1 <- matrix(c(6,2,8,4), nrow = 2, ncol = 2)
# checks:
m1 %*% n1
n1 %*% m1
solve(m1)
solve(n1)
my_matrix_obj <- makeCacheMatrix(m1)
cacheSolve(my_matrix_obj)
cacheSolve(my_matrix_obj)
n2 <- matrix(c(5/8, -1/8, -7/8, 3/8), nrow = 2, ncol = 2)
my_matrix_obj$set(n2)
my_matrix_obj$get()
cacheSolve(my_matrix_obj)
cacheSolve(my_matrix_obj)