Field Basics

using CMBLensing

Base Fields

The basic building blocks of CMBLensing.jl are CMB "fields", like temperature, Q or U polarization, or the lensing potential $\phi$. These types are all encompassed by the abstract type Field, with some concrete examples including FlatMap for a flat-sky map projection, or FlatQUMap for Q/U polarization, etc...

Flat fields are just thin wrappers around Julia arrays, e.g.

Ix = rand(2,2)
2×2 Array{Float64,2}:
 0.769888  0.342967
 0.629558  0.176974
f = FlatMap(Ix)
4-element 2×2-pixel 1.0′-resolution FlatMap{Array{Float64,2},ProjLambert{Float64}}:
 0.769888483070007
 0.629558281579595
 0.34296742929464474
 0.1769738495604145

When displayed, you can see the pixels in the 2x2 map have been splayed out into a length-4 array. This is intentional, as even though the maps themselves are two-dimensional, it is extremely useful conceptually to think of fields as vectors (which they are, in fact, as they form an abstract vector space). This tie to vector spaces is deeply rooted in CMBLensing, to the extent that Field objects are a subtype of Julia's own AbstractVector type,

f isa AbstractVector
true

The data itself, however, is still stored as the original 2x2 matrix, and can be accessed as follows,

f.Ix
2×2 Array{Float64,2}:
 0.769888  0.342967
 0.629558  0.176974

But since Fields are vectors, they can be tranposed,

f'
1×4 LinearAlgebra.Adjoint{Float64,FlatMap{Array{Float64,2},ProjLambert{Float64}}}:
 0.769888  0.629558  0.342967  0.176974

inner products can be computed,

f' * f
1.1380183072544985

and they can be added with each other as well as multiplied by scalars,

2*f+f
4-element 2×2-pixel 1.0′-resolution FlatMap{Array{Float64,2},ProjLambert{Float64}}:
 2.309665449210021
 1.888674844738785
 1.0289022878839342
 0.5309215486812435

Diagonal operators

Vector spaces have linear operators which act on the vectors. Linear operators correpsond to matrices, thus for a map with $N$ total pixels, a general linear operator would be an $N$-by-$N$ matrix, which for even modest map sizes becomes far too large to actually store. Thus, an important class of linear operators are ones which are diagonal, since these can actually be stored. CMBLensing uses Julia's builtin Diagonal to represent these. Diagonal(f) takes a vector f and puts it on the diagonal of the matrix:

Diagonal(f)
4×4 Diagonal{Float64,FlatMap{Array{Float64,2},ProjLambert{Float64}}}:
 0.769888   ⋅         ⋅         ⋅ 
  ⋅        0.629558   ⋅         ⋅ 
  ⋅         ⋅        0.342967   ⋅ 
  ⋅         ⋅         ⋅        0.176974

Multiplying this operator by the original map is then a matrix-vector product:

Diagonal(f) * f
4-element 2×2-pixel 1.0′-resolution FlatMap{Array{Float64,2},ProjLambert{Float64}}:
 0.5927282763638365
 0.3963436299054527
 0.11762665755697714
 0.031319743428232225

Note that this is also equal to the the pointwise multiplication of f with itself:

f .* f
4-element 2×2-pixel 1.0′-resolution FlatMap{Array{Float64,2},ProjLambert{Float64}}:
 0.5927282763638365
 0.3963436299054527
 0.11762665755697714
 0.031319743428232225

Field Tuples

You can put Fields together into tuples. For example,

a = FlatMap(rand(2,2))
b = FlatMap(rand(2,2));
FieldTuple(a,b)
8-element Field-2-Tuple{FlatMap{Array{Float64,2},ProjLambert{Float64}},FlatMap{Array{Float64,2},ProjLambert{Float64}}}:
 0.26848504129807793
 0.03871711653728793
 0.3677618131942122
 0.6745663664398445
 0.4830710944734635
 0.03811316207120319
 0.20283483103023525
 0.17628964128166547

The components can also have names:

ft = FieldTuple(a=a, b=b)
8-element Field-(a,b)-Tuple{FlatMap{Array{Float64,2},ProjLambert{Float64}},FlatMap{Array{Float64,2},ProjLambert{Float64}}}:
 0.26848504129807793
 0.03871711653728793
 0.3677618131942122
 0.6745663664398445
 0.4830710944734635
 0.03811316207120319
 0.20283483103023525
 0.17628964128166547

which can be accessed later:

ft.a
4-element 2×2-pixel 1.0′-resolution FlatMap{Array{Float64,2},ProjLambert{Float64}}:
 0.26848504129807793
 0.03871711653728793
 0.3677618131942122
 0.6745663664398445

FieldTuples have all of the same behavior of individual fields. Indeed, spin fields like QU or IQU are simply special FieldTuples:

fqu = FlatQUMap(a,b)
fqu isa FieldTuple
false

Field Vectors

in progress

Basis Conversion

All fields are tagged as to which basis they are stored in. You can convert them to other bases by calling the basis type on them:

f
4-element 2×2-pixel 1.0′-resolution FlatMap{Array{Float64,2},ProjLambert{Float64}}:
 0.769888483070007
 0.629558281579595
 0.34296742929464474
 0.1769738495604145
f′ = Fourier(f)
4-element 2×2-pixel 1.0′-resolution FlatFourier{Array{Complex{Float64},2},ProjLambert{Float64}}:
    1.9193880435046613 + 0.0im
    0.3063237812246422 + 0.0im
    0.8795054857945428 + 0.0im
 -0.025663378243818258 + 0.0im

Basis conversion is usually done automatically for you. E.g. here f′ is automatically converted to a FlatMap before addition:

f + f′
4-element 2×2-pixel 1.0′-resolution FlatMap{Array{Float64,2},ProjLambert{Float64}}:
 1.539776966140014
 1.25911656315919
 0.6859348585892895
 0.353947699120829

A key feature of Diagonal operators is they convert the field they are acting on to the right basis before multiplication:

Diagonal(f) * f′
4-element 2×2-pixel 1.0′-resolution FlatMap{Array{Float64,2},ProjLambert{Float64}}:
 0.5927282763638365
 0.3963436299054527
 0.11762665755697714
 0.031319743428232225

A FlatMap times a FlatFourier doesn't have a natural linear algebra meaning so its an error:

f * f′
MethodError: no method matching *(::FlatMap{Array{Float64,2},ProjLambert{Float64}}, ::FlatFourier{Array{Complex{Float64},2},ProjLambert{Float64}})
Closest candidates are:
  *(::Any, ::Any, ::Any, ::Any...) at operators.jl:538
  *(::LazyBinaryOp{^}, ::Field) at none:0
  *(::ChainRulesCore.DoesNotExist, ::Any) at /home/cosmo/.julia/packages/ChainRulesCore/7d1hl/src/differential_arithmetic.jl:28
  ...



Stacktrace:

 [1] top-level scope at In[21]:1

 [2] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091

Properties and indices

FlatMap and FlatFourier can be indexed directly like arrays. If given 1D indices, this is the index into the vector representation:

f
4-element 2×2-pixel 1.0′-resolution FlatMap{Array{Float64,2},ProjLambert{Float64}}:
 0.769888483070007
 0.629558281579595
 0.34296742929464474
 0.1769738495604145
f[1], f[2], f[3], f[4]
(0.769888483070007, 0.629558281579595, 0.34296742929464474, 0.1769738495604145)
f[5]
BoundsError: attempt to access 2×2 Array{Float64,2} at index [5]



Stacktrace:

 [1] getindex at ./array.jl:809 [inlined]

 [2] getindex(::FlatMap{Array{Float64,2},ProjLambert{Float64}}, ::Int64) at /home/cosmo/CMBLensing/src/base_fields.jl:28

 [3] top-level scope at In[24]:1

 [4] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091

Or with a 2D index, this indexes directly into the 2D map:

f[1,1], f[2,1], f[1,2], f[2,2]
(0.769888483070007, 0.629558281579595, 0.34296742929464474, 0.1769738495604145)

