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
100 lines (93 loc) · 5.21 KB
/
cachematrix.R
File metadata and controls
100 lines (93 loc) · 5.21 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
## Put comments here that give an overall description of what your
## functions do
## Two functions are defined below: "makeCacheMatrix" and "cacheSolve"
## The first function is a function with four functions ("methods") in it. They are
## "set", "get", "setinverse", and "getinverse".
## Normally, we do not call "setinverse" directly.
## The following is a usage example:
# > hilbert <- function(n) { i <- 1:n; 1 / outer(i - 1, i, "+") }
# > h8 <- hilbert(8); h8
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
# [1,] 1.0000000 0.5000000 0.3333333 0.25000000 0.20000000 0.16666667 0.14285714 0.12500000
# [2,] 0.5000000 0.3333333 0.2500000 0.20000000 0.16666667 0.14285714 0.12500000 0.11111111
# [3,] 0.3333333 0.2500000 0.2000000 0.16666667 0.14285714 0.12500000 0.11111111 0.10000000
# [4,] 0.2500000 0.2000000 0.1666667 0.14285714 0.12500000 0.11111111 0.10000000 0.09090909
# [5,] 0.2000000 0.1666667 0.1428571 0.12500000 0.11111111 0.10000000 0.09090909 0.08333333
# [6,] 0.1666667 0.1428571 0.1250000 0.11111111 0.10000000 0.09090909 0.08333333 0.07692308
# [7,] 0.1428571 0.1250000 0.1111111 0.10000000 0.09090909 0.08333333 0.07692308 0.07142857
# [8,] 0.1250000 0.1111111 0.1000000 0.09090909 0.08333333 0.07692308 0.07142857 0.06666667
# > a<-makeCacheMatrix(h8)
# > a$get()
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
# [1,] 1.0000000 0.5000000 0.3333333 0.25000000 0.20000000 0.16666667 0.14285714 0.12500000
# [2,] 0.5000000 0.3333333 0.2500000 0.20000000 0.16666667 0.14285714 0.12500000 0.11111111
# [3,] 0.3333333 0.2500000 0.2000000 0.16666667 0.14285714 0.12500000 0.11111111 0.10000000
# [4,] 0.2500000 0.2000000 0.1666667 0.14285714 0.12500000 0.11111111 0.10000000 0.09090909
# [5,] 0.2000000 0.1666667 0.1428571 0.12500000 0.11111111 0.10000000 0.09090909 0.08333333
# [6,] 0.1666667 0.1428571 0.1250000 0.11111111 0.10000000 0.09090909 0.08333333 0.07692308
# [7,] 0.1428571 0.1250000 0.1111111 0.10000000 0.09090909 0.08333333 0.07692308 0.07142857
# [8,] 0.1250000 0.1111111 0.1000000 0.09090909 0.08333333 0.07692308 0.07142857 0.06666667
# > cacheSolve(a)
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
# [1,] 64 -2016 20160 -92400 221760 -288288 192192 -51480
# [2,] -2016 84672 -952560 4656960 -11642400 15567552 -10594584 2882880
# [3,] 20160 -952560 11430720 -58212000 149688000 -204324119 141261119 -38918880
# [4,] -92400 4656960 -58212000 304919999 -800414996 1109908794 -776936155 216215998
# [5,] 221760 -11642400 149688000 -800414996 2134439987 -2996753738 2118916783 -594593995
# [6,] -288288 15567552 -204324119 1109908793 -2996753738 4249941661 -3030050996 856215352
# [7,] 192192 -10594584 141261119 -776936154 2118916782 -3030050996 2175421226 -618377753
# [8,] -51480 2882880 -38918880 216215998 -594593995 856215351 -618377753 176679358
# > cacheSolve(a)
# getting cached data
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
# [1,] 64 -2016 20160 -92400 221760 -288288 192192 -51480
# [2,] -2016 84672 -952560 4656960 -11642400 15567552 -10594584 2882880
# [3,] 20160 -952560 11430720 -58212000 149688000 -204324119 141261119 -38918880
# [4,] -92400 4656960 -58212000 304919999 -800414996 1109908794 -776936155 216215998
# [5,] 221760 -11642400 149688000 -800414996 2134439987 -2996753738 2118916783 -594593995
# [6,] -288288 15567552 -204324119 1109908793 -2996753738 4249941661 -3030050996 856215352
# [7,] 192192 -10594584 141261119 -776936154 2118916782 -3030050996 2175421226 -618377753
# [8,] -51480 2882880 -38918880 216215998 -594593995 856215351 -618377753 176679358
# > sh8 <- a$getinverse()
# > round(sh8 %*% h8)
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
# [1,] 1 0 0 0 0 0 0 0
# [2,] 0 1 0 0 0 0 0 0
# [3,] 0 0 1 0 0 0 0 0
# [4,] 0 0 0 1 0 0 0 0
# [5,] 0 0 0 0 1 0 0 0
# [6,] 0 0 0 0 0 1 0 0
# [7,] 0 0 0 0 0 0 1 0
# [8,] 0 0 0 0 0 0 0 1
# >
## Write a short comment describing this function
makeCacheMatrix <- function(x = matrix()) {
invx <- NULL ## invx : the inverse of x
set <- function(y) {
x <<- y
invx <<- NULL
}
get <- function() x
setinverse <- function(inverse) invx <<- inverse
getinverse <- function() invx
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## Write a short comment describing this function
## First, fetch the inverse of x by " x$getinverse()"
## if the fetched value is null, means the matrix x just setted before,
## then the inverse should be computed.
## Otherwise, the inverse need not be computed again, and directly return the cached value.
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
invx <- x$getinverse()
if(!is.null(invx)) {
message("getting cached data")
return(invx)
}
data <- x$get()
invx <- solve(data, ...)
x$setinverse(invx)
invx
}