## RationalQ – Testing for rationals in Mathematica

August 30th, 2009

Mathematica has a large set of functions that you can use to test the properties of numbers.  For example

IntegerQ[x]

returns True if x is an integer and False if it isn’t.  Of course you are not just restricted to asking if x is an integer or not.  For example, you can ask if it is an even number

EvenQ[x]

or an odd number

OddQ[x]

Perhaps you are wondering if x is prime

PrimeQ[x]

or even if it is an algebraic integer

AlgebraicIntegerQ[x]

The observant reader will notice that all of these functions end with a capital Q and a way of remembering this is to think that you are asking a question of the variable x.  So the question ‘Is x an integer‘ becomes, in Mathematica notation, IntegerQ[x].

I am currently working on a piece of code where I need to determine whether or not a particular number belongs to the set of rationals and I assumed that a suitable function would exist in Mathematica and that it would be called RationalQ[] so I was rather surprised to see that there is no such function in Mathematica 7.

So, I’ll just have to come up with my own.  A quick search resulted in the following function definition from Bob Hanlon

RationalQ[x_] := (Head[x] === Rational)

Which almost does what I need.  It handles the following correctly

RationalQ[1/2] (gives True)

RationalQ[Sqrt[2]] (Gives False)

but I needed a version of RationalQ that also returned True when passed an integer.  After all, the integers are just a subset of the rationals.  A moments thought resulted in

RationalQ[x_] := (Head[x] === Rational || IntegerQ[x]);

Which seems to work perfectly.  So, I offer the above function for anyone who is googling for a RationalQ function and I also ask the following questions to any Mathematica gurus who might be reading this

• Is there anything wrong with the above definition?
• Why isn’t such an obvious function not included in Mathematica as standard?

## Allowing the installation of non-market applications in Android

August 27th, 2009

The Android Market is not the only place where you can get applications for your Android phone.  As long as you are prepared to accept the increased risks then you can install applications from anywhere.  Android applications have the extension .apk and to allow their installation all I had to do on my HTC Hero was

• From the home screen press the Menu button and then tap on Settings.
• Tap on Applications.
• Tap on Unknown sources (allow install of non-Market applications) and follow the prompts.

This information is widely available on the web and there are  several other ways of doing it.  The only reason I am posting it here is as a reminder to self but it’ll be a bonus if it’s useful to anyone else.

## Finding prime numbers using voodoo regular expressions

August 26th, 2009

If you have learned how to program then the odds are that you have written a program that displays a list of prime numbers.  A prime number generator is a programming rite of passage that is almost up there with the ubiquitous ‘Hello World‘ application (for programmers who are going on to do numerical work at least).

There are various ways you might choose to do it but I bet that your first (or even your second or third) thought wouldn’t be ‘I know – I’ll use a regular expression‘. Someone known only as ‘Abigail’ obviously thought that the world desperately needed such a regex since he / she came up with the following incantation

^1?$|^(11+?)\1+$

Which is a regular expression that I assure you can be used to generate a list of all the prime numbers.  If you are in the mood for a puzzle then sit back, grab a cup of coffee and try to work out HOW it can generate the primes.

If you give up then head over to Python Programming for a full explanation along with Python code that demonstrates that it really does work.

Can anyone come up with regular expressions for other interesting series of numbers?

Update (27th August 2009): Someone pointed me to an alternative explanation here.

## Detexify answers ‘What’s the LaTeX code for this symbol?’

August 19th, 2009

There are a lot of symbols in mathematics and I mean a LOT!  Not content with the entire Greek alphabet, mathematicians have gone on to use symbols from other alphabets such as Hebrew.  Once they had run out of alphabets they went on to invent hundreds of symbols themselves – a symbol for every occasion.

So, you are writing a paper in an esoteric (or maybe not so esoteric) area of mathematics and, naturally, you are writing it in Latex.   Suddenly you think to yourself ‘What’s the LaTeX command for <insert weird and wonderful glyph here>’

Searching in vain through list after list of LaTeX symbols you get to thinking ‘If only I could just draw the symbol and have the computer tell me what the LaTeX command is‘.

