Skip to content

Commit 8be70a4

Browse files
committed
cachematrix.R successfully edited and tested
1 parent 7f657dd commit 8be70a4

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

cachematrix.R

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
11
## Put comments here that give an overall description of what your
22
## functions do
33

4-
## Write a short comment describing this function
5-
6-
makeCacheMatrix <- function(x = matrix()) {
7-
4+
# This function takes as input an non-singular square matrix 'X' and returns an matrix object 'X'
5+
# which has some functions to calculate the inverse matrix of 'X'.
6+
makeCacheMatrix <- function(X = matrix()) {
7+
A_inverse <- NULL
8+
set <- function() {
9+
X <<- y
10+
A_inverse <<- NULL
11+
}
12+
get <- function() X
13+
setinverse_matrix <- function(solve) A_inverse <<- solve
14+
getinverse_matrix <- function() A_inverse
15+
list(set = set, get = get, setinverse_matrix = setinverse_matrix, getinverse_matrix = getinverse_matrix)
816
}
917

1018

11-
## Write a short comment describing this function
12-
13-
cacheSolve <- function(x, ...) {
19+
# This method returns the inverse matrix of 'X'. If the inverse of 'X' hasn't been calculated the method
20+
# calculate inverse of 'X', stores the inverse of 'X' and returns it.
21+
# If the inverse of 'X' was already calculated the method just gets the inverse of 'X' stored and returns it.
22+
cacheSolve <- function(X, ...) {
1423
## Return a matrix that is the inverse of 'x'
24+
A_inverse <- X$getinverse_matrix()
25+
if(!is.null(A_inverse)) {
26+
message("getting cached data")
27+
return(A_inverse)
28+
}
29+
data <- X$get()
30+
A_inverse <- solve(data, ...)
31+
X$setinverse_matrix(A_inverse)
32+
A_inverse
1533
}

0 commit comments

Comments
 (0)