Skip to content

Commit e506394

Browse files
committed
Applying the functions cacheSolve and makeCacheMatrix
This is the first commit
1 parent 7f657dd commit e506394

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

cachematrix.R

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,44 @@
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.
37

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.
511

612
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)
824
}
925

1026

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.
1231

1332
cacheSolve <- function(x, ...) {
33+
1434
## 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
1544
}

0 commit comments

Comments
 (0)