forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
55 lines (48 loc) · 1.9 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
## Author: Sodbo Sharapov
## date: 2016.01.17
## makeCacheMatrix - function to load input matrix to an object that can store
## inverse matrix.
## Input - object contains square matrix with numeric values
## Output - a list of functions and their environment, which stores and operates with
## input matrix and it's inverse. By default inverse matrix is NULL object.
## It can be computed and cached using cacheSolve function.
## Example of usage:
## a <- makeCacheMatrix(matrix(rnorm(100),ncol=10))
## a$get()
makeCacheMatrix <- function(x = matrix()) {
inverse.matrix <- NULL
set <- function(y) {
x <<- y
inverse.matrix <<- NULL
}
get <- function() x
setinverse <- function(Solve) inverse.matrix <<- Solve
getinverse <- function() inverse.matrix
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## cacheSolve - function which estimate and cache inverse matrix.
## Input is an object produced by makeCacheMatrix function.
## Output - square matrix, which is inverse to matrix, stored in input object.
## After processing this function inverse matrix will be cached in input object.
## Afterwards you don't need to recompute inverse matrix. You can get it by
## calling x$getinverse() function.
## Example of usage:
## a <- makeCacheMatrix(matrix(rnorm(100),ncol=10))
## b <- cacheSolve(a)
## a$getinverse()
## Chech whether inversed matrix is really inverse matrix of a :)
## a$get() %*% a$getinverse()
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inverse.matrix <- x$getinverse()
if(!is.null(inverse.matrix)) {
message("getting cached data")
return(inverse.matrix)
}
data <- x$get()
inverse.matrix <- solve(data, ...)
x$setinverse(inverse.matrix)
inverse.matrix
}