Skip to content

Commit 171a63a

Browse files
committed
inverse solution for the matrix is solved
1 parent 7f657dd commit 171a63a

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

cachematrix.R

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,46 @@
44
## Write a short comment describing this function
55

66
makeCacheMatrix <- function(x = matrix()) {
7-
7+
## Symbol m holds the calcuated inverse of a given matrix
8+
m <- NULL
9+
## function set is used to refresh the matrix ( x ) and the inverse ( m )
10+
set <- function(y) {
11+
x <<- y
12+
m <<- NULL
13+
}
14+
15+
## function get is used to retrieve the matrix
16+
get <- function() x
17+
18+
## setinverse is used to set the inverse of a matrix into the symbol m
19+
setinverse <- function(inverse) m <<- inverse
20+
21+
## getinverse is used to get the inverse matrix assigned to the symbol m by the setinverse function
22+
getinverse <- function() m
23+
24+
## list will be the output when you create an object for makeCacheMatrix.
25+
## This list contains the alias names for the private fucntions defined within the makeCacheMatrix. So this alias will be used to invoke the functions by using the object of makeCacheMatrix.
26+
list(set = set, get = get,
27+
setinverse = setinverse,
28+
getinverse = getinverse)
829
}
930

1031

1132
## Write a short comment describing this function
1233

34+
## cacheSolve function is accepts the object of makeCacheMatrix and invoke the getInverse method. Incase already the inverse has been calculated and cached in the object then retrieve the cached value.
35+
## if thre is no inverse has been already calculated then get the matrix by using the get method and calculate the inverse by using the solve function and store the inverse value into the object by caling the setinverse function
36+
1337
cacheSolve <- function(x, ...) {
1438
## Return a matrix that is the inverse of 'x'
39+
40+
m <- x$getinverse()
41+
if(!is.null(m)) {
42+
message("getting cached data")
43+
return(m)
44+
}
45+
data <- x$get()
46+
m <- solve(data, ...)
47+
x$setinverse(m)
48+
m
1549
}

0 commit comments

Comments
 (0)