File tree Expand file tree Collapse file tree 1 file changed +31
-8
lines changed Expand file tree Collapse file tree 1 file changed +31
-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
1
+ # This file implements two functions: makeCacheMatrix and cacheSolve.
2
+ # Together they provide a way to construct a matrix object that caches
3
+ # its inverse computation, provinding faster access to it.
3
4
4
- # # Write a short comment describing this function
5
5
6
+ # makeCacheMatrix creates an object that represents a matrix that the
7
+ # inverse operation is cached for faster computations.
6
8
makeCacheMatrix <- function (x = matrix ()) {
7
-
9
+ x_inv <- NULL
10
+ set <- function (x_new ) {
11
+ x <<- x_new
12
+ x_inv <<- NULL
13
+ }
14
+ get <- function () {
15
+ x
16
+ }
17
+ set_inverse <- function (x_inv_new ) {
18
+ x_inv <<- x_inv_new
19
+ }
20
+ get_inverse <- function () {
21
+ x_inv
22
+ }
23
+ list (set = set , get = get , set_inverse = set_inverse , get_inverse = get_inverse )
8
24
}
9
25
10
-
11
- # # Write a short comment describing this function
12
-
26
+ # cacheSolve solves the inverse of the matrix x and caches its results
27
+ # so that later requests are not calculated again
13
28
cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
29
+ xi <- x $ get_inverse()
30
+ if (! is.null(xi )) {
31
+ message(" getting cached inverse matrix" )
32
+ return (xi )
33
+ }
34
+ x_data <- x $ get()
35
+ xi <- solve(x_data )
36
+ x $ set_inverse(xi )
37
+ xi
15
38
}
You can’t perform that action at this time.
0 commit comments