Field Basics
using CMBLensingBase 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 Matrix{Float64}:
0.670686 0.165992
0.982491 0.225331f = FlatMap(Ix)4-element 2×2-pixel 1.0′-resolution LambertMap{Array{Float64, 2}}:
0.6706859834487535
0.982490568082506
0.16599180688467818
0.22533066250450406When 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 AbstractVectortrueThe data itself, however, is still stored as the original 2x2 matrix, and can be accessed as follows,
f.Ix2×2 view(::Matrix{Float64}, :, :) with eltype Float64:
0.670686 0.165992
0.982491 0.225331But since Fields are vectors, they can be tranposed,
f'1×4 adjoint(::LambertMap{Array{Float64, 2}}) with eltype Float64:
0.670686 0.982491 0.165992 0.225331inner products can be computed,
f' * f1.4934345921832661and they can be added with each other as well as multiplied by scalars,
2*f+f4-element 2×2-pixel 1.0′-resolution LambertMap{Array{Float64, 2}}:
2.012057950346261
2.9474717042475183
0.49797542065403455
0.6759919875135122Diagonal 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, BaseField{Map, ProjLambert{Float64, Vector{Float64}, Matrix{Float64}}, Float64, Matrix{Float64}}}:
0.670686 ⋅ ⋅ ⋅
⋅ 0.982491 ⋅ ⋅
⋅ ⋅ 0.165992 ⋅
⋅ ⋅ ⋅ 0.225331Multiplying this operator by the original map is then a matrix-vector product:
Diagonal(f) * f4-element 2×2-pixel 1.0′-resolution LambertMap{Array{Float64, 2}}:
0.44981968839462166
0.9652877163710853
0.027553279952840296
0.050773907464718715Note that this is also equal to the the pointwise multiplication of f with itself:
f .* f4-element 2×2-pixel 1.0′-resolution LambertMap{Array{Float64, 2}}:
0.44981968839462166
0.9652877163710853
0.027553279952840296
0.050773907464718715Field 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{BaseField{Map, ProjLambert{Float64, Vector{Float64}, Matrix{Float64}}, Float64, Matrix{Float64}}, BaseField{Map, ProjLambert{Float64, Vector{Float64}, Matrix{Float64}}, Float64, Matrix{Float64}}}:
0.12180591647943195
0.7899381176219488
0.5292679219634888
0.5222792736386405
0.9667861030776298
0.8961648877855011
0.5251448431103856
0.7809086589730572The components can also have names:
ft = FieldTuple(a=a, b=b)8-element Field-(a,b)-Tuple{BaseField{Map, ProjLambert{Float64, Vector{Float64}, Matrix{Float64}}, Float64, Matrix{Float64}}, BaseField{Map, ProjLambert{Float64, Vector{Float64}, Matrix{Float64}}, Float64, Matrix{Float64}}}:
0.12180591647943195
0.7899381176219488
0.5292679219634888
0.5222792736386405
0.9667861030776298
0.8961648877855011
0.5251448431103856
0.7809086589730572which can be accessed later:
ft.a4-element 2×2-pixel 1.0′-resolution LambertMap{Array{Float64, 2}}:
0.12180591647943195
0.7899381176219488
0.5292679219634888
0.5222792736386405FieldTuples 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 FieldTuplefalseField 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:
f4-element 2×2-pixel 1.0′-resolution LambertMap{Array{Float64, 2}}:
0.6706859834487535
0.982490568082506
0.16599180688467818
0.22533066250450406g = Fourier(f)4-element 2×2-pixel 1.0′-resolution LambertFourier{Array{ComplexF64, 2}}:
2.044499020920442 + 0.0im
-0.37114344025357837 + 0.0im
1.2618540821420772 + 0.0im
-0.2524657290139266 + 0.0imBasis conversion is usually done automatically for you. E.g. here f′ is automatically converted to a FlatMap before addition:
f + g4-element 2×2-pixel 1.0′-resolution LambertMap{Array{Float64, 2}}:
1.341371966897507
1.964981136165012
0.3319836137693564
0.4506613250090082A key feature of Diagonal operators is they convert the field they are acting on to the right basis before multiplication:
Diagonal(f) * g4-element 2×2-pixel 1.0′-resolution LambertMap{Array{Float64, 2}}:
0.44981968839462166
0.9652877163710853
0.027553279952840303
0.05077390746471873A FlatMap times a FlatFourier doesn't have a natural linear algebra meaning so its an error:
f * gMethodError: no method matching *(::BaseField{Map, ProjLambert{…}, Float64, Matrix{…}}, ::BaseField{Fourier, ProjLambert{…}, ComplexF64, Matrix{…}})
Closest candidates are:
*(::Any, ::Any, ::Any, ::Any...)
@ Base operators.jl:587
*(::LazyBinaryOp{+}, ::Field)
@ CMBLensing ~/CMBLensing/src/specialops.jl:402
*(::LazyBinaryOp{-}, ::Field)
@ CMBLensing ~/CMBLensing/src/specialops.jl:402
...
Stacktrace:
[1] top-level scope
@ In[21]:1Properties and indices
FlatMap and FlatFourier can be indexed directly like arrays. If given 1D indices, this is the index into the vector representation:
f4-element 2×2-pixel 1.0′-resolution LambertMap{Array{Float64, 2}}:
0.6706859834487535
0.982490568082506
0.16599180688467818
0.22533066250450406f[1], f[2], f[3], f[4](0.6706859834487535, 0.982490568082506, 0.16599180688467818, 0.22533066250450406)f[5]BoundsError: attempt to access 2×2 Matrix{Float64} at index [5]
Stacktrace:
[1] getindex
@ ./essentials.jl:13 [inlined]
[2] getindex(f::BaseField{Map, ProjLambert{Float64, Vector{Float64}, Matrix{Float64}}, Float64, Matrix{Float64}}, I::Int64)
@ CMBLensing ~/CMBLensing/src/base_fields.jl:36
[3] top-level scope
@ In[24]:1Or with a 2D index, this indexes directly into the 2D map:
f[1,1], f[2,1], f[1,2], f[2,2](0.6706859834487535, 0.982490568082506, 0.16599180688467818, 0.22533066250450406)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:
ft8-element Field-(a,b)-Tuple{BaseField{Map, ProjLambert{Float64, Vector{Float64}, Matrix{Float64}}, Float64, Matrix{Float64}}, BaseField{Map, ProjLambert{Float64, Vector{Float64}, Matrix{Float64}}, Float64, Matrix{Float64}}}:
0.12180591647943195
0.7899381176219488
0.5292679219634888
0.5222792736386405
0.9667861030776298
0.8961648877855011
0.5251448431103856
0.7809086589730572ft[1]4-element 2×2-pixel 1.0′-resolution LambertMap{Array{Float64, 2}}:
0.12180591647943195
0.7899381176219488
0.5292679219634888
0.5222792736386405ft[2]4-element 2×2-pixel 1.0′-resolution LambertMap{Array{Float64, 2}}:
0.9667861030776298
0.8961648877855011
0.5251448431103856
0.7809086589730572ft[3]BoundsError: attempt to access @NamedTuple{a::BaseField{Map, ProjLambert{Float64, Vector{Float64}, Matrix{Float64}}, Float64, Matrix{Float64}}, b::BaseField{Map, ProjLambert{Float64, Vector{Float64}, Matrix{Float64}}, Float64, Matrix{Float64}}} at index [3]
Stacktrace:
[1] getindex
@ ./namedtuple.jl:167 [inlined]
[2] getindex(f::FieldTuple{@NamedTuple{…}, CMBLensing.BasisProd{…}, Float64}, i::Int64)
@ CMBLensing ~/CMBLensing/src/field_tuples.jl:33
[3] top-level scope
@ In[29]:1
Some type information was truncated. Use `show(err)` to see complete types.To get the underlying data arrays, use the object's properties:
f.Ix2×2 view(::Matrix{Float64}, :, :) with eltype Float64:
0.670686 0.165992
0.982491 0.225331You 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.Q4-element 2×2-pixel 1.0′-resolution LambertMap{SubArray{Float64, 2, Array{Float64, 3}, Tuple{Base.Slice{Base.OneTo{Int64}}, Base.Slice{Base.OneTo{Int64}}, Int64}, true}}:
0.12180591647943195
0.7899381176219488
0.5292679219634888
0.5222792736386405Or fqu.Qx which is shorthand for fqu.Q.Ix:
fqu.Q.Ix === fqu.QxtrueIf you convert f to Fourier space, it would have the Il property to get the Fourier coefficients of the $I$ component:
Fourier(f).Il2×2 view(::Matrix{ComplexF64}, :, :) with eltype ComplexF64:
2.0445+0.0im 1.26185+0.0im
-0.371143+0.0im -0.252466+0.0imFor convenience, you can index fields with brackets [] and any necessary conversions will be done automatically:
f[:Il]2×2 view(::Matrix{ComplexF64}, :, :) with eltype ComplexF64:
2.0445+0.0im 1.26185+0.0im
-0.371143+0.0im -0.252466+0.0imThis 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{ComplexF64, 3}, :, :, 1) with eltype ComplexF64:
-1.96329-0.0im 0.139803+0.0im
-0.661144+0.0im -0.326385+0.0imThe 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 specialFieldTupleslikeFlatQUMap, 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.