-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachematrix.R
44 lines (44 loc) · 1019 Bytes
/
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
# func 1
makeCacheMatrix <- function(X = matrix()) {
inverse <- NULL
set <- function(Y){
X <<- Y
inverse <<- NULL
}
get <- function() X
setinverse <- function(Inverse) inverse <<- Inverse
getinverse <- function() inverse
list(set=set,get=get,setinverse=setinverse,getinverse=getinverse)
}
#func 2
cacheSolve <- function(X, ...)
{
if(require("corpcor")){
print("corpcor package is there")
} else {
print("if not trying to install corpcor")
install.packages("corpcor")
if(require(corpcor)){
print("corpcor installed and loaded")
} else {
stop("not able to install corpcor package")
}
}
inverse <- X$getinverse()
if(!is.null(inverse))
message("matrix is in memory so take it from memory")
return(inverse)
}
message("inverse is not in memory so the inverse is to be computed")
data <- X$get()
inverse <- pseudoinverse(data, ...)
X$setinverse(inverse)
inverse
}
# example
b <- matrix(rpois(25,2), nrow = 5)
cd <- makeCacheMatrix(b)
cd$get()
cacheSolve(cd)
cacheSolve(cd)
invb <- cacheSolve(cd)