Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts
Monday, March 9, 2015
Horner's Rule for Polynomial Computation
Suppose I had a polynomial anxn + an-1xn-1 + … + a1x + a0, and a point x* at which I wanted to evaluate that polynomial.
Now our immediate inclination is to just substitute the point straight into the polynomial to get an(x*)n + an-1(x*)n-1 + … + a1(x*) + a0 and then work it through in the obvious way: we raise x* to the nth power and multiply by an, then we raise x* to the (n-1)th power and multiply by an-1, and so on, adding them all together at the end.
That seems like a lot of work though. We could be more efficient by working it the other way around: that is, starting with x* and caching the intermediate powers of x* as we work our way up. That would definitely cut down on some of the multiplications. But is there actually an even better way to do this polynomial evaluation?
As it turns out, there is – we can use Horner's Rule!
Not only is Horner's Rule more efficient than either of the above approaches, it is actually an optimal algorithm for polynomial computation: that is, we can prove mathematically that any other rule for polynomial computation can do no better than Horner's Rule.
So let's investigate how this rule works, and write up some Java code to test it out!
[We're back with the SICP exercise blog posts! This one is inspired by Exercise 2.34.]
Saturday, February 14, 2015
The N-Queens Puzzle and 0-1 Integer Linear Programming
We saw in the last post how we can tackle the N-Queens Puzzle recursively by considering each column in turn. This way, we can generate all of the possible solutions to the puzzle (eventually!)
However as we'll see today, we can also consider the puzzle in terms of an optimization, according to some constraints upon the rows, columns, and diagonals of the chessboard. By formulating the N-Queens puzzle as an instance of a 0-1 Integer Linear Program, we can pummel it with the full force of modern combinatorial optimization tools!
So that's the plan. First we'll develop a mathematical formulation for the N-Queens puzzle. Then we'll see how we can translate the variables and constraints of the problem into a matrix representation in Java code, which we can pass into some open-source linear programming tools: the SCPSolver together with the GNU Linear Programming Kit (GLPK).
Thursday, January 29, 2015
Higher-Order Functions and Accumulation
The notion of a higher-order function – that is, a function that operates on other functions – is a fundamental idea in mathematics.
As an easy example, take the concept of differentiation from Calculus. Whereas a "normal" function like sin(x) operates in terms of numbers (ie. pass it a number, and it'll return you a number as a result), the act of taking a derivative is a little different. When we take the derivative of a function, we get back another function.
This is actually an incredible idea. Now not only can we talk about transformations (of numbers), but we can talk about transformations of transformations!
So in yet another of my blog posts about things I've investigated while working my way through the Structure and Interpretation of Computer Programs, we're going to look at this idea of higher-order functions by considering the process of accumulation.
We'll write some procedures for calculating sums and products, and then show how we can abstract away the common process of successively combining new elements with a current total. We'll then go a step further and write a more general procedure for filtered-accumulation, where we only take those elements that satisfy a particular condition.
[The relevant SICP exercises for this one are 1.30, 1.31, 1.32, and 1.33.]
We're gonna be using good old Java today. In fact, this topic gives us the perfect opportunity to explore the new functional interfaces and lambda expressions of the latest Java 8 release!
Friday, January 9, 2015
Testing for Prime Numbers
On first glance, prime numbers seem pretty trivial; and yet, most of their more interesting properties still elude us. These numbers have been bugging mathematicians for as long as we've cared about them. All sorts of questions are still completely open:
- Every now and then we find a pair of primes that are separated by a single even number – does that keep happening forever?
(That
last one is literally a million-dollar question, by the way.)
From a computer science standpoint though, the fact that we know so little about
prime numbers is actually kind of useful. If I don't even know how
the primes are distributed, I can't easily factorise a massive
composite number into its prime constituents; not even if I used a
computer. However the reverse problem – taking the primes and
multiplying them together to get to the composite number – is much
easier. Such 'one-way functions' make for awesome cryptographic
algorithms, which are absolutely vital for digital security these
days.
We're
not going to worry too much about that end of things though, and
instead consider the problem of determining whether a given integer
is prime. (Inspired by the SICP
exercises as always: the relevant Scheme solutions are 1.22, 1.24,
1.27 and 1.28.)
We'll
be using good old Java for our programming language today as we
explore three different ways to determine the primality of a number:
- an exhaustive search of all the possible candidates,
- the Fermat test, which uses Fermat's Little Theorem in a probabilistic primality test,
- the Miller-Rabin test, a variant of the Fermat test that fixes one particular deficiency.
Saturday, January 3, 2015
Computing Pascal's Triangle
One of the coolest things about the Structure and Interpretation of Computer Programs has to be the exercises, in my view. Some of them are surprisingly deep explorations of mathematical or computational ideas, and even the ones that aren't still end up being quite interesting.
As the first real post for this blog, I
figured I'd start off with one of the exercises in that latter
category: not as deep, but still pretty cool. This exercise involves
computing the numbers of Pascal's Triangle.
[For those that are interested, in the
book we're dealing with Exercise 1.12 here. My worked solution in
Scheme is available in my GitHub repository. However, in this post
I'll be going wild with this exercise and looking at the problem for
all sorts of weird angles (and all of the code will be in the JVM
languages to keep with the blog's theme.) So the Scheme solutions are
still worth a look if you've read this post, and vice versa.]
Subscribe to:
Posts (Atom)