File tree Expand file tree Collapse file tree 1 file changed +35
-6
lines changed Expand file tree Collapse file tree 1 file changed +35
-6
lines changed Original file line number Diff line number Diff line change 1- # # Put comments here that give an overall description of what your
2- # # functions do
1+ # # This piece of code can be used to cache result of solve function to inverse
2+ # # square matrix. Solve funtion is called only once for each matrix.
33
4- # # Write a short comment describing this function
4+ # # Function (object) containing cached inversed matrix
55
66makeCacheMatrix <- function (x = matrix ()) {
7-
7+ cached <- NULL # variable to store cached value
8+ set <- function (y )
9+ {
10+ x <<- y
11+ cached <<- NULL # empty the cache, because matrix is new
12+ }
13+ get <- function ()
14+ {
15+ x
16+ }
17+ setInverted <- function (inverted )
18+ {
19+ cached <<- inverted # sets computed value to cache
20+ }
21+ getInverted <- function ()
22+ {
23+ cached # returns cached value
24+ }
25+ list (set = set , get = get , setInverted = setInverted , getInverted = getInverted )
826}
927
1028
11- # # Write a short comment describing this function
29+ # # This function returns cached result of solve function if it's exists.
30+ # # if not it computes it and set result to cache and then returns
1231
1332cacheSolve <- function (x , ... ) {
14- # # Return a matrix that is the inverse of 'x'
33+ inverted <- x $ getInverted()
34+ if (! is.null(inverted )) # checks if is cached
35+ {
36+ message(" from cache" )
37+ return (inverted )
38+ }
39+
40+ data <- x $ get()
41+ computed <- solve(data , ... ) # if not cached lets compute inversed matrix
42+ x $ setInverted(computed ) # set to cache
43+ computed
1544}
You can’t perform that action at this time.
0 commit comments