Well now you can!

Detexify is a new project from Philipp Kühl (who had the initial idea) and Daniel Kirsch (who implemented it) and is essentially an exercise in machine learning.  Sometimes it works perfectly (such as in the screenshot above) but other times it struggles a bit and you end up learning the commands for symbols you never even knew existed.

Teach the system

When it is struggling though, you can help it along.  Eventually you will find the symbol you were looking for and you can click on it to tell the system ‘That squiggle I drew – this is what I meant’ thus helping to train it for future searchers.

Other times though, you cannot blame it for not finding the symbol you meant.  For example I needed about 5 tries before I could get it to recognise my ham-fisted attempt at the lowercase zeta symbol.  This says a lot more about my poor handwriting and mouse skills than it does about the quality of Texify though.

Are you rubbish with the mouse?  Use your finger on your mobile phone then!

I found drawing even simple glyphs rather difficult with the mouse and soon found myself wishing that I could do it with my finger or a stylus so I was overjoyed to learn that Robin Baumgarten has released a version of Texify for Android mobile phones.  The Android app works exactly like the web version and connects to the server in order to do the actual recognition.

Iphone users haven’t been left out though since Daniel has released an app for that himself.

This is a great project that Daniel is now developing for his diploma thesis and you and you can read more about its progress over at his blog.

## Walking Randomly celebrates its second birthday

August 18th, 2009

Blimey! When I first started this blog I never even imagined that I would still be writing it two years on and yet here we are – still going strong.  Technically speaking, WR’s actual birthday is on the 20th but I won’t have net access that day so I’m writing this celebration post a couple of days early.

Let’s look at some statistics – most of which are useless but fun anyway

• Average number of daily visits: 1000 (ish)
• Number of RSS subscribers: 501 (up from 143 last year)
• Alexa Ranking: 1,016,417 (up about half a million from last year)
• Google Ranking: 5 (up 1 from last year)
• Number of posts: 296
• Number of comments: 737

I’ve never expected big numbers for this blog since it is a random collection of rather niche subjects (Maths software, mobile computing, games, programming etc) but some of the numbers above are far better than I could have ever imagined when I first started out.  For me, the statistics are only a tiny part of the story though since I don’t write to get statistics.  I write in the hope that someone, somewhere will find the content useful, interesting, fun and maybe all three.

One of the reasons why writing a blog is so much fun is the interaction you get with your readers.  I don’t make any money from Walking Randomly but I get paid handsomely in comments and many of them are worth their weight in gold.  Not only have I learned so much from all of you but I have also made a lot of interesting new friends and contacts from all over the world.  That’s something that’s very difficult to buy.

You people are awesome and without you this blog would be pointless so please keep reading, commenting, correcting me and teaching me and above all…THANK YOU!

## On Android and the HTC Hero (From an ex Windows Mobile guy)

August 17th, 2009

I have been a user of Windows Mobile devices for almost exactly four years getting through both a Dell Axim x50v and a HTC TyTN II along the way.  The TyTN II had been my daily driver (as Julie of The Gadgeteer might say) for just over 18 months and, although it has served me very well in so many ways, I have recently found myself wanting more and have been viewing my friend’s Android phones with envious eyes.

The HTC TyTN II is powered by Windows Mobile 6 and is a true monster of a phone in terms of both sheer physical size and list of features.  When I bought it, it represented state of the art in mobile phone technology and for almost a fortnight I could safely state that I had the best phone money could buy.  It does everything…phone, email, GPS navigation, personal organizer, games, Internet, multimedia, RSS reader, ebook reader, camera, mobile WiFi hotspot…the list just goes on and on.  We have grown to expect such long  feature lists in smartphones these days but way back in 2007 it stood out from the crowd.

It wasn’t long before I simply couldn’t do without it and, as anyone who knows me well will testify, I carried it everywhere. I spend a lot of time traveling and if I ever found myself without it then I would (sometimes quite literally) feel lost.  So, if it was so good and served me so well, why have I just defected over to Android with my new HTC Hero rather than getting the TyTN II’s more natural successor the HTC Touch Pro 2?

