File tree 1 file changed +40
-6
lines changed
1 file changed +40
-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
3
1
4
- # # Write a short comment describing this function
5
2
6
- makeCacheMatrix <- function (x = matrix ()) {
3
+ # The first function,makeCacheMatrix caches the inverse of input matrix and it
4
+ # is a list of functions for following tasks:
5
+ # 1.set the value of matrix
6
+ # 2.get the value of matrix
7
+ # 3.set the value of inverse
8
+ # 4.get the value of inverse
9
+
10
+ # The second function cacheSolve computes the value of inverse of matrix
11
+ # and if its already computed then it returns the cached value.
7
12
13
+
14
+ # #For caching the inverse matrix
15
+ makeCacheMatrix <- function (x = matrix ()) {
16
+ if (! is.matrix(x ))
17
+ stop(" Input should be a matrix." )
18
+ x_inv <- NULL
19
+ set <- function (y )
20
+ {
21
+ x <<- y
22
+ x_inv <- NULL
23
+ }
24
+
25
+ get <- function () x
26
+ set_inv <- function (invMat ) x_inv <<- invMat
27
+ get_inv <- function () x_inv
28
+ list (set = set ,
29
+ get = get ,
30
+ set_inv = set_inv ,
31
+ get_inv = get_inv )
8
32
}
9
33
10
34
11
- # # Write a short comment describing this function
35
+ # # For computing the inverse of matrix.
12
36
13
37
cacheSolve <- function (x , ... ) {
14
- # # Return a matrix that is the inverse of 'x'
38
+ x_inv <- x $ get_inv()
39
+ if (! is.null(x_inv ))
40
+ {
41
+ message(" getting cached value" )
42
+ return (x_inv )
43
+ }
44
+ mat <- x $ get()
45
+ invMat <- solve(mat )
46
+ x $ set_inv(invMat )
47
+ invMat
48
+
15
49
}
You can’t perform that action at this time.
0 commit comments