Suppose you have a differential equation that you want to solve numerically. An approach you might take is collocation, which is based on the idea that an unknown function \(u(z)\) can be approximated by a sum of \(N+1\) basis functions \(\phi_n(z)\):
- $$ u(z) \approx u_N(z) = \sum_ ...read more
Suppose you need to know if a given number is a multiple of, say, 5. There's a simple solution to this problem: the modulo operator. In many languages, it looks like this:
x % 5
And what it returns is the remainder when dividing x by 5. If that remainder ...read more
I have an exhibit at the Vancouver Mini Maker Faire this weekend (June 6-7, 2015). It's a DIY Spectrophotometer, that is very obviously home-made, but allows you to see the absorption spectrum of different liquid samples. For example, if you look at the spectrum, you can see for yourself ...read more
Suppose you're iterating through a list, and you want to remove elements under some condition. This seems pretty easy: you've got an element, and python lists have a
removemethod that you can call with that element.
However... let's try it with a simple example:read more
>> mylist = range ...
I had planned to write a blog post about how great Julia is. I spent many years using Matlab, both in grad school and in finance. Since then, I've been using NumPy for mathy things, but it feels really awkward, since it's just sitting on top of another ...read more
If you take any integer and rearrange the digits however you like, the difference between the original number and the rearranged number will always be divisible by 9.
number = 12345 rearranged = 43215 difference = 43215 - 12345 = 30870 difference / 9 = 3430
More generally, you can do the same with numbers ...read more
We all know that you should avoid writing the same code over and over: put it in a function and just call that function whenever you need it. But what if what you need is a class with a whole bunch of really similar methods, each with a different name ...read more
In Python, you can assign a default value to an optional varible like so:
def myFunction(x, y = 0): print x + y
myFunctioncan be called with either one or two arguments. If only one argument is supplied,
ywill have a value of 0.
This is all perfectly straightforward ...read more
My initial approach to the tiling problem was to find all the possible configurations, calculate a measure of the distance between same-colour tiles for each, and then select the configuration with the maximum distance. While this does reliably find the optimal solution, it also very quickly becomes infeasible as the ...read more
We have a play-mat: eight rubber tiles, two of each colour, that fit together. We had put it together in a 4x2 arrangement, without considering the colours. The result was ... displeasing. Two pink tiles were right next to each other, but all the other pairs were separated. It just looked ...read more
Inputting information through web forms is really easy under Twisted web. You just have to set up
render_GETto display a form, where each input element has a name (e.g. "name", "email", whatever). Then,
render_POST, taking an argument of request, can access these values as follows:read more
name = request.args ...