Skip to content

Commit 33ea4b9

Browse files
author
Andre Casimiro
committed
done cachematrix
1 parent 7f657dd commit 33ea4b9

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

cachematrix.R

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,38 @@
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.
34

4-
## Write a short comment describing this function
55

6+
# makeCacheMatrix creates an object that represents a matrix that the
7+
# inverse operation is cached for faster computations.
68
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)
824
}
925

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
1328
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
1538
}

0 commit comments

Comments
 (0)