Common C, C++, FORTRAN Libraries

From PHYSpedia
Jump to: navigation, search

There are a number of common libraries for computational physics and numerical analysis. Here are a few links for tutorials and information for installation of commonly-used tools.


Eigen (Linear Algebra)

Eigen is linear algebra library that supports computing the eigenvalue and eigenvectors of real and complex matrices. It is relatively simple to use because it is a pure template library, and therefore does not require you to compile anything in the library to use it, and it is written in standard C++, so it will work on all platforms (Linux, Mac OS, and Windows). All you need to do is download the library and put the source code in your project directory.

Using

Let's assume you have a C++ program called StateFinder that you want to use the Eigen library for to calculate eigenvalues of a matrix. You create a directory called StateFinder for the program, and you have a single source file named StateFinder.cpp (If you are using an IDE such as Visual Studio, Xcode, or Eclipse, it will have created the project directory for you). The following instructions will describe how to use the Eigen library for this project (It does. They will not install the library to be system wide. All that is required in this scenario is to download the Eigen library archive, unpack the archive, and copy the Eigen source directory to your project directory.

Downloading

Download the latest version of the library from http://eigen.tuxfamily.org. On the right side of the page, you should see "The latest stable release...". Click on the tar.bz2 (Linux and Mac), tar.gz (Linux and Mac), or zip (Windows) links. This will download a file whose name starts with eigen-eigen-. For example, eigen-eigen-6b38706d90a9.zip.

Unpacking

On Linux, use tar to unpack the tar.bz2 file,

> tar -xzf eigen-eigen-*.tar.bz2

The tar program will also unpack the tar.gz file,

> tar -xjf eigen-eigen-*.tar.gz

On windows, use 7-Zip, or some other zip program to "extract" the zip file contents.

Installing

To "install" Eigen, you only need to copy the Eigen source directory into you project directory. The directory you want is named Eigen. It should have several files in it, including Array and Dense. Copy this directory into your project directory. It should be in the same directory as you source file StateFinder.cpp.

Testing that it works

Now you can test that you have installed Eigen correctly by compiling and running the following program.

for non-Visual Studio compilers, use this

#include <iostream>

#include "Eigen/Dense"
using Eigen::MatrixXd;

 int main(int argc, char* argv[])
{
	MatrixXd m(2,2);
	m(0,0) = 3;
	m(1,0) = 2.5;
	m(0,1) = -1;
	m(1,1) = m(1,0) + m(0,1);
	std::cout << m << std::endl;
	return 0;
}

for Visual Studio compilers, use

#include <iostream>

#include "Eigen/Dense"
using Eigen::MatrixXd;

 int _tmain(int argc, _TCHAR* argv[])
{
	MatrixXd m(2,2);
	m(0,0) = 3;
	m(1,0) = 2.5;
	m(0,1) = -1;
	m(1,1) = m(1,0) + m(0,1);
	std::cout << m << std::endl;
	return 0;
}

This should output

3 -1
2.5 1.5

to the console.

This test program illustrates how to create a 2x2 matrix and set its components (elements). You can create an arbitrarily sized matrix in an analogous way.

This example program is given in the Eigen "Getting Started Guide". For more information on this example, and a second example, visit the Eigen site: http://eigen.tuxfamily.org/dox/GettingStarted.html

GSL (Special Functions)

GNU Scientific Library(GSL)

The GSL also has calls for C and C++ for a variety of linear algebra functions found in BLAS and other packages.

Boost Math Toolket (Special Functions)

The Boost Math Toolkit has several utilities for doing various types of mathematical operations including several special functions.

BLAS (Linear Algebra)

BLAS stands for "Basic Linear Algebra Subprograms". Originally written in FORTRAN in the late 70's, it is still used by many libraries to do "low level" linear algebra operations. It is still the standard by which the performance of all other linear algebra libraries are measured.

Linear Algebra in Linux (BLAS/LAPACK)

Setting up BLAS in Linux (yum-based systems)

Matrix Multiplication with BLAS and CBLAS

GSL BLAS Examples (such as matrix multiply)


Utilities

The Boost Property Tree Library is an excellent C++ library for managing configuration data. It provides a very simple method to store data in a tree structure and supports loading tree data from files in various format (XML, JSON, INI). It is similar to the dpath library for Python that provides path-like access to nested dict elements.