1 00:00:00,500 --> 00:00:02,890 The following content is provided under a Creative 2 00:00:02,890 --> 00:00:04,430 Commons license. 3 00:00:04,430 --> 00:00:06,730 Your support will help MIT OpenCourseWare 4 00:00:06,730 --> 00:00:11,120 continue to offer high quality educational resources for free. 5 00:00:11,120 --> 00:00:13,720 To make a donation or view additional materials 6 00:00:13,720 --> 00:00:17,680 from hundreds of MIT courses, visit MIT OpenCourseWare 7 00:00:17,680 --> 00:00:20,670 at ocw.mit.edu. 8 00:00:20,670 --> 00:00:23,320 CURRAN KELLEHER: So my name is Curran Kelleher. 9 00:00:23,320 --> 00:00:26,060 I'll be lecturing today about recursion and fractals. 10 00:00:26,060 --> 00:00:28,230 Justin Curry's not here today. 11 00:00:28,230 --> 00:00:30,110 So I'm going to fill in. 12 00:00:30,110 --> 00:00:33,070 So today I'm going to just do a bunch 13 00:00:33,070 --> 00:00:36,460 of example programs, computer programs that are recursive. 14 00:00:36,460 --> 00:00:38,710 Some of them don't make pictures, and some of them do. 15 00:00:38,710 --> 00:00:40,660 And when they make pictures, they're fractals. 16 00:00:40,660 --> 00:00:42,940 Fractals are things that are self-similar 17 00:00:42,940 --> 00:00:43,820 at different scales. 18 00:00:43,820 --> 00:00:46,420 So you can zoom in on them. 19 00:00:46,420 --> 00:00:49,720 We're going to take a break at 4 o'clock for 10 minutes. 20 00:00:49,720 --> 00:00:51,954 And then towards the end, hopefully I'll 21 00:00:51,954 --> 00:00:53,620 show you a bunch of examples of fractals 22 00:00:53,620 --> 00:00:56,020 and play some Bach music. 23 00:00:56,020 --> 00:01:03,010 So first of all, let's consider a recursive mathematical 24 00:01:03,010 --> 00:01:05,620 function, factorial. 25 00:01:05,620 --> 00:01:10,140 Something factorial-- like 3 factorial is 3 times 2 times 1. 26 00:01:10,140 --> 00:01:13,870 4 factorial is 4 times 3 times 2 times 1. 27 00:01:13,870 --> 00:01:18,590 So this is factorial, the exclamation point. 28 00:01:24,430 --> 00:01:30,610 So the way this is defined is actually recursive. 29 00:01:30,610 --> 00:01:32,260 So if you take anything factorial-- 30 00:01:32,260 --> 00:01:42,320 let's say n factorial is n minus 1 factorial. 31 00:01:42,320 --> 00:01:45,150 Let's say n is 4. 32 00:01:45,150 --> 00:01:46,570 n minus 1 would be 3. 33 00:01:46,570 --> 00:01:51,400 So 4 factorial is-- 34 00:01:51,400 --> 00:01:54,790 wait, n times n. 35 00:01:57,650 --> 00:02:00,600 So it's going to equal n times n minus 1 factorial. 36 00:02:00,600 --> 00:02:07,160 So 4 is n times this whole thing is 3 factorial. 37 00:02:07,160 --> 00:02:08,801 It's n minus 1 factorial. 38 00:02:12,490 --> 00:02:15,040 So this is a recursive definition. 39 00:02:15,040 --> 00:02:17,250 And if you look in the handout, I 40 00:02:17,250 --> 00:02:24,170 wrote a little computer program on page 3, I think, 41 00:02:24,170 --> 00:02:25,990 that does the factorial. 42 00:02:25,990 --> 00:02:29,070 So it goes like this. 43 00:02:40,290 --> 00:02:43,260 So just for those of you who don't know much about 44 00:02:43,260 --> 00:02:46,140 programming, def means define. 45 00:02:46,140 --> 00:02:49,000 So we're defining a function called Factorial. 46 00:02:49,000 --> 00:02:50,910 And it takes as an argument n. 47 00:02:50,910 --> 00:02:53,270 So you can call this function, pass it a number. 48 00:02:53,270 --> 00:02:56,070 And inside the function, it's referred to as n. 49 00:02:56,070 --> 00:03:05,130 So this factorial function says if n is greater than 1, 50 00:03:05,130 --> 00:03:12,630 return n times factorial of n minus 1. 51 00:03:20,200 --> 00:03:23,250 Else, return 1. 52 00:03:29,890 --> 00:03:33,380 So what makes this function recursive is the fact 53 00:03:33,380 --> 00:03:34,670 that it calls itself. 54 00:03:34,670 --> 00:03:40,550 Factorial is defined as n times factorial of something else. 55 00:03:40,550 --> 00:03:43,190 So a recursive function is a function that calls itself. 56 00:03:46,074 --> 00:03:47,240 So I'll give you an example. 57 00:03:47,240 --> 00:04:00,500 So if n is 5, say, the way we call this is, we say factorial 58 00:04:00,500 --> 00:04:02,030 of 5. 59 00:04:02,030 --> 00:04:07,610 So when we say this, it calls this function 60 00:04:07,610 --> 00:04:10,430 and gives n the number 5. 61 00:04:10,430 --> 00:04:12,860 So what it's going to do is n is going to be 5. 62 00:04:12,860 --> 00:04:14,120 So n is greater than 1. 63 00:04:14,120 --> 00:04:18,769 So we return n times factorial n minus 1. 64 00:04:18,769 --> 00:04:21,000 So say we're doing this algorithm. 65 00:04:21,000 --> 00:04:23,060 5 is n. 66 00:04:23,060 --> 00:04:27,330 So we're going to return 5 times factorial of n minus 1. 67 00:04:27,330 --> 00:04:30,830 So we call factorial with the value 4. 68 00:04:30,830 --> 00:04:33,920 And that's also greater than 1. 69 00:04:33,920 --> 00:04:37,900 So we return 4 times factorial 3. 70 00:04:37,900 --> 00:04:43,880 So 5 times 4 times factorial 3. 71 00:04:43,880 --> 00:04:48,710 So we loop-- we call itself a bunch of times 72 00:04:48,710 --> 00:04:50,720 until we get down to 1. 73 00:04:57,690 --> 00:04:59,440 So that's what actually ends up happening. 74 00:04:59,440 --> 00:05:01,400 This is recursion. 75 00:05:01,400 --> 00:05:04,690 So another simple example, which is sort of like the factorial, 76 00:05:04,690 --> 00:05:08,665 is the Fibonacci numbers, Fibonacci sequence. 77 00:05:19,520 --> 00:05:25,940 So the Fibonacci numbers, it goes 1, 1, and then 78 00:05:25,940 --> 00:05:27,890 the next one you add the first two together. 79 00:05:27,890 --> 00:05:30,902 So it goes 1, 1, 2. 80 00:05:30,902 --> 00:05:32,420 1 plus 2 is 3. 81 00:05:32,420 --> 00:05:39,550 2 plus 3 is 5, and so on, 8, blah, blah, blah. 82 00:05:39,550 --> 00:05:42,280 These are the Fibonacci numbers. 83 00:05:42,280 --> 00:05:45,350 So this is a recursive definition. 84 00:05:45,350 --> 00:05:46,940 Let's say this is-- 85 00:05:54,174 --> 00:05:57,300 this is like the number of the element. 86 00:05:57,300 --> 00:05:59,050 Like they're just numbers-- 87 00:05:59,050 --> 00:06:00,850 indices, if you will. 88 00:06:00,850 --> 00:06:08,470 So Fibonacci of 2 is Fibonacci of 0 plus Fibonacci of 1. 89 00:06:08,470 --> 00:06:11,620 And so Fibonacci of 5 is going to be Fibonacci of 3 90 00:06:11,620 --> 00:06:13,630 plus Fibonacci of 4. 91 00:06:13,630 --> 00:06:15,790 So generally, Fibonacci of n is going 92 00:06:15,790 --> 00:06:21,640 to be Fibonacci of a n minus 1 plus Fibonacci of n minus 2. 93 00:06:21,640 --> 00:06:26,500 So we could say that here. 94 00:06:26,500 --> 00:06:28,900 Instead of factorial, we call it Fibonacci. 95 00:06:28,900 --> 00:06:33,660 And we'll notice that they're almost the same thing. 96 00:06:33,660 --> 00:06:35,650 I'll say fib. 97 00:06:35,650 --> 00:06:47,370 If n is greater than 1, we return Fibonacci of n minus 1. 98 00:06:54,457 --> 00:07:00,360 And this gives us the Fibonacci numbers actually. 99 00:07:00,360 --> 00:07:03,360 Does that make sense to you guys? 100 00:07:03,360 --> 00:07:07,170 So in the handout, there are some example outputs 101 00:07:07,170 --> 00:07:08,964 of both of these. 102 00:07:08,964 --> 00:07:10,380 You could see that's what happens. 103 00:07:15,510 --> 00:07:19,230 So who has their copy of Godel, Escher, Bach today? 104 00:07:19,230 --> 00:07:24,780 So if you will, look on page 132 of Godel, Escher, Bach. 105 00:07:24,780 --> 00:07:26,880 Oh no, now I can't look at it. 106 00:07:26,880 --> 00:07:28,570 Oh well. 107 00:07:28,570 --> 00:07:31,230 132 of Godel, Escher, Bach has these two diagrams 108 00:07:31,230 --> 00:07:35,190 that are recursive transition networks. 109 00:07:35,190 --> 00:07:39,942 They define a grammar, sort of like English. 110 00:07:39,942 --> 00:07:40,650 It's not English. 111 00:07:40,650 --> 00:07:41,740 It's not complete. 112 00:07:41,740 --> 00:07:43,860 It's a simplified version of English. 113 00:07:43,860 --> 00:07:47,930 But it communicates the essence of the notion of a grammar, 114 00:07:47,930 --> 00:07:50,160 a recursive grammar. 115 00:07:50,160 --> 00:07:54,390 So you'll notice that-- 116 00:07:54,390 --> 00:07:56,260 it's hard to do it in my head. 117 00:07:56,260 --> 00:08:01,770 Fancy noun, one of the notes calls fancy noun again. 118 00:08:01,770 --> 00:08:04,540 It loops back out on itself. 119 00:08:04,540 --> 00:08:07,260 So this is where the recursion is. 120 00:08:07,260 --> 00:08:09,750 So what I did is I took this diagram 121 00:08:09,750 --> 00:08:13,410 and wrote a little computer program that whenever there's 122 00:08:13,410 --> 00:08:15,750 a choice of the transitions, it chooses one 123 00:08:15,750 --> 00:08:18,090 of those transitions at random. 124 00:08:18,090 --> 00:08:21,780 And this is the program I wrote. 125 00:08:21,780 --> 00:08:24,770 I think it's on page 5 of my handout. 126 00:08:24,770 --> 00:08:25,770 So if you look at that-- 127 00:08:29,300 --> 00:08:30,720 yeah, I wish I had the projector. 128 00:08:30,720 --> 00:08:31,799 It's unfortunate. 129 00:08:37,559 --> 00:08:40,100 Actually, can I look at the diagram? 130 00:08:45,060 --> 00:08:48,000 So if you look at fancy noun, we begin, 131 00:08:48,000 --> 00:08:49,310 and it calls ornate noun. 132 00:08:49,310 --> 00:08:52,850 And if you look at the program in the handout, 133 00:08:52,850 --> 00:08:54,797 you find fancy noun. 134 00:08:54,797 --> 00:08:55,880 It's sort of halfway down. 135 00:08:55,880 --> 00:08:58,788 It says the [? RTN ?] for fancy noun. 136 00:08:58,788 --> 00:09:04,270 Fancy noun equals-- and this is a function call. 137 00:09:04,270 --> 00:09:06,950 Well, first of all, fancy noun equals-- 138 00:09:06,950 --> 00:09:09,160 when you put curly braces around something, 139 00:09:09,160 --> 00:09:12,170 it makes it a function, pretty much. 140 00:09:12,170 --> 00:09:14,225 So fancy noun equals-- 141 00:09:16,940 --> 00:09:20,510 and it copies pretty much directly from the diagram. 142 00:09:20,510 --> 00:09:24,800 Ornate noun, which is also a function call, which 143 00:09:24,800 --> 00:09:27,250 is defined above, plus-- 144 00:09:27,250 --> 00:09:29,240 I'm back in fancy noun. 145 00:09:29,240 --> 00:09:34,600 So ornate noun plus pick from preposition 146 00:09:34,600 --> 00:09:37,100 or a relative pronoun or nothing. 147 00:09:37,100 --> 00:09:39,890 And if you look at the diagram in Godel, Escher, Bach, 148 00:09:39,890 --> 00:09:44,660 the arrows coming out of ornate noun point to relative pronoun, 149 00:09:44,660 --> 00:09:46,070 nothing-- the end-- 150 00:09:46,070 --> 00:09:48,030 and preposition. 151 00:09:48,030 --> 00:09:52,640 So you can make this into a computer 152 00:09:52,640 --> 00:09:55,700 program, which is what I did. 153 00:09:55,700 --> 00:09:58,280 So if you look at my program for a little while, 154 00:09:58,280 --> 00:10:02,000 you'll notice that all the arrows in the diagrams 155 00:10:02,000 --> 00:10:06,546 correspond to function calls in the program. 156 00:10:06,546 --> 00:10:08,420 And they're recursive because they eventually 157 00:10:08,420 --> 00:10:10,460 loop back on themselves. 158 00:10:10,460 --> 00:10:13,490 So here, Hofstadter is trying to communicate 159 00:10:13,490 --> 00:10:15,650 the fact that languages themselves are 160 00:10:15,650 --> 00:10:18,110 defined by recursive grammars. 161 00:10:18,110 --> 00:10:21,750 This is why we can nest sentences inside of each other. 162 00:10:21,750 --> 00:10:23,370 It's recursive. 163 00:10:23,370 --> 00:10:28,490 So recursion leads to nesting, and sometimes incident nesting. 164 00:10:28,490 --> 00:10:31,100 And that's where fractals come from. 165 00:10:31,100 --> 00:10:36,950 So right after this program in the handout, 166 00:10:36,950 --> 00:10:38,970 there's a sample output. 167 00:10:38,970 --> 00:10:43,130 So just read through some of those sample outputs. 168 00:10:43,130 --> 00:10:46,654 They're pretty funny. 169 00:10:46,654 --> 00:10:47,570 I have an old version. 170 00:10:47,570 --> 00:10:51,320 Can I look at someone's handout, just to read? 171 00:10:54,360 --> 00:10:58,000 So small, small bagel inside the strange cow. 172 00:10:58,000 --> 00:11:00,140 It makes sense as an English sentence. 173 00:11:00,140 --> 00:11:02,630 And it was generated by this computer program. 174 00:11:02,630 --> 00:11:05,136 So I think that's just fascinating. 175 00:11:05,136 --> 00:11:07,010 But of course, some of them don't make sense, 176 00:11:07,010 --> 00:11:10,490 like large small bagel that runs large. 177 00:11:10,490 --> 00:11:11,299 Small large horn. 178 00:11:11,299 --> 00:11:12,590 It just doesn't make any sense. 179 00:11:15,460 --> 00:11:16,145 So next example. 180 00:11:21,120 --> 00:11:24,920 So the next example on page 5, I think, is a tree. 181 00:11:24,920 --> 00:11:27,200 We're going to make a tree picture 182 00:11:27,200 --> 00:11:29,480 using recursive functions. 183 00:11:29,480 --> 00:11:42,859 So I'm going to write some pseudo code. 184 00:11:42,859 --> 00:11:43,650 It's not real code. 185 00:11:43,650 --> 00:11:46,440 It's sort of pseudo code to communicate the idea of what 186 00:11:46,440 --> 00:11:47,950 this program is doing. 187 00:12:04,940 --> 00:12:09,160 So we have a function that grows a tree. 188 00:12:09,160 --> 00:12:12,770 It starts from a single branch. 189 00:12:12,770 --> 00:12:15,260 I'll do it [? at that one ?] here. 190 00:12:15,260 --> 00:12:17,000 This is like the starting point. 191 00:12:17,000 --> 00:12:18,200 This is the entry point. 192 00:12:18,200 --> 00:12:22,870 So it says, like class, all this stuff, tree. 193 00:12:22,870 --> 00:12:27,640 Tree parentheses, that gets called when the program starts. 194 00:12:27,640 --> 00:12:31,630 So this function call, growtree, [? 0.50 ?] trunk height, 195 00:12:31,630 --> 00:12:34,300 all this stuff, is this first one. 196 00:12:34,300 --> 00:12:37,060 This is what initiates the process. 197 00:12:37,060 --> 00:12:47,190 And then what the function does is pretty much 198 00:12:47,190 --> 00:12:50,200 if the depth is greater than 0-- 199 00:13:16,880 --> 00:13:21,890 so this tree function calls itself twice, 200 00:13:21,890 --> 00:13:22,780 once for each branch. 201 00:13:25,670 --> 00:13:28,690 So the first time the program calls this function, 202 00:13:28,690 --> 00:13:29,560 it makes this. 203 00:13:29,560 --> 00:13:31,690 It draws it on the screen. 204 00:13:31,690 --> 00:13:42,150 So notice here, it adds the actual line. 205 00:13:42,150 --> 00:13:48,085 If you look at the code, it says add new JV line, x1, x2, 206 00:13:48,085 --> 00:13:49,830 that stuff. 207 00:13:49,830 --> 00:13:54,350 That actually adds a line to the screen. 208 00:13:54,350 --> 00:13:57,090 I wish I could show you the actual code running, 209 00:13:57,090 --> 00:13:59,940 but I can't. 210 00:13:59,940 --> 00:14:01,290 So this is the first time. 211 00:14:01,290 --> 00:14:06,800 And then depth is the number of times it's going to branch out. 212 00:14:06,800 --> 00:14:07,631 And so depth is 11. 213 00:14:07,631 --> 00:14:08,880 It's going to make a big tree. 214 00:14:11,810 --> 00:14:16,170 But what it's going to do is make two subbranches. 215 00:14:16,170 --> 00:14:18,180 This is growtree. 216 00:14:18,180 --> 00:14:21,510 This one correlates to this one here. 217 00:14:21,510 --> 00:14:24,480 This one corresponds to this one here. 218 00:14:24,480 --> 00:14:29,550 And if you look at the code in the handout, 219 00:14:29,550 --> 00:14:34,350 it says growtree x2 y2. 220 00:14:34,350 --> 00:14:37,970 x2, y2 is to the endpoint of the previous branch. 221 00:14:37,970 --> 00:14:39,780 Root length time size factor. 222 00:14:39,780 --> 00:14:44,050 So size factor is a factor by which it's going to scale. 223 00:14:44,050 --> 00:14:49,700 So this would be like maybe half the size, or 0.7. 224 00:14:49,700 --> 00:14:51,490 Size factor is 0.58. 225 00:14:51,490 --> 00:14:54,720 So it's about half the size. 226 00:14:54,720 --> 00:14:57,210 And root angle plus angle factor, 227 00:14:57,210 --> 00:14:59,250 root angle minus angle factor. 228 00:14:59,250 --> 00:15:02,300 These are in the two growtree function calls. 229 00:15:02,300 --> 00:15:08,290 Angle factor, which is pi over 4. 230 00:15:08,290 --> 00:15:11,200 It's exactly 45 degrees. 231 00:15:11,200 --> 00:15:13,590 So each time it branches, the two branches 232 00:15:13,590 --> 00:15:16,500 are going to branch out at that angle. 233 00:15:16,500 --> 00:15:21,930 And so here, say we do depth of 4 234 00:15:21,930 --> 00:15:23,430 when we call it the first time. 235 00:15:23,430 --> 00:15:26,010 The depth here is going to be 4. 236 00:15:26,010 --> 00:15:30,824 And then you pass into the function depth minus 1. 237 00:15:30,824 --> 00:15:35,730 So here, inside the function, it's generating this depth. 238 00:15:35,730 --> 00:15:37,410 It's going to be 3. 239 00:15:37,410 --> 00:15:40,020 And so it's going to keep going down until depth is 0. 240 00:15:40,020 --> 00:15:43,710 So here depth is-- 241 00:15:43,710 --> 00:15:50,380 well, depth is 4, 3, 2. 242 00:15:50,380 --> 00:15:51,356 That's 1. 243 00:15:55,157 --> 00:15:56,490 And it does it on this side too. 244 00:16:04,280 --> 00:16:05,810 So it just keeps going like this. 245 00:16:05,810 --> 00:16:09,020 This is a fractal. 246 00:16:09,020 --> 00:16:15,230 And you can imagine, if you were to continue this infinitely-- 247 00:16:15,230 --> 00:16:19,580 instead of saying depth of 10 or 11, just say depth of infinity. 248 00:16:19,580 --> 00:16:22,140 Say theoretically, if we could do this, 249 00:16:22,140 --> 00:16:26,751 this shape could be zoomed in on infinitely forever. 250 00:16:26,751 --> 00:16:28,250 And this is the notion of a fractal. 251 00:16:28,250 --> 00:16:32,187 And it would look the same as it does on the large scale. 252 00:16:32,187 --> 00:16:33,520 So any questions about the tree? 253 00:16:36,160 --> 00:16:40,910 So next we're going to do the Koch curve. 254 00:16:40,910 --> 00:16:45,610 The Koch curve is sort of similar to the tree, 255 00:16:45,610 --> 00:16:47,514 except that the rules are different. 256 00:16:50,300 --> 00:16:53,770 So first, conceptually, this is what a Koch curve is. 257 00:16:53,770 --> 00:16:57,530 You start with a line, or in some cases, a triangle 258 00:16:57,530 --> 00:17:00,280 to make the whole thing. 259 00:17:00,280 --> 00:17:02,630 And you divide it into three parts. 260 00:17:02,630 --> 00:17:05,200 And then you make an equilateral triangle, 261 00:17:05,200 --> 00:17:07,930 meaning the sides are all the same, 262 00:17:07,930 --> 00:17:09,400 out of the thing in the middle. 263 00:17:09,400 --> 00:17:10,680 And get rid of this line. 264 00:17:13,670 --> 00:17:14,800 So this is the rule. 265 00:17:14,800 --> 00:17:17,140 Go from a line to this thing. 266 00:17:17,140 --> 00:17:20,260 And then this rule is applied to each one of these segments. 267 00:17:20,260 --> 00:17:28,190 So you go like this, and so on, like to each of these smaller 268 00:17:28,190 --> 00:17:31,050 segments. 269 00:17:31,050 --> 00:17:34,200 So the fact that the simple rule is being applied to something 270 00:17:34,200 --> 00:17:36,300 over and over again, and the thing 271 00:17:36,300 --> 00:17:39,870 it's being applied to is the result 272 00:17:39,870 --> 00:17:44,700 of the previous execution of the rule, makes it recursive. 273 00:17:44,700 --> 00:17:48,219 So we could keep drawing it. 274 00:17:48,219 --> 00:17:50,010 Let's look at the actual code, the program. 275 00:17:53,610 --> 00:17:55,442 It says Koch. 276 00:17:55,442 --> 00:17:56,400 I think it's on page 7. 277 00:18:01,500 --> 00:18:03,345 So let's see, createCurve. 278 00:18:12,380 --> 00:18:15,470 What createCurve does essentially 279 00:18:15,470 --> 00:18:20,545 is call itself four times. 280 00:18:27,020 --> 00:18:29,150 And each of those four times corresponds 281 00:18:29,150 --> 00:18:33,477 to this one, this one, this one, and this one. 282 00:18:41,770 --> 00:18:44,960 So yeah, let's look at the actual code. 283 00:18:44,960 --> 00:18:47,300 You see these four function calls to createCurve 284 00:18:47,300 --> 00:18:49,330 in the middle there? 285 00:18:49,330 --> 00:18:56,470 x1, y1, ax, ay, cx, cy, by, all the stuff. 286 00:18:56,470 --> 00:19:04,840 If you look at the little red diagram, this point is x1, y1. 287 00:19:04,840 --> 00:19:07,900 And it's in the diagram what the points are. 288 00:19:07,900 --> 00:19:12,430 So the first function call pretty much 289 00:19:12,430 --> 00:19:15,470 says apply the rule to this segment. 290 00:19:15,470 --> 00:19:17,640 The second function call starts from a, 291 00:19:17,640 --> 00:19:19,750 which is this point here, which says 292 00:19:19,750 --> 00:19:22,030 apply the rule to this segment. 293 00:19:22,030 --> 00:19:25,720 The third one starts at c, which is the top point, 294 00:19:25,720 --> 00:19:28,890 and it says apply the rule to this segment. 295 00:19:28,890 --> 00:19:32,840 And the fourth one, apply it through to this one. 296 00:19:32,840 --> 00:19:39,440 So it's another beautiful recursive fractal. 297 00:19:39,440 --> 00:19:42,740 So what happens-- so this is the Koch curve. 298 00:19:42,740 --> 00:19:46,310 If you generalize this and add randomness to it-- 299 00:19:46,310 --> 00:19:49,430 say these points are a-- 300 00:19:49,430 --> 00:19:51,050 it doesn't matter what they're called. 301 00:19:51,050 --> 00:19:56,600 If you move this one up and down a little bit randomly-- 302 00:19:56,600 --> 00:19:59,780 so you start from this-- 303 00:19:59,780 --> 00:20:02,270 instead of making these points exact, 304 00:20:02,270 --> 00:20:04,220 make them like a little bit off. 305 00:20:04,220 --> 00:20:10,310 And then connect the lines and make a triangle there. 306 00:20:10,310 --> 00:20:12,015 And this is also a little bit off. 307 00:20:12,015 --> 00:20:13,640 And then if you keep doing this, adding 308 00:20:13,640 --> 00:20:16,610 a little bit of randomization each time, 309 00:20:16,610 --> 00:20:20,630 you actually get lines that look just like coastlines 310 00:20:20,630 --> 00:20:24,020 around pieces of land. 311 00:20:24,020 --> 00:20:26,120 And if you generalize this into three dimensions 312 00:20:26,120 --> 00:20:28,160 and do this randomness, it actually 313 00:20:28,160 --> 00:20:33,110 generates 3D mountains, like virtual 3D surfaces that look 314 00:20:33,110 --> 00:20:36,810 exactly like real mountains. 315 00:20:36,810 --> 00:20:40,360 So it's sort of strange, these recursive structures 316 00:20:40,360 --> 00:20:45,365 are definitely in nature. 317 00:20:45,365 --> 00:20:47,790 AUDIENCE: What does it mean for the front page, page 6 318 00:20:47,790 --> 00:20:50,909 that Koch simply has finite error but infinite [INAUDIBLE]?? 319 00:20:50,909 --> 00:20:51,950 CURRAN KELLEHER: Oh yeah. 320 00:20:51,950 --> 00:20:53,241 Yeah, I forgot to mention that. 321 00:20:53,241 --> 00:20:54,600 That's a really cool thing. 322 00:20:54,600 --> 00:20:59,660 So the Koch snowflake is when you start with a triangle, 323 00:20:59,660 --> 00:21:02,105 and you apply the rule to each size of the triangle. 324 00:21:04,640 --> 00:21:09,680 And you get this curve on all these sides. 325 00:21:09,680 --> 00:21:13,520 So it's been mathematically proven, or extrapolated, 326 00:21:13,520 --> 00:21:17,180 that if you were to do this rule an infinite number of times-- 327 00:21:17,180 --> 00:21:19,650 which you can do in math, because it's all theoretical-- 328 00:21:19,650 --> 00:21:23,300 the volume inside of this object will be finite. 329 00:21:23,300 --> 00:21:25,610 It's a definite amount. 330 00:21:25,610 --> 00:21:29,950 But the surface area is infinite. 331 00:21:29,950 --> 00:21:30,860 Oh, not surface area. 332 00:21:30,860 --> 00:21:31,443 The perimeter. 333 00:21:31,443 --> 00:21:33,740 The perimeter is infinite. 334 00:21:33,740 --> 00:21:37,640 That's because every time you apply the rule, you actually 335 00:21:37,640 --> 00:21:40,650 increase the perimeter. 336 00:21:40,650 --> 00:21:45,620 So this has a certain length. 337 00:21:45,620 --> 00:21:47,830 And then once you apply the rule to it, 338 00:21:47,830 --> 00:21:53,000 if you do this, then this new curve, 339 00:21:53,000 --> 00:21:56,100 the total length is longer than this one. 340 00:21:56,100 --> 00:21:58,370 So you can imagine if you do it infinitely, 341 00:21:58,370 --> 00:22:00,950 it's just going to be infinitely long. 342 00:22:00,950 --> 00:22:02,300 So it's sort of mind boggling. 343 00:22:02,300 --> 00:22:05,370 And this goes-- 344 00:22:05,370 --> 00:22:08,315 AUDIENCE: [INAUDIBLE] smaller and smaller? 345 00:22:08,315 --> 00:22:10,440 CURRAN KELLEHER: Yeah, it gets smaller and smaller, 346 00:22:10,440 --> 00:22:12,050 definitely. 347 00:22:12,050 --> 00:22:18,080 But if you do it theoretically an infinite number of times, 348 00:22:18,080 --> 00:22:19,650 it'll still exist. 349 00:22:19,650 --> 00:22:27,970 If you look at the picture on page 6, 350 00:22:27,970 --> 00:22:31,090 right next to the title, the Koch snowflake. 351 00:22:31,090 --> 00:22:34,810 That curve there, you see it? 352 00:22:34,810 --> 00:22:36,310 That's what it would look like, even 353 00:22:36,310 --> 00:22:38,190 if you did it an infinite number of times. 354 00:22:38,190 --> 00:22:44,265 AUDIENCE: So [INAUDIBLE] go on infinitely? 355 00:22:44,265 --> 00:22:45,390 CURRAN KELLEHER: Say again. 356 00:22:45,390 --> 00:22:51,430 AUDIENCE: [INAUDIBLE] maybe have a kind of snowflake 357 00:22:51,430 --> 00:22:52,424 [INAUDIBLE]? 358 00:22:56,700 --> 00:22:59,065 CURRAN KELLEHER: So you just cut it in half? 359 00:22:59,065 --> 00:23:00,840 AUDIENCE: Yeah, not in half, but just 360 00:23:00,840 --> 00:23:04,190 kind of [INAUDIBLE] the segment to go 361 00:23:04,190 --> 00:23:05,761 from one part of [INAUDIBLE]. 362 00:23:10,440 --> 00:23:12,536 CURRAN KELLEHER: I don't really understand. 363 00:23:12,536 --> 00:23:14,244 You can draw it on the board if you want. 364 00:23:14,244 --> 00:23:15,228 AUDIENCE: On the board? 365 00:23:15,228 --> 00:23:16,144 CURRAN KELLEHER: Yeah. 366 00:23:21,630 --> 00:23:24,350 AUDIENCE: Have like a circle-- 367 00:23:24,350 --> 00:23:26,025 I just had this part over here. 368 00:23:26,025 --> 00:23:28,820 And then even if this is finite area, 369 00:23:28,820 --> 00:23:33,480 you're saying that if I cut it like that, [INAUDIBLE].. 370 00:23:33,480 --> 00:23:35,230 CURRAN KELLEHER: Oh, I see. 371 00:23:35,230 --> 00:23:36,592 AUDIENCE: [INAUDIBLE]. 372 00:23:38,369 --> 00:23:39,910 CURRAN KELLEHER: I see what you mean. 373 00:23:39,910 --> 00:23:48,650 So say you had this Koch curve, this shape, 374 00:23:48,650 --> 00:23:50,630 and it were a string. 375 00:23:50,630 --> 00:23:51,710 And you would cut it. 376 00:23:51,710 --> 00:23:54,050 So the string would now be loose. 377 00:23:54,050 --> 00:23:57,740 And if you pulled it, it would go on forever. 378 00:23:57,740 --> 00:23:58,850 Yeah. 379 00:23:58,850 --> 00:23:59,474 It would. 380 00:23:59,474 --> 00:24:00,390 AUDIENCE: [INAUDIBLE]. 381 00:24:00,390 --> 00:24:02,090 CURRAN KELLEHER: Yeah. 382 00:24:02,090 --> 00:24:05,360 That's what it means to have infinite perimeter, which is 383 00:24:05,360 --> 00:24:07,420 why it's just so fascinating. 384 00:24:07,420 --> 00:24:08,796 It's crazy. 385 00:24:08,796 --> 00:24:10,670 People tried to measure the coast of Britain. 386 00:24:10,670 --> 00:24:12,180 How long is the coast of Britain? 387 00:24:12,180 --> 00:24:14,490 Have you heard about this problem? 388 00:24:14,490 --> 00:24:16,820 If you look at it on a large scale, 389 00:24:16,820 --> 00:24:19,102 like from a satellite image of the whole country, 390 00:24:19,102 --> 00:24:21,310 you could just draw a line around it and say oh yeah, 391 00:24:21,310 --> 00:24:22,340 it's this length. 392 00:24:22,340 --> 00:24:24,920 This is the length of the coast of Britain. 393 00:24:24,920 --> 00:24:27,020 But if you zoom in on it, you'll find 394 00:24:27,020 --> 00:24:31,940 that those big lines that you drew are actually really wrong. 395 00:24:31,940 --> 00:24:33,380 They don't actually line up. 396 00:24:33,380 --> 00:24:36,170 So if you make it more precise to its new zooming 397 00:24:36,170 --> 00:24:38,590 angle, the zooming-- 398 00:24:38,590 --> 00:24:40,010 it gets longer. 399 00:24:40,010 --> 00:24:42,050 And so actually the more that you zoom in 400 00:24:42,050 --> 00:24:46,194 on the coast of Britain, the longer the perimeter gets. 401 00:24:46,194 --> 00:24:49,729 AUDIENCE: [INAUDIBLE] just adding [INAUDIBLE].. 402 00:24:49,729 --> 00:24:51,770 CURRAN KELLEHER: Yeah, just adding little pieces. 403 00:24:51,770 --> 00:24:57,040 So say the actual coast of Britain is like that. 404 00:24:57,040 --> 00:25:00,750 But you look at it at this huge distance away. 405 00:25:00,750 --> 00:25:04,035 You say, like oh yeah, this is approximately that line. 406 00:25:04,035 --> 00:25:06,160 But if you look at it in more detail and refine it, 407 00:25:06,160 --> 00:25:07,868 you say, oh, it's not actually that line. 408 00:25:07,868 --> 00:25:10,860 It's maybe like these lines. 409 00:25:10,860 --> 00:25:13,864 But the new lines are actually-- the whole thing is longer. 410 00:25:13,864 --> 00:25:15,780 And if you do it even more and more precisely, 411 00:25:15,780 --> 00:25:18,220 you just get longer and longer and longer. 412 00:25:18,220 --> 00:25:20,280 So nobody's been able to really figure out 413 00:25:20,280 --> 00:25:21,870 how long the coast of Britain is. 414 00:25:21,870 --> 00:25:23,306 It's a fractal. 415 00:25:23,306 --> 00:25:26,570 AUDIENCE: [INAUDIBLE] is it even possible in this [INAUDIBLE] 416 00:25:26,570 --> 00:25:28,454 to have [INAUDIBLE]? 417 00:25:33,170 --> 00:25:35,750 CURRAN KELLEHER: Well, not really. 418 00:25:35,750 --> 00:25:41,480 I mean, so he asked, is it possible in this world 419 00:25:41,480 --> 00:25:44,690 to have something that's really infinite like that? 420 00:25:44,690 --> 00:25:46,850 No, it's not. 421 00:25:46,850 --> 00:25:50,616 Because I mean, there's only finite space on the Earth. 422 00:25:50,616 --> 00:25:53,096 AUDIENCE: Yeah, but even that, I mean, you just 423 00:25:53,096 --> 00:25:56,072 said that given [INAUDIBLE]. 424 00:25:59,604 --> 00:26:00,520 CURRAN KELLEHER: Yeah. 425 00:26:00,520 --> 00:26:05,250 So the Koch curve theoretically, in math language, 426 00:26:05,250 --> 00:26:07,670 if you do it mathematically, it has infinite perimeter 427 00:26:07,670 --> 00:26:08,990 but finite area. 428 00:26:08,990 --> 00:26:11,270 But keep in mind, this is a theoretical creation. 429 00:26:11,270 --> 00:26:14,270 It's just in the world of math. 430 00:26:14,270 --> 00:26:18,350 And it can't really exist in the universe. 431 00:26:18,350 --> 00:26:21,516 But things come pretty close to it in nature. 432 00:26:21,516 --> 00:26:22,640 It's not actually infinite. 433 00:26:22,640 --> 00:26:25,190 The coast line of Britain is not actually infinite. 434 00:26:25,190 --> 00:26:30,320 But it really resembles this sort of shape. 435 00:26:30,320 --> 00:26:31,430 So let's see. 436 00:26:35,040 --> 00:26:36,030 Yeah. 437 00:26:36,030 --> 00:26:38,160 So any questions about the Koch curve? 438 00:26:38,160 --> 00:26:41,180 It's really interesting. 439 00:26:41,180 --> 00:26:46,865 So the next example is the Sierpinski triangle, page 8. 440 00:26:49,980 --> 00:26:54,522 So the Sierpinski triangle is very interesting. 441 00:26:54,522 --> 00:26:55,022 Whoa. 442 00:27:02,420 --> 00:27:09,082 You take a big triangle, and you add a smaller triangle 443 00:27:09,082 --> 00:27:10,040 inside of it like this. 444 00:27:13,500 --> 00:27:17,510 So this is the rule for the Sierpinski triangle. 445 00:27:17,510 --> 00:27:22,617 And this is colored in or something. 446 00:27:22,617 --> 00:27:23,450 So this is the rule. 447 00:27:23,450 --> 00:27:25,880 And what you get is three new triangles. 448 00:27:25,880 --> 00:27:29,840 And then you apply the same rule to these new triangles. 449 00:27:35,090 --> 00:27:38,900 So that's recursion, when you apply a rule to something 450 00:27:38,900 --> 00:27:41,120 that you already applied a rule to. 451 00:27:41,120 --> 00:27:46,940 So you apply it again, and you get these even smaller things, 452 00:27:46,940 --> 00:27:48,890 these smaller triangles. 453 00:27:48,890 --> 00:27:53,370 And it goes down infinitely, if you do it infinitely. 454 00:27:53,370 --> 00:27:54,830 So imagine this. 455 00:27:54,830 --> 00:27:57,570 So this is one way of computing it, one way of doing it, 456 00:27:57,570 --> 00:27:59,680 one way of looking at the rule. 457 00:27:59,680 --> 00:28:04,010 But what I did, if you look in the lecture notes, 458 00:28:04,010 --> 00:28:08,460 I talk about iterated function system. 459 00:28:08,460 --> 00:28:13,100 So that means-- well, I'll just do it, 460 00:28:13,100 --> 00:28:14,360 and you'll see what it means. 461 00:28:14,360 --> 00:28:16,017 So let's say we start with a point. 462 00:28:16,017 --> 00:28:16,850 Say this point here. 463 00:28:16,850 --> 00:28:17,766 It could be any point. 464 00:28:21,360 --> 00:28:22,950 And we have three possible choices. 465 00:28:22,950 --> 00:28:25,200 This is called the chaos game. 466 00:28:25,200 --> 00:28:27,360 We have three possible choices of something 467 00:28:27,360 --> 00:28:28,440 to do with this point. 468 00:28:28,440 --> 00:28:30,360 We either bring it halfway to this point, 469 00:28:30,360 --> 00:28:33,940 halfway to this point, or halfway to this point. 470 00:28:33,940 --> 00:28:36,120 So let's say we bring it halfway to this point. 471 00:28:36,120 --> 00:28:39,460 We go right here, right in the middle. 472 00:28:39,460 --> 00:28:41,640 And what we do, in the iterated function system, 473 00:28:41,640 --> 00:28:44,040 we do this over and over and over again, 474 00:28:44,040 --> 00:28:46,710 picking randomly which one of the three points 475 00:28:46,710 --> 00:28:48,930 we go halfway towards. 476 00:28:48,930 --> 00:28:50,370 So say we go to this one next. 477 00:28:50,370 --> 00:28:52,050 We go here, halfway. 478 00:28:52,050 --> 00:28:54,030 Then we go halfway to this one. 479 00:28:54,030 --> 00:28:55,370 Halfway to this one again. 480 00:28:55,370 --> 00:28:56,710 Halfway to this one again. 481 00:28:56,710 --> 00:28:59,840 And then halfway to this one. 482 00:28:59,840 --> 00:29:02,730 Then halfway to this one. 483 00:29:02,730 --> 00:29:04,616 Halfway to this one. 484 00:29:04,616 --> 00:29:06,490 And halfway to this one, halfway to this one, 485 00:29:06,490 --> 00:29:08,400 and halfway to this one. 486 00:29:08,400 --> 00:29:10,470 So we just plot these points. 487 00:29:10,470 --> 00:29:15,180 And the program that I wrote, which is in the handout, 488 00:29:15,180 --> 00:29:16,740 does this. 489 00:29:16,740 --> 00:29:18,960 It executes this. 490 00:29:18,960 --> 00:29:22,350 It just keeps going, and it keeps plotting the points. 491 00:29:22,350 --> 00:29:26,210 And eventually what you get is the Sierpinski triangle. 492 00:29:26,210 --> 00:29:26,730 It's crazy. 493 00:29:29,810 --> 00:29:34,720 So page 8, it's-- the picture of this is on page 8. 494 00:29:34,720 --> 00:29:35,599 Yeah. 495 00:29:35,599 --> 00:29:37,140 AUDIENCE: So when you do it randomly, 496 00:29:37,140 --> 00:29:38,764 it doesn't matter which one you choose? 497 00:29:38,764 --> 00:29:41,140 No matter what you choose, whenever you do [INAUDIBLE],, 498 00:29:41,140 --> 00:29:42,934 it's going to go to the same? 499 00:29:46,305 --> 00:29:48,680 [INAUDIBLE] started the program again, but now this time, 500 00:29:48,680 --> 00:29:53,650 since it's going to have random [INAUDIBLE] same triangle? 501 00:29:53,650 --> 00:29:55,220 CURRAN KELLEHER: Yeah, exactly. 502 00:29:55,220 --> 00:29:59,900 So he's getting at say you run the program again, 503 00:29:59,900 --> 00:30:01,365 it's going to choose differently. 504 00:30:01,365 --> 00:30:03,740 Maybe it will choose, instead of going to this one first, 505 00:30:03,740 --> 00:30:05,330 it'll go to this one first. 506 00:30:05,330 --> 00:30:10,120 Say it does it 10 times or 100 times to this one. 507 00:30:10,120 --> 00:30:15,580 But the program chooses randomly which one to go to. 508 00:30:15,580 --> 00:30:18,230 [INAUDIBLE] it just chooses randomly. 509 00:30:18,230 --> 00:30:20,420 So he asked, even though it's random, 510 00:30:20,420 --> 00:30:22,130 will it still generate the same picture? 511 00:30:22,130 --> 00:30:24,680 And the answer is yes, it will. 512 00:30:24,680 --> 00:30:25,410 It's crazy. 513 00:30:25,410 --> 00:30:28,250 It's just fascinating. 514 00:30:28,250 --> 00:30:30,500 And we'll understand this better after we 515 00:30:30,500 --> 00:30:34,790 do the next example, which is making a fern, a fractal fern, 516 00:30:34,790 --> 00:30:36,736 which is really cool. 517 00:30:36,736 --> 00:30:38,360 So let's just look at the code quickly. 518 00:30:42,015 --> 00:30:42,890 And think about this. 519 00:30:42,890 --> 00:30:47,120 Is the code for the Sierpinski triangle recursive? 520 00:30:47,120 --> 00:30:50,630 So what it does is, see def drawSierpinski. 521 00:30:50,630 --> 00:30:53,270 That's the thing that draws the triangle. 522 00:30:53,270 --> 00:30:56,580 Def a, b, and c are these three points. 523 00:30:56,580 --> 00:31:01,640 So this is like a, b, c. 524 00:31:06,110 --> 00:31:10,260 Def points equals a list of a, b, and c. 525 00:31:10,260 --> 00:31:14,580 Current point is just start at point a. 526 00:31:18,900 --> 00:31:23,850 This statement while true means, execute this code repeatedly. 527 00:31:23,850 --> 00:31:26,490 Just keep doing it an infinite number of times, 528 00:31:26,490 --> 00:31:28,710 until you stop the program. 529 00:31:28,710 --> 00:31:32,160 So it says def next point equals pick from points. 530 00:31:32,160 --> 00:31:34,560 And pick from is a function defined above, 531 00:31:34,560 --> 00:31:37,320 which pretty much picks at random out of the list 532 00:31:37,320 --> 00:31:39,180 that you give it. 533 00:31:39,180 --> 00:31:42,420 So that pick from is the thing that picks randomly which 534 00:31:42,420 --> 00:31:44,550 one of these to go to. 535 00:31:44,550 --> 00:31:47,190 So it says, next point pick from points. 536 00:31:47,190 --> 00:31:49,380 So that gets you a point. 537 00:31:49,380 --> 00:31:52,470 And then current point x equals current point x plus next point 538 00:31:52,470 --> 00:31:54,190 x divided by 2. 539 00:31:54,190 --> 00:31:57,840 So what you're doing is averaging the x-coordinates 540 00:31:57,840 --> 00:31:59,430 of the current point and the point 541 00:31:59,430 --> 00:32:01,340 that you're going to go to next. 542 00:32:01,340 --> 00:32:03,300 And if you do that for x and y, what you do 543 00:32:03,300 --> 00:32:06,270 is that's going halfway between the current point 544 00:32:06,270 --> 00:32:09,510 and the next point. 545 00:32:09,510 --> 00:32:10,720 That's what that does. 546 00:32:10,720 --> 00:32:14,110 And [? image.fillpixel.point. ?] So that's what 547 00:32:14,110 --> 00:32:16,600 plots it on the screen. 548 00:32:16,600 --> 00:32:19,390 And the picture here was actually 549 00:32:19,390 --> 00:32:20,830 generated by this very program. 550 00:32:23,890 --> 00:32:25,900 So it keeps going, keeps plotting it. 551 00:32:25,900 --> 00:32:27,160 So here's the question. 552 00:32:27,160 --> 00:32:28,150 Is this recursive? 553 00:32:28,150 --> 00:32:30,280 Is this thing actually recursive? 554 00:32:30,280 --> 00:32:32,050 Because we said a recursive function 555 00:32:32,050 --> 00:32:34,510 is a function that calls itself. 556 00:32:34,510 --> 00:32:37,840 Let's hold off on that answer until after we do the next one. 557 00:32:37,840 --> 00:32:40,900 Any questions so far? 558 00:32:40,900 --> 00:32:43,560 So the next thing we're going to do is the fern. 559 00:32:43,560 --> 00:32:44,261 Yeah. 560 00:32:44,261 --> 00:32:47,700 AUDIENCE: [INAUDIBLE]. 561 00:32:47,700 --> 00:32:52,080 CURRAN KELLEHER: So recursion is-- 562 00:32:52,080 --> 00:32:54,660 generally, recursion is something which 563 00:32:54,660 --> 00:32:55,970 is defined in terms of itself. 564 00:32:59,740 --> 00:33:01,663 Something that loops back on itself. 565 00:33:01,663 --> 00:33:02,629 AUDIENCE: [INAUDIBLE]? 566 00:33:06,126 --> 00:33:07,459 CURRAN KELLEHER: Say that again? 567 00:33:07,459 --> 00:33:09,149 AUDIENCE: If you apply a rule-- it 568 00:33:09,149 --> 00:33:11,440 will become the same thing you started with. 569 00:33:11,440 --> 00:33:13,445 CURRAN KELLEHER: If you apply which rule? 570 00:33:13,445 --> 00:33:13,840 AUDIENCE: Any rule. 571 00:33:13,840 --> 00:33:14,660 CURRAN KELLEHER: The recursive rule? 572 00:33:14,660 --> 00:33:14,950 AUDIENCE: Yeah. 573 00:33:14,950 --> 00:33:16,866 CURRAN KELLEHER: It will become the same thing 574 00:33:16,866 --> 00:33:18,260 that it started with? 575 00:33:18,260 --> 00:33:19,430 No, not necessarily. 576 00:33:19,430 --> 00:33:25,780 Because each time it applies the rule, there is a slight change. 577 00:33:25,780 --> 00:33:28,250 With this factorial, it's not just 578 00:33:28,250 --> 00:33:31,550 saying n factorial equals n factorial. 579 00:33:31,550 --> 00:33:36,770 It's saying n factorial equals n minus 1 factorial. 580 00:33:36,770 --> 00:33:39,320 The factorial part is recursive, but it doesn't loop back 581 00:33:39,320 --> 00:33:41,330 on itself exactly. 582 00:33:41,330 --> 00:33:43,640 There's some change. 583 00:33:43,640 --> 00:33:46,430 And with recursive functions, at least-- 584 00:33:49,640 --> 00:33:52,611 so the function is defined, it calls itself. 585 00:33:52,611 --> 00:33:54,860 I don't know if this will really answer your question, 586 00:33:54,860 --> 00:33:58,230 but say if we didn't have these parts-- 587 00:33:58,230 --> 00:34:01,510 if the function just was this, def Fibonacci, 588 00:34:01,510 --> 00:34:05,510 return Fibonacci of n minus 1 n minus 2, what would happen 589 00:34:05,510 --> 00:34:08,870 is, it would call itself and just 590 00:34:08,870 --> 00:34:12,530 keep going infinitely, which is a problem. 591 00:34:12,530 --> 00:34:16,165 So all recursive functions, in order to do anything, 592 00:34:16,165 --> 00:34:17,540 have to bottom out at some point. 593 00:34:17,540 --> 00:34:18,290 They have to stop. 594 00:34:18,290 --> 00:34:20,420 And that's what this is. 595 00:34:20,420 --> 00:34:22,880 Only do this if n is greater than 1. 596 00:34:22,880 --> 00:34:27,040 If n is less than or equal to 1, return. 597 00:34:27,040 --> 00:34:28,940 So this stops it. 598 00:34:28,940 --> 00:34:34,540 So does that answer your question? 599 00:34:34,540 --> 00:34:36,000 You can ask it again, if you want. 600 00:34:36,000 --> 00:34:41,650 AUDIENCE: So for the triangle [INAUDIBLE] 601 00:34:41,650 --> 00:34:43,080 might be a recursion? 602 00:34:43,080 --> 00:34:44,324 CURRAN KELLEHER: It is. 603 00:34:44,324 --> 00:34:46,230 AUDIENCE: Because it has to return 604 00:34:46,230 --> 00:34:47,748 to some place [INAUDIBLE]. 605 00:34:52,409 --> 00:34:55,110 CURRAN KELLEHER: Only functions, computer functions, 606 00:34:55,110 --> 00:34:57,600 that are recursive need to bottom out. 607 00:34:57,600 --> 00:34:59,507 There are other kinds of recursion. 608 00:34:59,507 --> 00:35:03,170 AUDIENCE: Natural recursions [INAUDIBLE]?? 609 00:35:03,170 --> 00:35:04,920 CURRAN KELLEHER: Well, they do like. 610 00:35:04,920 --> 00:35:06,420 Natural recursions don't bottom out. 611 00:35:06,420 --> 00:35:09,620 Well, they do have to bottom out eventually. 612 00:35:09,620 --> 00:35:11,330 Take, for example, a tree in nature. 613 00:35:11,330 --> 00:35:12,980 It branches and branches and branches. 614 00:35:12,980 --> 00:35:14,930 But eventually it just gets to a leaf. 615 00:35:14,930 --> 00:35:16,430 And it stops branching. 616 00:35:16,430 --> 00:35:19,730 There's some sort of program that's executing inside 617 00:35:19,730 --> 00:35:22,160 of this tree that's recursive. 618 00:35:22,160 --> 00:35:25,850 And there's a certain signal that this program gets 619 00:35:25,850 --> 00:35:28,070 when the branch gets to a certain size, I think, 620 00:35:28,070 --> 00:35:29,190 or something. 621 00:35:29,190 --> 00:35:30,800 That signals it to generate a leaf. 622 00:35:30,800 --> 00:35:33,680 So I mean, those kind of recursive processes 623 00:35:33,680 --> 00:35:35,180 do bottom out. 624 00:35:35,180 --> 00:35:37,100 But this one is actually recursive. 625 00:35:37,100 --> 00:35:38,390 We'll see why in a minute. 626 00:35:38,390 --> 00:35:40,250 But it doesn't bottom out because it just 627 00:35:40,250 --> 00:35:42,414 keeps going forever. 628 00:35:42,414 --> 00:35:45,530 AUDIENCE: Do you mean that when you 629 00:35:45,530 --> 00:35:48,375 have [INAUDIBLE] they don't [INAUDIBLE] practical purposes 630 00:35:48,375 --> 00:35:49,824 like [INAUDIBLE]? 631 00:36:04,070 --> 00:36:06,164 But if you have to get a result from the program, 632 00:36:06,164 --> 00:36:08,404 then you have to say [INAUDIBLE].. 633 00:36:08,404 --> 00:36:09,320 CURRAN KELLEHER: Yeah. 634 00:36:09,320 --> 00:36:10,070 Exactly. 635 00:36:10,070 --> 00:36:10,580 Exactly. 636 00:36:10,580 --> 00:36:12,470 Yeah, that was very well put. 637 00:36:12,470 --> 00:36:14,780 So what he basically said was, if you 638 00:36:14,780 --> 00:36:19,190 want to get any real manifestation of recursion, 639 00:36:19,190 --> 00:36:22,280 it has to bottom out in order to return you the result. 640 00:36:22,280 --> 00:36:24,440 But yeah, you can imagine theoretical worlds 641 00:36:24,440 --> 00:36:28,040 where it doesn't bottom out, goes on forever. 642 00:36:28,040 --> 00:36:30,440 Actually, Douglas Hofstadter in the dialogue 643 00:36:30,440 --> 00:36:32,640 before this chapter does that. 644 00:36:32,640 --> 00:36:35,750 A genie has to ask a meta genie has to ask a meta genie. 645 00:36:35,750 --> 00:36:37,128 Did anybody read that? 646 00:36:37,128 --> 00:36:39,682 AUDIENCE: Yeah, but every time it does it, 647 00:36:39,682 --> 00:36:41,390 it gets smaller and smaller [INAUDIBLE].. 648 00:36:41,390 --> 00:36:42,150 CURRAN KELLEHER: Yeah. 649 00:36:42,150 --> 00:36:43,483 The time is smaller and smaller. 650 00:36:43,483 --> 00:36:46,950 So eventually, it would just repeat an infinite number 651 00:36:46,950 --> 00:36:48,080 of intensely small-- 652 00:36:48,080 --> 00:36:50,690 AUDIENCE: Yeah, but still only lasts a finite amount of time. 653 00:36:50,690 --> 00:36:51,690 CURRAN KELLEHER: Yeah. 654 00:36:51,690 --> 00:36:52,190 Yeah. 655 00:36:52,190 --> 00:36:54,300 Isn't that crazy? 656 00:36:54,300 --> 00:36:56,460 Yeah, it's really something to think about. 657 00:36:56,460 --> 00:36:57,490 Yeah. 658 00:36:57,490 --> 00:36:59,860 But it's theoretical. 659 00:36:59,860 --> 00:37:02,700 But it is very interesting to think about. 660 00:37:02,700 --> 00:37:05,490 So we'll see how this is recursive after we do the fern. 661 00:37:05,490 --> 00:37:07,310 So the fractal fern is next, page 9. 662 00:37:12,030 --> 00:37:14,650 Yeah, this is really, really cool. 663 00:37:26,630 --> 00:37:27,940 So the fractal fern. 664 00:37:27,940 --> 00:37:30,880 It looks beautiful, doesn't it? 665 00:37:30,880 --> 00:37:38,390 So we have this triangle. 666 00:37:38,390 --> 00:37:40,450 This isn't the picture in the handout. 667 00:37:40,450 --> 00:37:42,790 There's an outside triangle that's black. 668 00:37:42,790 --> 00:37:46,030 And then there's an inside triangle that's blue 669 00:37:46,030 --> 00:37:49,809 and some other triangles that [INAUDIBLE] leaves. 670 00:37:49,809 --> 00:37:51,850 So first of all, I want to talk about this notion 671 00:37:51,850 --> 00:37:53,396 of a coordinate transformation. 672 00:37:59,000 --> 00:38:01,130 Say we had a coordinate transformation 673 00:38:01,130 --> 00:38:05,100 between this Rectangle And this rectangle. 674 00:38:05,100 --> 00:38:06,800 What would happen-- it's a function 675 00:38:06,800 --> 00:38:09,710 on a point, a two-dimensional point. 676 00:38:09,710 --> 00:38:14,300 So say you had a point right here, this point. 677 00:38:14,300 --> 00:38:16,640 You apply this transformation to the point. 678 00:38:16,640 --> 00:38:18,290 And what you get is this point here. 679 00:38:21,360 --> 00:38:26,180 So you had this point here, and you apply the transformation 680 00:38:26,180 --> 00:38:30,080 to that point, you'll get this point here. 681 00:38:30,080 --> 00:38:33,390 It's just like a little copy of this. 682 00:38:33,390 --> 00:38:35,120 OK. 683 00:38:35,120 --> 00:38:38,250 So this is a function. 684 00:38:38,250 --> 00:38:42,140 This is the function part of iterated function systems. 685 00:38:42,140 --> 00:38:45,041 Iteration is the fact that you just keep doing it. 686 00:38:45,041 --> 00:38:47,540 And a system is the fact that there's more than one of them. 687 00:38:47,540 --> 00:38:48,950 It's like a bunch. 688 00:38:48,950 --> 00:38:51,170 In this case, it's three. 689 00:38:51,170 --> 00:38:53,960 I'll talk about it in a minute. 690 00:38:53,960 --> 00:39:07,055 So with a fractal fern, what we have is a rectangle like this. 691 00:39:14,050 --> 00:39:20,170 So the fractal fern is an iterative function system that 692 00:39:20,170 --> 00:39:23,270 has four possible functions. 693 00:39:23,270 --> 00:39:24,940 This one has three. 694 00:39:24,940 --> 00:39:27,400 Each one of those points is a function. 695 00:39:27,400 --> 00:39:29,600 Going halfway to one of those points is a function. 696 00:39:29,600 --> 00:39:32,950 But in the fern one, there are four functions. 697 00:39:32,950 --> 00:39:35,640 This is one of them. 698 00:39:35,640 --> 00:39:38,167 And each function is a coordinate transformation. 699 00:39:38,167 --> 00:39:40,000 This function is a coordinate transformation 700 00:39:40,000 --> 00:39:42,480 from this outer one, this outer rectangle 701 00:39:42,480 --> 00:39:44,930 to this inner rectangle. 702 00:39:44,930 --> 00:39:47,500 So if we had this point in this rectangle, 703 00:39:47,500 --> 00:39:50,200 and we applied this transformation, 704 00:39:50,200 --> 00:39:51,490 we would get this point here. 705 00:39:55,430 --> 00:40:03,120 So say we have the middle point of this outer rectangle, 706 00:40:03,120 --> 00:40:06,870 and we apply this transformation once. 707 00:40:06,870 --> 00:40:09,690 We would get the middle point of this inner rectangle, which 708 00:40:09,690 --> 00:40:10,860 is about right here. 709 00:40:14,440 --> 00:40:18,450 But here what we do is we say, OK, now 710 00:40:18,450 --> 00:40:22,350 this new point is actually in the outer rectangle. 711 00:40:22,350 --> 00:40:26,250 And we'll apply the transformation again on that. 712 00:40:26,250 --> 00:40:29,340 So this point in the outer rectangle maps 713 00:40:29,340 --> 00:40:34,650 to about this point in the smaller rectangle. 714 00:40:34,650 --> 00:40:37,890 And you say, OK, this point is now a point in this one, 715 00:40:37,890 --> 00:40:39,700 and you apply it again. 716 00:40:39,700 --> 00:40:41,820 And you get this point here and this point here 717 00:40:41,820 --> 00:40:46,440 and this point here and all these little points. 718 00:40:46,440 --> 00:40:49,920 And eventually, they just get smaller and smaller 719 00:40:49,920 --> 00:40:55,290 because the corner points of these two rectangles 720 00:40:55,290 --> 00:41:00,792 are at the same point I think [INAUDIBLE].. 721 00:41:00,792 --> 00:41:02,000 So let's look at another one. 722 00:41:04,270 --> 00:41:04,770 Let's see. 723 00:41:16,550 --> 00:41:23,118 So this is the thing that's going to map to. 724 00:41:23,118 --> 00:41:27,650 It's a line, but think of it as a rectangle. 725 00:41:27,650 --> 00:41:29,580 It's a coordinate transformation. 726 00:41:29,580 --> 00:41:35,270 So we go from any point in this outer rectangle 727 00:41:35,270 --> 00:41:36,810 to a point on here. 728 00:41:36,810 --> 00:41:40,700 So say if we have this point here in the outer rectangle, 729 00:41:40,700 --> 00:41:43,770 it's going to go to the top point of this line. 730 00:41:43,770 --> 00:41:45,375 If we have this point here, it's going 731 00:41:45,375 --> 00:41:47,000 to go to the bottom point of this line. 732 00:41:47,000 --> 00:41:48,416 If we have the center point, it'll 733 00:41:48,416 --> 00:41:50,820 go to the center of this line. 734 00:41:50,820 --> 00:41:54,350 So let's imagine an iterated function system 735 00:41:54,350 --> 00:41:55,910 with only these two functions. 736 00:41:55,910 --> 00:42:01,380 What's going to happen is-- and let's say each one is chosen-- 737 00:42:01,380 --> 00:42:05,340 well, let's say each one is chosen about half the time 738 00:42:05,340 --> 00:42:05,990 randomly. 739 00:42:05,990 --> 00:42:08,310 It picks between each one. 740 00:42:08,310 --> 00:42:11,340 Or no, let's say that it applies this mapping 741 00:42:11,340 --> 00:42:13,740 about 90% of the time. 742 00:42:13,740 --> 00:42:16,120 So say we start with this point here. 743 00:42:16,120 --> 00:42:20,030 It'll map to this point here and this point here, 744 00:42:20,030 --> 00:42:22,980 and it'll just go up and up into here, 745 00:42:22,980 --> 00:42:27,090 as long as this transformation is being applied. 746 00:42:27,090 --> 00:42:30,960 But say it does that like 100 times, 747 00:42:30,960 --> 00:42:34,367 and then one time it goes down to here. 748 00:42:34,367 --> 00:42:36,700 So say it's all the way up here, and we map to this one. 749 00:42:36,700 --> 00:42:38,170 It's going to start here. 750 00:42:38,170 --> 00:42:40,050 And it's going to map here. 751 00:42:40,050 --> 00:42:41,130 It's going to go up. 752 00:42:41,130 --> 00:42:44,580 And say we [? stop ?] at this point the computer decides 753 00:42:44,580 --> 00:42:46,740 to map to here. 754 00:42:46,740 --> 00:42:48,780 It's about [? 3/4 ?] up. 755 00:42:48,780 --> 00:42:51,840 It's going to go to [? 3/4 ?] up here. 756 00:42:51,840 --> 00:42:57,540 So because it's random, if you do this just forever, 757 00:42:57,540 --> 00:43:00,270 it's going to eventually fill in pretty much every point 758 00:43:00,270 --> 00:43:04,866 on this line, which is very interesting to think about. 759 00:43:04,866 --> 00:43:05,740 Any questions so far? 760 00:43:08,660 --> 00:43:14,880 So let's have this rectangle. 761 00:43:14,880 --> 00:43:18,140 All four of the transformations are from the outer rectangle, 762 00:43:18,140 --> 00:43:20,089 this big one, to one of the ones inside, 763 00:43:20,089 --> 00:43:21,380 one of the smaller ones inside. 764 00:43:24,620 --> 00:43:29,210 So say we have this point up here, 765 00:43:29,210 --> 00:43:31,550 and we apply this transformation to this one. 766 00:43:31,550 --> 00:43:33,050 It's going to go to this point here. 767 00:43:35,610 --> 00:43:37,350 Make sense? 768 00:43:37,350 --> 00:43:40,390 And if we have this one, it's going to go to this point here. 769 00:43:40,390 --> 00:43:44,500 So the transformation is pretty much going like this. 770 00:43:44,500 --> 00:43:48,660 So say our IFS, Iterated Function System, 771 00:43:48,660 --> 00:43:51,090 has these three. 772 00:43:51,090 --> 00:43:53,260 Say we go about halfway up here, and we 773 00:43:53,260 --> 00:43:54,660 choose to apply this mapping. 774 00:43:54,660 --> 00:43:57,870 It's going to go about here, the middle of this one. 775 00:43:57,870 --> 00:44:00,750 And then we apply this mapping a bunch of times, 776 00:44:00,750 --> 00:44:03,370 just keep going up and up and up. 777 00:44:03,370 --> 00:44:05,100 We apply this mapping again. 778 00:44:05,100 --> 00:44:07,140 But it's closer to the top this time. 779 00:44:07,140 --> 00:44:09,540 So it's going to be out here, closer 780 00:44:09,540 --> 00:44:12,080 to the top of this rectangle. 781 00:44:12,080 --> 00:44:14,490 And then we apply it again and again and again. 782 00:44:14,490 --> 00:44:17,070 And maybe it only goes up 1. 783 00:44:17,070 --> 00:44:18,420 So it will be down here. 784 00:44:18,420 --> 00:44:25,380 So eventually, it's going to fill in this rectangle 785 00:44:25,380 --> 00:44:26,952 with this pattern. 786 00:44:26,952 --> 00:44:28,160 It's going to look like this. 787 00:44:32,190 --> 00:44:36,570 And this-- since every point in here 788 00:44:36,570 --> 00:44:39,090 is probably going to get applied to this 789 00:44:39,090 --> 00:44:40,530 mapping a bunch of times. 790 00:44:40,530 --> 00:44:47,925 So what we're going to get is this fern structure. 791 00:44:52,380 --> 00:44:54,090 OK. 792 00:44:54,090 --> 00:44:56,376 Isn't that really cool? 793 00:44:56,376 --> 00:45:02,160 AUDIENCE: [INAUDIBLE] small triangle 794 00:45:02,160 --> 00:45:04,088 the points that you have in there 795 00:45:04,088 --> 00:45:09,807 apply to the biggest point [INAUDIBLE].. 796 00:45:09,807 --> 00:45:12,390 CURRAN KELLEHER: Are you talking about the Sierpinski triangle 797 00:45:12,390 --> 00:45:14,284 or the fern? 798 00:45:14,284 --> 00:45:15,075 AUDIENCE: The fern. 799 00:45:15,075 --> 00:45:17,865 If you have a bunch of points [INAUDIBLE].. 800 00:45:30,180 --> 00:45:31,614 CURRAN KELLEHER: Yeah, exactly. 801 00:45:31,614 --> 00:45:36,482 AUDIENCE: [INAUDIBLE] smaller and smaller [INAUDIBLE].. 802 00:45:36,482 --> 00:45:37,440 CURRAN KELLEHER: Right. 803 00:45:37,440 --> 00:45:38,830 You can't, actually. 804 00:45:38,830 --> 00:45:41,140 And this is-- 805 00:45:41,140 --> 00:45:45,220 OK, I'll go through an example. 806 00:45:45,220 --> 00:45:48,430 Say we have-- instead of just applying it to one point, 807 00:45:48,430 --> 00:45:50,800 we apply it to a bunch of points. 808 00:45:50,800 --> 00:45:56,770 Say all the points on this line, or almost all points. 809 00:45:56,770 --> 00:46:00,520 Say we apply this mapping a bunch of times. 810 00:46:00,520 --> 00:46:03,350 So we get this one, this one, this one, this one. 811 00:46:06,680 --> 00:46:11,480 And say we apply this mapping to all of these points. 812 00:46:11,480 --> 00:46:13,820 It's going to map to here. 813 00:46:13,820 --> 00:46:16,850 And then we do that again and again and again. 814 00:46:16,850 --> 00:46:20,750 And then what we have, say all those points, eventually 815 00:46:20,750 --> 00:46:23,370 are going to get mapped to this one. 816 00:46:23,370 --> 00:46:27,330 So you have a whole little copy of this thing in here. 817 00:46:27,330 --> 00:46:32,190 So you have these little branches. 818 00:46:32,190 --> 00:46:34,440 And then that is going to be mapped up here. 819 00:46:34,440 --> 00:46:37,070 And [INAUDIBLE] branches, branches, branches. 820 00:46:37,070 --> 00:46:41,900 And then since you have the smaller branches here, 821 00:46:41,900 --> 00:46:43,470 you have two levels down. 822 00:46:43,470 --> 00:46:46,800 And then this whole thing gets mapped back on to here, 823 00:46:46,800 --> 00:46:49,220 including the new branches. 824 00:46:49,220 --> 00:46:54,720 And it gets filled in as it goes on. 825 00:46:54,720 --> 00:47:00,640 But it doesn't just keep going up to here and just-- 826 00:47:00,640 --> 00:47:03,040 say we were to apply this mapping 100% of the time. 827 00:47:03,040 --> 00:47:05,460 It would just go up here and go nowhere else. 828 00:47:05,460 --> 00:47:10,250 But say we apply this mapping 7% of the time. 829 00:47:10,250 --> 00:47:12,060 It'll go up different lengths. 830 00:47:12,060 --> 00:47:16,460 And then it'll map back down there and go up and back down. 831 00:47:16,460 --> 00:47:20,560 And if we add this transformation, 832 00:47:20,560 --> 00:47:25,460 going from this one to this one, we have this. 833 00:47:25,460 --> 00:47:27,260 And it's recursive. 834 00:47:27,260 --> 00:47:30,710 The reason why it's recursive is because the mappings 835 00:47:30,710 --> 00:47:34,640 map onto themselves eventually. 836 00:47:34,640 --> 00:47:36,260 If we look at the code, it's really 837 00:47:36,260 --> 00:47:41,156 similar to the Sierpinski triangle code. 838 00:47:41,156 --> 00:47:42,530 The code itself is not recursive, 839 00:47:42,530 --> 00:47:44,770 but the mappings are. 840 00:47:44,770 --> 00:47:49,462 And that's what makes this whole thing recursive. 841 00:47:52,910 --> 00:47:55,270 So yeah, let's just look at the code a little bit. 842 00:47:55,270 --> 00:47:57,600 [? Class ?] fern. 843 00:47:57,600 --> 00:48:01,740 So [? drawFern, ?] while true-- 844 00:48:01,740 --> 00:48:03,290 so that means execute this. 845 00:48:03,290 --> 00:48:05,840 Just keep doing it over and over and over-- 846 00:48:05,840 --> 00:48:10,500 pick from these list of transformations. 847 00:48:10,500 --> 00:48:12,860 So the first one says maps to the stem. 848 00:48:12,860 --> 00:48:16,760 That means it goes from here to here, to the stem. 849 00:48:16,760 --> 00:48:19,640 The second one maps to the left branch, meaning it 850 00:48:19,640 --> 00:48:21,260 goes from here to this one. 851 00:48:21,260 --> 00:48:23,510 The third one maps to the right branch. 852 00:48:23,510 --> 00:48:25,070 It goes down here. 853 00:48:25,070 --> 00:48:27,860 And the fourth one maps from here to here. 854 00:48:27,860 --> 00:48:30,830 It just goes up like that. 855 00:48:30,830 --> 00:48:35,910 And following that, it says pick from a list of functions. 856 00:48:35,910 --> 00:48:38,150 And then a list of probabilities. 857 00:48:38,150 --> 00:48:42,180 It says 0.01, [? 0.07, ?] [? 0.07, ?] 0.85. 858 00:48:42,180 --> 00:48:45,620 So these are the probabilities at which 859 00:48:45,620 --> 00:48:48,530 these mapping functions are going to be chosen. 860 00:48:48,530 --> 00:48:51,560 If you choose them all equally, then the chances 861 00:48:51,560 --> 00:48:54,920 are very low that this mapping is 862 00:48:54,920 --> 00:48:58,070 going to occur more than like five times or something. 863 00:48:58,070 --> 00:49:00,080 So you just go up here and get mapped. 864 00:49:00,080 --> 00:49:03,770 So it would be a very sparse fractal. 865 00:49:03,770 --> 00:49:05,881 Most of the points would be down here. 866 00:49:05,881 --> 00:49:07,130 It just didn't look very good. 867 00:49:07,130 --> 00:49:07,630 I tried it. 868 00:49:07,630 --> 00:49:09,860 I wish I could code it and show you. 869 00:49:12,960 --> 00:49:16,130 So the 0.01 means that the mapping to the stem 870 00:49:16,130 --> 00:49:18,242 happens 1% of the time. 871 00:49:18,242 --> 00:49:19,700 The mapping to each of the branches 872 00:49:19,700 --> 00:49:21,110 happens 7% of the time. 873 00:49:21,110 --> 00:49:25,898 And the mapping up and spiraling happens 85% of the time. 874 00:49:28,590 --> 00:49:33,640 So I'm going to take a break now for 10 minutes. 875 00:49:33,640 --> 00:49:36,480 Any questions? 876 00:49:36,480 --> 00:49:39,000 So I guess we'll start up again. 877 00:49:43,550 --> 00:49:45,560 Before we leave these iterated function systems, 878 00:49:45,560 --> 00:49:49,310 I want to point out how the Sierpinski triangle is 879 00:49:49,310 --> 00:49:53,270 pretty much the same thing as the ferns in terms of mappings. 880 00:50:05,510 --> 00:50:09,560 With a Sierpinski triangle, we have these three mappings. 881 00:50:09,560 --> 00:50:15,261 One of them maps from this triangle to this triangle here. 882 00:50:18,420 --> 00:50:21,080 One of them maps from the big triangle to this triangle. 883 00:50:21,080 --> 00:50:24,450 And the other one maps from this triangle to this triangle. 884 00:50:24,450 --> 00:50:30,010 And each one of them are applied with equal probability. 885 00:50:30,010 --> 00:50:32,280 So if you think about it, going halfway 886 00:50:32,280 --> 00:50:35,290 from any of these points to one of the other points 887 00:50:35,290 --> 00:50:39,870 is essentially mapping it down. 888 00:50:39,870 --> 00:50:43,290 So say you have this one, this line. 889 00:50:43,290 --> 00:50:46,740 You apply the function to all these points. 890 00:50:46,740 --> 00:50:52,350 What it does is maps it down to that triangle. 891 00:50:52,350 --> 00:50:56,160 So you could see these triangles map onto themselves 892 00:50:56,160 --> 00:50:57,110 recursively. 893 00:51:00,190 --> 00:51:02,110 And that's why it actually-- there 894 00:51:02,110 --> 00:51:04,210 is a recursion going on here, but it's not 895 00:51:04,210 --> 00:51:05,020 in the code itself. 896 00:51:05,020 --> 00:51:07,150 The code itself is just repetitive. 897 00:51:07,150 --> 00:51:09,860 But what it's repeating is these recursive mappings 898 00:51:09,860 --> 00:51:11,542 onto themselves. 899 00:51:11,542 --> 00:51:12,474 Yeah. 900 00:51:12,474 --> 00:51:13,872 AUDIENCE: [INAUDIBLE]. 901 00:51:15,959 --> 00:51:18,000 CURRAN KELLEHER: There's always more than one way 902 00:51:18,000 --> 00:51:19,758 to represent an algorithm. 903 00:51:22,000 --> 00:51:22,500 Yeah. 904 00:51:22,500 --> 00:51:24,240 It's really fascinating, especially 905 00:51:24,240 --> 00:51:26,010 with these fractals and things. 906 00:51:26,010 --> 00:51:29,010 It seems like there's always another way 907 00:51:29,010 --> 00:51:31,650 to do it to achieve the same result. 908 00:51:31,650 --> 00:51:34,230 And that's one thing, especially Sierpinski's triangle. 909 00:51:34,230 --> 00:51:35,508 It's really amazing. 910 00:51:35,508 --> 00:51:36,912 AUDIENCE: [INAUDIBLE]. 911 00:51:39,560 --> 00:51:40,843 CURRAN KELLEHER: In my what? 912 00:51:40,843 --> 00:51:45,870 AUDIENCE: [INAUDIBLE] you don't have a recursive function. 913 00:51:45,870 --> 00:51:47,130 So [INAUDIBLE]? 914 00:51:52,027 --> 00:51:52,860 CURRAN KELLEHER: Ah. 915 00:51:52,860 --> 00:51:55,460 So how can you figure out if the output 916 00:51:55,460 --> 00:51:58,454 is going to be recursive without running the code? 917 00:51:58,454 --> 00:52:00,120 AUDIENCE: [INAUDIBLE] you have the code, 918 00:52:00,120 --> 00:52:02,222 but it has no recursive function. 919 00:52:02,222 --> 00:52:02,722 Yeah. 920 00:52:02,722 --> 00:52:06,439 You have [INAUDIBLE] recursive function [INAUDIBLE] how can 921 00:52:06,439 --> 00:52:07,980 you find out that the output is going 922 00:52:07,980 --> 00:52:11,630 to be recursive without actually [INAUDIBLE]?? 923 00:52:11,630 --> 00:52:14,660 CURRAN KELLEHER: So he said since the code 924 00:52:14,660 --> 00:52:17,960 itself is not recursive, how do you know that the output is 925 00:52:17,960 --> 00:52:18,877 going to be recursive? 926 00:52:18,877 --> 00:52:21,418 AUDIENCE: [INAUDIBLE] you don't have any recursive functions. 927 00:52:21,418 --> 00:52:23,881 CURRAN KELLEHER: Even without any recursive functions. 928 00:52:23,881 --> 00:52:24,380 Right. 929 00:52:24,380 --> 00:52:27,530 I mean, the thing is, when you write the code, 930 00:52:27,530 --> 00:52:29,570 if you realize that what the code is doing 931 00:52:29,570 --> 00:52:35,102 is mapping these two-dimensional mappings, 932 00:52:35,102 --> 00:52:36,560 you can sort of think in your head, 933 00:52:36,560 --> 00:52:39,530 OK, these mappings are going to be 934 00:52:39,530 --> 00:52:41,120 applied with equal probability. 935 00:52:46,340 --> 00:52:51,756 So just by mentally extrapolating in your head-- 936 00:52:51,756 --> 00:52:53,630 so you're sort of stepping out of the system. 937 00:52:53,630 --> 00:52:56,010 You're saying, OK, what's the thing actually doing? 938 00:52:56,010 --> 00:52:57,926 AUDIENCE: But are you even allowed to do that? 939 00:52:57,926 --> 00:53:01,017 I mean, can you just [INAUDIBLE] in the system? 940 00:53:01,017 --> 00:53:02,850 CURRAN KELLEHER: While you're in the system? 941 00:53:02,850 --> 00:53:04,760 Well, being in the system in this case 942 00:53:04,760 --> 00:53:07,640 is actually running the code and being 943 00:53:07,640 --> 00:53:10,420 a computer program running it. 944 00:53:10,420 --> 00:53:12,650 Stepping out of the system, but what I mean by that 945 00:53:12,650 --> 00:53:16,350 is taking a higher level view of what's going on. 946 00:53:16,350 --> 00:53:22,460 AUDIENCE: So when you abstract [INAUDIBLE] which 947 00:53:22,460 --> 00:53:23,764 actually does show recursion. 948 00:53:23,764 --> 00:53:24,680 CURRAN KELLEHER: Yeah. 949 00:53:24,680 --> 00:53:26,000 Exactly. 950 00:53:26,000 --> 00:53:27,610 Exactly. 951 00:53:27,610 --> 00:53:28,550 Yes, that's it. 952 00:53:28,550 --> 00:53:31,040 So he said when you abstract away 953 00:53:31,040 --> 00:53:33,140 from all the details of what's going on 954 00:53:33,140 --> 00:53:36,140 with the actual functions, you begin 955 00:53:36,140 --> 00:53:40,310 to perceive this higher level thing, which 956 00:53:40,310 --> 00:53:42,270 is itself recursion. 957 00:53:42,270 --> 00:53:42,770 Yeah. 958 00:53:42,770 --> 00:53:44,480 And has recursion in it. 959 00:53:44,480 --> 00:53:50,790 So if you abstract your thought process significantly enough, 960 00:53:50,790 --> 00:53:52,820 you'll be able to logically tell that it's going 961 00:53:52,820 --> 00:53:56,340 to create this recursive shape. 962 00:53:56,340 --> 00:53:57,063 Yeah. 963 00:53:57,063 --> 00:53:59,961 AUDIENCE: But it's not like a simple algorithm [INAUDIBLE].. 964 00:54:02,954 --> 00:54:03,870 CURRAN KELLEHER: Yeah. 965 00:54:03,870 --> 00:54:05,270 So you mean like a test? 966 00:54:05,270 --> 00:54:06,650 AUDIENCE: [INAUDIBLE]. 967 00:54:06,650 --> 00:54:07,010 CURRAN KELLEHER: No. 968 00:54:07,010 --> 00:54:08,135 You have to think about it. 969 00:54:08,135 --> 00:54:09,750 That's what humans can do. 970 00:54:09,750 --> 00:54:12,729 Like there's no strict test that could 971 00:54:12,729 --> 00:54:15,020 be applied to some computer program that would tell you 972 00:54:15,020 --> 00:54:18,494 whether or not it's going to create a recursive result. 973 00:54:18,494 --> 00:54:20,660 Because in this case, there's no recursive function. 974 00:54:20,660 --> 00:54:26,210 So the computer can't know, OK, can I abstract into-- 975 00:54:26,210 --> 00:54:29,620 only humans can do that yet, so far. 976 00:54:29,620 --> 00:54:33,570 And it's not coded up as a system of mappings. 977 00:54:33,570 --> 00:54:36,902 It's just these simple functions. 978 00:54:36,902 --> 00:54:39,392 I don't know. 979 00:54:39,392 --> 00:54:40,890 So yeah. 980 00:54:40,890 --> 00:54:42,860 It's really cool. 981 00:54:42,860 --> 00:54:45,650 So yeah, if you just think about mapping, mapping, mapping. 982 00:54:45,650 --> 00:54:47,540 Then you map it over here, all the stuff 983 00:54:47,540 --> 00:54:49,160 that was in here that goes infinitely 984 00:54:49,160 --> 00:54:55,160 down is now in this little subsection 985 00:54:55,160 --> 00:54:57,140 of this little triangle. 986 00:54:57,140 --> 00:54:58,820 And you just-- yeah. 987 00:54:58,820 --> 00:55:01,054 It's a recursive structure. 988 00:55:01,054 --> 00:55:02,720 So it's going to the next example, which 989 00:55:02,720 --> 00:55:04,730 is very interesting, the Mandelbrot set. 990 00:55:07,310 --> 00:55:09,470 First, now that we have this up and running, 991 00:55:09,470 --> 00:55:12,470 I just want to run the programs that 992 00:55:12,470 --> 00:55:17,490 are in the handout to give you a sense of what's going on. 993 00:55:17,490 --> 00:55:19,880 So the recursive transition network, let's 994 00:55:19,880 --> 00:55:22,950 run it and see what happens. 995 00:55:22,950 --> 00:55:24,680 It's going to generate 100 sentences. 996 00:55:24,680 --> 00:55:28,550 Because if you look at the code, there's 997 00:55:28,550 --> 00:55:30,860 a print statement that goes 100 times. 998 00:55:34,770 --> 00:55:36,260 Yeah, page 5. 999 00:55:36,260 --> 00:55:41,990 It says, [? 0to100.each, ?] print line, call the function 1000 00:55:41,990 --> 00:55:42,590 fancy noun. 1001 00:55:42,590 --> 00:55:45,950 So this is just fancy notation in the Groovy programming 1002 00:55:45,950 --> 00:55:49,100 language to-- oh, crap. 1003 00:55:49,100 --> 00:55:50,950 It's not working. 1004 00:55:50,950 --> 00:55:51,450 Oh well. 1005 00:55:51,450 --> 00:55:55,614 I'll just try to get this to work as I'm talking. 1006 00:55:55,614 --> 00:55:56,795 I can't do the examples. 1007 00:56:03,380 --> 00:56:07,410 Yeah, I've been having problems with this thing all day. 1008 00:56:07,410 --> 00:56:10,160 So the next example and the last example is the Mandelbrot set. 1009 00:56:13,370 --> 00:56:15,620 You guys probably have heard about the Mandelbrot set. 1010 00:56:15,620 --> 00:56:18,410 It's very famous, probably one of the most famous fractals 1011 00:56:18,410 --> 00:56:18,910 of all. 1012 00:56:24,030 --> 00:56:27,840 It's called Mandelbrot because this guy Mandelbrot 1013 00:56:27,840 --> 00:56:31,050 really loved fractals and tried to communicate the notion 1014 00:56:31,050 --> 00:56:32,240 of fractals to the world. 1015 00:56:32,240 --> 00:56:36,960 And this is really a great fractal. 1016 00:56:36,960 --> 00:56:42,780 So it'll get a little bit mathy, but that's OK. 1017 00:56:42,780 --> 00:56:46,120 So we'll have-- it's in the complex plane. 1018 00:56:46,120 --> 00:56:49,110 So that means that we have this plane where 1019 00:56:49,110 --> 00:56:51,150 these are the real numbers, and these 1020 00:56:51,150 --> 00:56:53,670 are the imaginary numbers. 1021 00:56:53,670 --> 00:56:57,770 I think this is [? the way it goes. ?] 1022 00:56:57,770 --> 00:57:03,660 So the Mandelbrot set is defined by the function z 1023 00:57:03,660 --> 00:57:05,160 equals z squared plus c. 1024 00:57:10,470 --> 00:57:15,010 And when you square a complex number-- 1025 00:57:15,010 --> 00:57:17,430 well, first of all, a complex number 1026 00:57:17,430 --> 00:57:18,630 is a point in this plane. 1027 00:57:18,630 --> 00:57:24,360 So say this point is-- 1028 00:57:24,360 --> 00:57:28,290 this is b, say, and this is a. 1029 00:57:28,290 --> 00:57:35,160 This point is represented by a plus bi. 1030 00:57:35,160 --> 00:57:37,420 And i is the square root of negative 1, 1031 00:57:37,420 --> 00:57:40,510 which is not really possible. 1032 00:57:40,510 --> 00:57:44,100 That's why it's called imaginary. 1033 00:57:44,100 --> 00:57:59,630 So if you multiply two complex numbers together, 1034 00:57:59,630 --> 00:58:02,780 it's not really that simple. 1035 00:58:02,780 --> 00:58:06,280 You have to do the actual multiplication out. 1036 00:58:06,280 --> 00:58:09,775 What you get is ac plus-- 1037 00:58:14,830 --> 00:58:17,170 I don't know if I should do it all out. 1038 00:58:17,170 --> 00:58:20,660 It'll take a little bit of time. 1039 00:58:20,660 --> 00:58:24,540 But you got this sort of a mapping function essentially 1040 00:58:24,540 --> 00:58:27,220 that's not really linear. 1041 00:58:27,220 --> 00:58:29,340 It's not really that predictable, 1042 00:58:29,340 --> 00:58:30,720 what it's going to do. 1043 00:58:30,720 --> 00:58:32,760 So you have this point here. 1044 00:58:35,400 --> 00:58:38,640 And so the way the algorithm works, 1045 00:58:38,640 --> 00:58:43,310 you take a point on the complex plane. 1046 00:58:43,310 --> 00:58:45,210 c gets the value of that point. 1047 00:58:45,210 --> 00:58:50,330 And then z starts off at 0, just 0, 0. 1048 00:58:50,330 --> 00:58:53,280 And you apply this function a bunch of times. 1049 00:58:53,280 --> 00:58:56,700 So say we apply this function once. 1050 00:58:56,700 --> 00:58:58,560 z equals z squared plus c. 1051 00:58:58,560 --> 00:59:01,110 So 0 squared is nothing plus c. 1052 00:59:01,110 --> 00:59:03,870 So the first iteration, z gets the value of c. 1053 00:59:03,870 --> 00:59:06,010 So z is going to be there. 1054 00:59:06,010 --> 00:59:07,440 And you apply the function again. 1055 00:59:10,352 --> 00:59:14,490 And this z is now the new z that we just got. 1056 00:59:14,490 --> 00:59:16,560 So z equals z squared plus c. 1057 00:59:16,560 --> 00:59:23,460 So z squared, you apply the squaring function. 1058 00:59:23,460 --> 00:59:24,330 And you add c. 1059 00:59:24,330 --> 00:59:27,960 You add the real and imaginary components of c. 1060 00:59:27,960 --> 00:59:32,120 So it just maps this point to some other point over here, 1061 00:59:32,120 --> 00:59:34,570 say. 1062 00:59:34,570 --> 00:59:38,240 Then you apply this again and again and again and again. 1063 00:59:38,240 --> 00:59:41,157 So apply it here, here. 1064 00:59:41,157 --> 00:59:42,740 So I don't know if I'm going too fast, 1065 00:59:42,740 --> 00:59:47,490 but we get this point after applying the function to this. 1066 00:59:47,490 --> 00:59:49,880 So now this is the new z. 1067 00:59:49,880 --> 00:59:53,570 And then the new z loops back around. 1068 00:59:53,570 --> 00:59:55,550 And so we apply now the z squared 1069 00:59:55,550 --> 00:59:59,780 plus c. c is always going to be the initial point, 1070 00:59:59,780 --> 01:00:01,630 but z will be changing. 1071 01:00:01,630 --> 01:00:04,460 And it maps to here, for example. 1072 01:00:04,460 --> 01:00:07,062 And now the new point is z. 1073 01:00:07,062 --> 01:00:13,790 [? And you see ?] z squared plus c again that maps right here. 1074 01:00:13,790 --> 01:00:21,347 So I wrote a little applet that demonstrates this mapping 1075 01:00:21,347 --> 01:00:22,680 and what it actually looks like. 1076 01:00:22,680 --> 01:00:23,180 Yeah. 1077 01:00:23,180 --> 01:00:25,520 AUDIENCE: [INAUDIBLE] z can be anywhere in the plane? 1078 01:00:25,520 --> 01:00:26,520 CURRAN KELLEHER: z can be anywhere? 1079 01:00:26,520 --> 01:00:27,470 AUDIENCE: [INAUDIBLE] starting point. 1080 01:00:27,470 --> 01:00:30,110 CURRAN KELLEHER: The starting point could be anywhere. 1081 01:00:30,110 --> 01:00:31,340 Yeah. 1082 01:00:31,340 --> 01:00:34,400 But when you actually run the algorithm, 1083 01:00:34,400 --> 01:00:39,800 you only go from negative 2 to 2. 1084 01:00:39,800 --> 01:00:43,190 Negative 2, 2. 1085 01:00:43,190 --> 01:00:45,320 Because that's the only region in which 1086 01:00:45,320 --> 01:00:47,810 interesting things happen with this fractal. 1087 01:00:47,810 --> 01:00:50,570 So I didn't finish explaining the algorithm. 1088 01:00:50,570 --> 01:00:53,420 So say a point in here. 1089 01:00:53,420 --> 01:00:55,480 A point that's very close to the origin-- 1090 01:00:55,480 --> 01:00:58,430 0, 0-- what it tends to do is spiral 1091 01:00:58,430 --> 01:01:01,940 inward as you iterate it. 1092 01:01:01,940 --> 01:01:05,780 But a point out here, what it tends to do-- 1093 01:01:05,780 --> 01:01:08,840 definitely a point outside of 2, what 1094 01:01:08,840 --> 01:01:10,730 it tends to do when you apply this function 1095 01:01:10,730 --> 01:01:14,030 is to get mapped out here, and then to get mapped over there, 1096 01:01:14,030 --> 01:01:17,150 and just go really far away. 1097 01:01:17,150 --> 01:01:22,160 So what we do to render the Mandelbrot set 1098 01:01:22,160 --> 01:01:28,580 is apply the function a number of times. 1099 01:01:28,580 --> 01:01:35,120 And if the point eventually goes outside this circle, 1100 01:01:35,120 --> 01:01:39,130 at the origin of radius 2-- sorry, it's a bad circle. 1101 01:01:39,130 --> 01:01:42,140 If the point eventually goes outside of this circle, 1102 01:01:42,140 --> 01:01:43,700 we stop performing the function. 1103 01:01:43,700 --> 01:01:45,620 Because we know that after it gets outside, 1104 01:01:45,620 --> 01:01:48,080 it's just going to keep going forever out. 1105 01:01:48,080 --> 01:01:50,720 It has escaped. 1106 01:01:50,720 --> 01:01:52,640 It's called the escape iterations, 1107 01:01:52,640 --> 01:01:55,010 the number of iterations it takes to escape 1108 01:01:55,010 --> 01:01:56,020 outside of the circle. 1109 01:01:56,020 --> 01:01:58,520 It's the escape iterations. 1110 01:01:58,520 --> 01:02:03,450 So what we do is we color the point 1111 01:02:03,450 --> 01:02:04,940 depending on its escape iterations. 1112 01:02:04,940 --> 01:02:07,190 So when we actually-- and the other part of it 1113 01:02:07,190 --> 01:02:10,820 is, if it never escapes, we color it black. 1114 01:02:10,820 --> 01:02:13,490 To actually test that, we just iterate 1115 01:02:13,490 --> 01:02:17,960 a certain number of times, say like 500 times or 70 times. 1116 01:02:17,960 --> 01:02:20,570 And if it hasn't escaped after that many iterations, 1117 01:02:20,570 --> 01:02:22,400 we sort of assume that it's never going to. 1118 01:02:22,400 --> 01:02:24,020 It's not valid. 1119 01:02:24,020 --> 01:02:26,120 So it's an approximation to the actual set. 1120 01:02:26,120 --> 01:02:29,450 But we have to do it to render the actual thing. 1121 01:02:29,450 --> 01:02:30,987 Then we color it black. 1122 01:02:30,987 --> 01:02:32,570 So when we actually render it, what we 1123 01:02:32,570 --> 01:02:36,260 do is go through each pixel on the screen 1124 01:02:36,260 --> 01:02:37,500 and apply this algorithm. 1125 01:02:37,500 --> 01:02:40,910 So for this point, this point becomes c. 1126 01:02:40,910 --> 01:02:44,390 And we iterate, test, iterate, test, iterate, test. 1127 01:02:44,390 --> 01:02:46,820 And if the test-- 1128 01:02:46,820 --> 01:02:49,280 what we're testing is if it's outside of the circle. 1129 01:02:49,280 --> 01:02:51,080 If it's outside of the circle, we 1130 01:02:51,080 --> 01:02:54,380 assign it a color based on the number of iterations it took. 1131 01:02:54,380 --> 01:02:57,830 And we do that for each one. 1132 01:02:57,830 --> 01:02:58,940 So we keep going. 1133 01:02:58,940 --> 01:03:03,220 And say this one, for example, is going to spiral in. 1134 01:03:03,220 --> 01:03:04,410 It's not a continuous line. 1135 01:03:04,410 --> 01:03:09,530 It just applies the function and spirals in. 1136 01:03:09,530 --> 01:03:11,210 So that's never going to escape. 1137 01:03:11,210 --> 01:03:13,400 So we're going to color it black. 1138 01:03:13,400 --> 01:03:18,500 And this applet is excellent at showing this. 1139 01:03:21,440 --> 01:03:26,060 So I implemented this algorithm in Java, 1140 01:03:26,060 --> 01:03:33,060 and it's going to output the point, 1141 01:03:33,060 --> 01:03:37,924 and it's going to draw the lines between each point. 1142 01:03:37,924 --> 01:03:38,840 So say it starts here. 1143 01:03:38,840 --> 01:03:42,450 It's going to draw a line here, here, here, here. 1144 01:03:42,450 --> 01:03:44,690 So what we're going to see is really cool. 1145 01:03:44,690 --> 01:03:47,630 So this is what we get in the end. 1146 01:03:47,630 --> 01:03:51,780 And as I move my mouse around, it does these tests. 1147 01:03:51,780 --> 01:03:55,480 So here we can see that there's only one iteration. 1148 01:03:55,480 --> 01:03:58,980 The one iteration gets the color light blue. 1149 01:03:58,980 --> 01:04:01,040 And then here, there are two iterations. 1150 01:04:01,040 --> 01:04:05,102 And they're listed up there, one, two. 1151 01:04:05,102 --> 01:04:07,310 So you can see all the way, wherever it's this color, 1152 01:04:07,310 --> 01:04:09,770 there are only two iterations before the point 1153 01:04:09,770 --> 01:04:14,170 gets outside of this circle to [INAUDIBLE] I think. 1154 01:04:17,220 --> 01:04:21,180 So as we go deeper in, it takes three iterations, four, five, 1155 01:04:21,180 --> 01:04:23,950 six, a lot of iterations. 1156 01:04:23,950 --> 01:04:27,180 So all the points that are colored, 1157 01:04:27,180 --> 01:04:29,520 they do eventually escape. 1158 01:04:29,520 --> 01:04:32,310 But all the points that are black don't escape. 1159 01:04:32,310 --> 01:04:34,790 And so the patterns that are inside are really interesting. 1160 01:04:34,790 --> 01:04:37,770 See these in the middle, they just sort of spiral in 1161 01:04:37,770 --> 01:04:41,040 and keep going. 1162 01:04:41,040 --> 01:04:43,800 The ones here, there's some looping pattern. 1163 01:04:43,800 --> 01:04:47,367 See, the function loops back on itself. 1164 01:04:47,367 --> 01:04:48,450 So it's sort of recursive. 1165 01:04:48,450 --> 01:04:50,533 Like I don't really understand how it's recursive, 1166 01:04:50,533 --> 01:04:53,580 but it sort of is. 1167 01:04:53,580 --> 01:04:56,810 So if you go out to this other nub, the second nub out, 1168 01:04:56,810 --> 01:05:00,090 it gets this really cool shape. 1169 01:05:00,090 --> 01:05:04,364 So what we can do is zoom in actually on this. 1170 01:05:04,364 --> 01:05:05,564 And it redraws it. 1171 01:05:05,564 --> 01:05:06,980 So it's calculating this algorithm 1172 01:05:06,980 --> 01:05:08,617 for every single point as we speak. 1173 01:05:08,617 --> 01:05:09,700 It's doing it really fast. 1174 01:05:13,430 --> 01:05:18,065 So we can see all these really cool shapes 1175 01:05:18,065 --> 01:05:19,190 that generate this fractal. 1176 01:05:21,970 --> 01:05:23,914 And it's just crazy. 1177 01:05:31,090 --> 01:05:34,180 So we can sort of see recursion here. 1178 01:05:34,180 --> 01:05:38,400 It looks like they're definitely some recursive thing going on. 1179 01:05:38,400 --> 01:05:40,720 And the recursive part of this is the fact 1180 01:05:40,720 --> 01:05:45,460 that z gets applied to itself. 1181 01:05:49,090 --> 01:05:52,880 The n-th z is equal to the z that 1182 01:05:52,880 --> 01:05:55,730 came before it squared plus c. 1183 01:05:55,730 --> 01:05:59,030 And then this z is reframed as the old z. 1184 01:05:59,030 --> 01:06:02,080 And this reframing and re-applying of the function 1185 01:06:02,080 --> 01:06:04,670 is what makes it recursive. 1186 01:06:04,670 --> 01:06:09,890 So yeah, it sort of makes sense. 1187 01:06:09,890 --> 01:06:12,830 Let's just go through the code quickly. 1188 01:06:12,830 --> 01:06:18,370 And then I want to show some really cool things. 1189 01:06:18,370 --> 01:06:20,600 Oh, where did it go? 1190 01:06:20,600 --> 01:06:22,025 AUDIENCE: [INAUDIBLE]. 1191 01:06:24,137 --> 01:06:25,970 CURRAN KELLEHER: So yeah, in the black part, 1192 01:06:25,970 --> 01:06:28,229 it just recurses infinitely. 1193 01:06:28,229 --> 01:06:29,770 And the code itself is not recursive, 1194 01:06:29,770 --> 01:06:33,880 but the mapping function, again, is what's recursive. 1195 01:06:33,880 --> 01:06:35,800 So yeah, the black parts are in the set. 1196 01:06:40,360 --> 01:06:43,630 So the real actual Mandelbrot set is just the black points. 1197 01:06:43,630 --> 01:06:47,710 We just color the other points so it looks pretty. 1198 01:06:47,710 --> 01:06:50,710 So let's look at the code. 1199 01:06:50,710 --> 01:06:53,020 Def [? drawMandelbrot. ?] See that there? 1200 01:06:57,230 --> 01:06:58,900 window.set negative 2, 2. 1201 01:06:58,900 --> 01:07:01,240 So it just sets the window, the plotting range 1202 01:07:01,240 --> 01:07:03,240 to be in this range. 1203 01:07:03,240 --> 01:07:07,970 For every x pixel in the height of the window, 1204 01:07:07,970 --> 01:07:11,050 or the width of the window, and every y pixel in the height, 1205 01:07:11,050 --> 01:07:17,330 c.real and c.imaginary get the x and y values on the screen. 1206 01:07:17,330 --> 01:07:20,170 So what we're doing there is saying, 1207 01:07:20,170 --> 01:07:24,790 basically the pixel gets this actual point in the space 1208 01:07:24,790 --> 01:07:25,990 that we're working-- 1209 01:07:25,990 --> 01:07:28,250 the complex plane. 1210 01:07:28,250 --> 01:07:32,290 Def iterations equals calculate iterations c. 1211 01:07:32,290 --> 01:07:33,400 And we pass it c. 1212 01:07:33,400 --> 01:07:35,260 Keep in mind, this is c. 1213 01:07:35,260 --> 01:07:38,020 The starting point is c. 1214 01:07:38,020 --> 01:07:42,880 So let's just stay inside this function for now. 1215 01:07:42,880 --> 01:07:46,330 Def color equals calculate color iterations. 1216 01:07:46,330 --> 01:07:48,400 So we're calculating the color based 1217 01:07:48,400 --> 01:07:52,086 on the number of iterations it took to escape. 1218 01:07:52,086 --> 01:07:53,710 And then fill the pixel with the color. 1219 01:07:53,710 --> 01:07:56,320 That plots it on the screen. 1220 01:07:56,320 --> 01:07:59,130 So let's look at the calculate iterations function. 1221 01:07:59,130 --> 01:08:01,000 Int calculate iterations. 1222 01:08:01,000 --> 01:08:04,720 This notation means that it will return an integer number. 1223 01:08:04,720 --> 01:08:06,610 It takes a complex number c. 1224 01:08:06,610 --> 01:08:09,940 So z real, z imaginary, start at 0. 1225 01:08:09,940 --> 01:08:11,470 Start there. 1226 01:08:11,470 --> 01:08:15,040 And then iteration starts off at 0. 1227 01:08:15,040 --> 01:08:16,930 So this is-- we're going to count 1228 01:08:16,930 --> 01:08:19,420 how many iterations it takes. 1229 01:08:19,420 --> 01:08:21,841 So while the circle contains z, that's 1230 01:08:21,841 --> 01:08:23,840 another function that just tests if it's inside. 1231 01:08:27,279 --> 01:08:30,819 And the number of iterations is less than max iterations. 1232 01:08:30,819 --> 01:08:33,729 Max iterations is the number of iterations 1233 01:08:33,729 --> 01:08:37,390 after which we assume it's just going to spiral inward and just 1234 01:08:37,390 --> 01:08:38,140 stay inside. 1235 01:08:41,290 --> 01:08:45,979 So while that stuff is true, z equals z squared plus c. 1236 01:08:45,979 --> 01:08:50,319 z equals z times z plus c, which [? is ?] [? squared. ?] So we 1237 01:08:50,319 --> 01:08:52,910 can do that because I overloaded the operators, 1238 01:08:52,910 --> 01:08:56,790 the multiplication and addition for a complex number. 1239 01:08:56,790 --> 01:08:57,790 So it behaves correctly. 1240 01:08:57,790 --> 01:09:03,490 It does this strange multiplication thing. 1241 01:09:03,490 --> 01:09:06,130 And so we do that, iterations plus plus. 1242 01:09:06,130 --> 01:09:10,600 That means we increment the number of iterations by one. 1243 01:09:10,600 --> 01:09:14,140 And calculate color just calculates a color 1244 01:09:14,140 --> 01:09:18,556 based on number of iterations. 1245 01:09:18,556 --> 01:09:19,680 So we have 20 minutes left. 1246 01:09:19,680 --> 01:09:26,156 I want to blow you away with music and fractals. 1247 01:09:29,000 --> 01:09:32,930 So I'm going to play three pieces of Bach. 1248 01:09:32,930 --> 01:09:36,540 And yeah, they're just great. 1249 01:09:36,540 --> 01:09:39,773 So the first one, it's actually not written by Bach. 1250 01:09:39,773 --> 01:09:40,939 Oh no, what's going on here? 1251 01:09:45,938 --> 01:09:46,979 It's not written by Bach. 1252 01:09:46,979 --> 01:09:49,319 But it's the "Little Harmonic Labyrinth," 1253 01:09:49,319 --> 01:09:55,200 which is the song that the dialogue is based on in Godel, 1254 01:09:55,200 --> 01:09:56,110 Escher, Bach. 1255 01:09:59,000 --> 01:10:02,030 English language has a grammar. 1256 01:10:02,030 --> 01:10:04,580 We could nest sentences inside, which we can nest more 1257 01:10:04,580 --> 01:10:08,360 sentences, [? in which ?] we can [? mention ?] more sentences. 1258 01:10:08,360 --> 01:10:10,100 And that itself was a nested sentence. 1259 01:10:10,100 --> 01:10:15,890 So music, sort of like English, has sort of a grammar to it. 1260 01:10:15,890 --> 01:10:19,160 These chord progressions and melodies and harmonies 1261 01:10:19,160 --> 01:10:23,600 particularly have a sort of a language in which they can 1262 01:10:23,600 --> 01:10:25,610 move between keys and whatnot. 1263 01:10:29,390 --> 01:10:32,150 So that's one way in which Bach embeds 1264 01:10:32,150 --> 01:10:34,220 recursion inside of music. 1265 01:10:34,220 --> 01:10:37,970 He has these nested harmonic structures 1266 01:10:37,970 --> 01:10:39,150 of chord progressions. 1267 01:10:39,150 --> 01:10:42,650 So the chord-- yeah, in chapter 5 of Godel, Escher, Bach, 1268 01:10:42,650 --> 01:10:43,970 he talks about it. 1269 01:10:43,970 --> 01:10:49,720 And the dialogue is modeled after that first song 1270 01:10:49,720 --> 01:10:52,070 that we heard, "Little Harmonic Labyrinth." 1271 01:10:52,070 --> 01:10:54,814 And melodies also can sort of have recursion, 1272 01:10:54,814 --> 01:10:56,105 in that they can have patterns. 1273 01:10:58,880 --> 01:11:01,900 Like this theme, for example. 1274 01:11:01,900 --> 01:11:06,160 This theme-- Bach does this a lot. 1275 01:11:06,160 --> 01:11:09,490 What he does is he takes themes, and he restates them 1276 01:11:09,490 --> 01:11:13,720 with maybe different harmonies and embeds them 1277 01:11:13,720 --> 01:11:18,050 inside smaller and larger scales. 1278 01:11:18,050 --> 01:11:19,690 So for example, I don't know if he 1279 01:11:19,690 --> 01:11:24,490 did this, maybe on a larger scale, 1280 01:11:24,490 --> 01:11:26,740 the actual harmony might actually 1281 01:11:26,740 --> 01:11:30,130 follow this sort of theme. 1282 01:11:30,130 --> 01:11:33,460 And inside those-- for a long period of time, 1283 01:11:33,460 --> 01:11:36,100 be this key, and a long period of time, be this key. 1284 01:11:36,100 --> 01:11:41,530 And then when you're in that key, he plays this theme. 1285 01:11:41,530 --> 01:11:42,980 So that's an example of nesting. 1286 01:11:42,980 --> 01:11:47,020 So recursion per se is not there, 1287 01:11:47,020 --> 01:11:49,760 but the result of recursion. 1288 01:11:49,760 --> 01:11:52,280 This is the distinction between recursive processes 1289 01:11:52,280 --> 01:11:54,070 and recursive structures. 1290 01:11:54,070 --> 01:11:56,230 Recursive structures are basically any structure 1291 01:11:56,230 --> 01:11:57,880 that has nesting to it. 1292 01:11:57,880 --> 01:12:00,340 So English language is a recursive structure 1293 01:12:00,340 --> 01:12:02,320 because it comes from a recursive grammar. 1294 01:12:02,320 --> 01:12:03,400 It's nesting. 1295 01:12:03,400 --> 01:12:07,480 And music also is a recursive structure, not 1296 01:12:07,480 --> 01:12:08,560 a recursive process. 1297 01:12:08,560 --> 01:12:10,054 AUDIENCE: So recursive process is 1298 01:12:10,054 --> 01:12:12,295 almost like a function that defines how you can 1299 01:12:12,295 --> 01:12:15,649 get the recursive structure. 1300 01:12:15,649 --> 01:12:16,690 CURRAN KELLEHER: Exactly. 1301 01:12:16,690 --> 01:12:17,190 That's it. 1302 01:12:17,190 --> 01:12:21,500 He said a recursive process is sort of like a function 1303 01:12:21,500 --> 01:12:24,470 or something that defines how you end up 1304 01:12:24,470 --> 01:12:25,610 with a recursive structure. 1305 01:12:25,610 --> 01:12:26,613 That's exactly it. 1306 01:12:26,613 --> 01:12:27,300 Yeah. 1307 01:12:27,300 --> 01:12:31,070 AUDIENCE: So [INAUDIBLE] just going like that, [INAUDIBLE].. 1308 01:12:44,510 --> 01:12:46,280 CURRAN KELLEHER: Yeah, it's hard to hear. 1309 01:12:46,280 --> 01:12:49,560 It takes a really fine musical ear to hear this. 1310 01:12:52,790 --> 01:12:55,650 So we'll see-- how much time-- 1311 01:12:55,650 --> 01:12:57,640 three minutes. 1312 01:12:57,640 --> 01:13:05,090 So we talk about pushing and popping of stacks. 1313 01:13:05,090 --> 01:13:09,470 So a function, a computer function, say F-- 1314 01:13:09,470 --> 01:13:10,220 Foo, actually. 1315 01:13:15,450 --> 01:13:16,670 Foo is a function. 1316 01:13:16,670 --> 01:13:20,890 Or better yet-- oh, no, forget it. 1317 01:13:20,890 --> 01:13:23,937 And inside this Foo, it calls a function bar. 1318 01:13:27,346 --> 01:13:29,294 And here's a function bar. 1319 01:13:33,690 --> 01:13:38,590 It does some stuff, and it calls maybe g 1320 01:13:38,590 --> 01:13:39,970 and does some other stuff. 1321 01:13:39,970 --> 01:13:41,138 And here's g. 1322 01:13:44,070 --> 01:13:47,970 g maybe does just one thing, and I don't know, ends. 1323 01:13:47,970 --> 01:13:52,320 So what you have is a stack, a call stack. 1324 01:13:52,320 --> 01:13:56,280 It happens in any grammar actually 1325 01:13:56,280 --> 01:13:58,160 that's sort of recursive. 1326 01:13:58,160 --> 01:14:00,557 So in computer programs and also in English, 1327 01:14:00,557 --> 01:14:01,890 you have this notion of a stack. 1328 01:14:01,890 --> 01:14:04,950 So here, you do some stuff. 1329 01:14:04,950 --> 01:14:06,300 You do some stuff. 1330 01:14:06,300 --> 01:14:09,870 And then when you call bar, your focus actually 1331 01:14:09,870 --> 01:14:12,360 changes to over here, bar. 1332 01:14:12,360 --> 01:14:15,630 But you retain sort of in your mind 1333 01:14:15,630 --> 01:14:18,450 or in the memory of a computer where you 1334 01:14:18,450 --> 01:14:20,420 were, where are you left from. 1335 01:14:20,420 --> 01:14:21,435 So you-- yeah. 1336 01:14:21,435 --> 01:14:22,770 AUDIENCE: [INAUDIBLE]. 1337 01:14:27,102 --> 01:14:28,060 CURRAN KELLEHER: Calls? 1338 01:14:28,060 --> 01:14:29,722 AUDIENCE: [INAUDIBLE] goals. 1339 01:14:29,722 --> 01:14:30,680 CURRAN KELLEHER: Goals. 1340 01:14:30,680 --> 01:14:31,700 Yeah, exactly. 1341 01:14:31,700 --> 01:14:34,700 So that's why computer programming is tough, 1342 01:14:34,700 --> 01:14:36,590 because you have a high level goal. 1343 01:14:36,590 --> 01:14:38,632 But then to get to that goal, just like you said, 1344 01:14:38,632 --> 01:14:40,423 you have other goals that you need to meet. 1345 01:14:40,423 --> 01:14:42,590 And each of those goals requires some other goals. 1346 01:14:42,590 --> 01:14:45,010 So it's actually recursive. 1347 01:14:45,010 --> 01:14:45,990 It's crazy. 1348 01:14:45,990 --> 01:14:51,380 So when this program executes, it does this stuff. 1349 01:14:51,380 --> 01:14:55,370 It puts this position, a, let's say, on the stack. 1350 01:14:55,370 --> 01:14:58,280 So this is the stack, a. 1351 01:14:58,280 --> 01:15:01,250 And it goes into here, bar, does all this stuff. 1352 01:15:01,250 --> 01:15:02,060 And it calls g. 1353 01:15:02,060 --> 01:15:05,960 But to remember where this is, let's call this position b. 1354 01:15:05,960 --> 01:15:08,360 It puts b on the stack. 1355 01:15:08,360 --> 01:15:10,620 And it calls g, and then does all this stuff. 1356 01:15:10,620 --> 01:15:12,170 And then after this, it returns. 1357 01:15:12,170 --> 01:15:16,040 And when it returns, it asks, where was I the last time? 1358 01:15:16,040 --> 01:15:18,312 And this is what the stack is for. 1359 01:15:18,312 --> 01:15:19,520 So where was I the last time? 1360 01:15:19,520 --> 01:15:20,480 I was at b. 1361 01:15:20,480 --> 01:15:22,330 So we pop. 1362 01:15:22,330 --> 01:15:24,080 Putting something on the stack is pushing. 1363 01:15:24,080 --> 01:15:25,820 Taking it off is called popping. 1364 01:15:25,820 --> 01:15:28,880 We pop it and say, OK, here's b. b is where we were. 1365 01:15:28,880 --> 01:15:29,900 We go back to there. 1366 01:15:29,900 --> 01:15:31,160 And we finish this. 1367 01:15:31,160 --> 01:15:33,730 Once we finish this, we say, oh, where we were the last time. 1368 01:15:33,730 --> 01:15:34,820 So that's a. 1369 01:15:34,820 --> 01:15:36,500 And then go back to a and finish. 1370 01:15:40,757 --> 01:15:43,360 AUDIENCE: [INAUDIBLE] make it so that you 1371 01:15:43,360 --> 01:15:44,560 have those things, right? 1372 01:15:44,560 --> 01:15:47,034 Those are things like [INAUDIBLE].. 1373 01:15:47,034 --> 01:15:48,200 CURRAN KELLEHER: Yeah, sure. 1374 01:15:48,200 --> 01:15:49,892 Yeah, exactly. 1375 01:15:49,892 --> 01:15:52,234 AUDIENCE: [INAUDIBLE]. 1376 01:15:52,234 --> 01:15:53,150 CURRAN KELLEHER: Yeah. 1377 01:15:53,150 --> 01:15:58,175 So the dialogue, which reflects "Little Harmonic Labyrinth," 1378 01:15:58,175 --> 01:15:59,300 has this sort of structure. 1379 01:15:59,300 --> 01:16:01,425 It goes inside something, inside yet another thing, 1380 01:16:01,425 --> 01:16:03,430 and then back out, and then [? back out. ?]