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
60 lines (53 loc) · 1.89 KB
/
cachematrix.R
File metadata and controls
60 lines (53 loc) · 1.89 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
##This file contains a set of functions to cache the computation necessary to INVERT a matrix,
##If you use these functions you will only have to make the computation once, and store it for future use.
##The code bellow expects the given matrix inverse to be COMPUTABLE, if for any reason you pass
##a matrix with no inverse (e.g. linear dependent columns) the calculation will throw an Error.
##We call the list() returned by makeCacheMatrix, the cacheMatrix object, or cm
##Usage
##mx = matrix(c(4,3,3,2), nrow=2, ncol=2)
##cm <-makeCacheMatrix(mx)
##cacheSolve(cm)
## [,1] [,2]
##[1,] -2 3
##[2,] 3 -4
##cm$get() %*% cm$getinverse()
## [,1] [,2]
##[1,] 1 0
##[2,] 0
##cm$get() %*% cacheSolve(cm)
##getting cached data
##[,1] [,2]
##[1,] 1 0
##[2,] 0 1
##makeCacheMatrix returns an "object" cachedMatrix that encapsulates a matrix into a clojure
##cachedMatrix$get -> returns the said matrix
##cachedMatrix$set(M) -> sets the internal matrix to a new matrix M
##cachedMatrix$setinverse() ->
##cachedMatrix$getinverse() -> retrieves the inverted matrix, or NULL if calculation not performed yet
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(m) {
x <<- m
inv <<- NULL
}
get <- function() x
setinverse <- function(inverse_to_store) inv <<- inverse_to_store
getinverse <- function() inv
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
##cacheSolve retrieves the inverse of a matrix given an "object" created using the makeChacheMatrix function.
##if the inverse already exists, it returns it, if it does not yet exists, it computes it only once and stores
##it on the cacheMatrix "object"
cacheSolve <- function(x, ...) {
inv <- x$getinverse()
if(!is.null(inv)) {
message("getting cached data")
return(inv)
}
data <- x$get()
inv <- solve(data)
x$setinverse(inv)
inv
}