Quite simply – I had gotten bored with Windows Mobile!

In the current mobile operating system marketplace, Windows Mobile is something of a dinosaur.  Sure, it gets the job done but it doesn’t have any passion – in a world of Ferrari’s and Lamborghini’s it’s a VW Golf.  It’ll get you to work every day no problem but it doesn’t get your pulse racing.

No longer did I want something that ‘just worked’ – I wanted something that was fun to use and not made by Apple.

### Enter the Hero

Over the last few months some of my friends have been buying Android powered phones – devices such as the HTC G1 and the HTC Magic – and, although they piqued my interest, they were just not good enough for me to take the plunge.  The Hero, on the other hand, was a completely different kettle of fish – it looks GORGEOUS and I had serious gadget lust for it.

I’ve had it for around 2 weeks now and thought I would share some of my thoughts on both the device itself and the Android operating system that it is based on.  I’m going to assume that you already know the basics about Android and that you’ve heard of the Hero and just jump straight in with some observations and answers to questions put to me by people interested in it.

### Is the ‘chin’ a problem?

If you take a look at the side profile of the Hero then you will see that it has what many commentators have referred to as ‘a big chin’

I have been asked several times ‘Do you find the chin a problem?’ and the simple answer is ‘no I don’t.’  In fact I quite like it as it makes the Hero look more like a phone and less like a PDA.  It also provides a convenient space for the standard Android compliment of buttons and trackball.  If HTC had copied the iPhone and gone for the featureless, buttonless blob look then everyone would berate them for copying Apple.  So HTC went for something that looks more distinctive and they get berated for that too – ho hum!

Another HTC Hero owner shares an office with me and we both agree – the chin is fine.  Don’t worry about the chin.

### Capacitive Touch Screens and Keyboards

Roughly speaking, there are two different types of touch screen technology used by modern mobile phones – resistive touch screens and capacitive touch screens.  The practical upshot is that resistive touch screens (as used by Windows Mobile and old Palm-OS devices) need a stylus in order to be effective whereas capacitive screens (as used by the iPhone, Palm Pre and Android phones) can be controlled by the lightest touch of my sweaty, fat finger.  It seems that the general consensus among bloggers is that resistive touch screens are old and bad whereas capacitive touch screens are new and good.

I disagree!  I don’t like capacitive touch screens very much – especially when I have to use them to type.  They just seem so imprecise compared to the trusty stylus that came with all of my old touch screen devices.  I find using the on screen keyboard of the Hero an absolute nightmare and cannot get a decent typing speed no matter how hard I try.  It’s not just the Hero’s fault though – I have used the HTC G1, Magic and Apple iPhone and I hate their onscreen keyboards as well.

My old HTC TyTN II came with a hardware keyboard and I never used it because I got along with its onscreen keyboard just fine.  I really wish I had a hardware keyboard with the Hero though.

I want my stylus back and while we are at it get off my lawn!

### On performance issues and task managers

I’ve read quite a few reviews of the Hero and some of them comment on the fact that it can get a bit sluggish at times.  They’ll say something like ‘I was playing an mp3 while browsing the web and the phone was updating my email, twitter, facebook, RSS feeds, weather and Google latitude position in the background when all of a sudden it felt a bit sluggish’

No s**t sherlock!

Not only is Android a multitasking operating system but the Hero comes with 7 different home screens for you to put your resource-hungry widgets on.  It is all too easy to ask your phone to perform an astonishing number of tasks simultaneously and this tends to slow things down a little.  Expecting otherwise is a little optimistic (to put it politely).

Yes, the Hero can suffer from slowdowns but in my experience it’s generally because you can ask it to do so much.  This isn’t helped by the fact that there seems to be no way of shutting applications down manually – they stay in memory (and often use CPU cycles) until the operating system decides that it’s time for them to go.  The problem is that Android and I have differing opinions on when an application should be shut down.

On my old phone I could press and hold the X icon in the top right hand corner of an application window to kill it (a nice little HTC modification to standard Windows Mobile behavior) but there is no equivalent (that I have found so far) on Android.  So, what you need is a task manager and taskiller does the job nicely – now I can shutdown applications whenever I like.

