Skip to content

Commit 72e9d79

Browse files
committed
Small changes
1 parent ce1049e commit 72e9d79

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

cachematrix.R

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
11
# Hi! I'm the guy from Ruby Planet! And I love clear code.
2-
# It was painfull for me to read initial version, so I've refactored all names.
2+
# It was painful for me to read initial version, so I've refactored all names.
33
# Please Enjoy R and this MOOC. If you are interested in detailed information
44
# please read detailed comment at the end of code.
55
# Thanks for your review!
66

7-
makeCacheMatrix <- function(local_matrix = matrix()) {
8-
# By default local_matrix is the empty matrix
7+
makeCacheMatrix <- function(original_matrix = matrix()) {
8+
# By default original_matrix is the empty matrix
99

1010
# Initializing variable supposed for cached solution
1111
cached_solution <- NULL
1212

13+
# Setting/Getting original matrix
1314
set_matrix <- function(arg) {
14-
local_matrix <<- arg
15+
original_matrix <<- arg
1516
cached_solution <<- NULL
1617
}
17-
get_matrix <- function() local_matrix
18+
get_matrix <- function() original_matrix
1819

19-
# Setting cached solution
20+
# Setting/Getting cached solution
2021
set_solved <- function(solution) cached_solution <<- solution
21-
# Getting cached solution
2222
get_solved <- function() cached_solution
2323

2424
# Return list of functions list of functions:
25-
# - `set_matrix` - set the value of the matrix
26-
# - `get_matrix` - get the value of the matrix
27-
# - `set_solved` - set the value of the inverted matrix
28-
# - `get_solved` - get the value of the inverted matrix
25+
# - `get_matrix`/`set_matrix` get/set the value of the original matrix
26+
# - `get_solved`/`set_solved` get/set the value of the inverted matrix
2927

3028
list(
3129
set_matrix = set_matrix, # ...$set_matrix
@@ -51,3 +49,31 @@ cacheSolve <- function(cache_matrix, ...) {
5149
return(solution)
5250
}
5351
}
52+
53+
## But how to check if this works ... and really caches?
54+
## First of all create matrix.
55+
56+
## This small matrix will *just* show you the message...
57+
## There is no difference in sence of time for such small matrix
58+
# m <- matrix(rnorm(16), nrow = 4)
59+
60+
## This will give metioned difference...
61+
# m <- matrix(rnorm(16000000), nrow = 4000)
62+
63+
## Assign vector with function to `mv` variable
64+
# mv <- makeCacheMatrix(m)
65+
66+
## Call cacheSolve() with `mv` as argument
67+
# cacheSolve(mv)
68+
69+
## You will get result with delay:
70+
# ...
71+
# [n, ] -1.0133707610 0.5960284005 -4.543966e-01
72+
# [n+1,] 1.7333438756 0.1842437258 3.645269e-01
73+
74+
## Call it again cacheSolve() with `mv` as argument
75+
## You will get result from cache. Here is output:
76+
# Getting cached matrix
77+
# ...
78+
# [n, ] -1.0133707610 0.5960284005 -4.543966e-01
79+
# [n+1,] 1.7333438756 0.1842437258 3.645269e-01

0 commit comments

Comments
 (0)