Skip to content

Commit e89020d

Browse files
committed
Programmin assignment 2, implement cacheSolve function to calculate inverted matrix
1 parent 7f657dd commit e89020d

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+
## Introduce `makeCacheMatrix` constructor function and `cacheSolve` function in
2+
## order to optimize getting of inverted matrix by caching already calculated results.
33

4-
## Write a short comment describing this function
4+
## Constuctor function for matrix object
5+
## with stores inverted version of it.
56

67
makeCacheMatrix <- function(x = matrix()) {
7-
8+
inverted <- NULL;
9+
set <- function(y) {
10+
x <<- y
11+
inverted <<- NULL
12+
}
13+
get <- function() x
14+
setinverted <- function(invertedValud) inverted <<- invertedValud
15+
getinverted <- function() inverted
16+
list(set = set, get = get,
17+
setinverted = setinverted,
18+
getinverted = getinverted)
819
}
920

1021

11-
## Write a short comment describing this function
22+
## Returns inverted matrix either from the cached version
23+
## or by explicitely calculating it with help of `solve` function.
24+
## x has to be `makeCacheMatrix` object.
1225

1326
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
27+
inverted <- x$getinverted()
28+
if(!is.null(inverted)) {
29+
message("getting cached data")
30+
return(inverted)
31+
}
32+
data <- x$get()
33+
inverted <- solve(data, ...)
34+
x$setinverted(inverted)
35+
inverted
1536
}

0 commit comments

Comments
 (0)