Tuesday, July 1, 2008


Blitz++ is a C++ class library for scientific computing, and it's aim is to give performance similar to that of Fortran 77/90.

I haven't tried it yet, but will try to get around to it soon...

C++ quick tutorial: const

The const keyword allows you to specify whether a particular variable can be modified or not.

The simplest usage of const is this, for example:

const int i = 8;

This just means that the variable i is always 8 and cannot be changed. An alternative way of writing this is:

int const i = 8;

which means that same thing. The value of the variable i cannot be changed. Clearly you must initialize constant variables when they are declared, as you can't change their value later! Using const in this context is an alternative to using #define, for example:

#define i 8

What about this example?

const int *px;

This basically means that *px is a const int, i.e. px is a pointer to a constant integer. The important thing to remember is that while *px can be pointed to any integer, you cannot change the value of the integer.

Here's another example:

int x;
int * const px = &x;

The difference here is that the const is after the *. The value x can be changed, however the address pointed to by px can't be changed.

Here's yet another example:

int x = 0;
const int * const px = &x;

In this case, we cannot change the value of x using the pointer px, and we cannot point px to another address.

Another usage is in passing parameters to functions. For example, firstly consider:

void myfunction(mystructure in);

Assume mystructure uses a large amount of memory. When myfunction is used, a copy of the structure is made which is available only inside the function. This may take a long time, and/or may use too much memory. Instead, we could pass the structure in by reference:

void myfunction(mystructure &in);

which means that inside the function we don't just have a copy of the mystructure variable, we have variable itself. This of course means that it is possible to modify this variable. If we want to ensure that it cannot be modified, we would make use of const:

void myfunction(mystructure const &in);

C++ quick tutorial: static

The static keyword has 3 different uses.

1. The simplest use is inside a function. If a function contains the following line:

static int count = 0;

the variable count will be set to zero only once, even if the function is called millions of times. For example, consider the following:

for (int i=0; i<1000; i++)
   static int count = 0;

Here, the variable count is set to zero only once, not 1000 times.

2. If one file has at the top:

static float pi = 3.141592653;

then the global variable pi can be used inside just this one file. Other files are not able to access the variable pi. With just this:

float pi = 3.141592653;

then other files can access the variable pi provided they contain:

extern float pi;

3. static can also be used inside classes. For example:

class myclass
   int i;

If we then create two objects of this class:

myclass c1, c2;

then c1 and c2 will contain their own independent variable i. However, if instead we defined:

class myclass
   static int i;

and then created two objects:

myclass c1, c2;

there is only a single variable called i. Any instance of the class myclass has the same value of i. To set the value of i we can do this:

int myclass::i = 0;

The syntax is clearly of the form:

type class_name::static_variable = value

The thing here that confuses some people is that the type must be specified here as well.

C++ quick tutorial: typedef

Suppose you need to define lots of variables as unsigned short int, for example:

unsigned int a[3];

It can be useful in these situations to use typedef, which enables you to create aliases to any of the usual C++ datatypes. For example:

typdef unsigned int UnsignedInt;
UnsignedInt a[3];

In this example we have created an alias called UnsignedInt which we can then use to declare variables of type unsigned int.

Another example, this time using char:

typedef char[60] field;
field name;

Here we have created an alias called field which can be used to declare variables of type char[60].

Friday, June 27, 2008

C++ templates: an introduction

Have you ever needed to have multiple versions of the same function, with the only difference being the date type used for the arguments and/or value returned? For example, you might need versions of the same function that work with int, float and double data types. The most obvious way to do this is to use overloading, and explicitly define the required functions, all with the same name and content except for the data type.

An alternative is to use a template. This allows you to determine a function with a generic data type. For example:

template <class T>
T SumArray(T *array, int num)
   T my_sum = 0;
   for (int i=0; i<num; i++)
      my_sum += array[i];

This is a very simple function that sums the first num values in an array. To use this for an integer array:

int arr[10];
int value = SumArray<int>(arr, 10);

Or you use it for doubles:

double arr[15];
double value = SumArray<double>(arr, 15);

This of course can be trivially extended to multiple data types, for example:

template <class T, class U>
double SumTwoArrays(T *array1, U *array2, int num1,
                    int num2)
   double my_sum = 0;
   for (int i=0; i<num1; i++)
      my_sum += array1[i];
   for (int i=0; i<num2; i++)
      my_sum += array2[i];

Here a double is always returned by the function, but it has two input arguments, which can be of different types.

Saturday, June 21, 2008

Fast Artificial Neural Network Library

The Fast Artificial Neural Network Library (FANN) is a good way to get started with neural networks.

Fast Artificial Neural Network Library is a free open source neural network library, which implements multilayer artificial neural networks in C.

The web page is here. The documentation contains some simple example which illustrate how to train and test a neural network. Cascade training is a useful feature, where neurons are added during training, which means that users don't need to determine how many neurons and hidden layers should be in the network before training.

Importing data into MySQL

Rather than using a Perl script, for example, data can be imported directly into MySQL using LOAD DATA LOCAL INFILE. Note that if you don't include LOCAL, the file you want to import must be located on the server, rather than a client.

For example, suppose you have an ASCII file containing comma-delimited data that you want to insert into the table my_table. Type:

INTO TABLE my_table
(field1, field2, field3, field4, field5);

Here we have specified that the data is comma-delimited, and each line of the input file corresponds to a row in the table. The columns that each piece of data should be inserted into are also specified.

MySQL can read only the lines in a data file containing a specified prefix if necessary. For example, if each line with real data begins with DATA_ENTRY, and you want to ignore all others, add:


MySQL will skip the prefix, and anything that comes before it. Lines not including the prefix will be completely skipped.