Sunday, February 20, 2011

pari GP terminal colours - readable darkbg

After 3 years or more of using Pari/GP, the default highlighting (in terminal), bothered me enough, to read up how to change it.

default(colors,darkbg)

Here is a comparison of the effects of the highlighting change:


The lines prefixed %7 and %8 and in between, show the output is now in grey, and input highlighting is now greenish.

The original "1, 6, 3, 4, 5, 2, 3" profile known as lightbg is reactivated briefly, so it can be compared again.

Making the change permanent on Debian by:

emacs /etc/gprc

and changing which lines are commented out, results in the top of my /etc/gprc file looking like this:



So now when I start up Pari/GP my highlighting is set okay from the outset:




Notes and further reading:
On Ubuntu you will want to use the keyword sudo at the front of the command for editing /etc/gprc

If you want a lighter alternative to emacs on your system, then zile is a lightweight alternative that supports basic editing. Alternatively your system default editor is probably already chosen and use that.

The pari/GP command ...

default(colors,d)

... can be used interactively if you do not wish to make the change permanent.
darkbg abbreviated to just 'd' should work.

Friday, January 7, 2011

r-project / r-cran and install.packages()

My version of the excellent statistics and mathematics package from r-project.org, is now 2.11, having updated to Debian Squeeze.

( The major work from Google Summer of code 2010 is probably in 2.12 released December 2010 )

I tend to install just the minimal r-project packages and then immediately add rgl for pretty plot3d.

But before rushing into install.packages("rgl"), perhaps I should check where exactly the install code will end up.



looks to me as if /usr/local/lib/R/site-library might be the destination (set in R_LIBS_SITE), so let us see...

install.packages("rgl") pops up a country ordered list, for you to select a mirror for the install


Interesting to see the number of mirrors, 5 for Germany, and 5 for Canada, and I should scroll down to below Taiwan to pick my country.


( Above confirms my guesswork that any install.packages on my system defaults to /usr/local/lib/R/site-library )

Now what happens next, really changes the course of this article...so read on.

The install.packages() command failed as it could not locate GL/gl.h or GL/glu.h, and here I consulted my laptop.
The laptop already has rgl installed and working, so I used find and dpkg, and discovered my silly mistake.

There is already a debian package in squeeze named 'r-cran-rgl' which will do all the hard work for me :)

apt-get install r-cran-rgl 

will replace the manual install.packages() method and get you into plot3d() in no time at all :)

( for those who are interested in where GL/glu.h lives, try libglu1-mesa-dev in support of manual install of rgl )


The plot3d() function in rgl allows you 'drag to spin' functionality so you can rotate, and examine the points from any angle you choose.

In the above I have dragged the back round to the front so zero is away from you, and hopefully, you can just make out the points curving up towards point (5,5,60) which is the end of the curve.

Some further illustrations of what rgl can do can be found on statmethods.net in section 'Spinning 3D Scatterplots'.


Further reading and links:
For technical folks you really want more packages and are interested in install.packages(), here is the function definition with arguments:

Sunday, November 28, 2010

Gnumeric isprime() trial division

Gnumeric is one of several spreadsheet choices available to a Debian and / or GNU / Linux user.

How useful is Gnumeric to a mathematician? Quite.

It does include several functions that a more general package LibreOffice / OpenOffice Calc / Excel might not include.

Statistics and Number Theory are areas where Gnumeric currently outpaces the competition, in terms of available functions.

Here is one example, the isprime() function:
/*
 * Returns -1 (out of bounds), 0 (non-prime), or 1 (prime).
 */
static int
isprime (guint64 n)
{
      int i = 1;
      guint64 p = 2;

      if (n <= 1)
            return 0;

      for (i = 1; p * p <= n; i++) {
            if (ithprime (i, &p))
                  return -1;
            if (n % p == 0)
                  return 0;
      }

      return 1;
}

Do not be alarmed if you are not comfortable looking through the above code, it is simply there so that I may elaborate next.

The method here is trial division, which is a reasonable way of going about things for integers say of a billion or so. It is implemented in C and so is likely pretty fast at getting it's answer.

In fact a little bit of trial and error can show (very roughly) where the threshold for this isprime() function lies:

Firstly a candidate number (fairly large by spreadsheet standards):

factorial(18)+1=6402373705728001

which isprime() answers with #LIMIT!

Now those handy zeros at the end of the number will serve to reduce the number in size a little. Removing a zero from ...001 give ...01 and a new number:  640237370572801

...which Gnumeric isprime() is happy to process and return FALSE.

