forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachematrix.R
More file actions
129 lines (112 loc) · 3.96 KB
/
cachematrix.R
File metadata and controls
129 lines (112 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
################################################################################
# #
# Specialization: Data Science - Foundations using R Specialization #
# Course: R Programming #
# #
# Author: Anderson Hitoshi Uyekita #
# Date: 2022/05/16 #
# Project: Programming Assignment 2 - Lexical Scoping (Week 3) #
# #
################################################################################
#' makeCacheMatrix Function
#'
#' Given the example function from the statement. I have made some minor changes
#' to adapt it to calculate the inverse of a Matrix.
#'
#' @param x Should be a any invertible matrix
#'
#' @examples
#'
#' \dontrun{
#' # Make it reproducible examples defining a fixed seed.
#' base::set.seed(2022)
#'
#' # Create a "toy" matrix.
#' A <- matrix(rnorm(9, 0, 1), nrow = 3)
#'
#' # Cacheable Matrix A
#' A_cache <- makeCacheMatrix(x = A)
#' }
#'
#' @export
makeCacheMatrix <- function(x = matrix()) {
# I have changed the variable name to "m_inverse" to make an allusion to matrix inverse.
# Variable initialization as NULL means I do not have this inverse matrix on my cache.
m_inverse <- NULL
# Inner function SET
# Update the Matrix stored in the Cacheable variable.
set <- function(y) {
x <<- y
m_inverse <<- NULL
}
# Inner function GET
# Retrieve the original Matrix (not the inverse one).
# Example: A_cache$get()
get <- function() {
x
}
# Inner function SETSOLVE
# CALCULATE the inverse matrix.
# Example: A_cache$setsolve()
setsolve <- function(solve) {
m_inverse <<- solve
}
# Inner function GETSOLVE
# GET the inverse matrix stored in cache.
# Example: A_cache$getsolve()
getsolve <- function() {
m_inverse
}
# Return a list of functions defined above.
# These function will be used by cacheSolve function.
list(set = set, get = get, setsolve = setsolve, getsolve = getsolve)
}
#' cacheSolve function
#'
#' This function computes the inverse of the special "matrix"
#'
#' @param x Should be a cacheable matrix (see the results of makeCacheMatrix function)
#'
#' @examples
#'
#' \dontrun{
#' # Make it reproducible
#' base::set.seed(2022)
#'
#' # Create a "toy" matrix.
#' A <- matrix(rnorm(9, 0, 1), nrow = 3)
#'
#' # Cacheable Matrix A
#' A_cache <- makeCacheMatrix(x = A)
#'
#' # Calculate the inverse of A_cache
#' A_cache_inverse <- cacheSolve(x = A_cache)
#'
#' # Compare the regular function solve with it.
#' base::identical(base::solve(A), A_cache_inverse)
#'
#' # TRUE means the both methods ends into the inverse matrix.
#' }
#'
#' @export
cacheSolve <- function(x, ...) {
# Use of GETSOLVE inner function.
# It should be NULL for the first time you will calculate it because we have defined it as NULL.
m_inverse <- x$getsolve()
# CASE A: m_inverse is not NULL, they will skip the inverting process and will take the data from cache.
if(!is.null(m_inverse)) {
message("getting cached data")
# Matrix stored in the cache.
return(m_inverse)
}
# CASE B: The m_inverse matrix is NULL, which means there is no data about this inverse matrix.
# Store the Matrix in a "temporally" variable
data <- x$get()
# Calculate the inverse using the Base package solve function.
m_inverse <- base::solve(data, ...)
# Store the inverse matrix in the Cacheable variable.
x$setsolve(m_inverse)
# Return the inverse matrix
m_inverse
}
# READINGS: http://adv-r.had.co.nz/Functional-programming.html#closures