Skip to content

Commit cd5e4e5

Browse files
committed
Uploading my solution
1 parent 7f657dd commit cd5e4e5

File tree

1 file changed

+141
-7
lines changed

1 file changed

+141
-7
lines changed

cachematrix.R

Lines changed: 141 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,149 @@
1-
## Put comments here that give an overall description of what your
2-
## functions do
3-
4-
## Write a short comment describing this function
1+
###############################################################################
2+
#
3+
# Copyright (C) 2015, Diego Rabatone Oliveira <diraol(at)diraol(dot)eng(dot)br>
4+
#
5+
# This file is part of Radar Parlamentar.
6+
#
7+
# Radar Parlamentar is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU Affero General Public License as published by
9+
# the Free Software Foundation, either version 3 of the License, or
10+
# (at your option) any later version.
11+
#
12+
# Radar Parlamentar is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU General Public License
18+
# along with it. If not, see <http://www.gnu.org/licenses/>.
19+
#
20+
#
21+
# Coursera R Programming Course <rprog-015>
22+
# Programming Assignment 2: Lexical Scoping
23+
#
24+
# This Program creates an object that stores a Matrix and it's inverse matrix
25+
# in a cached way (if there is no inverse matrix it will calculate and save,
26+
# if there is a cached inverse matrix than it will return the cached matrix)
27+
#
28+
###############################################################################
529

30+
###############################################################################
31+
# This function stores the matrix and the inverse matrix. It has setter and #
32+
# getter methods for both matrix and inverse matrix. #
33+
# It set's the inverse matrix as NULL by default when a new matrix is set. #
34+
# And it doesn't allow changes on specific positions of the matrix. #
35+
###############################################################################
636
makeCacheMatrix <- function(x = matrix()) {
37+
# inv = inverse from x matrix
38+
inv <- NULL
739

8-
}
40+
set <- function(y) {
41+
x <<- y
42+
inv <<- NULL
43+
}
944

45+
get <- function() x
46+
setinverse <- function(inverse) inv <<- inverse
47+
getinverse <- function() inv
1048

11-
## Write a short comment describing this function
49+
list(set = set, get = get,
50+
setinverse = setinverse,
51+
getinverse = getinverse
52+
)
53+
}
1254

