Skip to content

Commit f326b51

Browse files
committed
added functions body
1 parent e4eed41 commit f326b51

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

cachematrix.R

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,44 @@
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

66
makeCacheMatrix <- 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

1332
cacheSolve <- 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
}

0 commit comments

Comments
 (0)