|
1 |
| -## Put comments here that give an overall description of what your |
2 |
| -## functions do |
| 1 | +## Calculating the inverse of a matrix is very expensive operation, the following functions |
| 2 | +## were created to solve the inverse of the matrix and to cache it in a way |
| 3 | +## that is easy to be retreaved by any function. The inverse will be calculated only once |
| 4 | +## and saved inside the object function makeCacheMatrix. |
| 5 | +## Kindly note that we assume in the following code that the matrix assigned is always |
| 6 | +## invertible. |
3 | 7 |
|
4 |
| -## Write a short comment describing this function |
| 8 | +## This function creates a matrix which is a list containing function that |
| 9 | +##sets the value of the matrix, get the value of the matrix, set the |
| 10 | +##inverse of the matrix and gets the inverse of the matrix. |
5 | 11 |
|
6 | 12 | makeCacheMatrix <- function(x = matrix()) {
|
7 |
| - |
| 13 | + xinverse <- NULL |
| 14 | + set <- function(y){ |
| 15 | + x <<- y |
| 16 | + xinverse <- NULL |
| 17 | + } |
| 18 | + get <- function() x |
| 19 | + setinverse <- function(matinverse) xinverse <<- matinverse |
| 20 | + getinverse <- function() xinverse |
| 21 | + list (set = set, get=get, |
| 22 | + setinverse = setinverse, |
| 23 | + getinverse = getinverse) |
8 | 24 | }
|
9 | 25 |
|
10 | 26 |
|
11 |
| -## Write a short comment describing this function |
| 27 | +## This function calculates the inverse of the matrix created in "makeCacheMatrix", |
| 28 | +## it checks if the inverse of the matrix has been created, |
| 29 | +## if yes it will return the value, otherwise, it will create it and sets the inverse |
| 30 | +## in the cache via setinverse function. |
12 | 31 |
|
13 | 32 | cacheSolve <- function(x, ...) {
|
| 33 | + |
14 | 34 | ## Return a matrix that is the inverse of 'x'
|
| 35 | + xinverse <- x$getinverse() |
| 36 | + if (!is.null(xinverse)){ |
| 37 | + message("getting cached matrix inverse") |
| 38 | + return(xinverse) |
| 39 | + } |
| 40 | + data <- x$get() |
| 41 | + xinverse <- solve(data, ...) |
| 42 | + x$setinverse(xinverse) |
| 43 | + xinverse |
15 | 44 | }
|
0 commit comments