I asked this question on Stackoverflow, and got a nice answer, but one which I needed to think through a little more. Here's my conclusions.
My aim was to understand how to write robust code which could take scalars, but which would also do "as expected" on arrays. Let me expand a little on this, by using a slightly easier example than in the original question. Suppose f(x)
is a function which takes a scalar and returns a scalar. I then want that if x
is actually an array, of any shape, then f(x)
will return an array of the same shape as x
, namely the array obtained by applying f
to every entry.
In my new job, I find myself working with numpy (after a break of a couple of years, and now professionally, and not as a hobby.) Numpy is great, but it doesn't half require a little thinking upon occasion.
Read More →Suppose we have an array of 10 points in the plane. Should this be represented as a
numpy
array of shape(2,10)
or(10,2)
?
Given a population \( P \) and sampling at random ("with replacement") what's the expected number of samples I need to see 50% (or any fixed proportion) of the population.
I deliberately ask for "expected" because calculating expectations is often easier than getting a handle on the whole probability distribution. A trick is to exploit linearity: express the random variable of interest as a sum of random variables you can calculate the expectation of.
Read More →This post, very tangentially, relates to a quiz we set job candidates. If you are applying for a job at my current company, and somehow work out I work there, and find this, then you probably half deserve a job anyway.
Suppose you have a population \( P \) and some test as to whether a member of the population is good or bad. We want to find a "random good member". There are two methods that come to mind:
The first method has the virtue of being simple. The second method uses a lot of memory, if \(P\) is large. But on closer thought, what if the proportion of "good" members is rather small. The 2nd method is guaranteed to find a good member in \( O(\vert P \vert) \). How slow can the first method be?
Read More →At the dying ends of the work day, I came across this page and was initially confused by
public abstract class Enum<E extends Enum<E>>
Doesn't that look, well, horribly circular? Stackoverflow suggests this is common confusion.
Read More →LED lights seem a no-brainer: instant on, good colour reproduction, extremely energy efficient (cool to the touch after minutes of use). But they are expensive. I wonder what the payback time is?
Read More →More a note to a future self than aything else... In C++ we have the notion of Resource Acquisition Is Initialization which I think I have internalised as the following:
Read More →Any "resource" (memory, file, handle etc.) should be "owned" by an object. An object's constructor should allocate the resource, and the destructor should free the resource (with some appropriate choices then made for what copy/move constructors/assignments should do). By use of C++11
unique_ptr
andshared_ptr
this model can be extended to pointers.