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
68 lines (60 loc) · 1.67 KB
/
cachematrix.R
File metadata and controls
68 lines (60 loc) · 1.67 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
## functions do
## makeCacheMatrix
## input: a matrix
## outout: a matrix
## purpose: stores a matrix into cache,
## and retrieves a matrix from cache
## functions:
## set: sets the value
## get: retreives the value of a matrix from memory
## setInverse: sets the function to perform
## getinverse: gets the function to perform
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)
}
## makeCacheMatrix
## input: a function like makeCacheMatrix
## outout: Return a matrix that is the inverse of 'x'
## purpose: Takes in a matrix, checks to see if its cached...
## if it is not cached performs a inverse on hte matrix
## functions:
##
cacheSolve <- function(x, ...) {
m <- x$getinverse()
if(!is.null(m)) {
message("getting cached data")
return(m)
}
##get our data from o
data <- x$get()
##Validation
## Make Sure Matrix exists and is passed in'
if (is.null(data)){
message('please pass in a matrix to invert')
return(m)
}
## Make Sure Matrix is a square or else we cannot calculate
if (ncol(data) != nrow(data))
{
message('in order to inverse a matrix needs to be a square')
return(m)
}
## calculate invers
m <- solve(data, ...)
## store results into a cache
x$setinverse(m)
## return results
m
}