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
158 lines (112 loc) · 3.77 KB
/
cachematrix.R
File metadata and controls
158 lines (112 loc) · 3.77 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
## Put comments here that give an overall description of what your
## functions do
## This code is result of assignment in Proframming with R course in Coursera
## Programming Assignment 2: Lexical Scoping
## Original idea of how to use cachine is from:
## https://github.com/rdpeng/ProgrammingAssignment2 by Roger D. Peng
## Functions help to cache results of previous results for given input.
## This function wraps matrix and its inversion into kindda of object
## which uses lexical scoping to provide behaviour similar to objects in OOP
## programming.
## Function also checks if matrix is square-sized and if it has determinant=0
## Reread and resubmitted 2015-01-14
makeCacheMatrix <- function(x = matrix()) {
m <- NULL
# Function within function, sets input value and resets cached value
set <- function(y) {
# Assign to parent environment variable
x <<- y
m <<- NULL
}
# Function within function, returns argument
get <- function() x
# Function to set inversed matrix of original input
setInverse <- function(inverse) m <<- inverse
getInverse <- function() m
getInverseCalculate <- function(...) {
if (!is.null(m)) {
message("getting cached inverse")
return(m)
}
if (ncol(x) != nrow(x) ) {
warning("Number of Rows and columns of input are not equal")
m <<- NA
return(m)
}
if (det(x) == 0) {
warning("Matrix is singular and have no invertible matrix")
m <<- NA
return(m)
}
m <<- solve(x, ...)
m
}
# set hooks
list(set = set
, get = get
, setInverse = setInverse
, getInverse = getInverse
, getInverseCalculate = getInverseCalculate
)
}
## This function takes input of type "makeCacheMatrix" and returns its
## matrix inversed. This function is presented as is to comply with assignement
## requirement "This function computes the inverse of the special "matrix"
## returned by makeCacheMatrix above. If the inverse has already been
## calculated (and the matrix has not changed), then the cachesolve
## should retrieve the inverse from the cache."
cacheSolve <- function(x, ...) {
ref <- x
if (is.array(x)) {
t <- x
x <- makeCacheMatrix(t)
ref <- x
}
m <- ref$getInverse()
if(!is.null(m)) {
message("getting cached data")
return(m)
}
data <- ref$get()
m <- solve(data, ...)
ref$setInverse(m)
m
## Return a matrix that is the inverse of 'x'
}
## This function takes input of type "makeCacheMatrix" and returns its
## matrix inversed, but calculation is done in makeCacheMatrix
cacheSolve2 <- function(x, ...) {
ref <- x
if (is.array(x)) {
t <- x
x <- makeCacheMatrix(t)
ref <- x
}
m <- ref$getInverseCalculate()
m
## Return a matrix that is the inverse of 'x'
}
## Test Calculation:
## > rm(t)
## > t <- makeCacheMatrix(matrix(c(1,2,3,1,4,5,6,5,6), c(3,3)))
## > cacheSolve(t)
## [,1] [,2] [,3]
## [1,] 0.1 -2.4 1.9
## [2,] -0.3 1.2 -0.7
## [3,] 0.2 0.2 -0.2
## > cacheSolve(t)
## getting cached data
## [,1] [,2] [,3]
## [1,] 0.1 -2.4 1.9
## [2,] -0.3 1.2 -0.7
## [3,] 0.2 0.2 -0.2
## Test cache usage
##> t <- makeCacheMatrix(matrix(rnorm(1000*1000), c(1000,1000)))
## > system.time(cacheSolve(t))
## user system elapsed
## 1.48 0.00 1.48
## > system.time(cacheSolve(t))
## getting cached data
## user system elapsed
## 0 0 0
##> rm(t)