File tree Expand file tree Collapse file tree 1 file changed +63
-0
lines changed Expand file tree Collapse file tree 1 file changed +63
-0
lines changed Original file line number Diff line number Diff line change @@ -13,3 +13,66 @@ makeCacheMatrix <- function(x = matrix()) {
13
13
cacheSolve <- function (x , ... ) {
14
14
# # Return a matrix that is the inverse of 'x'
15
15
}
16
+
17
+
18
+
19
+ # # A pair of functions that inverse matrices with caching
20
+
21
+ makeCacheMatrix <- function (m = matrix ()) {
22
+ # Initialize the inverse property
23
+ i <- NULL
24
+
25
+
26
+ # Set the matrix
27
+ set <- function (mat ) {
28
+ m <<- mat
29
+ i <<- NULL
30
+ }
31
+
32
+ # get the matrix
33
+
34
+ get <- function () {
35
+ # return the matrix
36
+ m
37
+ }
38
+ # the inverse matrix
39
+
40
+ setinverse <- function (inverse ){
41
+ i <<- inverse
42
+ }
43
+
44
+ # get the inverse matrix
45
+
46
+ getinverse <- function (){
47
+ i
48
+ }
49
+ # return list of methods
50
+ list (set = set ,
51
+ get = get ,
52
+ setinverse = setinverse ,
53
+ getinverse = getinverse )
54
+ }
55
+
56
+
57
+ # Part two
58
+
59
+ cacheSolve <- function (x , ... ) {
60
+ # return the inverse of x
61
+ m <- x $ getinverse()
62
+
63
+ if (! is.null(m )) {
64
+
65
+ message(" getting cached data." )
66
+
67
+ return (m )
68
+ }
69
+
70
+ data <- x $ get()
71
+ # the inverse via matrix multiplication
72
+ m <- solve(data ) %*% data
73
+
74
+ x $ setinverse(m )
75
+
76
+ # return the matrix
77
+ m
78
+ }
You can’t perform that action at this time.
0 commit comments