Back to Index

fast.ai APL study session 12


Chapters

0:0
11:10 Calculus Book
16:53 Basic Math Operators
30:48 Complex Numbers

Transcript

All right, so, yes, Sarada, you're going to talk about the APO competition. I will share my screen because you've put lots of cool stuff there. I think that is a really fast way to learn and try to find a problem while learning it. Okay, so there's a whole thread here.

Aitan Leis had some very nice solutions, I thought. Yeah. Yeah, tell us about your experience. Look at you, all Gold Cups. I love the way we've gone from, like, let's set up a study session to try and learn all the glyphs. And Sarada's like, no, let's get 10 trophies in the Crop and Solving competition.

Good on you. Yeah, I think I really stretch myself, but I think I learn a lot during the process as well. I ask a lot of questions, try to read a lot of documentation, but I still find the documentation quite technical. I really learned from the example, and again, really helpful, point me to some direction, but some of them I still can't get past my head, because his video actually really good in the past competition example.

He really- Oh, Adam's videos? Yes. Yeah, and now someone starts sharing, why it's not the fastest one, definitely I want to learn from other people, but it's really good to force me to think different, to using APL to solve the problem, even this is very short and small problem, but it's really good exercise.

Yeah, I learned a lot. Thank you. This is a great thread, because it kind of, this particular post here actually goes through each of his solutions, and we seem to have quite, especially this one, really elegant solution. Yeah, I actually put, we put together, so people can go into the logbook and breaking down the solution as well, and other person, our AK1537 here.

I'll tell you what, let me make this a wiki. There we go, so maybe you can put a link to it there. Yeah. So I, at the moment, I put all people's solution in one place, and then I profile them to see who is the fastest solution, so people can learn from the best.

Awesome. I love it. Did anybody else try more of the competition questions? No, me neither. I did give it a try, but I got stuck on the second or third question, and it was just about comparing arrays, and then I looked at the solutions, and people are using the built-in glyphs, not equals, where I tried to go via each entry, so I think that this is super helpful that people are sharing so freely their solutions.

I looked at the website, and it seems that the competition for this year is not available right now. Like, you cannot go onto the website and submit your results yet, but there are competitions for the previous years that are available, so hopefully this year's will be available soon as well.

Okay. So, if you look where you can try to solve a problem yourself first, and then you can look for help on the forums, I think that's… And my recommendation for these would be, like, spend at least a couple of hours on a problem yourself before looking up the solution, and also try doing all the solutions, you know, like all the questions, so if you get stuck on one, kind of like if you're doing a math test at school, you're just like, okay, let's move onto the next one, and then, yeah, you'll learn more by seeing the dumb things it turns out you did, or vice versa, than just reading about it.

Jeremy, I have a suggestion or question, maybe. I'm curious to know how you are planning to use APL in your workflow. I asked a question on the forum, and somebody suggested Pineapple, which I find very useful for APL to be used in Python and vice versa. And just to clarify, you're talking about P, Y, and APL, right?

Yes. I got stuck installing it on Mac, but that's another separate issue. If this is the way that you envision using it in your workflow, meaning that, oh, a little bit of a clever code to clean up data and do pre- and post-processing in the entire workflow inside Python, I thought it might be useful to experience that with you as a part of the walkthrough.

Sure. Before I do, let me just say hello, because I think you're a new face. How do I pronounce your name? Is it Rameen? Rameen. Yes, yeah. How about you joining us from today? San Diego. San Diego. Oh, that's a lovely town. That's great. I've been watching your videos carefully and pausing them and doing stuff in the background.

Well, thanks for joining. So I think of this in a similar way. I don't know if any of you have read Richard Feynman's autobiography. He described how he got- he liked following problems that he found interesting. And so he got interested in watching people spitting plates on their fingers at the university cafeteria, and he decided to take some time to study the motion of those plates, because that was the thing that seemed most interesting to him at the time.

Later on, it turned out that the math and physics he developed in thinking about that problem turned out to be exactly what was needed to solve some critical foundational problems in quantum mechanics. That's, you know, I have a similar thing about IPLs, things interesting and fun. It's my interest is in it mainly as a notation.

I have always been kind of interested in mathematics, but the notation has always gotten my way. I think I'm like, I must be like particularly anal or something because I just find it so imprecise and like or like the meaning of it varies from paper to paper or area to area.

And like, I don't know how to like look things up and find out exactly what it means. And then when I try to find out exactly what this thing means, it's hard to pin down. And so I ended up at university studying metamathematics and formal logic and that like helped me a little bit because I kind of went back to like, well, what is mathematics and building it up from scratch?

