####################################################### ## Supplement to Homework 6 -- 36-350 ## Functions to be debugged ## 1. regexp 1 -- names. my.regexpr.1 <- function (str1) { regmatches (str1, gregexpr("[A-z]+ [A-z]+", str1)) } theIncredibles <- '"Supers"—humans with superpowers—are forced into civilian relocation programs after facing several lawsuits from peripheral damage caused by their crime fighting activities. Fifteen years after relocation, Bob and Helen Parr, formerly Mr. Incredible and Elastigirl, and their children Violet, Dash, and Jack-Jack live as a suburban family. Bob is unsatisfied with suburban life and his white-collar job and longs for the glory days. On some nights, Bob and his old friend Lucius Best, formerly Frozone, perform vigilante work, unbeknownst to Helen. One day, his supervisor refuses to let him stop a mugging and Bob injures him, resulting in him losing his job. Returning home, Bob finds a message from a woman named Mirage, who convinces him to become Mr. Incredible again and gives him a mission to destroy a malfunctioning robot called the Omnidroid promising a substantial reward. Arriving on NomanisanIsland, Bob is able to defeat the robot by tricking it into ripping out its own power source.' ## 2. regexp 2 -- dates. my.regexpr.2 <- function (str1) { regmatches (str1, gregexpr("[A-z]+ [0-9]+ [0-9]{4}", str1)) } terminator2 <- "2 EXT. CITY RUINS - NIGHT Same spot as the last shot, but now it is a landscape in Hell. The cars are stopped in rusted rows, still bumper to bumper. The skyline of buildings beyond has been shattered by some unimaginable force like a row of kicked-down sandcastles. Wind blows through the desolation, keening with the sound of ten million dead souls. It scurries the ashes into drifts, stark white in the moonlight against the charred rubble. A TITLE CARD FADES IN: LOS ANGELES, July 11, 2029 3 ANGLE ON a heap of fire-blackened human bones. Beyond the mound is a vast tundra of skulls and shattered concrete. The rush hour crowd burned down in their tracks. 4 WE DISSOLVE TO a playground... where intense heat has half-melted the jungle gym, the blast has warped the swing set, the merry-go-round has sagged in the firestorm. Small skulls look accusingly from the ash-drifts. WE HEAR the distant echo of children's voices... playing and laughing in the sun. A silly, sing-songy rhyme as WE TRACKS SLOWLY over seared asphalt where the faint hieroglyphs of hopscotch lines are still visible. CAMERA comes to rest on a burnt and rusted tricycle... next to the tiny skull of its owner. HOLD ON THIS IMAGE as a female VOICE speaks: VOICE 3 billion human lives ended on August 29th, 1997. The survivors of the nuclear fire called the war Judgment Day. They lived only to face a new nightmare, the war against the Machines... A metal foot crushes the skull like china. TILT UP, revealing a humanoid machine holding a massive battle rifle. It looks like a CHROME SKELETON... a high-tech Death figure. It is the endoskeleton of a Series 800 terminator. Its glowing red eyes compassionlessly sweep the dead terrain, hunting." ## 3. dgamma my.dgamma.log <- function (xx, shape, rate) { shape*log(rate) - lgamma(shape) + (shape-1)*xx - rate*xx } ## 4. dnorm my.dnorm.log <- function (xx, mean, sd) { -1/2*log(2*pi*sd) - (xx-mean)^2/sd/2 } ## 5. function zero-finder. zero.finder <- function (fn, start, tol=1e-6, delta=1e-4) { pp <- start repeat { grad.fn <- (fn(pp+delta) - fn(pp-delta))/(2*delta) pp <- pp + fn(pp)/grad.fn if (abs(fn(pp)) < tol) break } pp } ## 6. function maximizer. maximizer <- function (fn, start, tol=1e-6, delta=1e-4) { pp <- start repeat { grad.fn <- (fn(pp+delta) - fn(pp-delta))/(2*delta) hess.fn <- (fn(pp+delta) + fn(pp-delta) - 2*fn(pp))/(delta^2) pp <- pp - grad.fn/hess.fn if (abs(grad.fn) < tol) break #fn(pp) } pp } ## 7. dynamic.fibonacci <- function (nn) { my.fib <- c(1,2) for (kk in 3:nn) my.fib[kk] <- my.fib[kk-1] + my.fib[kk-2] return(my.fib[nn]) }