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
92 lines (82 loc) · 2.73 KB
/
cachematrix.R
File metadata and controls
92 lines (82 loc) · 2.73 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#########################################################
# Coursera - R Programming Course
# Week 3 - Peer Review Assignment
#
# Objective:
# Cache the result of a matrix inversion into
# an appropriate object, since a matrix inversion
# is a costly operation.
#
# The presented source code will be subjected
# to a peer-review.
#########################################################
#########################################################
# makeCacheMatrix(x):
# - Arguments:
# * x: An invertible matrix. Otherwise, an empty
# matrix will be assumed.
#
# - Usage:
# returns an object that contains an invertible
# matrix, x, and the the respective cached
# result from the inversion operation.
#
#########################################################
makeCacheMatrix <- function(x = matrix()) {
#Sets the initial cached value
cache <- NULL
#Definition of the set function
#i.e. when a new matrix value is stored in
#the returned object
set <- function(y) {
#substitutes the value
x <<- y
#cleans the cached result
cache <<- NULL
}
#Function that retrieves the invertible matrix
get <- function() x
#Function that sets the matrix inversion result
#in cache
setinverse <- function(inverted) cache <<- inverted
#Method that retrieves the cache
getinverse <- function() cache
#Returns the object that contains the
#appropriate function calls in a list.
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
#########################################################
# cacheSolve(x, ...) :
# - Arguments
# * x: an object containing an invertible matrix
# and the respective cache from the matrix
# inversion (see makeCacheMatrix).
# * ...: additional parameters provided to the
# matrix inversion function (see solve).
#
# - Usage:
# Given an object, x, from makeCacheMatrix(x),
# computes the matrix inversion when x does not
# have the result in cache.
# Otherwise, it returns the cached result.
#########################################################
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
inverted <- x$getinverse()
#If the value is not null, then we can retrieved
#the cached result
if(!is.null(inverted)) {
message("getting cached data")
return(inverted)
}
#otherwise, it retrieves the invertible matrix
data <- x$get()
#computes the matrix inversion
inverted <- solve(data, ...)
#sets the value in the cache
x$setinverse(inverted)
#and returns the matrix inversion result
inverted
}