But I keep getting stuck on notation, you know, and also I'm somebody who's highly intuitive. So I really like to experiment with things. I like to know if my experiments are right. So like I'm really envious of how I don't know if you've seen Adam on the forum is able to like take an APL expression and treat it like an algebra, you know, he'll go through symbol replacement exercises and at each point, he'll say, here's the symbol replacement rule I'm doing and manipulate an APL expression into a new APL expression that clarifies some situation.

I want to. Yeah. So he's he's my hero. I want to be like that. I want to be able to treat it as as an algebra, you know, and use it to deepen my understanding of things. I mean, I use it a lot right now to teach my daughter math.

And so she's six and her friend is eight. And so I tutor them in math and we we have been doing a series of books called Beast Academy, which were kind of great cartoons, super fun, very, you know, compelling for kids. And they kind of as they math developed, we ran out of those and we, you know, needed new things to cover, you know, so like they're most of the math stuff they're doing is around like grade 11 and grade 12 now.

And the problem is that materials are boring, dry presentation. And even the like the Beast Academy books, it's by a group put out of problem solving. And they have fantastic materials, but the highest, the high school level stuff is called out of problem solving and it removes all the comics and all the cool characters.

And so it's not fun anymore. So, you know, a couple, you know, a few days ago, we were trying to cover a topic. And after 40 minutes, my daughter's just like, I'm so bored. You know, can't we do some APL? And I was like, oh, of course we can.

We can do this exact topic with APL. So we just we stopped the video, opened up APL and I said, OK, solve this math problem with APL. And then they're engaged again and it's fun and they're trying things out and they're both sharing their screens so I can see them trying things.

And then, you know, why why did that work? So, yeah, for me, it's kind of intellectual interest. And I feel like there's a. I can see it starting the revolution in education. Well, that's what Iverson thought, you know, and he, you know, he wrote this these books with with high school teachers and somewhere here, you know, here's OK.

So here's a calculus book. Let me turn my virtual background off. OK, so here's a calculus book and it's a calculus book in APL. So, you know, it has exercises and derivations and whatnot, but rather than using normal math notation, it's all in APL, you know. And so it covers, let's see.

You know, it's got all kinds of interesting. Math problems and Taylor's series and why is that students make it more compelling APL is intimidating can be well, not when you're six. Because, you know, when you're six, you're everything's new and everything looks weird. You know, so the biggest problem in our math is when we hit sigma notation and I just couldn't get anywhere with it.

Yeah, because there's a lot of assumptions going on. Like I just sort of thought that was such an easy thing, I thought, how could anybody ever find it difficult? But then when we got there with the kids, it's like, OK, well, there's this like implied loop where this index is like looping through the thing.

And then the thing inside the index is like getting each value of that. And then there's this kind of like inserted sum. And it's just that that's just a lot of abstractions going on. So when we did it in NumPy, that helped a lot, you know. But then when we did it in APL, we actually learned plus slash means insert plus between each pair of things in a sequence, you know, OK, so sigma's last step is it does plus slash.

And like you can try it and you can see it and then we're like, OK, and then the thing that happens inside is there's an expression that gets evaluated. So yeah, it was much less intimidating to people who haven't yet learned normal math notation because this is a math notation that they could execute experiment with, has extremely well defined rules.

If they're not sure what something is, they can look it up and they help, you know, OK, that was interesting. Thanks for explaining. Yeah, no worries. OK, I am on. I just wanted to second like Adam, the nation, their awesome hit his rank operator. I use that all the time now that we showed it on that one, that one video.

Yes. Yes, I'm listening to the arraycast podcast. Yes, yeah, I have two. It's great. Got to it yet, but there's two somewhat recent episodes called leading axis theory and transpose. And they spent about three hours covering what Adam did, you know, in 45 minutes when he was here, so much better when you've got it visually and I kind of listen to the episode and I was like, yeah, yeah, yeah, I know all that now.

But it was interesting hearing him talk about transpose as well as being as important as the rank stuff. So it might be a valuable thing to check out if you haven't seen it. OK, so I'll share my screen. This time we didn't forget. That's right. Oh, also the concept of trains, I found them useful and non intuitive.

Yeah, we haven't got to that yet. Because we're trying to do all the glyphs first, yes. Does anybody kind of remember, I guess, do we leave off in the middle of something? Last one was mostly Adam, so yeah, OK, I guess we're kind of up to here. Actually, I wouldn't mind moving these from stuff we haven't done yet to stuff we are doing because it really belongs right at the top, basic math operators.

