Kevin Huffenberger  

Fast and exact spin-s spherical harmonic transforms

(aka. "spinsfast")

This software implements the fast algorithm for spherical harmonics described in Huffenberger and Wandelt, ApJS 189 (2010) 255-260, released under GPL version 3.

Download source

Revision 104, 13 Apr 2012.

New for revision 104: added python module. Generally made easier to compile by making fitsio, gsl, and healpix optional for the library.

Older versions


  1. Edit the configuration file (or make a new file) called build/ so that the proper locations for header and library files are included.
  2. Set the environment variable "build" so that it points to that file. ie in bash, say

    export build=build/

  3. Build the library and example codes with


    This will create the file lib/libspinsfast.a, and also several example programs.

  4. (Optional) Run the example programs to test the library.

Example programs

These are included to show how to implement various functions and features in the library. All the codes generate random spherical harmonic coefficients, transform to real space, then transform back to harmonic space and compare to the original coefficients.

When working properly, these codes should return a maximum fractional error in the set of harmonic coefficients at the level of a few 10^-13 for band limit lmax = 127. This gradually increases with lmax, so that it is a few 10^-12 at lmax = 2048.

Python example_spin

Run this script with:

python example/

It will generate some random harmonic coefficients, transform them into a map, then transform back and report the maximum error. It also showed an image of the real part of the map.


This code has four arguments:

example_spin spin lmax Nphi Ntheta-1

Running a spin s = -3 set of transforms at lmax = 127 and the minimum number of pixels to support that band limit, which is Nphi = Ntheta = 256, is executed as

./bin/example_spin -3 127 256 255


This code has three arguments:

example_multispin lmax Nphi Ntheta-1

and will compute for the (hard-coded but easily changed) spin values -3, 0, 1, 2, and 17. Example run:

./bin/example_multispin 127 256 255


This code shows how to make spin 0,+/-2 transforms of intensity and polarization map, which is a common task for cosmic microwave analysis.

This code has three arguments:

example_iqu lmax Nphi Ntheta-1

Example run:

./bin/example_iqu 127 256 255

Problems or bugs

Please let Kevin know at huffenbe *at*

Subsequent work by McEwen and Wiaux have identified a factor of two more efficient sphere-to-torus extension. This lowers the memory requirements, and speeds up the (time-wise sub-dominant) FFT portion of the code. Since I expect the speed improvements to be modest, this has not been a high priority, but it is an improvement nonetheless, so we'll implement it at some point in a future revision.

Occasionally, running with very low lmax (<30), the example codes will segfault. This disappears when compiled with less agressive optimization. (Martin Reinecke identified a minor bug, now fixed, which may have been responsible.)