Montag, 18. Juli 2011

Sharing data among Mathematica notebooks

With time my Mathematica-notebooks increase in size and complexity. That's when I decide to split my notebooks into several files. Also, sometimes I want my results out of the notebooks which possibly change upon each evaluation.

One way is to create a package stored to a notebook (.nb-file) which is convenient for function definitions. Another way is to write Mathematica expressions to an ASCII-file with the suffix ".m" with the command "Save".

Here, I have some expressions:

f[x_] := x Log[x]
a = Table[f'[i], {i, 0.1, 1, 0.1}]
p = ListPlot[a]
ascii = FromCharacterCode[Range[32, 126]]

With this command I can retrieve the names of the expressions in the "Global" scope, or context:

Names["Global`*"]

And finally, this is how I store all expressions which have names starting with "a" to a .m-file:

mfile = NotebookDirectory[]~StringJoin~"results.m"
Save[mfile, #] & /@ Names["Global`a*"]

In another notebook I can load these expressions like this

<<results.m;

The .m-file itself looks this way:

a = {62, 58, 46, 115, 47, 77, 110, 111, 72, 94}
ascii = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"

Freitag, 8. Juli 2011

How to create publication style figures with Mathematica

Recently, I've been using Mathematica a lot - both for data processing and analysis as well as for plots. Plotting is easily done and quite selfexplaining, or at least well documented. Publication style figures of plots have little more demands: labled axes, legends, etc. I tinkered around quite a lot with such figures and I'd like to share my output:

As said a good looking plot can be created quite fast:

<< PlotLegends`

l1 = RandomReal[{2900, 3100}, 10]

fig1 = ListPlot[{l1, l2},
  Joined -> True, PlotStyle -> {Red, Green},
  PlotMarkers -> Automatic,
  PlotLegend -> {"list1", "list2"}, LegendShadow -> None,
  LegendPosition -> {1.1, -0.25}
  ]




A figure with two plots and the legend having its own part requires some more code:

pm = {Graphics[{Red, Disk[ImageScaled[{0.5, 0.5}], .025]}],
  Graphics[{Green, Disk[ImageScaled[{0.5, 0.5}], .025]}]}
pm2 = {
  Graphics[{Red, Disk[ImageScaled[{0.5, 0.5}], .25]}],
  Graphics[{Green, Disk[ImageScaled[{0.5, 0.5}], .25]}]
  }

fig = Show[
  GraphicsRow[{
    ListPlot[{l1, l2}, Joined -> True, PlotMarkers -> pm,
     PlotStyle -> {Red, Green}],
    Graphics[
     Legend[Transpose[{pm2, {"list1", "list2"}}],
      LegendShadow -> None]],
    ListPlot[l3, Joined -> True, PlotStyle -> Black,
     PlotMarkers -> Automatic]},
   ImageSize -> Full
   ],
  Graphics[Text[Style["(a)", 14], {0, 20}]],
  Graphics[Text[Style["(b)", 14], {400, 20}]],
  Graphics[Text[Style["(c)", 14], {800, 20}]](*,Frame->True*)
  ]



Unfortunatly, the figures shown are in "PNG" format and they do not look as nice as in "EPS" format. (For some reason I can't upload "EPS" formatted pictures.)

Montag, 4. Juli 2011

Computermodel of the Protein "Ubiquitin" in a Hydrated Ionic Liquid

everything that living things do can be understood in terms of the jiggling and wiggling of atoms - Richard Feynman
In this spirit I am studying biomolecules using computer simulations.

These two pictures show the model: The protein "ubiquitin" in a hydrated ionic liquid as solvent.

(a) The protein is represented by van der Waals balls, the hydrated ionic liquid by a transparent green mass and the simulation boundaries, a truncated octahedron, by black lines.

(b) Same as in (a) except that the solvent is reduced to solvent molecules within a 10 angstrom radius around the 76th amino acid of ubiquitin.

Donnerstag, 5. Mai 2011

Precision and Rounding

I deal with a lot of computer generated data. Molecular dynamics simulations provide me with terabytes of data in form of numbers which are stored as floating point numbers. These floating point numbers represent real numbers with a certain machine dependent maximum error, the machine epsilon. Here is an example in python:


>>> a=0.1
>>> a
0.10000000000000001

So, the computer stores the real number "0.1" as the floating point number "0.10000000000000001". This is indeed an acceptable error. Let's have a look at another example:

>>> a=[6.04, 6.08, 5.99, 6.01, 6.04, 6.02]
>>> sum(a)/len(a)
6.0299999999999985

The average of the array "a" is computed as "6.0299999999999985". Here, one doesn't know if it is precisely that value or if it is the average of floating point numbers representing the values given in "a". In this case we have:

>>> a
[6.04, 6.0800000000000001, 5.9900000000000002, 6.0099999999999998, 6.04, 6.0199999999999996]

The machine epsilon is dependent on the computer and is stored to a variable. In C this variable is stored in the standard library header file "float.h", in python one can retrieve it by:

>>> import sys
>>> sys.float_info.epsilon
2.2204460492503131e-16

Most programs I use do not respect this machine epsilon, this error. For instance, they just bluntly return:

average +- standard deviation
6.0299999999999985 +- 0.028284271247461926

This gives information which is simply not true, that is the average and the standard deviation. It's simply the nearest possible floating point number representation in the computer for the actual real number results. If I calculate the average by hand I get the real number "6.03".

floating point number, real number
6.0299999999999985, 6.03

Besides that wouldn't fit into a table reporting this result. So let's round that value to the least significant digit. Fortunately, I have a standard deviation which indicates the least significant digit. (This is just the way I learned it at school.)

Round up (yes, round up to make sure) the standard deviation or the known error to two significant digits:

0.028284271247461926 ~ 0.029

Round the average to the lowest decimal digit of the rounded standard deviation:

6.0299999999999985 ~ 6.030

Voila:

6.030 +- 0.028


To automate that I've found some time ago an algorithm to round a number "num" to an arbitrary number of significant digits "nsig":


from math import *
def roundto(num, nsig):
  if num == 0:
    return 0
  lg = int( log( abs(num) ) / log(10.0) )
  r = 10 ** ( lg - (nsig-1) )
  return int( num/r + 0.5 ) * r

>>> roundto(32.25235,4)
32.25
>>> roundto(32.25235,5)
32.252000000000002

Unfortunately, as one can see, the rounded number will be represented again by a floating point number! Therefore an adequate print statement would be required as well.

In python, I recently found a function doing almost the same, "round". The difference is that you specify the decimal to round to as opposed to the number of significant digits in "roundto".

>>> round(32.25235,2)
32.25
>>> round(32.25235,-1)
30.0

I reckon people committed to implementing arithmetic and other mathematical operations on computers have given a lot of attention to prevent these machine epsilon errors from build up. At least the simulation algorithms are designed keeping this in mind.

Freitag, 25. März 2011

First Acquaintance

This blog is intended to collect my thoughts and ideas and to make them publicly available. I guess most of the posts will relate to my work, a doctoral thesis in molecular dynamics, and to other scientific topics.

I'm coming from the biological sciences and I've been working with molecular dynamics for pretty much four years by now. (Molecular dynamics just stands for a method to simulate atoms on a computer.) This part of my life in front of a keyboard and a screen have left their mark: I've learnt to program and I have come across topics in computer science, mathematics and physics. The curious and enthusiastic guy I am, I dived into all of them - well, at least I tried. So here I'll jot down the results of these ramblings.

What's more, I've read somewhere that writing down one's thoughts can help clarify and structure them. Actually, that somewhere was a blog post from Jeff Atwood, a co-founder of the brilliant website stackoverflow.com. Therefore, I'll use this blog also to recapitulate my ramblings in software development, science and as an intellectual exercise.

Also, I like the possibility to add comments and discuss the posts. Please, feel free to comment!

Finally, I don't want to withhold my first source of inspiration: The "guy on a pogo stick", a reference to a character appearing in "Mickey Mouse" comics that bridged many would-be dull moments in my life. That character is the inventor Gyro Gearloose, or in German, Daniel Düsentrieb, invented by Carl Barks and translated into German by Erika Fuchs who I both admire. He always comes up with ingenious inventions which unfortunately prove useless, like a solvent that dissolves everything but cannot be stored in a container since it would dissolve it immediately. Daniel Düsentrieb's workshop and inventions always inspired me.

Yours truly