/
angular diameter distance.ipynb
2330 lines (2330 loc) · 72.5 KB
/
angular diameter distance.ipynb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
"metadata": {
"language": "haskell",
"name": "",
"signature": "sha256:c13fcd3a07400ad7f14adee05669e961684e07ccd3e72fa68a299e89e8a1edfa"
},
"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: <https://twitter.com/doug_burke>."
]
},
{
"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": []
},
{
"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": []
},
{
"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": []
},
{
"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": []
},
{
"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": []
},
{
"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": []
},
{
"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": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"g 1.2"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"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": []
},
{
"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": []
},
{
"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": []
},
{
"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": []
},
{
"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": []
},
{
"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": []
},
{
"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": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"absolute 1.0e-6 (trap g 0 2)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"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": []
},
{
"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": []
},
{
"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": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"take 3 (simpson g 0 2)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"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": []
},
{
"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": []
},
{
"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": []
},
{
"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": []
},
{
"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": []
},
{
"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."