29/05/2012
In a previous entry, we had a look at linear interpolation and concluded that we should prefer some kind of smooth interpolation, maybe a polynomial.

However, we must use a polynomial of sufficient degree so that neighboring patches do not exhibit very different slopes on either side of known points. This pretty much rules out quadratic polynomials, because polynomials of the form
are only capable of expressing (strictly) convex (or concave) patches. A quadratic piece-wise function would look something like:
Read the rest of this entry »
3 Comments |
algorithms, Mathematics, programming | Tagged: Cubic, Cubic Interpolation, interpolation, Quadratic, Quadratic Interpolation |
Permalink
Posted by Steven Pigeon
22/05/2012
The Fibonacci numbers are very interesting for many reasons, from seed heads to universal coding (such as Taboo Codes). Just figuring how to compute them efficiently is plenty of fun.

The classical formulation of the Fibonacci numbers is given by the following recurrence:
Read the rest of this entry »
1 Comment |
algorithms, Mathematics, programming | Tagged: Fibonacci, Fibonacci Numbers, pinecone, Rabbits |
Permalink
Posted by Steven Pigeon
15/05/2012
In a couple of different occasions I discussed the topic of interpolation, without really going into the details. Lately, I had to interpolate data and that got me interested (again) in interpolation; and I think I should share some of the things I learned.

In this first post (of a series), let us begin by the simplest interpolation of all (after constant interpolation): linear interpolation.
Read the rest of this entry »
3 Comments |
algorithms, Mathematics, programming, Science | Tagged: algebra, interpolation, Linear Interpolation |
Permalink
Posted by Steven Pigeon
01/05/2012
Quite a while ago, I presented the Collatz conjecture and I was then interested in the graphical representation of the problem—and not really going anywhere with it.

In this entry, let us have a look at the implementation of the Collatz function.
Read the rest of this entry »
1 Comment |
algorithms, assembly language, C, C-plus-plus, Mathematics, programming | Tagged: assembly language, Collatz, Conjecture |
Permalink
Posted by Steven Pigeon
20/03/2012
On a number of previous installment of this series, I’ve discussed permutations, generating uniform random points in a triangle, on a sphere, the inversion method, even recycling expensive random bits. In this entry, I will use the rejection method to generate random numbers from disjoint ranges.

For simplicity, let us consider only the uniform case, where all values are equally likely to be drawn. So instead of drawing a number
from a range, say
, we’re interested in the case where the set is composed from several intervals, something like
. We may think of drawing uniformly from
and retry if we “fall in the gaps”, that is, if we draw a number in
or
.
A first, it doesn’t seem like a bad plan, especially if the “holes” are rather small and easy to miss—that is, we have a good chance of hitting in an allowable range in a very few tries. For example, if the ranges we’re interested in look like
and
, drawing from
really doesn’t sound like a good idea.
But what if we compacted the ranges?
Read the rest of this entry »
Leave a Comment » |
algorithms, Mathematics, programming | Tagged: IP, IP v4, pseudo-random, random, ranges, uniform random |
Permalink
Posted by Steven Pigeon
13/03/2012
As part of an open-source project I’m working on (right now, we are still at the technical feasibility stage where we explore and eliminate technical risks, full disclosure will come later) we have to issue session numbers. They’re not session numbers in the usual sense, but they still need to be unique, and not amenable to simple attacks.

There are a couple of ways of generating unique session numbers. RFC 4122-compliant unique IDs is one possible way.
Read the rest of this entry »
2 Comments |
algorithms, bit twiddling, hacks, programming, Python | Tagged: RFC 4122, session, session id, SSL, urandom, UUID |
Permalink
Posted by Steven Pigeon
06/03/2012
The nice thing about the trigonometric circle is that it is, quite so indeed, a circle. But it’s also a disadvantage when you consider the implementations of functions manipulating angles, especially
.

This is particularly a problem when you’re not so much interested by a specific angle (which is at all time always well-defined) than by a series of angles varying in time. If you’re certain that your angle is always within
or within
, you do not expect problems. What if, on the contrary the angle wraps around?
Read the rest of this entry »
Leave a Comment » |
algorithms, machine learning, Mathematics, programming | Tagged: angle, π, Sphere, spherical coordinates, trigonometric circle, trigonometry, unit circle |
Permalink
Posted by Steven Pigeon
28/02/2012
A couple of months ago (already!) 0xjfdube produced an excellent piece on table-based trigonometric function computations. The basic idea was that you can look-up the value of a trigonometric function rather than actually computing it, on the premise that computing such functions directly is inherently slow. Turns out that’s actually the case, even on fancy CPUs. He discusses the precision of the estimate based on the size of the table and shows that you needn’t a terrible number of entries to get decent precision. He muses over the possibility of using interpolation to augment precision, speculating that it might be slow anyway.

I started thinking about how to interpolate efficiently between table entries but then I realized that it’s not fundamentally more complicated to compute a polynomial approximation of the functions than to search the table then use polynomial interpolation between entries.
Read the rest of this entry »
1 Comment |
algorithms, assembly language, C, C-plus-plus, C99, embedded programming, Mathematics, programming | Tagged: cos, cosine, MacLaurin Series, Polynomial, Polynomial Approximation, SIN, Sine, Taylor Series |
Permalink
Posted by Steven Pigeon
31/01/2012
The float and double floating-point data types have been present for a long time in the C (and C++) standard. While neither the C nor C++ standards do not enforce it, virtually all implementations comply to the IEEE 754—or try very hard to. In fact, I do not know as of today of an implementation that uses something very different. But the IEEE 754-type floats are aging. GPU started to add extensions such as short floats for evident reasons. Should we start considering adding new types on both ends of the spectrum?

The next step up, the quadruple precision float, is already part of the standard, but, as far as I know, not implemented anywhere. Intel x86 does have something in between for its internal float format on 80 bits, the so-called extended precision, but it’s not really standard as it is not sanctioned by the IEEE standards, and, generally speaking, and surprisingly enough, not really supported well by the instruction set. It’s sometimes supported by the long double C type. But, anyway, what’s in a floating point number?
Read the rest of this entry »
1 Comment |
algorithms, C, C-plus-plus, data compression, data structures, hacks, machine learning, programming | Tagged: double, extended, float, IEEE 754, octuple, quadruple |
Permalink
Posted by Steven Pigeon
24/01/2012
So in the two previous parts of this series, we have looked at the selection algorithm and at sorting networks for determining efficiently the (sample) median of a series of values.

In this last installment of the series, I consider an efficient (but approximate) algorithm based on heaps to compute the median.
Read the rest of this entry »
7 Comments |
algorithms, C, C-plus-plus, data structures, hacks, programming | Tagged: heap, max-heap, med-heap, median, min-heap, selection |
Permalink
Posted by Steven Pigeon