If you are wondering, then the new number ending ...2801 has a factor of 2801, how interesting, and completely accidental.

Anyway, I have provided a 15 digit number that isprime() will process, and demonstrated that it does have a limit to what it will attempt.

So no, you are not going to find huge primes by using entries in a Gnumeric spreadsheet, however it does provide very convenient environment, for exploring integer construction.

Should you use Gnumeric, to test out your hypothesis for prime number patterns?
Well why not, there is value in knowing that constructive forms hold true up to 15 digits certainly.

Later you will maybe explore a dedicated number theory program, there are several great open source offerings. However Gnumeric seems like a great place to just dive in.

Links and further reading: 

Tuesday, October 26, 2010

Problem solving - when really stuck try...

  • A cup of tea/coffee
  • Social chat with a colleague
  • Walk in the *fresh air
  • Visit a huge building with great architecture/decor
Most of those points are really obvious, but all too easy to forget, when engrossed in a tough Mathematical problem.

*Without realising it you might be really need the fresh air!
( If you work in a Science building and have your own small office, then sitting in there all day with the door closed, will reduce the air quality. )

Visiting the Science Museum in London, a local historic building, or a local Cathedral usually works for me also.
( The Cathedral is not my way of seeking 'something divine', but they are usually architecturally interesting and generally not full of hustle and bustle )

For some, a Distraction can also work, and there is a Psychological Science Article summary here giving some details.

The full 2008 research paper by Chen-Bo Zhong, Dijksterhuis, and Galinsky can be found at utoronto.ca here.

Another option, which sometimes works, is to pick a marginally related problem, and plan to come back to the original in a few days time.

Sunday, October 10, 2010

gmp library - including methods in your c code

.
The gnu Gmp library has many methods for dealing with huge integers.
( It does a lot more than that but I keep a narrow focus for this article )


Here are some links to the documentation for versions 5 and older version 4

When writing your own programs in C, that make use of gmp, you will use an include such as:

#include <gmp.h>

...and then perhaps if working with mpz Integers you might use some of the Integer functions of gmp.

Diving in and compiling with gcc, you might see 'undefined reference to ...' messages, until you have the gmp headers installed, and tell gcc about that.


Compiling against the gmp library is different than simply using the gmp library, and you may need an additional package installed.

On Debian and Ubuntu the source files and/or headers are kept in separate packages from the executables, so as not to bloat your system.

Install the source for gmp using the following command:

apt-get install libgmp-dev

(*Older Debian 6 Squeeze package is named libgmp3-dev rather than libgmp-dev)

...which brings down the necessary files so that gcc can use them in it's compile.


I used aptitude rather than apt-get in the above but the result is the same.

Recap:
  • Use in your C program #include <gmp.h>
  • Have the dev package installed on your system
  • Use the -lgmp flag when you invoke gcc
  • gcc arguments -lm and -lgmp should be the LAST argument
      [ as shown in the first screenshot ]

Note: I use GNU/Linux and give my instructions for Debian and similar systems. If you use a different platform, then I am unable to help with that and you should look elsewhere for instructions tailored to your chosen platform.


(optional) Integer functions of gmp:

What follows are some hints/personal notes, which serve as a useful reminder to me personally.

( Please consult the official manual, using the links in the first paragraph, as a definitive source of explanation. )


/* mpz_sub_ui versus mpz_sub where ui indicates unsigned integer rather than mpz processing */

/* mpz_cdiv_q versus mpz_tdiv_q will ceiling rather than truncate when calculating quotient */

/* mpz_cmp_ui Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, or a negative value if op1 < op2. */
.

Thursday, September 30, 2010

5 seconds to get million digits of pi - easy benchmark

Here is a great way to benchmark a new system, or pick the leader from a pool of desktop systems.

Okay so you are planning to do some mathematical exploration and you have at your disposal a laptop, and a couple of desktops.

Which one to use?

You can be very scientific about this (more later), or you can just press a few keys on each and pick the winner.

  • apt-get install python-gmpy
  • cd /usr/share/doc/python-gmpy/examples/test
  • python test_large.py
5 seconds for the desktop Amd 7850 which is in a year old Asus P2-M3A3200
25 seconds for the laptop, an Intel T5550 based Dell and is 2 years old
My other desktop came inbetween those two, just slightly beating the laptop.



The winning system is running Debian Squeeze 64 bit and Python 2.6.6:



I mentioned earlier about this being a quick but unscientific way of choosing a suitable machine for you mathematical task.

You will perhaps have a particular task in mind. If it is linear algebra based then you might want to run some sample tests using octave, or scilab, or sage.

