-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.r
46 lines (40 loc) · 1.14 KB
/
cachematrix.r
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
## The functions do nothing else than the example
## makeVector and cachemean functions (which actually also work for matrices
## the only difference is that all "mean" have been replaced with "inverse"
## and one case of "solve" (ln 35)
## caching does not work directly on matrices
## a special cacheMatrix is needed for that purpose
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
set <- function(y) {
x <<- y
m <<- NULL
}
get <- function() x
setinverse <- function(inverse) m <<- inverse
getinverse <- function() m
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## cacheSolve, when called upon a cacheMatrix first,
## it creates the cached invert matrix which is retrieved
## at every further call
cacheSolve <- function(x, ...) {
m <- x$getinverse()
if(!is.null(m)) {
message("getting cached data")
return(m)
}
data <- x$get()
m <- solve(data, ...)
x$setinverse(m)
return(m)
}
## used for testing
## y <- matrix(1:4,2)
## solve(y)
## cy <- makeCacheMatrix(y)
## print(cy)
## cacheSolve(cy)
## cacheSolve(cy)