– Fluent Python
(This book is so good)

Take 4 at a stable blog
– Fluent Python
(This book is so good)
From the book Fluent Python (which you can get from the Humble Bundle right now):
What would the following piece of code do?
Four choices:
t becomes (1,2,[30,40,50,60])The answer is the link, which is quite surprising.
It turns out that t[2].extend([50, 60])doesn’t break Python, and this riddle is really a super esoteric corner case…
Good news; my advisor and I submitted our first paper this past Monday. I’ve uploaded a draft here for those curious.
I think I always forget how privileged I am. I benefited from many points laid out in the article by Mr. Levy: my parents explicitly moved to one of the best school zones in Tallahassee just for me to get a better shot at college applications, I also had the benefit of them expediting the citizenship process so I could apply as an American citizen, and they provided enough money for all the extracurricular I needed. All this after living in graduate student housing for 2 years.
I like to think that I can do what I am doing without my parents’ support, but the truth is I can’t. So much of success is dependent on having a good family. I don’t think any of the people currently in my graduate program came from an under-privileged home… and to be honest, all of my friends are quite affluent.
Do I agree with the article? Yes. Do I think it’s enough? No. College is the end of the road which starts from pre-K. Certainly universities can offer more aide, or allocate more slots for under-represented students, but will this really pull them up? I don’t think so; the system right now is too fundamentally broken for a top-down approach.
I really liked this problem that my friend Shamile gave during dinner:
Consider 50 people lined up in a random order. Show that there exists a subsequence of length 8 such that the height is non-increasing or non-decreasing.
The solution is quite elegant. Assign an ordered pair
to person
in the line such that the first coordinate is the length of the longest subsequence which is increasing from person
, and the second coordinate is the length of the longest subsequence which is decreasing from the same person.
Let’s assume by contradiction that the longest such length is less than 8; this implies that the set of acceptable coordinates is from (0, 0) to (7,7). There is only 49 total combinations, so by pigeonhole principle, there exists a pair of the same number. But this cannot be! The rest is left to the reader.
I always forget this fact, so hopefully typing this out will help.
Let
be a triangle with vertices
, then there exists unique linear functions
such that
. This is the so called barycentric coordinates.
The fact here is that
where
is the length of the edge opposite vertex
, and
is the area of the triangle. The vector
is the unit outward normal.
The derivation is simple. The direction of the gradient is towards the highest growth, and as the barycentric coordinates are linear functions, it’s clear that
is the correct direction. The scaling is to simply note the area as
where
is the shortest distance from the edge to the vertex. This gives us the inverse of the slop as the barycentric coordinate has a value of 1 at the vertex.
The title is probably misleading, but this is a lesson I needed to talk about.
I wrote out some simple code for the quadrature over the reference triangle last time, which involves a double loop. To my chagrin, my immediate reaction to speeding up the code was to put it into Cython, and give it some type declaration.
This did speed up my integrals, but not as much as vectorization. By simply condensing one of the loops into a dot product, and using vector-function evaluation, I sped up my code a substantial amount, especially with higher order integration of “hard” functions.
To see what I mean, consider the following function
The speedup I get is staggering.
Quadrature rules on the interval
is pretty well-documented in many different textbooks (see for example Cheney and Kincaid). By transformations, we can easily integrate across the whole real line spectrum. Going into two dimensions, we can take tensor products to integrate rectangles easily. On the other hand, integration on the 2D simplex (i.e. triangles) is a bit harder. There really exists two types of quadrature rules for the triangle:
The latter offers better convergence with smaller points, and has better spread-out points than the tensor product version, but offers less technical support through software packages; we refer the reader to the following paper. We will discuss the first, and exposit a little more; I will be borrowing heavily from Karniadakis and Sherwin’s fluid dynamics textbook.
Suppose our triangle at (-1, -1), (1, -1), and (-1, 1) (so called reference triangle) uses
as it’s coordinates. If we use the change of coordinates
, it’ll map the triangle to the square defined by (-1, -1), (1, -1), (-1, 1) and (1,1). Hence, we can express the integral using a change of variables (details omitted)
.
We note that the weird factor is simply the change of variables Jacobian. Now it’s easy to use Gaussian quadrature on this! But there are better version, as we have the Jacobian term; looking up the quadrature rules, we see that Gauss-Jacobi fits our bill. We refer the reader to page 144 of the Sherwin textbook to see the full-details.
A simple implementation is below (this is far from the best). I guess I’ll talk about this at the end of the week.