Exercícios


Seção 12.1


Item a

error_fac <- function(x){
  if(x < 0){
    stop("'x' tem que ser maior que zero!")
  }
  
  if(x == 0){
    return(1)
  } else {
    return(x * error_fac(x-1))
  }
}

#i
error_fac(5)
## [1] 120
#ii
error_fac(8)
## [1] 40320
#iii
error_fac(-8)
## Error in error_fac(-8): 'x' tem que ser maior que zero!


Item b

inv <- function(lista, noinv = NA, nonmat = "not a matrix", sil = TRUE){
  
  #checando se o primeiro argumento é uma lista
  if(is.list(lista) == FALSE){stop("'lista' deve ter classe lista!")}
  
  #checando se lista tem pelo menos um argumento
  if(length(lista) < 1){stop("'lista' deve ter pelo menos um membro")}
  
  #checando se nonmat é string e, se nao, tentar coerção
  if(is.character(nonmat) == FALSE){
    nonmat <- as.character(nonmat)
    warning("'nonmat' sofreu coerção para tipo string pois não o era")
  }
  
  #loop buscando cada membro i da lista
  for (i in 1:length(lista)){
    
    if (is.matrix(lista[[i]])){
      elem <- try(solve(lista[[i]]), silent = sil)
      

        if(class(elem)=="try-error"){
          lista[[i]] <- noinv
        } else{
          lista[[i]] <- elem
        }
      
    } else {
      lista[[i]] <- nonmat
    }
    
  }
  
  return(lista)
}


#i
list(1:4,matrix(1:4,1,4),matrix(1:4,4,1),matrix(1:4,2,2)) %>% inv()
## [[1]]
## [1] "not a matrix"
## 
## [[2]]
## [1] NA
## 
## [[3]]
## [1] NA
## 
## [[4]]
##      [,1] [,2]
## [1,]   -2  1.5
## [2,]    1 -0.5
#ii
list(1:4,matrix(1:4,1,4),matrix(1:4,4,1),matrix(1:4,2,2)) %>% inv(noinv = Inf, nonmat = 666)
## [[1]]
## [1] "666"
## 
## [[2]]
## [1] Inf
## 
## [[3]]
## [1] Inf
## 
## [[4]]
##      [,1] [,2]
## [1,]   -2  1.5
## [2,]    1 -0.5
#iii
list(1:4,matrix(1:4,1,4),matrix(1:4,4,1),matrix(1:4,2,2)) %>% inv(noinv = Inf, nonmat = 666, sil = FALSE)
## Error in solve.default(lista[[i]]) : 'a' (1 x 4) must be square
## Error in solve.default(lista[[i]]) : 'a' (4 x 1) must be square
## [[1]]
## [1] "666"
## 
## [[2]]
## [1] Inf
## 
## [[3]]
## [1] Inf
## 
## [[4]]
##      [,1] [,2]
## [1,]   -2  1.5
## [2,]    1 -0.5
#iv
list(diag(9),matrix(c(0.2,0.4,0.2,0.1,0.1,0.2),3,3),
  rbind(c(5,5,1,2),c(2,2,1,8),c(6,1,5,5),c(1,0,2,0)),
  matrix(1:6,2,3),cbind(c(3,5),c(6,5)),as.vector(diag(2))) %>%
  inv(noinv = "unsuitable matrix")
