Guest User

Untitled

a guest
Jan 13th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. ## prereq:
  2. ## 1) How to define a function
  3.  
  4. ## Security level
  5. ## Warning: Does not stop execution
  6. ## Error: Stop execution
  7.  
  8. ## example of warning
  9.  
  10. log(-1)
  11.  
  12. ## example of error
  13.  
  14. log("ABC")
  15.  
  16. ## example of non-warning and non-error, but potentially problematic
  17.  
  18. log(NA)
  19. mean(c(1,2,43,5,6,7,NA))
  20. 10/0
  21.  
  22. ### Real life bugs are not only about error / warning.
  23.  
  24. ## how to throw an error / warning
  25. stop(2 > 3)
  26. stopifnot(3==4)
  27.  
  28. warning("You should go to the Hong Kong Python usergroup!")
  29.  
  30. suppressWarnings(log(-1))
  31.  
  32. ## let's define a simple function
  33.  
  34. simpleFx <- function(x) {
  35. if (x > 0) {
  36. return("Positive")
  37. } else {
  38. return("Negative")
  39. }
  40. }
  41.  
  42. # let's try
  43.  
  44. simpleFx(10)
  45. simpleFx(-10)
  46. simpleFx(50/10)
  47. simpleFx(pi)
  48. simpleFx(10/0)
  49.  
  50. # let's try some corner cases
  51.  
  52. simpleFx(0)
  53.  
  54. simpleFx(NA)
  55. simpleFx(log(-1))
  56. simpleFx(NaN)
  57.  
  58. NaN > 0
  59.  
  60. # let try something even crazy
  61. # press C-c C-c to stop
  62.  
  63. # finding square root using bisection method
  64.  
  65. mySqrt <- function(x, epsilon = 0.001) {
  66. lowest <- 0
  67. highest <- x
  68. guess <- mean(c(lowest, highest))
  69. while(abs((guess * guess) - x) > epsilon) {
  70. if ((guess * guess) > x) {
  71. highest <- guess
  72. } else {
  73. lowest <- guess
  74. }
  75. }
  76. return(guess)
  77. }
  78.  
  79. ### infinite loop! Why?
  80. ### oldskool way: adding print statement
  81.  
  82.  
  83. mySqrtdebug <- function(x, epsilon = 0.001) {
  84. lowest <- 0
  85. highest <- x
  86. guess <- mean(c(lowest, highest))
  87. while(abs((guess * guess) - x) > epsilon) {
  88. cat(paste0("guess: ", str(guess)))
  89. cat(paste0("Highest: ", str(highest)))
  90. cat(paste0("Lowest: ", str(lowest)))
  91. cat((paste0("Diff: ", str(abs((guess*guess) - x)))))
  92. if ((guess * guess) > x) {
  93. highest <- guess
  94. } else {
  95. lowest <- guess
  96. }
  97. }
  98. return(guess)
  99. }
  100.  
  101.  
  102. ### guess is not updated!
  103.  
  104. mySqrtdebug <- function(x, epsilon = 0.001) {
  105. lowest <- 0
  106. highest <- x
  107. guess <- mean(c(lowest, highest))
  108. while(abs((guess * guess) - x) > epsilon) {
  109. print(guess)
  110. # cat(paste0("Highest: ", str(highest)))
  111. # cat(paste0("Lowest: ", str(lowest)))
  112. # cat((paste0("Diff: ", str(abs((guess*guess) - x)))))
  113. if ((guess * guess) > x) {
  114. highest <- guess
  115. } else {
  116. lowest <- guess
  117. }
  118. guess <- mean(c(lowest, highest))
  119. }
  120. return(guess)
  121. }
  122.  
  123.  
  124. # how to detect the problem without inserting print / cat statement
  125.  
  126. debug(mySqrt)
  127. mySqrt(5) # c (or just enter), Q, help | print out the value of guess after each iteration | ls()
  128. undebug(mySqrt)
  129. mySqrt(5)
  130.  
  131.  
  132. mySqrt <- function(x, epsilon = 0.001) {
  133. lowest <- 0
  134. highest <- x
  135. guess <- mean(c(lowest, highest))
  136. while(abs((guess * guess) - x) > epsilon) {
  137. if ((guess * guess) > x) {
  138. highest <- guess
  139. } else {
  140. lowest <- guess
  141. }
  142. guess <- mean(c(lowest, highest))
  143. }
  144. return(guess)
  145. }
  146.  
  147. debug(mySqrt)
  148. mySqrt(5)
  149. undebug(mySqrt)
  150.  
  151. mySqrt(6)
  152.  
  153. mySqrt(100)
  154. mySqrt(25.8) * mySqrt(25.8)
  155.  
  156. ### corner cases
  157.  
  158. mySqrt(0.000001) # Accuracy problem
  159. mySqrt(-1) # also, it should return a complex number
  160. mySqrt(99999999)
  161. mySqrt(Inf) #die
  162. mySqrt(NaN) #die too
  163. mySqrt(NA) #die three
  164.  
  165. debug(mySqrt)
  166. mySqrt(-1)
  167. mySqrt(0.000001) # Accuracy problem
  168.  
  169. # defensive programming
  170.  
  171. mySqrt <- function(x, epsilon = 0.001) {
  172. if (!(is.numeric(x) & !is.infinite(x) & x > 0)) stop("Invalid input")
  173. if (x < epsilon) stop("x is smaller than tolerance.")
  174. lowest <- 0
  175. highest <- x
  176. guess <- mean(c(lowest, highest))
  177. while(abs((guess * guess) - x) > epsilon) {
  178. if ((guess * guess) > x) {
  179. highest <- guess
  180. } else {
  181. lowest <- guess
  182. }
  183. guess <- mean(c(lowest, highest))
  184. }
  185. return(guess)
  186. }
  187.  
  188. debug(mySqrt)
  189. mySqrt(Inf)
  190. mySqrt(NaN)
  191. mySqrt(NA)
  192. mySqrt(-1)
  193. mySqrt(0.000001)
  194.  
  195. ## Example from the SICP
  196.  
  197. ## Newton-Rhapson method
  198.  
  199. sqrtNewt <- function(x, epsilon=0.0001) {
  200. if (!(is.numeric(x) & !is.infinite(x) & x > 0)) stop("Invalid input")
  201. if (x < epsilon) stop("x is smaller than tolerance.")
  202. guess <- max(c(x,1)) / 2
  203. delta <- abs(guess^2 - x)
  204. while (delta > epsilon) {
  205. guess <- mean(c(guess, (x/guess)))
  206. delta <- abs(guess^2 - x)
  207. }
  208. return(guess)
  209. }
  210.  
  211. sqrtNewt(Inf)
  212. sqrtNewt(NaN)
  213. sqrtNewt(NA)
  214. sqrtNewt(-1)
  215. sqrtNewt(0.000001)
  216. sqrtNewt(100)
  217. sqrtNewt(500)
  218.  
  219.  
  220. newton <- function(x, f, epsilon=0.0001) {
  221. if (!(is.numeric(x) & !is.infinite(x) & x > 0)) stop("Invalid input")
  222. if (x < epsilon) stop("x is smaller than tolerance.")
  223. guess <- max(c(x,1)) / 2
  224. delta <- abs(f(guess) - x)
  225. while (delta > epsilon) {
  226. guess <- mean(c(guess, (x/guess)))
  227. delta <- abs(f(guess) - x)
  228. }
  229. return(guess)
  230. }
  231.  
  232. sq <- function(x) {
  233. return(x^2)
  234. }
  235.  
  236. newton(100, sq)
  237.  
  238. # or even, aka lambda function
  239.  
  240. newton(100, function(x) x^2)
  241.  
  242. newton(100, function(x) x^3)
  243.  
  244. debug(newton)
  245. newton(100, function(x) x^3)
  246.  
  247. # the theory is wrong, because the improvement function depends on f(x)
  248. # i.e. Universial guess <- mean(c(guess, (x/guess))) as guess improvement is wrong
  249.  
  250.  
  251. newton <- function(x, f, imp, epsilon=0.0001) {
  252. if (!(is.numeric(x) & !is.infinite(x) & x > 0)) stop("Invalid input")
  253. if (x < epsilon) stop("x is smaller than tolerance.")
  254. guess <- max(c(x,1)) / 2
  255. delta <- abs(f(guess) - x)
  256. while (delta > epsilon) {
  257. guess <- imp(x, guess)
  258. delta <- abs(f(guess) - x)
  259. }
  260. return(guess)
  261. }
  262.  
  263. newton(100, function(x) x^2, function(x, y) { mean(c(y, (x/y))) })
  264.  
  265. newton(90, function(x) x^3, function(x, y) { ((x/(y^2)) + (2 * y)) / 3 })
  266.  
  267.  
  268. bisec <- function(x, f, epsilon = 0.001) {
  269. if (!(is.numeric(x) & !is.infinite(x) & x > 0)) stop("Invalid input")
  270. if (x < epsilon) stop("x is smaller than tolerance.")
  271. lowest <- 0
  272. highest <- x
  273. guess <- mean(c(lowest, highest))
  274. while(abs(f(guess) - x) > epsilon) {
  275. if (f(guess) > x) {
  276. highest <- guess
  277. } else {
  278. lowest <- guess
  279. }
  280. guess <- mean(c(lowest, highest))
  281. }
  282. return(guess)
  283. }
  284.  
  285.  
  286. bisec(100, function(x) x^2)
  287. bisec(1000, function(x) x^3)
  288. bisec(10000, function(x) x^4)
Advertisement
Add Comment
Please, Sign In to add comment