Skip to content

Commit 1232c0f

Browse files
committed
functions to calculate and cache inverse of a matrix
1 parent 7f657dd commit 1232c0f

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

cachematrix.R

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,42 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
1+
## Funtions to cache the inverse of matrix.
2+
## Inverse is calculated and stored the first time cacheSolve is called.
3+
## Subsequent calls return the stored inverse value
4+
## Usage instruction
5+
## Call makeCacheMatrix passing in matrix to be inversed
6+
## Call cacheSolve passing in the value returned by makeCacheMatrix
57

8+
## Creates object that will cache inverse of the matrix. Pass resulting object
9+
## to cacheSolve as parameters
10+
## parameter: matrix to be inversed
611
makeCacheMatrix <- function(x = matrix()) {
7-
12+
s <- NULL
13+
set <- function(y) {
14+
x <<- y
15+
s <<- NULL
16+
}
17+
get <- function() x
18+
setsolve <- function(solve) s <<- solve
19+
getsolve <- function() s
20+
list(set = set, get = get,
21+
setsolve = setsolve,
22+
getsolve = getsolve)
23+
invisible()
824
}
925

1026

11-
## Write a short comment describing this function
12-
27+
## Calculates inverse of a matrix and stores results. Subsequent calls return
28+
## stored value
29+
## Parameter: pass in object returned by makeCacheMatrix
30+
## Return: inverse of passed matrix
1331
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
32+
s <- x$getsolve()
33+
if(!is.null(s)) {
34+
message("getting cached data")
35+
return(s)
36+
}
37+
data <- x$get()
38+
s <- solve(data, ...)
39+
x$setsolve(s)
40+
## Returns matrix that is the inverse of 'x'
41+
s
1542
}

0 commit comments

Comments
 (0)