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
58 lines (56 loc) · 2.29 KB
/
cachematrix.R
File metadata and controls
58 lines (56 loc) · 2.29 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
## These functions store a matrix and calculate its inverse matrix, keeping
## the result stored in the cache, so it's not necessary to recalculate the
## result.
## Function makeCacheMatrix() : this is a object that will keep the values of
## the matrix and its inverse. This object is controlled by its inner
## functions: set(), get(), setinverse(), getinverse(), reset()
## TIP: First assign this function to a variable then call the inner functions.
## v <- makeCacheMatrix(); v$set(matrix(rnorm(1:9), nrow = 3)); v$get()
makeCacheMatrix <- function(mtx = matrix()) {
## when declaring the new matrix, reset the variables
invs <- NULL
## define the inner function set() that stores the matrix and reset the
## inverse matrix
set <- function(y) {
mtx <<- y
invs <<- NULL
}
## define the inner function get() that returns the original matrix
get <- function() mtx
## define the inner function setinverse() that stores the value of the
## inverse matrix
setinverse <- function(inverse) invs <<- inverse
## define the inner function getinverse() that returns the inverse matrix
getinverse <- function() invs
## this is a new function that resets the values stored in this object
reset <- function() {
mtx <<- NULL
invs <<- NULL
}
## list the inner functions of this function
list(set = set, get = get, setinverse = setinverse, getinverse = getinverse,
reset = reset)
}
## Function cacheSolve() : this is a function which has the makeCacheMatrix object
## as first argument, and will calculate the inverse matrix stored in this
## object. This function will also keep the calculated matrix in the object for
## future reference.
## TIP: Inform the variable assigned for the first function in this function call
## cacheSolve(v)
cacheSolve <- function(x, ...) {
## get the content of inverse matrix stored in the object
invs <- x$getinverse()
## if the inverse matrix was already calculated then return it to the console
if(!is.null(invs)) {
message("getting cached data")
return(invs)
}
## if there is no inverse matrix yet, get the original matrix...
mtx <- x$get()
## and calculate it with the solve function, ...
invs <- solve(mtx, ...)
## store the inverse matrix in the object...
x$setinverse(invs)
## and return the result
invs
}