File tree 1 file changed +30
-4
lines changed
1 file changed +30
-4
lines changed Original file line number Diff line number Diff line change 2
2
# # functions do
3
3
4
4
# # 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
6
7
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 )
8
19
}
9
20
10
21
11
22
# # 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
13
25
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
15
41
}
You can’t perform that action at this time.
0 commit comments