File tree Expand file tree Collapse file tree 1 file changed +42
-7
lines changed Expand file tree Collapse file tree 1 file changed +42
-7
lines changed Original file line number Diff line number Diff line change 1
- # # Put comments here that give an overall description of what your
2
- # # functions do
1
+ # # Function to make a special matrix object that can cache its inverse when
2
+ # # computer second function to compute inverses but calling for a cache first
3
3
4
- # # Write a short comment describing this function
4
+ # # This function creates a special "matrix" object that can cache its
5
+ # # inverse.
5
6
6
7
makeCacheMatrix <- function (x = matrix ()) {
7
-
8
+ inverse <- NULL
9
+ set <- function (y ) {
10
+ # setting function
11
+ x <<- y
12
+ inverse <<- NULL
13
+ }
14
+ get <- function () {
15
+ # getting function
16
+ x
17
+ }
18
+ setinverse <- function (value ) {
19
+ # set inverse within scope
20
+ inverse <<- value
21
+ }
22
+ getinverse <- function () {
23
+ inverse
24
+ }
25
+
26
+ list (
27
+ set = set , get = get ,
28
+ setinverse = setinverse ,
29
+ getinverse = getinverse
30
+ )
8
31
}
9
32
10
-
11
- # # Write a short comment describing this function
33
+ # # This function computes the inverse of the special "matrix" returned by
34
+ # # makeCacheMatrix above. If the inverse has already been calculated (and the
35
+ # # matrix has not changed), then cacheSolve should retrieve the inverse from
36
+ # # the cache.
12
37
13
38
cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
39
+ # # Return a matrix that is the inverse of 'x'
40
+ # # assumption matrix is always invertable
41
+ inverse <- x $ getinverse()
42
+ if (! is.null(inverse )) {
43
+ message(" getting cached data" )
44
+ return (inverse )
45
+ }
46
+ data <- x $ get()
47
+ inverse <- solve(data , ... )
48
+ x $ setinverse(inverse )
49
+ inverse
15
50
}
You can’t perform that action at this time.
0 commit comments