File tree Expand file tree Collapse file tree 1 file changed +35
-8
lines changed Expand file tree Collapse file tree 1 file changed +35
-8
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
3
-
4
- # # Write a short comment describing this function
1
+ # # Funtions to cache the inverse of matrix.
2
+ # # Inverse is calculated and stored the first time cacheSolve is called.
3
+ # # Subsequent calls return the stored inverse value
4
+ # # Usage instruction
5
+ # # Call makeCacheMatrix passing in matrix to be inversed
6
+ # # Call cacheSolve passing in the value returned by makeCacheMatrix
5
7
8
+ # # Creates object that will cache inverse of the matrix. Pass resulting object
9
+ # # to cacheSolve as parameters
10
+ # # parameter: matrix to be inversed
6
11
makeCacheMatrix <- function (x = matrix ()) {
7
-
12
+ s <- NULL
13
+ set <- function (y ) {
14
+ x <<- y
15
+ s <<- NULL
16
+ }
17
+ get <- function () x
18
+ setsolve <- function (solve ) s <<- solve
19
+ getsolve <- function () s
20
+ list (set = set , get = get ,
21
+ setsolve = setsolve ,
22
+ getsolve = getsolve )
23
+ invisible ()
8
24
}
9
25
10
26
11
- # # Write a short comment describing this function
12
-
27
+ # # Calculates inverse of a matrix and stores results. Subsequent calls return
28
+ # # stored value
29
+ # # Parameter: pass in object returned by makeCacheMatrix
30
+ # # Return: inverse of passed matrix
13
31
cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
32
+ s <- x $ getsolve()
33
+ if (! is.null(s )) {
34
+ message(" getting cached data" )
35
+ return (s )
36
+ }
37
+ data <- x $ get()
38
+ s <- solve(data , ... )
39
+ x $ setsolve(s )
40
+ # # Returns matrix that is the inverse of 'x'
41
+ s
15
42
}
You can’t perform that action at this time.
0 commit comments