Skip to content

Commit 2e19f2f

Browse files
committed
fill functions with code
1 parent 7f657dd commit 2e19f2f

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

cachematrix.R

+30-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,40 @@
22
## functions do
33

44
## Write a short comment describing this function
5-
5+
## makeCacheMatrix constructs an object for caching matrix inverse
6+
## it has methods for getting and setting source matrix and cached inverse value
67
makeCacheMatrix <- function(x = matrix()) {
7-
8+
inv <- NULL
9+
set <- function(y) {
10+
x <<- y
11+
inv <<- NULL
12+
}
13+
get <- function() x
14+
setinv <- function(inverse) inv <<- inverse
15+
getinv <- function() inv
16+
list(set = set, get = get,
17+
setinv = setinv,
18+
getinv = getinv)
819
}
920

1021

1122
## Write a short comment describing this function
12-
23+
## cacheSolve is a function that gets matrix inverse from cache if already solved
24+
## and if not it solves inverse and stores value to cache
1325
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
26+
##check for matrix to be square
27+
data <- x$get()
28+
if (nrow(data) != ncol(data)){
29+
message("Error: Matrix should be square")
30+
}
31+
32+
## Return a matrix that is the inverse of 'x'
33+
inv <- x$getinv()
34+
if(!is.null(inv)) {
35+
message("getting cached data")
36+
return(inv)
37+
}
38+
inv <- solve(data, ...)
39+
x$setinv(inv)
40+
inv
1541
}

0 commit comments

Comments
 (0)