Skip to content

Commit cd03104

Browse files
committed
Implemented caching
Used new R class system
1 parent 7f657dd commit cd03104

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

cachematrix.R

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
1+
## Creating a special object for calculating properties of matrix
2+
## e.g. inverse with caching options
33

4-
## Write a short comment describing this function
4+
## Class for creating matrices with
5+
## ability to cache results of operations
56

6-
makeCacheMatrix <- function(x = matrix()) {
7+
CachedOpsMatrix <- setRefClass("CachedOpsMatrix",
8+
fields = list(
9+
value = "matrix",
10+
inverse = function(){
11+
if(is.null(my.inverse))
12+
my.inverse <<- solve(value)
13+
else message("getting cached data")
14+
my.inverse
15+
}),
16+
methods = list(
17+
initialize = function(...){
18+
my.inverse <<- NULL
19+
callSuper(...)
20+
})
21+
)
22+
23+
## Creating new caching object
724

25+
makeCacheMatrix <- function(x = matrix()) {
26+
CachedOpsMatrix$new(value = x)
827
}
928

1029

11-
## Write a short comment describing this function
30+
## Calculating inverse of matrix
31+
## once it was calculated result is cached
32+
## and returned in any consecutive call
1233

1334
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
35+
x$inverse
1536
}

0 commit comments

Comments
 (0)