forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachematrix.R
More file actions
72 lines (58 loc) · 2.79 KB
/
Copy pathcachematrix.R
File metadata and controls
72 lines (58 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
## This file contains two functions makeCacheMatrix and cacheSolve.
##
## The makeCacheMatrix function creates a special matrix, which allows the caching of the inverse of the matrix.
## It makes heavy use of the <<- operator. It causes a search to made through parent environments for an existing definition of the variable being assigned.
## For more information on the <<- operator enter ?assignOps into your R console.
##
## The cacheSolve function uses the matrices created by makeCacheMatrix and returns the inverse of the matrix.
## The cacheSolve function first checks if the matrix inverse has been cached and if so returns the chached inverse.
## If no cached inverse could be found (inverse equals NULL) the inverse of the matrix is computed.
## The cacheSolve does not checks if the matrix is invertable (as required by the assignment).
##
## Example:
## create a new random 3x3 matrix with cachable inverse, compute inverse, compute matrix %*% inverseOfMatrix to get the identity matrix
## theMatrix <- makeCacheMatrix(matrix(runif(9), 3, 3));
## cacheSolve(theMatrix);
## theMatrix$getInverse() %*% theMatrix$get();
## The function creates a special cachematrix,
## which allows the caching of the inverse of the matrix to avoid repeated computation.
## It is actually returns a list of four functions:
## set, get, setInverse, getInverse.
makeCacheMatrix <- function(x = matrix()) {
matrixInverse <- NULL
## this function sets m to y and the matrix inverse to NULL, this is the initial state of the matrix
set <- function(y)
{
x <<- y
matrixInverse <<- NULL
}
# returns the value of the matrix
get <- function() x
## allows to set the matrix inverse
setInverse <- function(inverse) matrixInverse <<- inverse
## returns the inverse of a matrix
getInverse <- function() matrixInverse
## combines the previously defined functions into a list
list(set = set, get = get,
setInverse = setInverse,
getInverse = getInverse)
}
## The function uses matrices created by makeCacheMatrix function.
## It first checks if a cached inverse is available (not NULL).
## If it is, the cached inverse is returned. If the cached inverse is NULL
## then it is computed by the solve() function, cached and returned.
cacheSolve <- function(x, ...) {
## Check if the inverse of the matrix inverse has been priviously computed. If so return the cached matrix inverse.
matrixInverse <- x$getInverse()
if(!is.null(matrixInverse)) {
message("getting cached data")
return(matrixInverse)
}
## compute inverse of the matrix
data <- x$get()
matrixInverse <- solve(data)
## cache matrix inverse
x$setInverse(matrixInverse)
## Return a matrix that is the inverse of 'x'
matrixInverse
}