So what I might do is add this pipe operator or vertical bar operator. That's actually a pretty straightforward one, OK, so we're going to do vertical bar, and we type help, oh, smoking, never quite sure what's going to work on a new machine. So they call it style or vertical bar.

I believe it's called magnitude, yes, it is, all right, so unfortunately, because I'm using a Mac today, I do not have a screen I can draw on, which is a bummer, so hopefully we can find a picture of what magnitude is, OK. So the magnitude of a complex number is how far away that complex number is from the origin if you represent it as Cartesian coordinates.

So if you've got a number a plus bi, so to remind you, i means the square root of minus 1, and you can represent any of these complex numbers using a single real a and a single real b, even if you've got squares or square roots or multipliers or whatever, because you can always simplify it down to a real number part plus a real number times the square root of minus 1.

So this is basically any complex number you can represent in this form. And then you could draw it, and you can draw it by putting it on a Cartesian coordinate plane by going right by a units and then up by b units to the coordinate a, b. Does that make sense so far?

Anybody who's not, I think, Radek, you're not, said you're not so comfortable with complex stuff, does this make sense to you so far? OK, so this is a triangle, it's a right angle triangle, and so how far away is this dot from the origin? Well, the answer is it's going to be the square root of a squared plus b squared, because it's a right angle triangle, and Pythagoras told us so.

And so if you think about it then, what about this number here? This number here is 1 plus 0i, also known as 1. So how far away is that from the origin? 1, which is equal to the square root of 1 squared plus 0 squared. What about this number here, i?

OK, how far away is this? Well, square root of 0 squared plus 1 squared, also 1. OK, so the magnitude of a real number is the number itself, the magnitude of a complex number is the size of an only imaginary part, is the size of the imaginary component, it's a real number, and the complex number more generally is root a squared plus b squared.

So that's what this means. And to remind you, 3j4 is how you write 3 plus 4i. So we could also do-- oh, and in fact, let's do them all together now that we're getting fancy. What happened to my APL part? So negatives, the distance that negative 3 is from the origin is 3.

And again, negative 3 squared is 9, plus 0 squared is 9, square root of that is 3. OK, so dyadic, I think they call residual-- yeah, residual or modulus. Let me move that one down to later. OK, so modulus, so if we did-- oh, they've probably got some examples, we could still-- they do.

So this tells you the residual you're left over with if you do 2 divided into 7. So in other words, 7 divided by 2 is 3, remainder 1. 13 divided by 10 is 1. But it's negative. So if you've got a negative right-hand side, then what they actually do is they go further.

They treat that as being they say that's 2 remained as 7. Or else if you have a positive right-hand side, 8 divided by 2 and a half is-- how do we get negative 2 here-- is 4 remainder minus 2. So I haven't really seen this idea before, but normally in school, remainders are done always positive.

And they have a very clear answer, which is 10 goes into 13 one time with 3 remainder. 2 goes into 8 four times with 0 remainder. But for negatives, you can overshoot it on the positive side or the negative side. And so in APL, if you have a negative left-hand side, you'll get a negative remainder.

And if you have a positive left-hand side, you'll get a positive remainder. The reason this is also called modulus is because if you're measuring the number of minutes past the hour, for example, then after-- let's go back a little bit so it doesn't go up. Then after 1 second, the minute hand will be at 1.

After 4 seconds, the minute hand will be at 4, et cetera. After 59 minutes, the minute hand will be at 59. After 60 minutes, the minute hand will be at 0. So clocks count modulo 60. So modulo is the same as what's the remainder if you divide by this.

And modulo arithmetic is used in cryptography a lot. And it kind of comes up all over the place. If you're measuring the angle that's made as you turn around a circle, if you go more than 360 degrees, you come back where you come from. So those measurements will be modulo 360.

Does that make sense? So then times-- Monadic times tells you the direction. And if you look here, the number 1-- there's a real number-- is to the right. And so you could say its direction is 1. The number negative 5 is to the left. You could say its direction is minus 1.

The number 5i, its direction is straight up. You could say its direction is i. So the direction of a number is if you draw a vector from the origin out to that number on this plane, where abouts would it cross the unit circle? And so in most languages that don't deal particularly with complex numbers, we have a function called sine, S-I-G-N, that returns the sine of a number.

And it normally returns 1 if it's positive and minus 1 if it's negative. For real numbers, monadic times is exactly that. Look at this. Monadic times 3.1. In other words, sine 3.1 is 1. Monadic times of negative 2. In other words, sine of negative 2 is negative 1. And then normally, in most languages, sine of 0 is called 0.