## [[1]]
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
##  [1,]    1    0    0    0    0    0    0    0    0
##  [2,]    0    1    0    0    0    0    0    0    0
##  [3,]    0    0    1    0    0    0    0    0    0
##  [4,]    0    0    0    1    0    0    0    0    0
##  [5,]    0    0    0    0    1    0    0    0    0
##  [6,]    0    0    0    0    0    1    0    0    0
##  [7,]    0    0    0    0    0    0    1    0    0
##  [8,]    0    0    0    0    0    0    0    1    0
##  [9,]    0    0    0    0    0    0    0    0    1
## 
## [[2]]
## [1] "unsuitable matrix"
## 
## [[3]]
##              [,1]       [,2]        [,3]        [,4]
## [1,]  0.019900498 -0.2288557  0.35820896 -0.79104478
## [2,]  0.203980100  0.1542289 -0.32835821  0.64179104
## [3,] -0.009950249  0.1144279 -0.17910448  0.89552239
## [4,] -0.054726368  0.1293532  0.01492537 -0.07462687
## 
## [[4]]
## [1] "unsuitable matrix"
## 
## [[5]]
##            [,1] [,2]
## [1,] -0.3333333  0.4
## [2,]  0.3333333 -0.2
## 
## [[6]]
## [1] "not a matrix"
#v
inv(lista = "hello")
## Error in inv(lista = "hello"): 'lista' deve ter classe lista!
#iv
inv(lista = list())
## Error in inv(lista = list()): 'lista' deve ter pelo menos um membro



Início



Seção 12.2


Item a

prog_test_fancy <- function(n, ...){
  result <- 0
  progbar <- txtProgressBar(min=0,max=n,...) #initialize a bar object
  for(i in 1:n){
    result <- result + 1
    Sys.sleep(0.5)
    setTxtProgressBar(progbar,value=i) #update the bar
  }
  close(progbar) #terminate bar
  return(result)
}

t1 <- Sys.time()
prog_test_fancy(50, style = 3, char = "r")
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |r                                                                     |   2%
  |                                                                            
  |rrr                                                                   |   4%
  |                                                                            
  |rrrr                                                                  |   6%
  |                                                                            
  |rrrrrr                                                                |   8%
  |                                                                            
  |rrrrrrr                                                               |  10%
  |                                                                            
  |rrrrrrrr                                                              |  12%
  |                                                                            
  |rrrrrrrrrr                                                            |  14%
  |                                                                            
  |rrrrrrrrrrr                                                           |  16%
  |                                                                            
  |rrrrrrrrrrrrr                                                         |  18%
  |                                                                            
  |rrrrrrrrrrrrrr                                                        |  20%
  |                                                                            
  |rrrrrrrrrrrrrrr                                                       |  22%
  |                                                                            
  |rrrrrrrrrrrrrrrrr                                                     |  24%
  |                                                                            
  |rrrrrrrrrrrrrrrrrr                                                    |  26%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrr                                                  |  28%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrr                                                 |  30%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrr                                                |  32%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrrrr                                              |  34%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrrrrr                                             |  36%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrrrrrrr                                           |  38%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrrrrrrrr                                          |  40%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrrrrrrrrr                                         |  42%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr                                       |  44%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr                                      |  46%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr                                    |  48%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr                                   |  50%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr                                  |  52%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr                                |  54%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr                               |  56%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr                             |  58%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr                            |  60%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr                           |  62%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr                         |  64%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr                        |  66%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr                      |  68%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr                     |  70%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr                    |  72%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr                  |  74%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr                 |  76%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr               |  78%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr              |  80%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr             |  82%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr           |  84%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr          |  86%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr        |  88%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr       |  90%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr      |  92%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr    |  94%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr   |  96%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr |  98%
  |                                                                            
  |rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr| 100%
## [1] 50
t2 <- Sys.time()

t2-t1
## Time difference of 25.42898 secs


Item b

myfibrec2 <- function(n){
  if(n<0){
    warning("Assuming you meant 'n' to be positive -- doing that instead")
    n <- n*-1
  } else if(n==0){
    stop("'n' is uninterpretable at 0")
  }
  if(n==1||n==2){
    return(1)
  } else {
    return(myfibrec2(n-1)+myfibrec2(n-2))
  }
}

myfibvectorTRY <- function(nvec){
  t1 <- Sys.time()
  nterms <- length(nvec)
  result <- rep(0,nterms)
  progbar <- txtProgressBar(min=0,max=nterms, style = 3, char = "%")
  for(i in 1:nterms){
    attempt <- try(myfibrec2(nvec[i]),silent=T)
    if(class(attempt)=="try-error"){
      result[i] <- NA
    } else {
      result[i] <- attempt
    }
    setTxtProgressBar(progbar,value=i)
  }
  close(progbar)
  t2 <- Sys.time()
  print(t2-t1)
  return(result)
}

