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
84 lines (69 loc) · 1.91 KB
/
cachematrix.R
File metadata and controls
84 lines (69 loc) · 1.91 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
73
74
75
76
77
78
79
80
81
82
83
84
## Functions that enable the cache-ing the calculations of the inverse of a
## matrix. In the first run it calculate the inverse of a matrix, if it was
## already calculated, and the data was not changed, it return the cached inverse
#' Matrix pseudo object that is able to cache the inverse of a matrix
#'
#' @param x A matrix that will be used as source data
#'
#' @return List of methods to call [get, set, getInverse, setInverse]
makeCacheMatrix <- function(x = matrix()) {
i <- NULL
set <- function(y){
x <<- y
i <<- NULL
}
get <- function() x
setInverse <- function(inverse) i <<- inverse
getInverse <- function() i
list(set=set, get=get, setInverse=setInverse, getInverse=getInverse)
}
#' Validates that matrix object that it is non-null, numeric and invertible
#'
#' @param data A matrix that is invertible
#'
#' @return logical TRUE (validated) or FALSE (non-valid)
#'
validate <- function(data){
if (is.null(data)){
error("Data is null")
return(FALSE)
}
if (sum(apply(a, c(1,2), is.numeric)) / length(a) != 1){
error("Matrix is not fully numeric, expect numeric matrix")
return(FALSE)
}
if (det(data) == 0){
error("Matrix is not invertable, determinant is 0, returning NA")
return(FALSE)
}
TRUE
}
#' Gets the inverse of a makeCacheMatrix, using cache if possible
#'
#' @param x makeCacheMatrix closure with an invertible matrix in it
#'
#' @param ... aditional parameters passed to solve
#'
#' @return Inverse of x
#'
#' @example
#' x <- matrix(c(5,1,0,
#' 3,-1,2,
#' 4,0,-1), nrow=3, byrow=TRUE)
#' mat <- makeCacheMatric(x)
#' cacheSolve(mat)
cacheSolve <- function(x, ...) {
inv <- x$getInverse()
if (!is.null(inv)){
message("using cached data")
return(inv)
}
data <- x$get()
## validate data
if (!validate(data)) {
return(NA)
}
inv <- solve(data, ...)
x$setInverse(inv)
inv
}