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
58 lines (49 loc) · 1.88 KB
/
cachematrix.R
File metadata and controls
58 lines (49 loc) · 1.88 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
## Put comments here that give an overall description of what your
## functions do
## Write a short comment describing this function:
## makeCacheMatrix first checks whether the input matrix is square
## and if its determinant is different than 0 if either of this conditions
## are not mat it prints a hint and exits. In case the matrix is square and
## its determinant is different than 0 (an inverse of the matrix exists),
## makeCacheMatrix creates a special matrix, a list, which components
## are functions that:
## set the value of the matrix
## get the value of the matrix
## set the value of the matrix inverse
## get the value of the matrix inverse
makeCacheMatrix <- function(x = matrix()) {
if (dim(x)[1] != dim(x)[2]) {
print ("Please provide a square matrix as input")
return
} else if (det(x)==0) {
print ("Please provide a square matrix")
print ("with determinant != than 0 as input")
return
}
inv <- matrix(data=NA,nrow=dim(x)[1],ncol=dim(x)[1])
set <- function(y){
x <<- y
inv <<- matrix(data=NA,nrow=dim(y)[1],ncol=dim(y)[1])
}
get <- function() x
setinv <- function(inverse) inv <<- inverse
getinv <- function() inv
list(set =set, get = get, setinv = setinv, getinv = getinv)
}
## Write a short comment describing this function
## cacheSolve takes the special matrix object created by makeCacheMatrix
## and checks whether an inverse for the matrix passes as argument to
## makeCacheMatrix already exists (checks whether the matrix does not
## contain NAs) if not it computes the inverse and sets in the cache
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inv <- x$getinv()
if (!anyNA(inv)) {
message("getting cached data")
return(inv)
}
data <- x$get()
inv <- solve(data,...)
x$setinv(inv)
inv
}