Package: sersic
# -*- coding: utf-8 -*-========Sérsic========This is an implementation of the exact deprojection of Sérsic surfacebrightness profiles as described in:"Analytical expressions for the deprojected Sérsic model"Baes and GentileAstronomy and Astrophysics, Volume 525:A136 (2011)http://arxiv.org/abs/1009.4713This code depends on the mpmath python library(http://mpmath.googlecode.com) for an implementation of the Meijer Gfunction required by the Baes and Gentile (hereafter B+G) formulas forrational values of the Sérsic index.  B+G also give formulas forirrational Sérsic indices, but I could not find an implementation ofthe required Fox H function.  Therefore this code requires rationalSérsic indices, but any irrational number can be approximatedarbitrarily well by some rational number, so... no problem.The code also depends on scipy (http://scipy.org), but the dependenceis mostly for testing: doing numerical integrals and such.  If youtrust that the code passes the tests and don't want to run themyourself (ha!), the only dependence on scipy is finding the root(scipy.optimize.newton) in the function bg_constants(), which would bepretty easy to replace.This was implemented for use use in the paper:"On Galaxies and Homology"Novak et. al.Monthly Notices of the Royal Astronomical Society, 424:635 (2012)http://arxiv.org/abs/1205.2533The B+G formulas are not simple but not terribly complex.  However, Iwanted to be quite sure that I had correctly implemented the formulas(and that the formulas themselves were correct), so I put significanteffort into comprehensive tests.  That testing is the primary virtueof this particular implementation of the B+G formulas, and is myreason for releasing this code.To test the implementation of the B+G formulas, I also implemented theapproximate formulas for deprojected Sérsic profiles described in:"Dark matter in elliptical galaxies - I. Is the total mass densityprofile of the NFW form or even steeper?"Mamon and LokasMonthly Notices of the Royal Astronomical Society, Volume 362:95 (2005)http://arxiv.org/abs/astro-ph/0405466Additional tests use the parameterization of the Sérsic profile givenin:Galactic AstronomyBinney and MerrifieldPrinceton University Press, 1998For more information:http://pypi.python.org/pypi/sersic/For the source code:http://launchpad.net/sersicInstallation============First install the mpmath python library (http://mpmath.googlecode.com).To run the tests and avoid implementing your own root finding routine,install numpy (http://www.numpy.org) and scipy (http://www.scipy.org).But you've got those installed already, right?The sersic module can be installed by any one of the followingstandard incantations:pip sersicoreasy_install sersicorpython setup.py installIf you want to install in your home directory, you can add the --userflag to any of the above.To run the tests:python test.pyUsage=====Very likely the only function any user will care about is:def luminosity(pp, qq, reff=1.0, lum=1.0) where pp and qq are thenumerator and denominator of the Sersic index (both integers) so thatnn=pp/qq, reff is the projected half-light radius, and luminosity isthe total luminosity.  This returns a function that takes a radius andreturns a luminosity density.>>> lum = luminosity(5,3)>>> lum(1.1)>>> lum([1.1, 2.2, 3.3])All of the other functions in the module are just to help make surethat the Baes + Gentile expressions are implemented correctly.If you find that the calculations are taking a long time, you can tryreducing the precision of the calculation of the Meijer G function.The mpmath library is designed to be able to work to arbitraryprecision, and is by default set to ~double precision.  Forinformation on changing the precision, see:http://mpmath.googlecode.com/svn/trunk/doc/build/basics.html#setting-the-precisionKeep in mind that small values of the numerator and denominator arefaster than large ones (e.g. luminosity(2,1) is fast,luminosity(200,99) is slow.The function returns arbitrary precision floats defined by the mpmathlibrary.  They should behave as normal floats, but you may want tocast them to Python floats to avoid invoking mpmath's more carefularithmetic.License=======The code is released under the MIT license, so you should be able todo whatever you want with it.If you use this software to produce a scientific publication or if youincorporate this code into a larger project, I would appreciate it ifyou send me a note at greg.novak@gmail.com