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
43 lines (34 loc) · 1002 Bytes
/
cachematrix.R
File metadata and controls
43 lines (34 loc) · 1002 Bytes
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
## makeCacheMatrix is a constructor for matrix class with cached inverse method.
## cacheSolve is an API for getting the cached value of matrix inverse.
## Matrix class with cached inverse method
## Methods:
## setMatrix(M) - sets the matrix as M
## getMatrix() - retrives the previously set matrix
## inverse() - calculate the matrix inverse
makeCacheMatrix <- function(x = matrix()) {
# Not sure that I could change variable name in this assigment.
cinverse <- matrix()
setMatrix <- function(m) {
x <<- m
cinverse <<- matrix()
}
getMatrix <- function() x
inverse <- function(){
if(identical(cinverse, matrix()))
{
# Calculating matrix inverse...
cinverse <<- solve(x)
return(cinverse)
}
else
{
# Matrix inverse is cashed
return(cinverse)
}
}
list(setMatrix = setMatrix, getMatrix = getMatrix, inverse = inverse)
}
## API for makeCacheMatrix$inverse
cacheSolve <- function(x, ...) {
x$inverse()
}