So, so far, this is all exactly the same as sine. It's called that in Python, Python. Oh, I guess it doesn't. NumPy has sine. So why doesn't Python have a sine function? Yeah, I don't remember using sine in just Python. Huh? Was that? Yeah, I don't remember using sine without NumPy in Python.

Yeah, that says there was a patch, but it wasn't accepted because they couldn't agree on what it should return. So they didn't do it at all. That's funny. So in NumPy, I wonder if NumPy's sine handles complex numbers. That sounds to be something slightly different. That's interesting. All right.

So I kind of like this about APL is I find really consistently it tends to extend basic ideas from other languages and from math to cover, like, more things in a more general way, which I think is really cool. Okay, I guess I should mention complex numbers here. Okay, so here we can see, so what is the magnitude of 3 plus 4i?

So the magnitude of 3 plus 4i, well, we would start with the number 3 plus 4i, and then we would divide it by its length, which we know is magnitude of 3 plus 4i, and that gives us 0.6 plus 0.8i. So that's going to be the, you know, so 0.6 comma 0.8 would be its coordinates on the unit circle down, you know, somewhere around here in this case.

I'm not going to include it here, but I think, you know, as Adam said, I was trying to think about a way to do this as a train. I think this should already work, right? So when you have two of these next to each other, it first of all applies this.

Is that right? I can't remember which border it's in. Yeah, the first one applies that. So you can just delete the left hand side maybe. You can't just delete the left. Oh, well, you could delete the left hand side if you're in J, but APL doesn't work that way.

APL would treat this as being the reciprocal of the magnitude. If you put parenthesis on it. Still no, that's the way J works. It's not the way APL works. Okay. You're thinking of a type of train called a hook. But in APL, you would need to do, you would need to use the self operator.

So you would do that with the self operator, which I can't remember how to write it. So I don't have to find it. It's the tilde diuresis. Capital P, capital P. Oh, did I get it wrong? Yeah, no, I got it right. F tilde diuresis. You can do that again.

Tilde diuresis y equals y f tilde f y. I could answer you last week. All right. Well, that's an interesting puzzle for somebody to figure out. Is the binding for the complex number less high than? No, this binds as tightly as upper bar or full stop would as a decimal or yeah, or whatever.

I'm 99.99% sure. Yep. H a four, F three, two, four. Oh, that's interesting. So, oh, oh, I see the issue. The issue is that there's an implied parentheses here. Because we go right to left. So actually, if I did this, that would break. And so I have a feeling what I actually need and I what I should do really is look it up properly.

Yeah, but what you actually need is beside because beside. Yes, beside is the thing which behaves exactly the way we want. And it's the same as putting two things next to each other, as long as you don't mess with the kind of execution order. So here's beside it applies the monadic g first, and then applies the dyadic to the result, which is what we want here.

So I think that means. Alright, now, what do we expect this to work? So it's going to go. I don't know if it's. Okay, that does work. That's very interesting, isn't it? So I guess the thing is here, it starts looking left, right? I still want to get better at these parsing rules.

It starts looking left and it says, okay, that's an operator. So we're not done yet. Okay. And so then given it's found an operator, it then needs to find a function. And it's found a function here, but I guess it's not a complete function because it's got an operator to its left.

And so it keeps looking until it's got a complete function. Something like that. I'd love to see like a really clear documentation of this parsing rule. Like how is this passed? Yeah, I think you're right. And then there's a way to do it with train and without the last symbol for the what's called without the symbol.

Right. Without the tilde. I'm pretty sure you're thinking of J. It's got a hook in J. I'm pretty sure APL does not have such a thing. See the response from the Adam gave to my computing the mean of a vector. One of them was equivalent to this and one of them, the last.

No, no, that was different. He had a fork. A fork is a list of three verbs in a row. So you're thinking of something else, which is the definition of mean, which is very interesting, but we are kind of skipping ahead, which is equal to the sum divided by the count, which is tally, which I can't remember how to type.

Double quote. OK, that's the main. OK, and this is different because we here we've got one, two, three verbs. And the definition of these three verbs in a row, it's called a fork. And what it does is it applies this to Omega and this to Omega and this to the two results.

So sum of Omega divided by count of Omega. But as we're skipping ahead, Jeremy, there's an option you can turn on with the box option to show trains and functions is like parse trees. I posted it in the chat. OK, great. And presumably that would just be if we remove this.