Here is a useful link to benchmarks that include some linear algebra/matrix operations:

Whilst looking around for benchmark results, I came across this 2007 post, that includes a 300x300 matrix timing test, and here are my results:



(optional) more technical specs for the Asus:
  • Chipset is AMD 780G
  • Socket is AM2+
  • Onboard Graphics are Radeon HD 3200
  • Memory installed is PC2-6400 DDR2-800 

( The above is by no means cutting edge, but for a £200 desktop system, as the benchmark shows, results are not too shabby )

(optional) Matrix commands in text format if you would like to repeat that 300x300 test in your own program:

M = rand(300,300);
t=0.25; T=2;
tic; inv(M) * (expm(-t*M) - expm(-T*M)); toc;

( I hope I made it clear from the link earlier that those commands appeared on a forum at wilmott.com
In particular blondie and spacemonkey posters used those commands for timing tests )

( The install commands such as apt-get which I have used to install gmpy are specific to my platform GNU/Linux. I am unable to provide help with other platforms, and suggest you might want to find a more suitable howto, that gives useful benchmarks including an install guide for your platform. )

Wednesday, September 15, 2010

sage 'open source mathematics' - some compilation experience

sage (or sagemath) is a unifying front-end to many computerised algebra and mathematical programs.
Previously I have used sage on Ubuntu Karmic and loved it.

Just now there is no official Debian or Ubuntu package for sage, and so installing it on my new system (Debian Squeeze) gave me a choice:
  1. Install the prebuilt binary package that sage themselves provide
  2. Download the source and issue 'make' on my own machine.
There is little advantage to building from source (option 2), unless you will be running mathematical procedures that run over several nights.

In particular if you are new to sage then the prebuilt binary packages are the 'no hassle' way of getting up and running.

If you just want to see what sage is about, then the live cd, is an excellent way to try things out (no change to your system at all)


Compiling and having sage 'self check':

Summary: make test

Note: I work on GNU/Linux and so my instructions are particular to that platform. Seek out the install guides at sagemath.org if you want help with another platform.

Download the latest .tar file from a mirror listed here.
( My .tar was around 300MB and I checked its md5sum matched against the site )

Untar into /usr/local/ or /opt/local/ or your home directory, as you prefer
( I will use /opt/local for this article )

Have a quick look at the readme file at /opt/local/sage-4.5.3/README.txt

Follow the install guide at sagemath.org in conjunction with this article.

I mentioned about getting sage to 'self check', and what I meant by that, was running the inbuilt mathematical tests that come with sage.

Glancing at README.txt you might be thinking of setting SAGE_CHECK="yes", however this is a different 'self check'*

( *Sage is made up of a number of .spkg files and SAGE_CHECK="yes" will initiate integrity checks on those .spkg files and checks for compatibility with your system )

Running make test initiates a two stage process:
  1. The build
  2. mathematical test suite
The test suite (step 2) took a couple of hours on a 2.8 ghz machine running Debian Squeeze 64 bit.


When you are all done then you might want to issue the following command:
chown -R someuser:root /opt/local/sage-4.5.3/

( replace someuser with the the username you normally log in with )

*** End of main article. ***
( The next section is for those interested in lower level details of sagemath source and .spkg testing in particular )

Bundling python within an application and compatibility:

Sage does not use Python that already existed on your system, and instead, bundles Python 2.6.4 to support all sage applications.

This is a perfectly valid approach* but does freeze in time, that bundled Python, against a moving target (your system and upgrades)

( *as a Debian user I would really like a version of sage that integrates more fully with optimised libraries debian itself took the trouble to install. This is not currently the case and I understand some of the why and why not involved. )

In particular Python 2.6.4 bundled in sage 4.5.3 is probably missing a patch to the file test_zlib.py

Unpatched (as supplied by sagemath.org) my output using SAGE_CHECK="yes" was:
test test_zlib failed -- Traceback (most recent call last):
  File "/opt/local/sage-4.5.3/spkg/build/python-2.6.4.p9/src/Lib/test/test_zlib.py", line 84, in test_baddecompressobj
    self.assertRaises(ValueError, zlib.decompressobj, 0)
AssertionError: ValueError not raised

322 tests OK.
3 tests failed:
    test_distutils test_httpservers test_zlib

...but then applying the patch with a manual edit to the file test_zlib.py


...sees test_zlib outcome being different as shown:

test_zlib
323 tests OK.
2 tests failed:
    test_distutils test_httpservers

When the next version of sage (4.6) is officially released, hopefully it will include Python 2.6.5 or newer, so as to have that patch already applied.

Links and reference: