1
1
# 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.
3
3
# Please Enjoy R and this MOOC. If you are interested in detailed information
4
4
# please read detailed comment at the end of code.
5
5
# Thanks for your review!
6
6
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
9
9
10
10
# Initializing variable supposed for cached solution
11
11
cached_solution <- NULL
12
12
13
+ # Setting/Getting original matrix
13
14
set_matrix <- function (arg ) {
14
- local_matrix <<- arg
15
+ original_matrix <<- arg
15
16
cached_solution <<- NULL
16
17
}
17
- get_matrix <- function () local_matrix
18
+ get_matrix <- function () original_matrix
18
19
19
- # Setting cached solution
20
+ # Setting/Getting cached solution
20
21
set_solved <- function (solution ) cached_solution <<- solution
21
- # Getting cached solution
22
22
get_solved <- function () cached_solution
23
23
24
24
# 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
29
27
30
28
list (
31
29
set_matrix = set_matrix , # ...$set_matrix
@@ -51,3 +49,31 @@ cacheSolve <- function(cache_matrix, ...) {
51
49
return (solution )
52
50
}
53
51
}
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