#i

myfibvectorTRY(nvec = c(3,2,7,0,9,13))
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |%%%%%%%%%%%%                                                          |  17%
  |                                                                            
  |%%%%%%%%%%%%%%%%%%%%%%%                                               |  33%
  |                                                                            
  |%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                                   |  50%
  |                                                                            
  |%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                       |  67%
  |                                                                            
  |%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%            |  83%
  |                                                                            
  |%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%| 100%
## Time difference of 0.009974003 secs
## [1]   2   1  13  NA  34 233
##i

myfibvectorTRY(nvec = c(1:35))
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |%%                                                                    |   3%
  |                                                                            
  |%%%%                                                                  |   6%
  |                                                                            
  |%%%%%%                                                                |   9%
  |                                                                            
  |%%%%%%%%                                                              |  11%
  |                                                                            
  |%%%%%%%%%%                                                            |  14%
  |                                                                            
  |%%%%%%%%%%%%                                                          |  17%
  |                                                                            
  |%%%%%%%%%%%%%%                                                        |  20%
  |                                                                            
  |%%%%%%%%%%%%%%%%                                                      |  23%
  |                                                                            
  |%%%%%%%%%%%%%%%%%%                                                    |  26%
  |                                                                            
  |%%%%%%%%%%%%%%%%%%%%                                                  |  29%
  |                                                                            
  |%%%%%%%%%%%%%%%%%%%%%%                                                |  31%
  |                                                                            
  |%%%%%%%%%%%%%%%%%%%%%%%%                                              |  34%
  |                                                                            
  |%%%%%%%%%%%%%%%%%%%%%%%%%%                                            |  37%
  |                                                                            
  |%%%%%%%%%%%%%%%%%%%%%%%%%%%%                                          |  40%
  |                                                                            
  |%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                                        |  43%
  |                                                                            
  |%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                                      |  46%
  |                                                                            
  |%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                                    |  49%
  |                                                                            
  |%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                                  |  51%
  |                                                                            
  |%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                                |  54%
  |                                                                            
  |%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                              |  57%
  |                                                                            
  |%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                            |  60%
  |                                                                            
  |%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                          |  63%
  |                                                                            
  |%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                        |  66%
  |                                                                            
  |%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                      |  69%
  |                                                                            
  |%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                    |  71%
  |                                                                            
  |%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                  |  74%
  |                                                                            
  |%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                |  77%
  |                                                                            
  |%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%              |  80%
  |                                                                            
  |%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%            |  83%
  |                                                                            
  |%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          |  86%
  |                                                                            
  |%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        |  89%
  |                                                                            
  |%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%      |  91%
  |                                                                            
  |%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    |  94%
  |                                                                            
  |%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  |  97%
  |                                                                            
  |%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%| 100%
## Time difference of 29.93387 secs
##  [1]       1       1       2       3       5       8      13      21      34
## [10]      55      89     144     233     377     610     987    1597    2584
## [19]    4181    6765   10946   17711   28657   46368   75025  121393  196418
## [28]  317811  514229  832040 1346269 2178309 3524578 5702887 9227465


Item c

stand <- function(n){
  t1 <- Sys.time()
  vec <- c(1, 1)
  for (i in 3:n){
    vec[i] <- vec[i-1] + vec[i-2]
  }
  return(vec)
  t2 <- Sys.time()
  print(t2-t1)
}

# é preferível a versão direta e não recursiva
stand(35)
##  [1]       1       1       2       3       5       8      13      21      34
## [10]      55      89     144     233     377     610     987    1597    2584
## [19]    4181    6765   10946   17711   28657   46368   75025  121393  196418
## [28]  317811  514229  832040 1346269 2178309 3524578 5702887 9227465



Início