Note: there is no overhead to indexing f in this way as compared to working directly on the underlying array.

For other fields which are built on FieldTuples, 1D indexing will instead index the tuple indices:

ft
8-element Field-(a,b)-Tuple{FlatMap{Array{Float64,2},ProjLambert{Float64}},FlatMap{Array{Float64,2},ProjLambert{Float64}}}:
 0.26848504129807793
 0.03871711653728793
 0.3677618131942122
 0.6745663664398445
 0.4830710944734635
 0.03811316207120319
 0.20283483103023525
 0.17628964128166547
ft[1]
4-element 2×2-pixel 1.0′-resolution FlatMap{Array{Float64,2},ProjLambert{Float64}}:
 0.26848504129807793
 0.03871711653728793
 0.3677618131942122
 0.6745663664398445
ft[2]
4-element 2×2-pixel 1.0′-resolution FlatMap{Array{Float64,2},ProjLambert{Float64}}:
 0.4830710944734635
 0.03811316207120319
 0.20283483103023525
 0.17628964128166547
ft[3]
BoundsError: attempt to access NamedTuple{(:a, :b),Tuple{FlatMap{Array{Float64,2},ProjLambert{Float64}},FlatMap{Array{Float64,2},ProjLambert{Float64}}}}
  at index [3]



Stacktrace:

 [1] getindex at ./namedtuple.jl:112 [inlined]

 [2] getindex(::Field-(a,b)-Tuple{FlatMap{Array{Float64,2},ProjLambert{Float64}},FlatMap{Array{Float64,2},ProjLambert{Float64}}}, ::Int64) at /home/cosmo/CMBLensing/src/field_tuples.jl:31

 [3] top-level scope at In[29]:1

 [4] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091

To get the underlying data arrays, use the object's properties:

f.Ix
2×2 Array{Float64,2}:
 0.769888  0.342967
 0.629558  0.176974

You can always find out what properties are available by typing f.<Tab>. For example, if you typed ft then hit <Tab> you'd get:

ft |> propertynames
(:fs, :a, :b)

For a FieldTuple like the FlatQUMap object, fqu, you can get each individual Q or U field:

fqu.Q
4-element 2×2-pixel 1.0′-resolution FlatMap{SubArray{Float64,2,Array{Float64,3},Tuple{Base.Slice{Base.OneTo{Int64}},Base.Slice{Base.OneTo{Int64}},Int64},true},ProjLambert{Float64}}:
 0.26848504129807793
 0.03871711653728793
 0.3677618131942122
 0.6745663664398445

Or fqu.Qx which is shorthand for fqu.Q.Ix:

fqu.Q.Ix === fqu.Qx
true

If you convert f to Fourier space, it would have the Il property to get the Fourier coefficients of the $I$ component:

Fourier(f).Il
2×2 Array{Complex{Float64},2}:
  1.91939+0.0im    0.879505+0.0im
 0.306324+0.0im  -0.0256634+0.0im

For convenience, you can index fields with brackets [] and any necessary conversions will be done automatically:

f[:Il]
2×2 Array{Complex{Float64},2}:
  1.91939+0.0im    0.879505+0.0im
 0.306324+0.0im  -0.0256634+0.0im

This works between any bases. For example. fqu is originally QUMap but we can convert to EBFourier and get the El coefficients:

fqu[:El]
2×2 view(::Array{Complex{Float64},3}, :, :, 1) with eltype Complex{Float64}:
   -1.34953-0.0im   0.735126+0.0im
 -0.0770366+0.0im  -0.418413+0.0im

The general rule to keep in mind for these two ways of accessing the underlying data is:

  • Properties (i.e. f.Ix) are type-stable and get you the underlying data arrays, even recursively from special FieldTuples like FlatQUMap, etc... If these arrays are modified, they affect the original field.
  • Indices (i.e. f[:Ix]) are not type-stable, and may or may not be one of the underlying data arrays (because a basis conversion may have been performed). They should be used for getting (not setting) data, and in non-performance-critical code.