Some commentators have expressed the view that the Hero should have come with a meatier processor in order to allow it to better deal with the kind of multitasking load that a typical user will throw at it and, of course, I agree – to a point.  We all want faster processors but if HTC had done that then we probably wouldn’t have the Hero today – we’d have it tomorrow.  I’m really glad that we have it today.

I once overheard someone saying (or maybe I read it – I can’t remember) that the Hero had ‘Tomorrow’s software running on today’s hardware.’  Whether or not you think this is a good thing depends on how badly you want to see tomorrow’s software I suppose and I REALLY wanted to see tomorrow’s software.

I, for one, am happy with the result.  As long as you don’t ask it to do too much at once then performance tends to be pretty good (most of the time).  Applications such as Taskiller help with this and I fully expect it to get better with future software updates.

### The Marketplace

Finding applications for Windows Mobile can be a wearying experience and I have suffered from all of the following issues at some point or another with my old phone.

• The website that sells the application you want isn’t compatible with Pocket Internet Explorer so you can’t actually navigate to the application on your phone.
• You find an app that costs 99 cents for the iPhone and \$15.99 for Windows Mobile.  The Windows Mobile version isn’t as good either!
• You find the app that you want and it is free of charge.  However, you can’t install it over the air – you have to hook it up to your computer and use ActiveSync to install it (admittedly happening less these days).
• The website that carried the application that everyone raves about has disappeared so you can’t install it.
• You want to buy the application but you really can’t be bothered to enter all of your personal and credit card details YET AGAIN for YET ANOTHER application provider.

The Android Marketplace solves all of these problems instantly and is a joy to use.  What’s more, it isn’t controlled by some bizarre dictatorship that arbitrarily decides which applications can appear on it and which can’t.  You can also download and install applications from places other than the marketplace if you so wish without illegally jail-breaking your phone .

I like The Marketplace a lot.  It’s not without issues but I’ll talk about those another day – this post is getting too long already.

### Sense UI and multi-touch

The Hero is the first device to use HTC’s gorgeous looking Sense UI (an upgrade will be coming out soon for older Android phones it seems). I have to admit – it was videos like the one below that got me hooked on the Hero. It’s just beautiful!

If you have come from the Windows Mobile world then you may have heard of HTC’s TouchFlo 3D and Sense is essentially a development of that software.  Not only is it very pretty but it is also very useful – giving you seven, fully customizable home screens along with full integration with services such as Twitter and Facebook.  You can put all kinds of different things on these home screens ranging from simple application shortcuts, through toggle switches (Wifi on/off for example) to mini-applications called widgets.

The Hero comes with a nice selection of both standard Android widgets and some extra HTC widgets such as a calendar, various clock faces, a photo album, twitter client, music player and Footprints (a geo-tagging widget).  I find that some of the simplest widgets are the most useful such as the messaging widget that allows me to flick through all of my SMS messages as if they were a stack of paper on the screen or the Wifi on/off toggle switch.

### Flash Support

The Hero has support for Adobe Flash right out of the box unlike older Android phones and the iPhone (At the time of writing). I haven’t used it much but it seems to work just fine. I’ll write more on this in the future when I have had more experience.

### Google Integration

As you might expect, integration with google services such as email and calendar work perfectly.  It’s often simple things that transform the way you use a device and, for me, google calendar is one of them.  I can enter an appointment on my desktop machine and moments later it appears on my phone or vice-versa.  The same goes for adding new contacts.

This might seem like a silly little thing to get excited about but I would say that, because of this integration, I use my phone’s calendar a lot more than I did before I had the Hero and so I am ever so slightly more organised.

### Where’s the File Manager?

Back in my Windows Mobile days I used the built in File Manager a LOT. So much in fact that I eventually installed a better alternative called Total Commander.  Android, it seems, doesn’t come with a file manager by default but there are a couple on the market place.  One that I have used a little is called ASTRO (free by the way) but I haven’t used it enough to tell you if I think it’s good or not yet.

