{ "metadata": { "language": "haskell", "name": "", "signature": "sha256:fac6495c475dca927e2630a6997cfca32c4809d1b232e480bb6c82f7b9f0523d" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "The information presented here is placed in the public domain, and was written by\n", "[Doug Burke](https://plus.google.com/+DougBurke). The \n", "[notebook](https://github.com/DougBurke/astro-haskell/blob/master/notebooks/angular%20diameter%20distance.ipynb)\n", "used to create this page is available, and questions can be\n", "asked using the\n", "[GitHub issues page](https://github.com/DougBurke/astro-haskell/issues)\n", "or via Twitter: ." ] }, { "cell_type": "heading", "level": 1, "metadata": { "hidden": false }, "source": [ "Haskell and Cosmology" ] }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "... or at least, a little bit of each.\n", "\n", "Although I mention a few bits and pieces below, I'm going to leave it to\n", "[Ned Wright's Cosmology Tutorial](http://www.astro.ucla.edu/~wright/cosmolog.htm)\n", "to explain things properly. Ned's\n", "[javascript Cosmology Calculator](http://www.astro.ucla.edu/~wright/CosmoCalc.html)\n", "should also be preferred to any of the code presented here, as I am ignoring\n", "some things (e.g. the contribution of neutrinos).\n", "\n", "This is not intended to be a Haskell tutorial, rather it is meant as\n", "an example showing that it is possible to use Haskell for Astronomy. \n", "Haskell is rather different to the computer languages commonly used in Astronomy,\n", "so I try and provide Python equivalents as I go along.\n", "One thing I am not going to do is try and convince you that Haskell\n", "is the *one true language* for Astronomy, since it isn't.\n", "I also don't think that many of the selling points of functional programming,\n", "or using a static typing system with type inference, can easily be\n", "shown in an IHaskell notebook.\n", "\n", "If you are interested in learning more about Haskell, \n", "then I suggest you look at one of the several guides to Haskell out there on the internet, such as\n", "[Chris Allen's Learn Haskell](https://github.com/bitemyapp/learnhaskell),\n", "[How to Learn Haskell](https://acm.wustl.edu/functional/haskell.php),\n", "and\n", "[Stephen Diehl's \"What I wish I knew when learning Haskell\"](http://www.stephendiehl.com/what/).\n", "The newly-renovated [Haskell home page](https://www.haskell.org/) is also a useful resource." ] }, { "cell_type": "heading", "level": 1, "metadata": { "hidden": false }, "source": [ "Last time the notebook was run" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import Data.Time\n", "getCurrentTime" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "2015-03-04 00:49:42.578756 UTC" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": { "hidden": false }, "source": [ "What is the aim of this page?" ] }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "I was recently re-reading the paper\n", "[\"Experience Report: Type-checking Polymorphic Units for Astrophysics Research in Haskell\"](http://www.cis.upenn.edu/~eir/papers/2014/units/units.pdf)\n", "by Takayuki Muranushi and Richard A. Eisenberg (Haskell Symposium 2014, Gothenburg, Sweden)\n", "and wanted to try out the \n", "[units](https://hackage.haskell.org/package/units) package. I have some (private) code that calculates\n", "cosmological quantities (distance, time, volume) that uses a different approach for handling\n", "units in Haskell (via the\n", "[dimensional](https://hackage.haskell.org/package/dimensional)\n", "package), and thougt I'd try converting the code to use `units`. Then I thought, I should\n", "put it online for all to see - an a moment of perhaps ill-advised grandeur - and so here we are.\n", "Unfortunately, during the development of this notebook, I found that I can't actually build\n", "the `units` module (since I am using the default `ghc` version provided by Ubuntu 14.10, which\n", "is version 7.6.3), so I ended up using a variant of the `dimensional` package.\n", "\n", "I had best point out that the following is not going to present a robust, well-thought\n", "out, API for a Cosmology library!" ] }, { "cell_type": "heading", "level": 2, "metadata": { "hidden": false }, "source": [ "What is this document?" ] }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "This is an\n", "[IHaskell](http://gibiansky.github.io/IHaskell/)\n", "notebook, which uses the notebook features of\n", "[IPython](http://ipython.org/) to support an interactive Haskell environment.\n", "Now, Haskell compilers tend to provide an interactive environment - commonly known as a\n", "[`repl`](http://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop) - but the\n", "advantage of IHaskell is the HTML support and easy display of non-textual items (so,\n", "similar to why people like IPython notebooks when there's the `ipython` command-line\n", "environment).\n", "\n", "Since this is an interactive environment, there are several differences to how code would\n", "be written and run in compiled code, but we can ignore these for now." ] }, { "cell_type": "heading", "level": 2, "metadata": { "hidden": false }, "source": [ "How do you install and setup the code?" ] }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "This section can be skipped if you are not interested in trying the code out.\n", "\n", "For this analysis, I am using the default set of packages on my Ubuntu 14.10 machine: `ghc` version 7.6.3, the\n", "Haskell compiler, and `cabal-install`\n", "(at version 1.20.0), which is used for package management (e.g. is is something like `pip`).\n", "Later versions should (hopefully) work, but earlier versions may not (the `cabal sandbox` command needs a `cabal-install` version of at least 1.18 and I don't know how well ghc version 7.4 or earlier are going to work).\n", "\n", "With these installed, and having moved to a new directory, I set up a \"sandbox\" environment in which to\n", "install the Haskell packages (to avoid conflicting packages and to make it easy to remove or update changes for just this project), and enter\n", "\n", " % cabal update\n", " % cabal sandbox init\n", " % cabal install integration chart-diagrams ihaskell ihaskell-blaze --dry-run\n", " Resolving dependencies...\n", " In order, the following would be installed (use -v for more details):\n", " Boolean-0.2.3\n", " NumInstances-1.4\n", " ...\n", " % cabal install integration chart-diagrams ihaskell ihaskell-blaze\n", " Resolving dependencies...\n", " Notice: installing into a sandbox located at\n", " /home/dburke/posts/cosmo/.cabal-sandbox\n", " Downloading Boolean-0.2.3...\n", " Downloading NumInstances-1.4...\n", " Downloading OneTuple-0.2.1...\n", " ...\n", " ... and wait quite a while\n", " ...\n", " \n", "This will create (if it doesn't already) the directory `~/.cabal/` as well as `.cabal-sandbox/` in the current directory.\n", "\n", "At this point you should be able to say\n", "\n", " % cabal repl\n", " GHCi, version 7.6.3: http://www.haskell.org/ghc/ :? for help\n", " Loading package ghc-prim ... linking ... done.\n", " Loading package integer-gmp ... linking ... done.\n", " Loading package base ... linking ... done.\n", " Prelude> import Numeric.Integration.TanhSinh\n", " Prelude Numeric.Integration.TanhSinh> :quit\n", " Leaving GHCi.\n", "\n", "which checks that things are set up correctly.\n", "\n", "We now need to run IHaskell, which will then set up its own version of IPython (unless it can find one it is able to use), and store it in `~/.ihaskell/`.\n", "\n", " % mkdir notebooks\n", " % cabal exec IHaskell -- notebook --serve=notebooks/\n", " Did not detect IHaskell-installed or system IPython.\n", " IHaskell will now proceed to install IPython (locally for itself).\n", " Installing IPython in IHaskell's virtualenv in 10 seconds. Ctrl-C to cancel.\n", " ...\n", " ... this will install into ~/.ihaskell\n", " ...\n", " Successfully installed ipython-2.4.1\n", " Creating IPython profile.\n", " ...\n", " ... normal ipython notebook output\n", " ...\n", " 2015-02-09 19:28:24.287 [NotebookApp] The IPython Notebook is running at: http://localhost:8778/\n", " 2015-02-09 19:28:24.287 [NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).\n", " \n", "At this point you should be able to go the URL where the server is running and start a notebook to follow along. If you just\n", "want to try a few things out, then the \n", "[Try Haskell!](https://tryhaskell.org/)\n", "and\n", "[FP Complete](https://www.fpcomplete.com/page/project-build)\n", "sites provide on-line environments for trying out Haskell.\n", "\n", "For reference, the versions of the major packages used in this notebook are (`dimensional-tf` is added in later):\n", "\n", " cabal exec ghc-pkg -- list | egrep -i 'ihaskell|integration|dimensional|chart' \n", " Chart-1.3.3\n", " Chart-diagrams-1.3.3\n", " dimensional-tf-0.3.0.1\n", " ihaskell-0.4.3.0\n", " ihaskell-blaze-0.1.0.0\n", " integration-0.2.0.1" ] }, { "cell_type": "heading", "level": 2, "metadata": { "hidden": false }, "source": [ "Setting things up" ] }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "Here I just load in a few modules that will be used later, for displaying\n", "a graph. In Haskell, the form `import a` loads all the symbols defined for\n", "export from the module `a`; that is, it is like Python's `from a import *`.\n", "There are ways to import just one or more symbols, or\n", "to load in a module using a qualified name, both of which are used later." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import IHaskell.Display\n", "import Graphics.Rendering.Chart.Backend.Diagrams\n", "\n", "import Graphics.Rendering.Chart.Easy" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "Normally we would just install the \n", "[ihaskell-charts](https://hackage.haskell.org/package/ihaskell-charts) \n", "package and not have to bother with this, but version `0.1.0.0` of\n", "this package does not build with version `1.3` of\n", "[Chart](https://hackage.haskell.org/package/Chart). There is a fix\n", "for this on the GitHub version of `ihaskell-charts` but it's also\n", "possible to write it directly by converting the output of Chart\n", "into a form that the \n", "[ihaskell-blaze](https://hackage.haskell.org/package/ihaskell-blaze)\n", "package knows how to display." ] }, { "cell_type": "code", "collapsed": false, "input": [ "-- For the purposes of this notebook you do not need to understand this\n", "-- block. It may be useful to point out that in Haskell, comments are\n", "-- introduced by \"--\" (or bracketed by \"{-\" and \"-}\", but I don't plan\n", "-- to use that style here).\n", "--\n", "instance IHaskellDisplay (Renderable a) where\n", " display renderable = do\n", " (svg, _) <- renderableToSVG renderable 450 300\n", " -- now let blaze worry about displaying the SVG\n", " display svg" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": { "hidden": false }, "source": [ "What is the distance between two points?" ] }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "I want to calculate the \n", "[angular-diameter distance](http://en.wikipedia.org/wiki/Angular_diameter_distance)\n", "to a source, so that I can find out what the projected separation between\n", "two points is when all I know is a redshift and an angle. The parameters we\n", "are interested in are the redshift of the source, $z$, the\n", "matter-density ($\\Omega_m$), the vacuum-density ($\\Omega_\\lambda$),\n", "and Hubble's constant ($H_0$).\n", "\n", "The angular-diameter distance, $d_A$, is \n", "\n", "$$d_A (z; \\Omega_m, \\Omega_\\lambda) = \\frac{c}{H_0} \\frac{d_c(z; \\Omega_m, \\Omega_\\lambda)}{1+z}$$\n", "\n", "where $d_c$ is the \n", "[comoving distance](http://en.wikipedia.org/wiki/Comoving_distance)\n", "to the source. For simplicity,\n", "I am going to assume that we are only interested in flat cosmologies; that\n", "is, where $\\Omega_m + \\Omega_\\lambda = 1$ (i.e. $\\Omega_k = 0$), which\n", "leaves me to calculate:\n", "\n", "$$d_A (z; \\Omega_m) = \\frac{c}{H_0} \\frac{\\int_0^z f(z; \\Omega_m) dz}{1+z}$$\n", "\n", "where\n", "\n", "$$f (z; \\Omega_m) = 1 / \\sqrt{ (1+z)^2 (1 + \\Omega_m z) - (2+z) (1-\\Omega_m) z }$$\n", "\n", "So, I start by defining the $f$ function, which has arguments of $\\Omega_m$ (`om`)\n", "and redshift (`z`):" ] }, { "cell_type": "code", "collapsed": false, "input": [ "-- Calculate f(z; omega_m). The first argument is the matter density\n", "-- and the second the redshift.\n", "f :: Double -> Double -> Double\n", "f om z = let ol = 1 - om\n", " t = (1 + z)^2 * (1 + om*z) - (2 + z) * ol * z\n", " in 1 / sqrt t" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "There are several things to note here:\n", "\n", " - The first two lines are comments.\n", "\n", " - The third line gives the signature of the function: the label before `::` is the name of the\n", " function and the types of the arguments are separated by the arrows (`->`), with the last \n", " type being the value calculated by the function. So, `f` takes two `Double` values and\n", " returns a `Double`.\n", " \n", " This signature line is *not* required, since the Haskell compiler can determine the types.\n", " However the types it infers a more general than the ones I gave (it has worked out that\n", " both `Float` or `Double` could be used). I chose to force `Double` since the integration\n", " routine I use below works on `Double` types and it simplifies some of the discussion\n", " below, such as when asking for the type of the `g` function.\n", "\n", " - The function arguments on line four are separated by spaces rather than brackets and commas.\n", "\n", " - The definition of the function (lines four to siz) uses the `let xxx in yyy` form, which defines a set of\n", " local symbols (in this case `ol` and `t`) which are then used to calculate a value.\n", " \n", " - Function application does not use brackets, so the denominator of the fraction is\n", " just `sqrt t`. However, precedence rules means that brackets may be needed to clarify\n", " what is an argument. For instance, if I had not used the temporary symbol `t` then I would\n", " have had to write `1 / sqrt ((1 + z)^2 * (1 + om*z) - (2 + z) * ol * z)`.\n", " \n", "The above is similar to the Python code:\n", "\n", " def f(om,z):\n", " ol = 1 - om\n", " t = (1 + z)**2 * (1 + om*z) - (2 + z) * ol * z\n", " return 1 / math.sqrt(t)\n", "\n", "I now want to check how $f$ varies with redshift for a given $\\Omega_m$ value (in this case\n", "I want to use a value of `0.3`, since that is close to the \n", "[currently-preferred](http://www.astro.ucla.edu/~wright/cosmolog.htm#News) cosmological \n", "model). I can do this by\n", "taking advantage of\n", "[partial application](http://en.wikipedia.org/wiki/Partial_application) to easily create a function\n", "that calls `f` with the first argument fixed to `0.3`:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "g = f 0.3" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "The Python version would be something like\n", "\n", " def g(z):\n", " return f(0.3, z)\n", " \n", "or\n", "\n", " g = lambda z: f(0.3, z)\n", "\n", "Since I am often going to be interested in how things vary with redshift, I am going to\n", "make this parameter the last one, so that it makes it easy to partially apply\n", "the functions I write (as in the case of making `g` a specialized version of `f`).\n", "\n", "The `:type` command - which is part of the interactive Haskell environment rather than \n", "Haskell itself - reports the type of a symbol: in this case it shows us that `g` is\n", "a function that takes a `Double` and returns a `Double` (so it has one less \"`Double ->`\"\n", "term than `f`)." ] }, { "cell_type": "code", "collapsed": false, "input": [ ":type g\n", ":type f" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "f :: Double -> Double -> Double" ], "metadata": {}, "output_type": "display_data", "text": [ "f :: Double -> Double -> Double" ] }, { "html": [ "g :: Double -> Double" ], "metadata": {}, "output_type": "display_data", "text": [ "g :: Double -> Double" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "I am going to gloss over the fact that Haskell functions only ever accept a single argument, using \n", "[curried functions](http://learnyouahaskell.com/higher-order-functions) to implement what appear to be multi-argument functions.\n", "It is an important point to understand if you are going to be writing Haskell, \n", "but I don't want to derail the discussion too much.\n", "\n", "Getting back to the code, I can evaluate `g` for $z=0$ and then $z=1.2$ \n", "(taking advantage of the automatic-display capabilities of the\n", "IHaskell notebook; in an actual Haskell program we would have to say something like `print (g 0)` to display the value): " ] }, { "cell_type": "code", "collapsed": false, "input": [ "g 0" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "1.0" ] } ], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "g 1.2" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "0.506733622988661" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "I want to evaluate this function for a number of different redshifts, so I want to \n", "automate this process, which leads to lists and the `map` function.\n", "\n", "In Haskell, lists of objects are represented by comma-separated values enclosed by\n", "`[]` brackets - e.g. `[1.2, 3.4, 9.8]` - and have a type of `[a]`, where `a` is the\n", "type of the list element. The `map` function takes a function which accepts one\n", "argument, as well as a list of arguments, and returns a list of values created\n", "by applying the function to each argument. That is, it has a type of:" ] }, { "cell_type": "code", "collapsed": false, "input": [ ":type map" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "map :: forall a b. (a -> b) -> [a] -> [b]" ], "metadata": {}, "output_type": "display_data", "text": [ "map :: forall a b. (a -> b) -> [a] -> [b]" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "For this discussion we can ignore the `forall a b` part of the type.\n", "\n", "To be general, the function supplied to map can have different types for its input and output lists,\n", "(so `a` and `b` above)\n", "but here we will use `g` which means that the input and output lists have a type\n", "of `[Double]`:" ] }, { "cell_type": "code", "collapsed": false, "input": [ ":type map g" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "map g :: [Double] -> [Double]" ], "metadata": {}, "output_type": "display_data", "text": [ "map g :: [Double] -> [Double]" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "which means that the following evaluates `g` with `z=0`, `z=0.4`, and `z=3.2`:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "map g [0, 0.4, 3.2]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "[1.0,0.8102546544402055,0.20884883995609949]" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "I want to look at `g` evaluated for a range of redshits\n", "between 0 and 10, so let's create the list of redshifts \n", "(a common scheme when naming\n", "variables in Haskell is to add a `s` for a list of values, hence the\n", "use of `zs` here) and evaluate `g` on each element:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "zs = [0::Double, 0.1 .. 10]\n", "gs = map g zs" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "The form `[a, a+dx .. b]` is similar to NumPy's `numpy.arange(a, b+dx, dx)`, so this is similar to the Python/NumPy code\n", "\n", " zs = numpy.arange(0, 10.1, 0.1)\n", " # You could use Python's map\n", " gs = map(g, zs)\n", " # or a list compreshension\n", " gs = [g(z) for z in zs]\n", " \n", "Note that I am ignoring the difference between Python lists and NumPy arrays here.\n", "\n", "I did not need to add `::Double` to `0` when creating `zs`, but did so just to keep the types \"concrete\". If I had said\n", "\n", " zs = [0, 0.1 .. 10]\n", " \n", "then `gs` would still be a list of `Double` values, but the type of `zs` would be\n", "\n", " forall t. (Enum t, Fractional t) => [t]\n", " \n", "and I don't really want to try and explain \n", "[Haskell type classes](http://learnyouahaskell.com/types-and-typeclasses)\n", "(the names to the left of the `=>`) here.\n", "\n", "We can finally take advantage of the display code I wrote at the start of the notebook, and\n", "use the routines from the \n", "[`Graphics.Rendering.Chart.Easy` module](http://hackage.haskell.org/package/Chart-1.3.3/docs/Graphics-Rendering-Chart-Easy.html)\n", "of the\n", "[Chart package](http://hackage.haskell.org/package/Chart)\n", "to display `gs` versus `zs`:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "toRenderable (do\n", " layout_title .= \"f\"\n", " layout_x_axis . laxis_title .= \"Redshift\"\n", " layout_y_axis . laxis_title .= \"f(z)\"\n", " plot (line \"O_matter=0.3 O_lambda=0.7\" [zip zs gs])\n", " )" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n" ], "metadata": {}, "output_type": "display_data", "text": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "" ] } ], "prompt_number": 13 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "This command doesn't look like anything I've shown so far, and I don't really want to explain it too much, but the\n", "`toRenderable` function takes a list of actions, which describes the plot, and converts it a value with a\n", "`Renderable` type, defined in the\n", "[Chart](http://hackage.haskell.org/package/Chart/) package,\n", "which is itself then converted into SVG and automatically\n", "displayed by the notebook, due to the code I wrote way back at the start.\n", "\n", "The list of actions is created using \n", "[`do` notation](http://learnyouahaskell.com/a-fistful-of-monads#do-notation), \n", "which is a way of representing a chain of actions - in this case things\n", "like \"set the X-axis label to 'Redshift'\" and \"plot up this list of (x,y) points\" - in Haskell. This is *very* terse\n", "and it's best if you just let your eyes skip over the code and focus on the plot instead." ] }, { "cell_type": "heading", "level": 3, "metadata": { "hidden": false }, "source": [ "Integrating $f$" ] }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "To calculate the angular-diameter distance I need to integrate `f`. The \n", "[\"Why functional programming matters\" (link to PDF)](http://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf)\n", "article by John Hughes provides a nice implementation that highlights\n", "some of the ways that \n", "[lazy evaluation](http://en.wikipedia.org/wiki/Lazy_evaluation) can be used to \n", "simplify code. However, it's easier to use a pre-canned solution,\n", "and in this case I have chosen to use the \n", "[`integration`](https://hackage.haskell.org/package/integration) package:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import Numeric.Integration.TanhSinh" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 14 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "There are several ways to integrate up a function, and the results can be bounded to a relative\n", "or absolute limit. The function `g` is \"well behaved\", so a simple integration scheme is likely\n", "to be sufficient. The runs below - where I have approximated $\\int_0^2 g(z) dz$,\n", "show that both Simpson's and the trapezoidal methods agree,\n", "so I shall use `trap` (the absolute limit of `1.0e-6` is excessive but the run times are not\n", "prohibitive here):" ] }, { "cell_type": "code", "collapsed": false, "input": [ "absolute 1.0e-6 (simpson g 0 2)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "Result {result = 1.2094712042710498, errorEstimate = 1.0959402274579588e-9, evaluations = 49}" ] } ], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": [ "absolute 1.0e-6 (trap g 0 2)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "Result {result = 1.2094712042709923, errorEstimate = 3.2879929889872983e-9, evaluations = 25}" ] } ], "prompt_number": 16 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "The result of `absolute` is a Haskell record, but all we need worry about now is that the\n", "value of the integrated function can be retrieved from it by using `result`, as shown below:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "result (absolute 1.0e-6 (trap g 0 2))" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "1.2094712042709923" ] } ], "prompt_number": 17 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "If you are wondering what just happened: the `simpson` and `trap` routines take a function of one parameter (the function\n", "to integrate, so `g` here) and a range over which to integrate ($z=0$ to $z=2$ here), and return a list of approximations (the `[Result]` return type) which (hopefully) converge on the answer. The `absolute` routine takes a limit value and a list of results, returning the first one whose error estimate is smaller than the given limit." ] }, { "cell_type": "code", "collapsed": false, "input": [ ":type simpson\n", ":type trap\n", "\n", ":type absolute\n", ":type relative" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "simpson :: (Double -> Double) -> Double -> Double -> [Result]" ], "metadata": {}, "output_type": "display_data", "text": [ "simpson :: (Double -> Double) -> Double -> Double -> [Result]" ] }, { "html": [ "trap :: (Double -> Double) -> Double -> Double -> [Result]" ], "metadata": {}, "output_type": "display_data", "text": [ "trap :: (Double -> Double) -> Double -> Double -> [Result]" ] }, { "html": [ "absolute :: Double -> [Result] -> Result" ], "metadata": {}, "output_type": "display_data", "text": [ "absolute :: Double -> [Result] -> Result" ] }, { "html": [ "relative :: Double -> [Result] -> Result" ], "metadata": {}, "output_type": "display_data", "text": [ "relative :: Double -> [Result] -> Result" ] } ], "prompt_number": 18 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "We can look at these return values, using the `take 3` function that returns the first three elements of a list, \n", "to see that the result has converged quickly:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "take 3 (trap g 0 2)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "[Result {result = 1.2094712009829993, errorEstimate = 1.478233999528955e-4, evaluations = 13},Result {result = 1.2094712042709923, errorEstimate = 3.2879929889872983e-9, evaluations = 25},Result {result = 1.2094712042710354, errorEstimate = 4.296563105299356e-14, evaluations = 49}]" ] } ], "prompt_number": 19 }, { "cell_type": "code", "collapsed": false, "input": [ "take 3 (simpson g 0 2)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "[Result {result = 1.2095204754496505, errorEstimate = 0.4029106946610779, evaluations = 13},Result {result = 1.20947120536699, errorEstimate = 4.927008266042421e-5, evaluations = 25},Result {result = 1.2094712042710498, errorEstimate = 1.0959402274579588e-9, evaluations = 49}]" ] } ], "prompt_number": 20 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "Getting back to the task at hand, I can define `daH` as a function that accepts $\\Omega_m$ and $z$ \n", "and returns the angular-diameter distance, in units of the Hubble length, by saying:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "daH om z = let fs = result (absolute 1.0e-6 (trap (f om) 0 z))\n", " in fs / (1+z)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 21 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "The equivalent Python would be something like\n", "\n", " def daH(om,z):\n", " # I am assuming here that the integrate and trap methods are\n", " # defined somewhere else.\n", " fs = integrate(lambda z: f(om,z), 0, z, method=trap, abstol=1.0e-6)\n", " return fs / (1+z)\n", "\n", "Although not given explicitly, the compiler can infer that the two input arguments, and the result,\n", "have a type of `Double`:" ] }, { "cell_type": "code", "collapsed": false, "input": [ ":type daH" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "daH :: Double -> Double -> Double" ], "metadata": {}, "output_type": "display_data", "text": [ "daH :: Double -> Double -> Double" ] } ], "prompt_number": 22 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "As I want to plot up the angular-diameter distance as a function of redshift for several\n", "different cosmologies, I define a helper function that, given an $\\Omega_m$ value, returns\n", "a list of $(z, dA(z))$ points, which is in the form needed by the `line` command used\n", "earlier." ] }, { "cell_type": "code", "collapsed": false, "input": [ "-- This uses the list of redshifts defined earlier.\n", "-- \n", "-- (daH om) has type Double -> Double\n", "-- zs has type [Double]\n", "-- so map (daH om) zs has type [Double]\n", "--\n", "-- zip's signature is [a] -> [b] -> [(a,b)]\n", "-- that is, it pairs up corresponding elements of\n", "-- the two input arrays. This means that\n", "-- the output of zip zs (map (daH om) zs)\n", "-- is [(Double, Double)], where the first element\n", "-- of eachpair is the redshift, and the second element\n", "-- the anular-diameter distance for this redshift\n", "-- (in units of the Hubble length).\n", "--\n", "calcDAH om = zip zs (map (daH om) zs)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 23 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "The Python equivalent would be:\n", "\n", " def calcDAH(om):\n", " zs = np.arange(0, 10.1, 0.1)\n", " return [(z, daH(om,z)) for z in zs]" ] }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "This can then be used to display the results for several cosmologies:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "toRenderable (do\n", " layout_title .= \"Angular-diameter distance\"\n", " layout_x_axis . laxis_title .= \"Redshift\"\n", " layout_y_axis . laxis_title .= \"dA(z)\"\n", " plot (line \"O_m=0.1 O_l=0.9\" [calcDAH 0.1])\n", " plot (line \"O_m=0.3 O_l=0.7\" [calcDAH 0.3])\n", " plot (line \"O_m=0.9 O_l=0.1\" [calcDAH 0.9])\n", " )" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n" ], "metadata": {}, "output_type": "display_data", "text": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "" ] } ], "prompt_number": 24 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "The plot shows the non-monotonic nature of the angular-diameter distance." ] }, { "cell_type": "heading", "level": 3, "metadata": { "hidden": false }, "source": [ "Converting to a physical distance" ] }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "To get a value with physical units, we multiply `daH` by `c/`$H_0$, which,\n", "if the speed of light is in `km/s` and $H_0$ in `km/s/Mpc`, will give a value\n", "in Megaparsecs:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "-- Calculate the angular-diameter distance for a flat Cosmology\n", "-- with a matter density of om, Hubble constant of H0 (in km/s/Mpc),\n", "-- and redshift. The result is in Mpc.\n", "--\n", "da om h0 z = daH om z * c / h0\n", " where\n", " -- speed of light in km/s\n", " c = 299792.458" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 25 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "For variety I chose to use the form `xxx where yyy` rather than `let yyy in xxx`, as used in earlier examples." ] }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "This routine can then be used to calculate angular-diameter distances for a range of redshifts:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "map (da 0.3 70) [0, 0.5, 1.0, 1.2, 2.0]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "[0.0,1259.083597288827,1651.9144029435865,1710.6506180601218,1726.6206914696231]" ] } ], "prompt_number": 26 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "As a sanity check, I used Ned Wright's\n", "[CosmoCalc](http://www.astro.ucla.edu/~wright/CosmoCalc.html) calculator and recorded\n", "the distances for several redshifts. These are stored in the `nedWrightDA` list,\n", "and then I calculate the ratio of the distance I calculate to those from `CosmoCalc`\n", "(the `rel` list). As can be seen, the values are all close to 1.0. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "nedWrightDA = [1259.0, 1651.8, 1710.5, 1726.3]\n", "\n", "-- zipWith has a type of (a -> b -> c) -> [a] -> [b] -> [c], and\n", "-- applies the function (first argument) to the elements in the two input\n", "-- lists, creating the output list. Here I supply a list of redshifts\n", "-- and a list of the values from CosmoCalc for those redshifts, and\n", "-- then return the angular-diameter distance calculated for the\n", "-- redshift divided by the CosmoCalc value.\n", "--\n", "-- The (\\lambda z nw -> da 0.3 70 z / nw) fragment is Haskell syntax\n", "-- for creating an anonymous function that takes two arguments (z and nw\n", "-- here) and then applying the function body (in this case divide the\n", "-- value of \"da 0.3 70 z\" by nw).\n", "--\n", "-- I could have written this in several lines, and avoided the need for\n", "-- all these comments!\n", "rel = zipWith (\\z nw -> da 0.3 70 z / nw) [0.5, 1.0, 1.2, 2.0] nedWrightDA\n", "\n", "rel" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "[1.0000663997528412,1.00006925956144,1.0000880549898403,1.000185768099185]" ] } ], "prompt_number": 27 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "The Python equivalent for calculating `rel` would be something like (where I have\n", "decided to use shorter variable names than above):\n", "\n", " da = [da(0.3,70,z) for z in [0.5, 1, 1.2, 2]]\n", " nw = [1259.0, 1651.8, 1710.5, 1726.3]\n", " rel = np.asarray(da) / np.asarray(nw)\n", "\n", "I can now repeat the previous plot, this time with physical units:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "calcDA om = zip zs (map (da om 70) zs)\n", "\n", "toRenderable (do\n", " layout_title .= \"Angular-diameter distance; H_0 = 70 km/s/Mpc\"\n", " layout_x_axis . laxis_title .= \"Redshift\"\n", " layout_y_axis . laxis_title .= \"dA(z) [Mpc]\"\n", " plot (line \"O_m=0.1 O_l=0.9\" [calcDA 0.1])\n", " plot (line \"O_m=0.3 O_l=0.7\" [calcDA 0.3])\n", " plot (line \"O_m=0.9 O_l=0.1\" [calcDA 0.9])\n", " )" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n" ], "metadata": {}, "output_type": "display_data", "text": [ "\n", "\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "" ] } ], "prompt_number": 28 }, { "cell_type": "heading", "level": 3, "metadata": { "hidden": false }, "source": [ "Calculating the separation between two points" ] }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "To get the linear distance between two points at the same redshift, separated by an angle of theta,\n", "we can use the small-angle approximation: convert the angle to radians and then multiply it by the\n", "angular-diameter distance. Astronomers use all kinds of funny units for angles; in this case I\n", "am assuming the input angle has units of arcminutes. As there are 60 arcminutes to a degree, the\n", "conversion to radians is just $\\pi / (180*60)$." ] }, { "cell_type": "code", "collapsed": false, "input": [ "-- Given Omega_m, H_0 in km/s/Mpc, z, and an angle in arcminutes,\n", "-- return the linear scale in kpc.\n", "adist om h0 z angle = 1000 * d * a\n", " where\n", " -- convert angle, in arcminutes, into radians\n", " a = pi * angle / (180 * 60)\n", " \n", " -- d is in Mpc\n", " d = da om h0 z" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 29 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "For a redshift of 2, with a closed cosmology and $\\Omega_m = 0.3$, the \n", "`CosmoCalc` calculator calculate a scale of 8.370 kiloparsecs per arcsec. How close\n", "do I get (given that one arcsec is 1/60 of an arcminute and I intentionally made\n", "`adist` return values in kpc):" ] }, { "cell_type": "code", "collapsed": false, "input": [ "adist 0.3 70 2 (1/60)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "8.370893333112804" ] } ], "prompt_number": 30 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "So, not bad agreement. As a reminder, the code I had to write to get this (stripping out all the non-essential bits, including niceties like type signatures and comments, from above) was:\n", "\n", " f om z = let ol = 1 - om\n", " in 1 / sqrt( (1+z)^2 * (1 + om*z) - (2+z) * ol * z )\n", " \n", " daH om z = let fs = result (absolute 1.0e-6 (trap (f om) 0 z))\n", " in fs / (1+z)\n", " \n", " da om h0 z = daH om z * c / h0\n", " where\n", " c = 299792.458\n", " \n", " adist om h0 z angle = 1000 * d * a\n", " where\n", " a = pi * angle / (180 * 60)\n", " d = da om h0 z\n", " \n", "If I wanted to make this code usable I would spend some time thinking about whether it is worth defining types\n", "especially for the matter density, Hubble's constant, or redshift values, to clarify routines like `adist` which take four `Double` values and it is easy to mix them up - e.g. swapping `om` and `h0` - leading to incorrect results. However,\n", "that's a discussion for another day." ] }, { "cell_type": "heading", "level": 2, "metadata": { "hidden": false }, "source": [ "Adding units and quantities" ] }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "The above calculation is fine, but I'd like to explicitly include the units in the calculation, so that\n", "you know the answer is in Mpc (or some other unit). There are several Haskell packages that\n", "support this; I have previously used the\n", "[dimensional](https://hackage.haskell.org/package/dimensional) \n", "package and there is also\n", "[unittyped](https://hackage.haskell.org/package/unittyped)\n", "and\n", "[quantities](https://hackage.haskell.org/package/quantities), but today\n", "I want to try the [units](https://hackage.haskell.org/package/units) package.\n", "As I have not tried out the \n", "[support for quantities and units in astropy](http://www.astropy.org/astropy-tutorials/Quantities.html)\n", "I do not provide Python equivalents for the rest of this notebook.\n", "\n", "Unfortunately, using ghc version 7.6.3 means that only the first version (`1.0.0`) of `units` can\n", "be used, which is a problem, since it doesn't build! So I decided to try out the\n", "[dimensional-tf](https://hackage.haskell.org/package/dimensional-tf) \n", "package, since it a different approach to the `dimensional` package, which I have \n", "used. The difference between the two is not really relevant here; we can just look at\n", "is as being one way to model quantities and units in a physical calculation in Haskell.\n", "I hope to have time to repeat this with a more-recent version of `ghc` so that I can \n", "try out `units`.\n", "\n", "To install the necessary package we need to use `cabal` again:\n", "\n", " cabal install dimensional-tf --dry-run\n", " ... check that no problems are reported\n", " cabal install dimensional-tf\n", " ... this should be relatively quick as there's not that many\n", " ... dependencies that need to be built\n", "\n", "The following statement is similar to Python's `import foo.bar.baz as bob`\n", "statement; symbols defined by the module must be preceeded by the quaifier\n", "`P`. The reason for doing this is that this module provides versions of\n", "common mathematical operations - such as multiplication - which would\n", "conflict with the default versions. It will make it clear what version\n", "I am using, but is visually distracting, as you will soon see!" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import qualified Numeric.Units.Dimensional.TF.Prelude as P" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 31 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "I start by defining the speed of light, `c`, in meters per second (you can use `P.meter` or `P.metre` here):" ] }, { "cell_type": "code", "collapsed": false, "input": [ "c = 299792458.0 P.*~ (P.meter P./ P.second)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 32 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "The inferred type for this is confusing:" ] }, { "cell_type": "code", "collapsed": false, "input": [ ":type c" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "c :: forall a. Fractional a => Quantity (Dim (S Z) Z (N (S Z)) Z Z Z Z) a" ], "metadata": {}, "output_type": "display_data", "text": [ "c :: forall a. Fractional a => Quantity (Dim (S Z) Z (N (S Z)) Z Z Z Z) a" ] } ], "prompt_number": 33 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "This can be fixed by giving an explicit type, as shown below (it's not actually necessary\n", "to do so, but I do so because I can):" ] }, { "cell_type": "code", "collapsed": false, "input": [ "c :: P.Velocity Double\n", "c = 299792458.0 P.*~ (P.meter P./ P.second)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 34 }, { "cell_type": "code", "collapsed": false, "input": [ ":type c" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "c :: Velocity Double" ], "metadata": {}, "output_type": "display_data", "text": [ "c :: Velocity Double" ] } ], "prompt_number": 35 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "The type is now more readable, but in reality this is just a synonym for the original type (except that the\n", "`Fractional` constraint has been replaced by the use of an explicit type `Double`, which is a member of the\n", "[`Fractional` type class](http://hackage.haskell.org/package/base-4.7.0.2/docs/Prelude.html#t:Fractional)).\n", "Although not relevant here - as I'm treating the module as something of a black box - the \n", "dimensions of the value are encoded here using\n", "[Peano numbers](https://wiki.haskell.org/Peano_numbers), where `Z` indicates `0`, `S a` is one more than `a` (so\n", "`S Z` is `1` and `S (S (S Z))` is `3`), \n", "and `N a` is used to negate `a`. The `Dim` statement from the original type of `c` can therefore\n", "be read as `Dim 1 0 -1 0 0 0 0`, so this suggests that the first element refers to length and the third refers\n", "to time. I believe the reason for using Peano numbers here is because they are inductively defined (the base\n", "case is 0 and the inductive step is add one), which means that the type checker can match up things as needed.\n", "One thing to note, if you've read this far into the paragraph, is that this information is only relevant when\n", "the code is compiled. This means that you only \"pay\" for the checking when you build\n", "the code (which can, unfortunately, take some time for particularly complex cases) rather than when you run it.\n", "\n", "More information on the API of the `dimensional-tf` package\n", "can be found in the \n", "[source code](https://github.com/bjornbm/dimensional-tf/blob/master/Numeric/Units/Dimensional/TF.lhs)\n", "to this module, as it uses a literate programming style which means that the comments do not\n", "get displayed in the standard \n", "[module documentation](https://hackage.haskell.org/package/dimensional-tf-0.3.0.1/docs/Numeric-Units-Dimensional-TF.html).\n", "The main thing to note just now is that there are types for quantities - such as the speed of light\n", "in meters per second - and for units - such as second. These types are labelled\n", "`Quantity` or `Unit` and contain the relevant dimension information (the `Dim` type seen above)\n", "and the type of the numerical value (e.g. `Double`). Synonyms are used to make these\n", "more readable, such as `Velocity` meaning `Quantity DVelocity` and\n", "`DVelocity` being `Dim (S Z) Z (N (S Z)) Z Z Z Z`, which can sometimes be a bit confusing\n", "when you are trying to work out how everything fits together.\n", "\n", "The symbol `c` now retains both the quantity and the units, as displaying it shows:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "c" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "2.99792458e8 m s^-1" ] } ], "prompt_number": 36 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "The quantity stored in `c` can be extracted, converting to a given unit, using the `/~` operator,\n", "as shown below (note that this is one point where the use of the `P` prefix is visually distracting):" ] }, { "cell_type": "code", "collapsed": false, "input": [ "c P./~ (P.kilo P.meter P./ P.second)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "299792.458" ] } ], "prompt_number": 37 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "Using it in a calculation is also somewhat unsatisfting, since you need to use the mathematical\n", "operators from the module - leading to visual clutter if you use the approach I have taken\n", "here - and the need to use constants such as `P._2` to represent the value `2`:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "c P./ P._2" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "1.49896229e8 m s^-1" ] } ], "prompt_number": 38 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "Alternatively, I can \"convert\" a value into a dimensionless number by using the form `value *~ one` (which is needed\n", "when there is no pre-defined constant):" ] }, { "cell_type": "code", "collapsed": false, "input": [ "c P./ (2 P.*~ P.one)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "1.49896229e8 m s^-1" ] } ], "prompt_number": 39 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "Here I define a symbol that represents one arcminute. The use of `1::Double` is just to make sure that this is a `Double`\n", "value, rather than a more general one, in a similar manner to how `c` was defined." ] }, { "cell_type": "code", "collapsed": false, "input": [ "arcmin = (1::Double) P.*~ P.minuteOfArc" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 40 }, { "cell_type": "code", "collapsed": false, "input": [ ":type arcmin" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "arcmin :: Quantity DPlaneAngle Double" ], "metadata": {}, "output_type": "display_data", "text": [ "arcmin :: Quantity DPlaneAngle Double" ] } ], "prompt_number": 41 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "One of the reasons for using a system which also encodes units is to make it\n", "an error for combining incompatible values; in this case trying to add\n", "together the speed of light and an angle is an error. Admittedly, a\n", "rather confusing one if you're not used to the Haskell type system." ] }, { "cell_type": "code", "collapsed": false, "input": [ "arcmin + c" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "Couldn't match type \u2018Numeric.NumType.TF.S Numeric.NumType.TF.Z\u2019 with \u2018Numeric.NumType.TF.Z\u2019
Expected type: P.Quantity P.DPlaneAngle Double
Actual type: P.Velocity Double
In the second argument of \u2018(+)\u2019, namely \u2018c\u2019
In the expression: arcmin + c
" ], "metadata": {}, "output_type": "display_data", "text": [ "Couldn't match type \u2018Numeric.NumType.TF.S Numeric.NumType.TF.Z\u2019 with \u2018Numeric.NumType.TF.Z\u2019\n", "Expected type: P.Quantity P.DPlaneAngle Double\n", " Actual type: P.Velocity Double\n", "In the second argument of \u2018(+)\u2019, namely \u2018c\u2019\n", "In the expression: arcmin + c" ] } ], "prompt_number": 42 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "I guess that the \"native\" value for angles is radians, so let's check this:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "arcmin" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "2.908882086657216e-4" ] } ], "prompt_number": 43 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "which is one arcminute in radians, since:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "pi / (60 * 180)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "2.908882086657216e-4" ] } ], "prompt_number": 44 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "I can also check that the numerical values are equal directly, by\n", "using `/~ radian` to extract then numeric value from `arcmin`:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "arcmin P./~ P.radian == (pi / (60*180))" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "True" ] } ], "prompt_number": 45 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "If I had not \"converted\" `arcmin` from a quantity to a standard value, using `/~ radian`, then\n", "I would have come across another rather frightening error message:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "arcmin == (pi / (60*180))" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "No instance for (Floating (P.Quantity P.DPlaneAngle Double)) arising from a use of \u2018pi\u2019
In the first argument of \u2018(/)\u2019, namely \u2018pi\u2019
In the second argument of \u2018(==)\u2019, namely \u2018(pi / (60 * 180))\u2019
In the expression: arcmin == (pi / (60 * 180))

