Redirecting stdOut in Groovy


Groovy (programming language)
Image via Wikipedia

I am adding this because – while it is out there on the web – it took me some time to find it and I suppose this helps make it more visible in search engines.

I was writing a Groovy unit test for a void function that normally would print to the screen. The only simple way to test would be to redirect stdOut to a string and then test the string.

This is the (or one) way to do it:

public void testPrint() {
//based on solution on stackoverflow.com
//redirect stdOut and check string is formatted
//correctly
def bufStr = new ByteArrayOutputStream()
def oldStdOut = System.out;
def newStdOut = new PrintStream(bufStr)
System.out = newStdOut
objBeingTested.print()
System.out = oldStdOut
String prtTestStr = bufStr.toString()
assertTrue(Something-about-the-expected-output)
}

Advertisements

The importance of Turing’s findings


Yesterday I was speaking to a friend – who knows about computers but not about computer science – who told me this blog was just too difficult: so I thought I’d seek to order my own thoughts after reading The Annotated Turing and assist humanity in general (!) by writing a piece that explains what I think is the most fundamental finding from Turing’s famous On Computable Numbers paper.

To begin one must remember that Turing’s paper is not about electronic computers at all: indeed while in the paper he refers to “computers” – what he means is a person who computes.

Turing’s paper does describe an entirely theoretical mechanical machine that replicates the actions of a “computer” in solving a mathematical process through simple steps. You could think of those steps in this form if you were the computer adding 1 to 1:

  • Read in the first number: 1
  • Read in the operation: +
  • Read in the second number: 1
  • Apply the operation to both numbers
  • Write out the answer

Turing’s argument is that the only numbers we can compute are those that can be computed in this mechanical way. This, in itself, was – and in a way remains – a controversial statement because it says that at the heart of human consciousness is a mechanical process and that, fundamentally, there is nothing special about consciousness.

In a way this flies in the face of all our experience of (digital) computers. Whatever else we know about them we know they cannot “think” – they are certainly mechanical (in the sense that they solve problems in the step-by-step way outlined above ) but they are so radically less powerful, it would seem, that our consciousness, our thinking must be built upon something else.

This – the idea that consciousness is more than mechanical – is rather out of favour these days but it still has its adherents: you can read more about the fascinating “hard problem of consciousness” if you want to read more about it, for here we are going to take the materialist approach and argue that the brain is indeed a mechanical processor, albeit one that (perhaps because of massive parallelism, or specific adaptation, or whatever) is hugely ahead of where our electronic computers are today in certain fields (though the gap is clearly closing on just about all fronts).

So if we accept that the only means available of calculating is a mechanical process then we must also accept Turing’s finding that we can only calculate a tiny minority of numbers or problems.

To explain this we need to look at the concept of infinity, or more properly infinities, because, at least according to the widely accepted theory of Georg Cantor, there is more than one infinity – indeed there are an infinite number of infinities all of a different size – or order as the size is referred too.

Sounds like nonsense, doesn’t it? But here’s a mind experiment to help you understand.

Think of a ruler, marked out in centimetres (or inches if you are old fashioned). But this ruler stretches on infinitely – so an infinite number of centimetre markings are present. But each one of these markings can be associated with a natural number, zero, one, two and so on. This is an infinite list which is said to be enumerable (countable) – a number can be assigned each member of the set of markings on the ruler. The number of members a set has is called its cardinality, and so this set has what is called the cardinality of aleph null:

aleph null

But think of that ruler again – how many points are there between the different markings? Clearly we can divide the ruler up over and over again and that means we must have a bigger infinity than aleph zero.

This infinity is of this order  – two raised to the power of aleph null – we discuss why below:

two to the power of aleph null

An aside: the continuum hypothesis

According to the continuum hypothesis  aleph one is the so-called cardinality of the continuum. (Aleph one being the next biggest set of infinite numbers than aleph null: the continuum hypothesis):

aleph one

And so, goes the theory, aleph one is of the same order as the number of real numbers. A real number being an integer number followed by a decimal point and an infinite (aleph null order) set of numbers – in the case of the natural numbers this is an infinite set of zeros, etc. Hence, the hypothesis states:

aleph one equals two to the power of aleph null

======

In any case the important number here is two to the power of aleph null – the order of the set of real numbers.

And why is this the order of the real numbers – because it is the order of the power set of the natural numbers. The power set is the number of subsets of a given set:

Powersets explainedTo explain why this is the order of the set of real numbers think of a binary (0 or 1) expansion after the decimal point – each digit will then be countable – ie there will be a first digit, a tenth digit and a ten millionth digit – that means it is of order aleph null, but the way in which the 0 and 1s can be ordered is the power set of the countable numbers, in other words two to the power of aleph null: a number that by definition is too big to count:

Too big to count

Now, what Turing’s paper shows is that we can only build a machine that computes (in other words calculate) a countable number of numbers – in other words we can only calculate aleph null numbers – or alternatively, solve aleph null problems.

But the number of real numbers is two to the power of aleph null, a much bigger number (to repeat, it is actually too big to count).

This is was a shattering result to a generate of mathematicians who were accustomed to the idea that all mathematical problems were solvable.

On 8 August 1900, one of greatest mathematicians of the nineteenth and twentieth century, David Hilbert, told the Second International Congress of Mathematicians in Paris:

“However unapproachable these [mathematical] problems may seem to us and however helpless we stand before them, we have, nevertheless, firm conviction that their solution must follow by a finite number of purely logical processes … This conviction of the solvability of every mathematical problem is a powerful incentive to the worker. We hear within us the perpetual call. There is the problem. Seek its solution. You can find it by pure reason, for in mathematics there is no ignorabimus.”

But on 12 November 1936, the formal publication date of Turing’s paper, that confidence was destroyed for ever. An uncountable number of mathematical problems simply cannot be solved.

Now available to all


I think I have more or less got this right now:

Paging explained/simplified
Paging explained/simplified

This is also now available on git: https://github.com/mcmenaminadrian/Paging

This was also helpful in explaining how the curves work.

And of course this – The LATEX Graphics Companion – was also essential.

Copyright (c) Adrian McMenamin, 2011, reuse licensed under the CC0 Licence.

Struggling to make up for the art failings


Have spent the whole night trying to get LaTeX and METAPOST to draw my simple paging system diagram. Have got this far:

My eldest daughter has a piece of art going on display at a gallery tomorrow. All I can say is that she doesn’t get her great skill at this from me!

Another book for the wish list?


Donald Knuth at a reception for the Open Conte...
Image via Wikipedia

I have to admit that I originally bought my boxed set of  The Art of Computer Programming more because I thought I had to own it, rather than because I did. After all, it’s what all serious or even semi-serious programmers own…

So it was a pleasant surprise that, last year, it was of real practical use when Knuth‘s writings on stacks, lists and trees were the core of one half of one of the modules on my MSc course.

Now, though, the great man has published a further volume and so I have to decide if I want to keep up…. hmmm.

More at Slashdot.

Found my copy of “Basic Computer Games”


a tic tac toe game
Image via Wikipedia

This book – Basic Computer Games – is now, justly, regarded as classic and my brother (don’t tell him I have got it) and I spent many evenings and weekends typing in the code from it into our ZX80 and later a Spectrum.

Tic-tac-toe-full-game-tree-x-rational
Image via Wikipedia

Pristine copies are said to sell for many hundreds of pounds, though so many were sold at the time that second-hand copies are very cheap.

Right now I have a coursework exercise of writing a game – what I used to call exie-ohsies, but in England is called “noughts and crosses” and in the US “tic-tac-toe” – in the Groovy programming language.

There is a tic-tac-toe program in the book and I wonder if it would be practical to reimplement that in Groovy (suitably acknowledged of course)?

Update: having looked more closely at the assignment I don’t think the BASIC code will fit too well. A pity, because it might have been fun.

The opposite of science, but could be fun


Venn diagram for P, NP, NP-Complete, and NP-Ha...
Image via Wikipedia

I have created a prediction market on Vladimir Romanov’s P=NP proposal. Starting price for it being proved correct by the end of 2011 is very low – or the odds are long, depending on how you look at it (10 cents wins $99.90 ie 999/1).

Have a bet – it’s free and it’s fun.

It is here.

Possibly the most important news you will read this year


This an example of how a public and private ke...
Image via Wikipedia

Apparently P==NP. (So public key encryption – used for internet commerce – is broken and many more problems than we previously thought are quickly solvable).

At least that is the suggestion you can read here. Slashdot also has this here.

If it’s true then the revolution has just begun. If it’s false, well, tomorrow’s another day…