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
41 lines (35 loc) · 1.15 KB
/
cachematrix.R
File metadata and controls
41 lines (35 loc) · 1.15 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
## This is Programming Assignment 2 for the R Progrmming Coursera class demonstrating Lexical Scoping
## Function makeCacheMatrix creates a vector of functionss that
## - Set the value of a matrix
## - Gets the value of the matrix
## - Calculates the inverse of said matrix
## - Gets the inverse of said matrix
makeCacheMatrix <- function(X = matrix()) {
M <- NULL
set <- function(Y){
X <<- Y
M <<- NULL
}
get <- function() X
setSolve <- function(solve) M <<- solve
getSolve <- function() M
list(set = set, get = get, setSolve = setSolve, getSolve=getSolve)
}
## Function cacheSolve calcultes the inverse of the matric created with
## makeCacheMatrix. If the inverse has already been calculated, it
## retrives it from cache. Otherwise it calculates it via the setSolve
## function.
## Note : the native R function solve(A) (see help(solve) for more info)
## is used to calculate the inverse of a matrix A
cacheSolve <- function(X, ...) {
## Return a matrix that is the inverse of 'x'
M <- X$getSolve()
if(!is.null(M)){
message("getting cached data")
return (M)
}
data <- X$get()
M <- solve(data, ...)
X$setSolve(M)
M
}