### 3.5mm Headphone Socket

This is another one of those small details that make all the difference…my TyTN II didn’t have a standard headphone socket so I either had to use some special ones  (which I would lose and they seemed to be close to impossible to buy on the high street) or an adapter socket (which I would lose) and so eventually I gave up and stopped listening to music and podcasts on the thing.

I still lose my headphones of course but now I can replace them with any old cheapo pair bought from almost any shop on the high street.  Music, podcasts, YouTube….welcome back into my life.

### A rose by any other name…

OK so here’s my secret: I don’t have a HTC Hero. Oh no, what I have is a T-Mobile G2 Touch which is a HTC Hero that has been renamed as a G2 Touch. No other difference…just the name. I have no idea why T-Mobile insist on doing this and I’m having no part in it. When I call T-Mobile I always refer to it as the HTC Hero and they always know what I mean. They don’t even correct me any more.

It must cost T-Mobile money to re brand it in this way and it’s just a waste of time.

### Application Recommendations

I’ve found the following fun,useful or both and all of them are available from the Marketplace.  Expect to see a lot more about Android Apps from me in the future.

• Astro (File Manager – free)
• DoggCatcher (RSS feed reader and podcast manager –  commercial but worth every penny)
• Hunt the Wumpus (old, text based game.  Good memories for us old timers. – free)
• Target (Word game that you’ll have played in many a newspaper – free)
• TasKiller (Task manager – free)
• Wikitude (Augmented reality browser – free)

Feel free to tell me of your favourites in the comments section.

### Would I recommend it to my wife?

My wife is perfectly happy with most modern technology but she tends to be uncompromising when it comes to functionality and she doesn’t like using any technology for the sake of it. For example, she doesn’t care if she uses Linux or Windows as long as the web browser works and she can access her email. If a colleague sends her a word document and it won’t open in Linux then no amount of jibber-jabber from me will convince her that Linux is the best option because her laptop equipped with Windows and Office will open it just fine.

The same goes for phones. She doesn’t care what operating system her phone runs as long as a) it works as a phone and b) the camera isn’t COMPLETELY crap.

Now, the Hero works just great as a phone UNTIL you try and use it on a (admittedly rare in the UK) bright sunny day. On such a day the screen is completely washed out and you can barely see it which means that you struggle to see what you are dialing because you can’t see the keypad!

Guess what the weather was like when she first asked if she could borrow my phone!

So, onto the camera. The good news is that it’s better than the one on the TyTN II so I am happy BUT (and it’s a big but) the camera on the TyTN II was possibly the worst camera that I have ever had the misfortune to use and so saying the Hero’s one is better is a bit like saying I can ride a unicycle better than a fish.

It’s the usual HTC camera story: slow to focus and actually take a picture, not so good indoors but not too bad outdoors. It’s got 5MP though which is an improvement. The practical upshot is that it’s not as good as the camera on her (much cheaper) dumb-phone (as opposed to smart-phone) and so she is less than impressed with my whizzy new toy.

Finally, we get to boot-up time. Yesterday, we were at a friend’s house for supper and I was asked if I could phone a taxi to take us home at the end of the night. Unfortunately I had switched the Hero off completely to try and stop me from playing with it and so I had to boot it back up. We sat and watched the boot sequence in silence for thirty seconds or so before she said

‘Oh my god that’s crap!’

No…I wouldn’t recommend it to my wife.

### Bluetooth problems

Personally, I need Bluetooth for one thing and one thing only – file transfer.  Guess what the Hero’s Bluetooth stack can’t do?  That’s right ladies and gentlemen – you cannot transfer files to and from other devices straight out of the box.  I find this rather quaint to be honest – almost every other phone in existence can do this but my shiny new smartphone can’t.

There is an application that might save me – Bluex – but you need to root your phone in order to use it fully and that’s not something I am prepared to do just yet so I haven’t tried it.

### The Verdict

I love the HTC Hero and (capacitive touch screen and Bluetooth issues aside) I am really glad that I moved from Windows Mobile to Android and the HTC Hero. More interesting than Windows Mobile, more open than the iPhone and more apps than Palm Pre it’s the mobile OS of the future and I am glad to be a part of it.