Oh, why wouldn't you always want that? It's beautiful, isn't it? Yeah. I'm still a little bit of a pain kind of deciphering where all of the arguments are supposed to end up, but. Yeah. Well, not too bad, right? Because you've got to. This this is obviously going to apply to the right hand side.

So I guess that's OK. So. And yeah, but I mean, you can always end you have I think that those pictures they have is nice of like what the side does. Right. Well, let's put that at the top, maybe. I've actually got a complex section there. So I should move the complex description.

Or just maybe I'll copy it. Okay, maybe it would even be useful to see. Or J zero. See, that's how you just do. All right. Okay, so I think we can now get back to our stuff for 2022 APL competition. So that was our kind of group by thing.

And okay, so that, and did we have mod addict and dyadic? This is dyadic. And this is. Right, right, right. We've got the monadic and dyadic versions of the function created by the operator. Cool. All right, so the next in our list of these two. Let's do this one then, shall we?

Oops, something went funny happen there. So this is called key. And that's not better. Okay. Okay, this is called left shoe. I like it. Cool name. Oops, I changed the wrong name. Left shoe. Okay. Monadic is in closed and dyadic has a couple of meetings. And close. So this enclosed thing happens, it gets used all the time.

And it seems to be something like what Jay calls boxing or Kind of creating like references or something. Okay, so we're basically That's a list. And this is row of that list. Row of the list. Okay, and then. So this is a list with three things in it. This is a list with two things in it.

And the second of the things in the list is the list two, three. Does that make sense? And then this is gonna be a list with one thing in it. Because it's a. This is a scaler. That's interesting, yes. Okay, of course, this is a scaler. So it's empty, it's got an empty rank, an empty shape, sorry.

So this is a, okay, so. I think that's interesting. Right, and close, how do I type in close? See. Right, right, right, okay, so that's what it does. So it creates a scaler or an atom containing a list. And you've got to do that because. Arrays kind of like conceptually only contain atoms.

So when you strand it like this, it's actually creating, I guess it's enclosing this list. Which I think is the same as concatenating. It is, see, okay, so if I concatenate the scaler one with the atom, enclose two, three, that's what this is. Does that make sense? It's like a void pointer or turning it into a single object that you can store and move around.

Has anybody used this? It seems to come up quite a bit. Serato, did you use it in any of your solutions or anybody else used it? >> Yeah, especially for good thing, partition thingy. >> Yeah, tell us more. >> I think that is your example, sorry, or I think quite expendable already.

It's just a simple idea, but once you put the element, it's one chunk and then you can operate individually. >> I just got somebody at the door, just a moment. Sorry about that. I guess we see it in the docs quite a bit, so yeah. All right, let's see if we've got time to do dyadic.

Sorry. >> I was going to say like using it on a single, what is it? A single scaler, isn't it, does not enclose anything? So that might be a case to look at. >> Yeah, great, okay. Yeah, so that does nothing. Yeah, okay, if y is a scaler, it's a scaler.

Otherwise, it has a depth whose magnitude is one greater than the magnitude of the depth of y. And you can add axes. And I guess we're not meant to be worrying too much about axes, because it says they're a bit old fashioned. And we're definitely not old fashioned, so that's fine.

So dyadic, God, I don't know. What's our quad ml equal to? Let's just focus on the default, shall we? One, okay, partitioned enclose. >> APO Wiki has better examples for this. >> All the partitioned enclose? >> For dyadic, yes. >> Just click on enclose, I think. >> Okay, where's the good examples?

>> Is the first one dyadic? Do you consider that dyadic? >> Well, it's got a plus before it, which is, or is that what it is? Is it an operator? >> There's an optional K, that's the axis. I'm confused about this, they're doing plus. No, this is monadic, because when you have two functions in a row, it applies this monadically and then dyadically to the result with the left-hand side.

>> And this is a good example for monadic, because it's not intuitive, but I didn't think it's gonna do that. Did you? >> Let's think about that. Okay, so all right, so that's gonna turn the right into a scalar. And so then it's gonna be one, two, three, plus four, five, one.

Yeah, okay, so if you did one, two, three, plus one, it would add one to each of these three things. So if you do one, two, three, plus an enclosed array, it adds this to each of these three things, which is actually very, yes, that's very instructive and helpful.

>> Oh, we could have used this with a tally operator for that DNA competition question. 456 would be a CGT and the left hand side would have been the DNA sequence. >> Sounds interesting. Great. Okay, well, we're kind of out of time. So we'll do the dyadic version next time.

Thanks, everybody. Bye. >> Thank you. Bye. >> Bye. >> Adios, everybody.