55+
###############################################################################
56+
# This function verify if the matrix has a cached version of the inverted #
57+
# matrix. If it has, then the function returns the cached version. If not, #
58+
# then it calculates the inverted matrix, caches it and then returns it. #
59+
###############################################################################
1360
cacheSolve <- function(x, ...) {
14-
## Return a matrix that is the inverse of 'x'
61+
inv <- x$getinverse()
62+
63+
if(!is.null(inv)) {
64+
message("getting cached data")
65+
return(inv)
66+
}
67+
68+
data <- x$get()
69+
inv <- solve(data)
70+
x$setinverse(inv)
71+
72+
#returns the inverted matrix
73+
inv
1574
}
75+
76+
################################################################################
77+
# Testing outputs #
78+
#
79+
# > set.seed(42)
80+
# > mymat <- matrix(rnorm(25),5,5)
81+
# > mymat
82+
# [,1] [,2] [,3] [,4] [,5]
83+
# [1,] 1.3709584 -0.10612452 1.3048697 0.6359504 -0.3066386
84+
# [2,] -0.5646982 1.51152200 2.2866454 -0.2842529 -1.7813084
85+
# [3,] 0.3631284 -0.09465904 -1.3888607 -2.6564554 -0.1719174
86+
# [4,] 0.6328626 2.01842371 -0.2787888 -2.4404669 1.2146747
87+
# [5,] 0.4042683 -0.06271410 -0.1333213 1.3201133 1.8951935
88+
#
89+
# > solve(mymat)
90+
# [,1] [,2] [,3] [,4] [,5]
91+
# [1,] 0.68183594 -1.078746 -1.142347 0.7439971 -1.484073
92+
# [2,] 0.09613058 -1.419227 -2.292037 1.3808827 -2.411346
93+
# [3,] -0.06909654 1.819392 2.192748 -1.1807929 2.654590
94+
# [4,] 0.14181901 -1.118677 -1.694580 0.7147536 -1.640329
95+
# [5,] -0.24590901 1.090359 1.502458 -0.6939420 2.093756
96+
#
97+
# > solve(solve(mymat))
98+
# [,1] [,2] [,3] [,4] [,5]
99+
# [1,] 1.3709584 -0.10612452 1.3048697 0.6359504 -0.3066386
100+
# [2,] -0.5646982 1.51152200 2.2866454 -0.2842529 -1.7813084
101+
# [3,] 0.3631284 -0.09465904 -1.3888607 -2.6564554 -0.1719174
102+
# [4,] 0.6328626 2.01842371 -0.2787888 -2.4404669 1.2146747
103+
# [5,] 0.4042683 -0.06271410 -0.1333213 1.3201133 1.8951935
104+
#
105+
# > cach1 <- makeCacheMatrix(mymat)
106+
#
107+
# > cach1$get()
108+
# [,1] [,2] [,3] [,4] [,5]
109+
# [1,] 1.3709584 -0.10612452 1.3048697 0.6359504 -0.3066386
110+
# [2,] -0.5646982 1.51152200 2.2866454 -0.2842529 -1.7813084
111+
# [3,] 0.3631284 -0.09465904 -1.3888607 -2.6564554 -0.1719174
112+
# [4,] 0.6328626 2.01842371 -0.2787888 -2.4404669 1.2146747
113+
# [5,] 0.4042683 -0.06271410 -0.1333213 1.3201133 1.8951935
114+
#
115+
# > cach1$getinverse()
116+
# NULL
117+
#
118+
# > cacheSolve(cach1)
119+
# [,1] [,2] [,3] [,4] [,5]
120+
# [1,] 0.68183594 -1.078746 -1.142347 0.7439971 -1.484073
121+
# [2,] 0.09613058 -1.419227 -2.292037 1.3808827 -2.411346
122+
# [3,] -0.06909654 1.819392 2.192748 -1.1807929 2.654590
123+
# [4,] 0.14181901 -1.118677 -1.694580 0.7147536 -1.640329
124+
# [5,] -0.24590901 1.090359 1.502458 -0.6939420 2.093756
125+
#
126+
# > cach1$getinverse()
127+
# [,1] [,2] [,3] [,4] [,5]
128+
# [1,] 0.68183594 -1.078746 -1.142347 0.7439971 -1.484073
129+
# [2,] 0.09613058 -1.419227 -2.292037 1.3808827 -2.411346
130+
# [3,] -0.06909654 1.819392 2.192748 -1.1807929 2.654590
131+
# [4,] 0.14181901 -1.118677 -1.694580 0.7147536 -1.640329
132+
# [5,] -0.24590901 1.090359 1.502458 -0.6939420 2.093756
133+
#
134+
# > cacheSolve(cach1)
135+
# getting cached data
136+
# [,1] [,2] [,3] [,4] [,5]
137+
# [1,] 0.68183594 -1.078746 -1.142347 0.7439971 -1.484073
138+
# [2,] 0.09613058 -1.419227 -2.292037 1.3808827 -2.411346
139+
# [3,] -0.06909654 1.819392 2.192748 -1.1807929 2.654590
140+
# [4,] 0.14181901 -1.118677 -1.694580 0.7147536 -1.640329
141+
# [5,] -0.24590901 1.090359 1.502458 -0.6939420 2.093756
142+
#
143+
# > round(cach1$get() %*% cach1$getinverse(),10)
144+
# [,1] [,2] [,3] [,4] [,5]
145+
# [1,] 1 0 0 0 0
146+
# [2,] 0 1 0 0 0
147+
# [3,] 0 0 1 0 0
148+
# [4,] 0 0 0 1 0
149+
# [5,] 0 0 0 0 1

0 commit comments

Comments
 (0)