No instance for (Fractional (P.Quantity P.DPlaneAngle Double)) arising from a use of \u2018/\u2019
In the second argument of \u2018(==)\u2019, namely \u2018(pi / (60 * 180))\u2019
In the expression: arcmin == (pi / (60 * 180))
In an equation for \u2018it\u2019: it = arcmin == (pi / (60 * 180))

No instance for (Num (P.Quantity P.DPlaneAngle Double)) arising from a use of \u2018*\u2019
In the second argument of \u2018(/)\u2019, namely \u2018(60 * 180)\u2019
In the second argument of \u2018(==)\u2019, namely \u2018(pi / (60 * 180))\u2019
In the expression: arcmin == (pi / (60 * 180))
" ], "metadata": {}, "output_type": "display_data", "text": [ "No instance for (Floating (P.Quantity P.DPlaneAngle Double)) arising from a use of \u2018pi\u2019\n", "In the first argument of \u2018(/)\u2019, namely \u2018pi\u2019\n", "In the second argument of \u2018(==)\u2019, namely \u2018(pi / (60 * 180))\u2019\n", "In the expression: arcmin == (pi / (60 * 180))\n", "\n", "No instance for (Fractional (P.Quantity P.DPlaneAngle Double)) arising from a use of \u2018/\u2019\n", "In the second argument of \u2018(==)\u2019, namely \u2018(pi / (60 * 180))\u2019\n", "In the expression: arcmin == (pi / (60 * 180))\n", "In an equation for \u2018it\u2019: it = arcmin == (pi / (60 * 180))\n", "\n", "No instance for (Num (P.Quantity P.DPlaneAngle Double)) arising from a use of \u2018*\u2019\n", "In the second argument of \u2018(/)\u2019, namely \u2018(60 * 180)\u2019\n", "In the second argument of \u2018(==)\u2019, namely \u2018(pi / (60 * 180))\u2019\n", "In the expression: arcmin == (pi / (60 * 180))" ] } ], "prompt_number": 46 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "I want to be able to handle lengths which contain parsecs, so I need to create a unit\n", "for this, using the `prefix` function:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "parsec :: P.Unit P.DLength Double\n", "parsec = P.prefix 3.085678e16 P.metre" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 47 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "With this, I can now create distances in units of mega parsecs:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "3.2 P.*~ P.mega parsec" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "9.874169600000001e22 m" ] } ], "prompt_number": 48 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "and even define a unit for the Hubble constant:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "hubbleConstant :: P.Unit P.DFrequency Double\n", "hubbleConstant = P.kilo P.meter P./ P.second P./ P.mega parsec" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 49 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "I included the explicit type to make things a bit more readable - the\n", "inferred value is \n", "`Dimensional DUnit (Dim Z Z (N (S Z)) Z Z Z Z) Double` - and also to ensure that the equation I used is sensible (i.e. that\n", "the units are correct). As an example, if I had missed out the \"per second\"\n", "part then I would have got the following error (I promise that you *can*, \n", "with enough experience, work out what's going on from these error messages):" ] }, { "cell_type": "code", "collapsed": false, "input": [ "wrongConstant :: P.Unit P.DFrequency Double\n", "wrongConstant = P.kilo P.meter P./ P.mega parsec" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "Couldn't match type \u2018Numeric.NumType.TF.Z\u2019 with \u2018Numeric.NumType.TF.N Numeric.NumType.TF.Pos1\u2019
Expected type: P.Unit P.DFrequency Double
Actual type: Numeric.Units.Dimensional.TF.Dimensional P.DUnit (P.Div P.DLength P.DLength) Double
In the expression: P.kilo P.meter P./ P.mega parsec
In an equation for \u2018wrongConstant\u2019: wrongConstant = P.kilo P.meter P./ P.mega parsec
" ], "metadata": {}, "output_type": "display_data", "text": [ "Couldn't match type \u2018Numeric.NumType.TF.Z\u2019 with \u2018Numeric.NumType.TF.N Numeric.NumType.TF.Pos1\u2019\n", "Expected type: P.Unit P.DFrequency Double\n", " Actual type: Numeric.Units.Dimensional.TF.Dimensional P.DUnit (P.Div P.DLength P.DLength) Double\n", "In the expression: P.kilo P.meter P./ P.mega parsec\n", "In an equation for \u2018wrongConstant\u2019: wrongConstant = P.kilo P.meter P./ P.mega parsec" ] } ], "prompt_number": 50 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "When the `hubbleConstant` is applied to a value, the result has a type of `Quantity DFrequency Double`,\n", "as shown below:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "70 P.*~ hubbleConstant" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "2.268545194929607e-18 s^-1" ] } ], "prompt_number": 51 }, { "cell_type": "code", "collapsed": false, "input": [ ":type 70 P.*~ hubbleConstant" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "70 P.*~ hubbleConstant :: Quantity DFrequency Double" ], "metadata": {}, "output_type": "display_data", "text": [ "70 P.*~ hubbleConstant :: Quantity DFrequency Double" ] } ], "prompt_number": 52 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "Since I will want to use this type a few times below, I define an alias for it, using the `type`\n", "command. After this, I can use `HConstant` to simplify signatures of functions that take\n", "a Hubble constant as a parameter; I also add in a `Length` alias, since this will also\n", "come in useful." ] }, { "cell_type": "code", "collapsed": false, "input": [ "type HConstant = P.Quantity P.DFrequency Double\n", "type Length = P.Quantity P.DLength Double" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 53 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "With these definitions I should now be able to replicate the angular-diameter\n", "calculation, but this time tracking the types as well. As a reminder, I want:\n", "\n", " f om z = let ol = 1 - om\n", " in 1 / sqrt( (1+z)^2 * (1 + om*z) - (2+z) * ol * z )\n", " \n", " daH om z = let fs = result (absolute 1.0e-6 (trap (f om) 0 z))\n", " in fs / (1+z)\n", " \n", " da om h0 z = daH om z * c / h0\n", " where\n", " c = 299792.458\n", " \n", " adist om h0 z angle = 1000 * d * a\n", " where\n", " a = pi * angle / (180 * 60)\n", " d = da om h0 z\n", " \n", "Since `f` and `daH` are dimensionless, I'll just re-use them and concentrate on\n", "`da` and `adist`." ] }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "There are four main points in the conversion of `da`:\n", "\n", " 1. I added a signature to ensure that the Hubble constant has the required\n", " units. This trades off generic behavior (accepting any quantity)\n", " against some measure of type safety (in the sense that I want to make\n", " sure the second argument has the units of Hubble's constant). The\n", " results below would not change if I did not give the signature.\n", " \n", " 2. I have *not* had to convert `c` from `m/s` to `km/s` (or, equivalently,\n", " explictly convert the Hubble constant to account for the use of both\n", " `km` and `Mpc`).\n", " \n", " 3. The value returned by `daH` is converted to a dimensionless quantity\n", " by \"multiplying\" it by `one`.\n", " \n", " 4. The result does not look too different from the original form." ] }, { "cell_type": "code", "collapsed": false, "input": [ "da2 :: Double -> HConstant -> Double -> Length \n", "da2 om h0 z = (daH om z P.*~ P.one) P.* c P./ h0" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 54 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "The conversion for `adist` is also simple." ] }, { "cell_type": "code", "collapsed": false, "input": [ "adist2 om h0 z angle = da2 om h0 z P.* angle" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 55 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "Since I did not give a type signature, the inferred version does not\n", "require that the angle be dimensionless (shown by the fact that `d'`,\n", "which represents the dimensions of the value, \n", "is listed as `forall` rather than being an explicit value):" ] }, { "cell_type": "code", "collapsed": false, "input": [ ":type adist2" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "adist2 :: forall d'. Double -> HConstant -> Double -> Dimensional DQuantity d' Double -> Dimensional DQuantity (Mul DLength d') Double" ], "metadata": {}, "output_type": "display_data", "text": [ "adist2 :: forall d'. Double -> HConstant -> Double -> Dimensional DQuantity d' Double -> Dimensional DQuantity (Mul DLength d') Double" ] } ], "prompt_number": 56 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "I can fix this by explicitly specifying that the fourth argument is an angle by saying" ] }, { "cell_type": "code", "collapsed": false, "input": [ "adist3 :: Double -> HConstant -> Double -> P.Quantity P.DPlaneAngle Double -> Length\n", "adist3 = adist2" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 57 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "The difference in type signatures can be seen if I give a non-angle value for the\n", "last argument: the `adist2` form returns a value whereas the `adist3`\n", "version raises an error:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "adist2 0.3 (70 P.*~ hubbleConstant) 2 (1 P.*~ P.meter)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "5.327795482012604e25 m^2" ] } ], "prompt_number": 58 }, { "cell_type": "code", "collapsed": false, "input": [ "adist3 0.3 (70 P.*~ hubbleConstant) 2 (1 P.*~ P.meter)" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "Couldn't match type \u2018Numeric.NumType.TF.S Numeric.NumType.TF.Z\u2019 with \u2018Numeric.NumType.TF.Z\u2019
Expected type: P.Unit P.DPlaneAngle Double
Actual type: P.Unit P.DLength Double
In the second argument of \u2018(P.*~)\u2019, namely \u2018P.meter\u2019
In the fourth argument of \u2018adist3\u2019, namely \u2018(1 P.*~ P.meter)\u2019
" ], "metadata": {}, "output_type": "display_data", "text": [ "Couldn't match type \u2018Numeric.NumType.TF.S Numeric.NumType.TF.Z\u2019 with \u2018Numeric.NumType.TF.Z\u2019\n", "Expected type: P.Unit P.DPlaneAngle Double\n", " Actual type: P.Unit P.DLength Double\n", "In the second argument of \u2018(P.*~)\u2019, namely \u2018P.meter\u2019\n", "In the fourth argument of \u2018adist3\u2019, namely \u2018(1 P.*~ P.meter)\u2019" ] } ], "prompt_number": 59 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "With this, I can repeat the earlier calculation, which gave\n", "\n", " adist 0.3 70 2 (1/60) = 8.370893333112804" ] }, { "cell_type": "code", "collapsed": false, "input": [ "adist3 0.3 (70 P.*~ hubbleConstant) 2 (1 P.*~ P.secondOfArc)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "2.5829881398332853e20 m" ] } ], "prompt_number": 60 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "Oops: I want the answer in `kpc`:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "adist3 0.3 (70 P.*~ hubbleConstant) 2 (1 P.*~ P.secondOfArc) P./~ P.kilo parsec" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "8.370893333112805" ] } ], "prompt_number": 61 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "It is easy to change units, such as to use an angle in arcminutes and to convert\n", "the answer to `Mpc`:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "adist3 0.3 (70 P.*~ hubbleConstant) 2 (2.4 P.*~ P.minuteOfArc) P./~ P.mega parsec" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "1.2054086399682438" ] } ], "prompt_number": 62 }, { "cell_type": "heading", "level": 2, "metadata": { "hidden": false }, "source": [ "Finishing it all off: luminosities" ] }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "The luminosity of a source is equal to $4 \\pi d_l^2 f_x$, where $d_l$ is the \n", "distance to the source and $f_x$ its flux (this assumes a bolometric flux, since it ignores the\n", "[K correction](http://en.wikipedia.org/wiki/K_correction)).\n", "\n", "For a source at a cosmological distance, the distance used is the\n", "[luminosity distance](http://en.wikipedia.org/wiki/Luminosity_distance) - which is why I\n", "used the subscript $l$ - and it is defined as\n", "\n", "$$d_l = (1+z) d_c$$" ] }, { "cell_type": "code", "collapsed": false, "input": [ "dlH om z = let fs = result (absolute 1.0e-6 (trap (f om) 0 z))\n", " in fs * (1+z)\n", " \n", "dl :: Double -> HConstant -> Double -> Length\n", "dl om h0 z = (dlH om z P.*~ P.one) P.* c P./ h0" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 63 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "Trying it out, I get (using the same Cosmology as previously) a luminosity distance to an object at $z=2$\n", "of:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "dl 0.3 (70 P.*~ hubbleConstant) 2" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "4.7950159338113435e26 m" ] } ], "prompt_number": 64 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "which, in Astronomer friendly units, is:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "dl 0.3 (70 P.*~ hubbleConstant) 2 P./~ P.mega parsec" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "15539.586223226608" ] } ], "prompt_number": 65 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "The [CosmoCalc](http://www.astro.ucla.edu/~wright/CosmoCalc.html) page returns \n", "15537.0 Mpc or 50.675 Gly. Now, can I easily convert the answer above to light years?\n", "\n", "For this, I need to import the `year` value from the \"non SI\" module of `dimensional-tf`" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import Numeric.Units.Dimensional.TF.NonSI (year)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 66 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "and use it - together with the definition of `c` above, to calculate the distance\n", "light travels in a year. I am not happy with the following, as I calculate a value \n", "in meters, remove the meters, then make it into a unit of meters, but I couldn't\n", "see how else to convert a `Quantity` - which is what `c * year` is - into\n", "a `Unit`." ] }, { "cell_type": "code", "collapsed": false, "input": [ "lightYear = let d = c P.* (1 P.*~ year) P./~ P.meter\n", " in P.prefix d P.meter" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 67 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "With this, I can convert the distance into light years to compare to the `cosmoCalc` value:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "dl 0.3 (70 P.*~ hubbleConstant) 2 P./~ P.giga lightYear" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "50.6833584119991" ] } ], "prompt_number": 68 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "Finally it's time to create a function that converts a flux to a luminosity:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "luminosity :: Double -> HConstant -> Double -> P.Irradiance Double -> P.RadiantIntensity Double\n", "luminosity om h0 z flux = P._4 P.* P.pi P.* d P.* d P.* flux\n", " where\n", " d = dl om h0 z" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 69 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "As an X-ray astronomer I'm used to working in units of `ergs` (please don't ask why),\n", "so I create a few units built on this: one for flux and one for luminosity." ] }, { "cell_type": "code", "collapsed": false, "input": [ "erg = P.prefix (1.0e-7::Double) P.joule\n", "\n", "ergLum = erg P./ P.second\n", "\n", "ergFlux = ergLum P./ P.square (P.centi P.meter)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 70 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "With these I can finally calculate the luminosity of a source ($z=2$, $\\Omega_m=0.3$, $\\Omega_\\lambda=0.7$,\n", "$H_0 = 70\\ {\\rm km/s/Mpc}$) which has a flux of $2 \\times 10^{-13}\\ {\\rm erg/cm^{2}/s}$." ] }, { "cell_type": "code", "collapsed": false, "input": [ "lum = luminosity 0.3 (70 P.*~ hubbleConstant) 2 (2e-13 P.*~ ergFlux)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 71 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "This value is" ] }, { "cell_type": "code", "collapsed": false, "input": [ "lum" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "5.7785645507043004e38 m^2 kg s^-3" ] } ], "prompt_number": 72 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "which can be converted to several units; such as ${\\rm Watts}$" ] }, { "cell_type": "code", "collapsed": false, "input": [ "lum P./~ P.watt" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "5.7785645507043004e38" ] } ], "prompt_number": 73 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "which is - unsurprisingly - the same numerical value, or $\\rm{erg / cm^2 / s}$, \n", "which has a numerical value $10^7$ times this:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "lum P./~ ergLum" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "5.778564550704301e45" ] } ], "prompt_number": 74 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "Don't forget that this would need some form of K-correction, unless you have a bolometric flux measurement." ] }, { "cell_type": "heading", "level": 2, "metadata": { "hidden": false }, "source": [ "Just one more thing ..." ] }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "Whilst I have your attention, how about calculating the \n", "[critical density](http://en.wikipedia.org/wiki/Friedmann_equations#Density_parameter)\n", "of the Universe, which is defined as:\n", "\n", "$$\\rho_{\\rm crit} = \\frac{3 H^2}{8 \\pi G}$$\n", "\n", "For this, I need a value for the \n", "[Gravitational constant](), \n", "where the `meter ^ pos3` and `second ^ pos2` terms represent $m^3$ and $s^2$\n", "respectively." ] }, { "cell_type": "code", "collapsed": false, "input": [ "bigG = 6.67428e-11 P.*~ (P.metre P.^ P.pos3 P./ P.kilo P.gram P./ P.second P.^ P.pos2)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 75 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "With this, I can write out the equation to calculate the critical density:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "criticalDensity h = P._3 P.* h P.^ P.pos2 P./ (P._8 P.* P.pi P.* bigG)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 76 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "Applying this to $H_0 = 70\\ {\\rm km/s/Mpc}$ gives:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "c70 = criticalDensity (70 P.*~ hubbleConstant)\n", "c70" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "9.203899006458818e-27 m^-3 kg" ] } ], "prompt_number": 77 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "which I can convert to other units, such as metric tons per cubed Astronomical Unit:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "c70 P./~ (P.kilo P.metricTon P./ P.astronomicalUnit P.^ P.pos3)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "display_data", "text": [ "30.814000174159247" ] } ], "prompt_number": 78 }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "The use of the `P.` prefix is definitely distracting here. The way I've used the `dimensional`\n", "package in the past is to have most of the code in a module, and in that module\n", "I have imported the module directly. This means that the code can be written in\n", "the more-natural form\n", "\n", " criticalDensity h = _3 * h ^ pos2 / (_8 * pi * bigG)\n", "\n", "The downside to this is that normal arithmetic becomes more verbose." ] }, { "cell_type": "heading", "level": 2, "metadata": { "hidden": false }, "source": [ "The end" ] }, { "cell_type": "markdown", "metadata": { "hidden": false }, "source": [ "There you go; I hope you enjoyed it. If you have any questions, then please use the\n", "[GitHub issues page](https://github.com/DougBurke/astro-haskell/issues) or\n", "contact me on Twitter at\n", "." ] } ], "metadata": {} } ] }