Comments welcomed.

## Nah…exams aren’t being dumbed down at all!

August 14th, 2009

I have to admit that when I first read this story I had to check that the date wasn’t April 1st but alas it is genuine.  The AQA (Assessment and Qualifications Alliance) in the UK saw fit to award a teenage boy a certificate for using one of his local buses.  The student in question didn’t even know he was being examined until he received his certificate in the post.

Yes, Bobby McHale from Bury in Greater Manchester is now the proud recipient of an AQA qualification called ‘Using Public Transport (Unit 1)‘ where he demonstrated the following skills

1. walk to the local bus stop;
2. stand or sit at the bus stop and wait for the arrival of a public bus;
3. enter the bus in a calm and safe manner;
4. be directed to a downstairs seat by a member of staff;
5. sit on the bus and observe through the windows;
6. wait until the bus has stopped, stand on request and exit the bus;

They don’t just dish this qualification out to anyone though….Bobby’s brother failed!

## Genstat 12.1 released

August 14th, 2009

Genstat, a general-purpose statistics package that seems to have a good following in the biosciences has been upgraded to version 12.1.  A selection from the ‘whats new?’ section:

• Release 12.1 includes 2 new directives, 42 new procedures and 41 new functions.
• Support for Cornell ecology file formats.
• Support for MapQTL loc and map files.
• Support for R/QTL rotated comma-delimited separate genotype (csvsr) files.
• Support for Stata 10 files.
• Quantile regression menu for fitting quantile regression models.
• Parallel regression analyses.
• Ability to display confidence intervals for parameter estimates.
• Display all standard error of differences in General Analysis of Variance menu.
• Trellis plot of means.
• Biplots added to principal components and canonical variates analysis menus.
• Permutation tests availible for MANOVA.

Check out the full list of new features over at VSNI’s website.

Since I am not a statistician and I don’t support statistics software, I hadn’t heard about Genstat until fairly recently but they seem to be a very cool company.  Take a look at the free Genstat Discovery Edition for example which is for use by not-for-profit research organizations, charities and educational institutes based in the developing world.

Commercial software companies who give something back to the community in ways like this always earn respect in my eyes.

## Walking Randomly through the web #2

August 12th, 2009

Here’s some stuff that I found cool / useful / interesting while on my travels around the interweb.  My previous random stroll can be found here.

Puzzles with a distinctive mathy-flavour

Games that have nothing to do with maths at all

Take a laser and a computer and the world’s your mollusc

Python links that I’ve found useful

On why failure is good for you and why we should all have the right to die

Do you want something featured in my next random walk of the web?  Post a comment or email me and tell me what you’ve found or witten and why I (and others) should pay it a visit.  Links that are about mathematics, science, software or gadgets are much more likely to get included.

## Mark 22 of the NAG toolbox for MATLAB released

August 11th, 2009

The latest version of NAG’s toolbox for MATAB has been released for Linux users (Windows versions will follow at a later date) and is available for download over at NAG’s website.

This release syncs the MATLAB toolbox up with Mark 22 of the NAG Fortran library and there is a lot of interesting new functionality including:

• Global Optimisation
• Wavelets
• ProMax rotations
• Improved Sobol quasi random number generator (good for up to 50000 dimensions)
• Option Pricing Formulae
• Lambert’s W function
• A fast quantile routine
• Matrix exponentials

I really like the NAG toolbox for MATLAB for the following reasons (among others):

• It has saved my employer, The University of Manchester, a lot of money since we don’t need as many licenses for toolboxes such as Statistics, Optimisation, Curve Fitting and Splines.
• It can speed up MATLAB calculations – see my article on MATLAB’s interp1 function for example
• It has some functionality that can’t be found in MATLAB.
• Their support team is superb.

Although written for the previous version of the NAG Toolbox for MATLAB, the following articles will be useful for anyone who wants to get started with the toolbox.

Previous articles written by me about the NAG toolbox for MATLAB include