The cosmo.el package is a cosmological calculator for the Emacs text editor (also available from the MELPA repository). It provides interactive commands for handy computation of cosmological distance measures defined in Hogg (1999) for ΛCDM models. For instance, the command M-x cosmo-calculator displays a summary table.

All cosmological quantities are computed at a given value of the gravitational redshift of photons frequency due to the expansion of the Universe. The ΛCDM model is characterized by the following parameters (set through the command M-x cosmo-set-params):

(The curvature density parameter today is automatically derived from the others above according to Friedmann's equation.)

The package is mainly meant to provide a handy reference to common cosmological quantities directly from the text editor. It is not supposed to be used for heavy computations (Elisp itself is certainly not the optimal choice for numerical computations).

Using cosmo.el as a library

Basic usage

Besides the interactive command, some function can also be imported in other Lisp programs (names with a double hyphen -- are for functions and variables that are meant to be for internal use only). First load the package:

(require 'cosmo)

Some function deal with generic numerical utilities. For instance, numerical quadrature (below we integrate x2 from 0 to 1 with Simpson's rule) or fundamental functions that extend Elisp mathematical library (below we call the hyperbolic sine and its inverse).

;; integrate(x^2, x, 0, 1) = 1/3
(cosmo-qsimp '(lambda (x) (* x x)) 0 1)
;; => 0.3333331523586992

(cosmo-sinh 0.5)
;; => 0.5210953054937474

(cosmo-asinh 0.5)
;; => 0.48121182505960347

Each distance measure can be called through a function with a name similar to the respective interactive command, extended by a -get- infix. For instance, the function cosmo-get-luminosity-distance corresponds to the command M-x cosmo-luminosity-distance. It takes as argument the redshift z. For instance, the luminosity distance in units of Mpc at redshift z=0.1 is:

(cosmo-get-luminosity-distance 0.1)     ; Mpc
;; => 459.48679270216024

This provides a handy interface for exploratory computations from any Emacs buffer. For instance, let's compute the first derivative of the luminosity distance DL'(z) at z=0.1. (All symbols and definitions follow Hogg (1999); cosmo-inv-efunc is 1/E(z).)

(defun DM-prime (z)
  "Comoving distance (transverse) first derivative in Mpc."
  (* (cosmo-inv-efunc z) (cosmo-get-hubble-distance)))

(defun DL-prime (z)
  "Luminosity distance first derivative in Mpc."
  (let ((DM (cosmo-get-transverse-comoving-distance z))
        (DMp (DM-prime z)))
    ;; DL(z) = (1+z) * DM(z), hence DL'(z) = (1+z) * DM'(z) + DM'(z).
    (+ (* (1+ z) DMp) DM)))

(DL-prime 0.1)                          ; Mpc
;; => 4914.0412065178225

Let's check that DL'(z) / DH = 1, where DH is the Hubble distance. Note that rather than setting global variables, we use a let statement.

(let* ((z 0.0)
       (DLp (DL-prime z))
       (DH (cosmo-get-hubble-distance)))
  (/ DLp DH))
;; => 1.0

Customize Options

Cosmological parameters are stored in a hash table called cosmo--params. As the double hyphen in the name indicates, the variable is meant for internal use only. However, nothing prevents to do it if someone is aware of the consequences. For instance, the following function can be used to set cosmological parameters without calling the interactive command M-x cosmo-set-params (and it may become part of the package interface).

(defun cosmo-set-my-default (H0 omatter olambda orel)
  "Set default test cosmological parameters.
Argument H0 Hubble parameter [Km/s/Mpc].
Argument OMATTER matter density parameter today.
Argument OLAMBDA cosmological constant density parameter.
Argument OREL relativistic density parameter today."
  (puthash "H0 [Km/s/Mpc]" H0 cosmo--params)
  (puthash "omatter" omatter cosmo--params)
  (puthash "olambda" olambda cosmo--params)
  (puthash "orel" orel cosmo--params)
  nil)

(cosmo-set-my-default 67.0 0.27 0.7 4e-5)

(cosmo-get-ocurvature)
;; => 0.029900000000000027

(cosmo-get-luminosity-distance 0.1)     ; Mpc
;; => 480.4648601303271

Once the new cosmological parameters are set, the curvature parameter changes consistently, as well as other values such as the luminosity distance.

Let's now increase the precision of numerical integrals. This can done interactively through Emacs M-x customize command, and adapting the options under the Cosmo group. Alternatively, the respective variables that control the fractional precision and the maximum number of steps can be set as follow. Note that we change them within a let statement, so that the change will only take place locally and the global values are unchanged. (The requested fractional precision below is quite high, so it may take a while to run with Elisp.)

(let ((cosmo-int-prec 1e-8)             ; Default: 1e-3
      (cosmo-int-maxsteps 25))          ; Default: 20
  (cosmo-get-luminosity-distance 0.1))  ; Mpc
;; => 481.64846692907696

Let's consider a final example. First, improve the default precision. Then, set a list of redshifts z and compute the respective luminosity distance DL(z) (mapping the respective function to each redshift). Retrieve also the Hubble distance DH. Finally, define a list of reduced luminosity distance values dL(z) = DL(z) / DH at each redshift. (The computation is carried out within a let statement to avoid modifying global values.)

(let* ((cosmo-int-prec 1e-4)
       (redshifts '(0.1 0.5 1.0))
       (DLs (mapcar 'cosmo-get-luminosity-distance redshifts)) ; Mpc
       (DH (cosmo-get-hubble-distance))                        ; Mpc
       (dL ()))                                                ; DL/DH
  (dolist (DL DLs) (push (/ DL DH) dL))
  (reverse dL))
;; => (0.10757189225827068 0.6651149194795717 1.5614017653281118)