Regula Falsi

01/11/2016

The regular falsi, or method of false position, is a method to solve an equation in one unknown, from an initial “guess”. Guesses are progressively refined, by a rather simple method as we will see, until the exact answer is reached or until convergence is reached. The method is useful when you don’t really know how to divide by arbitrary values (as it was the case in Ancient Times) or when the equation is cumbersome.

Read the rest of this entry »


Horner’s Method

25/10/2016

It’s not like evaluating polynomial is something that comes up that frequently in the average programmer’s day. In fact, almost never, but it brings up a number of interesting questions, such as, how do we evaluate it very efficiently and how much of a simplification in the computation is actually a simplification?

The generic polynomial has the form

a_0+a_1x+a_2x^2+a_3x^3+\cdots+a_nx^n.

If we evaluate this naïvely, we end up doing O(n^2) multiply and O(n) additions. Even using the fast exponentiation algorithm, we still use O(n \lg n) multiplies. But we can, very easily, get down to O(n).

Read the rest of this entry »


Fractional Bits (Part III)

18/10/2016

A long time ago, we discussed the problem of using fractions of bits to encode, jointly, values without wasting much bits. But we considered then only special cases, but what if we need to shift precisely by, say, half a bit?

But what does shifting by half a bit even means?

Read the rest of this entry »


Count like an Egyptian (Part III)

11/10/2016

In previous posts, I’ve looked into unit fractions, also known as “Egyptian fractions”, fractions where the numerator is 1 and the denominator n, some integer. We have noticed that finding good representation for a fraction a/b as a sum of unit fraction is not as trivial as it may first seem. Indeed, we noticed that the greedy algorithm, the one that always pick the largest unitary fraction that does not exceed the rest as the next term, may lead to long series of unit fractions, sometimes with large denominators, even for simple fraction. For example,

\displaystyle \frac{412}{1000}=\frac{103}{250}=\frac{1}{3}+\frac{1}{13}+\frac{1}{574}+\frac{1}{699563}.

In a previous post, I suggested decomposing a/b in an “easy way”, which was often better than the greedy algorithm. Surely, we can do better, but how?

Read the rest of this entry »


Yet Another Square Root Algorithm (part III)

04/10/2016

Finding good, fast, and stable numerical algorithms is usually hard, even for simple problems like the square root, subject we’ve visited a number of times. One method we haven’t explored yet, is using the Taylor series. The Taylor series for \sqrt{n} around a is given by:

\displaystyle \sqrt{n}=\sqrt{a}+\frac{1}{2}\frac{n-a}{\sqrt{a}}-\frac{1}{8}\frac{(n-a)^2}{a^{3/2}}+\frac{1}{16}\frac{(n-a)^3}{a^{5/2}}+\cdots

Can we exploit that to compute efficiently, and with some precision, square roots?

Read the rest of this entry »


Weird binomial coefficients

27/09/2016

The binomial coefficients find great many uses in combinatorics, but also in calculus. The usual way we understand the binomial coefficients is

\displaystyle \binom{n}{k}=\frac{n!}{k!(n-k)!},

where n and k are integers. But what do you do with \displaystyle \binom{\frac{1}{2}}{k}?! Is it even defined?

Read the rest of this entry »


Padé Approximants

20/09/2016

Sometimes, you need to compute a function that’s complex, cumbersome, or which your favorite language doesn’t quite provide for. We may be interested in an exact implementation, or in a sufficiently good approximation. For the later, I often turn to Abramovitz and Stegun’s Handbook of Mathematical Function, a treasure trove of tables and approximations to hard functions. But how do they get all these approximations? The rational function approximations seemed the most mysterious of them all. Indeed, how can one come up with

e^x \approx 1-0.9664x+0.3536x^2

for e^x, for 0\leqslant x\leqslant\ln 2?

Well, turns out, it’s hard work, but there’s a twist to it. Let’s see how it’s done!

Read the rest of this entry »


The protoshadok number system

13/09/2016

We’re so used to our positional notation system that we can’t really figure out how to write numbers in other systems. Most of the ancient systems are either tedious, complicated, or both. Zero, of course, plays a central role within that positional system. But is it indispensable?

shadok

In one of my classes, I discuss a lot of different numeration systems (like Egyptian, Babylonian, Roman and Greek) to explain why the positional system solves all, or at least most, of these systems’ problems. I even give the example of Shadok counting (in french) to show that the basis used isn’t that important (it still has to be greater than one, and, while not strictly necessary, preferably a positive integer). But can we write numbers in a positional system without zero?

Read the rest of this entry »


Sums of powers

15/03/2016

When you’re studying algorithms, there is a number of series and expression that keep showing up. A while ago, I discussed the special form \sum_{i=1}^n i a^i. But that’s not the only one. Another form that keeps showing up is

\displaystyle s_k(n)=\sum_{i=1}^n i^k.

We can look the particular form this sum takes for specific k in some compendium (like CRC’s tables and formulae, that have seen 30-something editions over the last half century), or… you can find the formula yourself. It’s a bit tedious, but not that complicated.

Read the rest of this entry »


Real dice.

01/03/2016

Suppose you want to draw randomly a number between 0 and 1, with multiple throws of a six sided dice, what would you write down on its face?

dice

Read the rest of this entry »