|
2 | 2 | ## In case there is the matrix already calculated the cached value is used instead of calculation |
3 | 3 | makeCacheMatrix <- function(x = matrix()) { |
4 | 4 |
|
5 | | - ## Initialization |
| 5 | + # Initialization of cache to null |
6 | 6 | invValX <- NULL |
7 | 7 |
|
| 8 | + # Store matrix |
8 | 9 | set <- function(Y) |
9 | 10 | { |
10 | 11 | x <<- y |
11 | 12 | invValX <<- NULL |
12 | 13 | } |
13 | 14 |
|
| 15 | + # Get matrix |
14 | 16 | get <- function() x |
15 | 17 |
|
16 | 18 | # Calculate the inverse value |
17 | 19 | setinverse<- function(inverse) invValX <<-inverse |
18 | 20 | getinverse <- function() invValX |
19 | 21 |
|
| 22 | + # Return a list object |
20 | 23 | list(set = set, get = get, setinverse = setinverse, getinverse = getinverse) |
21 | 24 | } |
22 | 25 |
|
23 | 26 | ## The function cacheSolve returns the inverse of a matrix by function makeCacheMatrix. |
24 | 27 | ## If the object is not stored in cached it is created and set to cached and retuned to calling context. |
25 | 28 | cacheSolve <- function(x, ...) |
26 | 29 | { |
27 | | - ## Get the inverse value of 'x' if any |
| 30 | + # Get the inverse value of 'x' if any |
28 | 31 | invValX <- x$getinverse() |
29 | 32 |
|
30 | | - ## is there any object in cache? |
| 33 | + # is there any object in cache? |
31 | 34 | if (!is.null(invValX)) |
32 | 35 | { |
33 | | - message("The cached value is returned.") |
| 36 | + message("The cached inverse value is returned.") |
34 | 37 | return(invValX) |
35 | 38 | } |
36 | 39 |
|
37 | | - ## Cache is empty, calculate the value and store it in cache |
| 40 | + # Cache is empty, calculate the value and store it in cache |
| 41 | + message("Calculating a new inverse value and storing it in cache.") |
38 | 42 | invValX <- solve(x$get()) |
39 | | - #set the object into cache |
| 43 | + # Set the object into cache |
40 | 44 | x$setinverse(invValX) |
41 | 45 |
|
42 | 46 | return(invValX) |
|
0 commit comments