# 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 Matrix{Float64}:
0.287145  0.997093
0.102938  0.775643
f = FlatMap(Ix)
4-element 2×2-pixel 1.0′-resolution LambertMap{Array{Float64, 2}}:
0.28714518682097934
0.10293834258407608
0.9970927110615834
0.7756430887202712

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 view(::Matrix{Float64}, :, :) with eltype Float64:
0.287145  0.997093
0.102938  0.775643

But since Fields are vectors, they can be tranposed,

f'
1×4 adjoint(::LambertMap{Array{Float64, 2}}) with eltype Float64:
0.287145  0.102938  0.997093  0.775643

inner products can be computed,

f' * f
1.6888647362200724

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 LambertMap{Array{Float64, 2}}:
0.861435560462938
0.30881502775222824
2.99127813318475
2.3269292661608136

## 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, BaseField{Map, ProjLambert{Float64, Vector{Float64}, Matrix{Float64}}, Float64, Matrix{Float64}}}:
0.287145   ⋅         ⋅         ⋅
⋅        0.102938   ⋅         ⋅
⋅         ⋅        0.997093   ⋅
⋅         ⋅         ⋅        0.775643

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

Diagonal(f) * f
4-element 2×2-pixel 1.0′-resolution LambertMap{Array{Float64, 2}}:
0.08245235831445513
0.01059630237395661
0.9941938744521381
0.6016222010795225

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 LambertMap{Array{Float64, 2}}:
0.08245235831445513
0.01059630237395661
0.9941938744521381
0.6016222010795225

## 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{BaseField{Map, ProjLambert{Float64, Vector{Float64}, Matrix{Float64}}, Float64, Matrix{Float64}}, BaseField{Map, ProjLambert{Float64, Vector{Float64}, Matrix{Float64}}, Float64, Matrix{Float64}}}:
0.9926399933502194
0.9012631042579775
0.952204827317816
0.9834795087243372
0.574978288371474
0.5452000220863134
0.8197091716086747
0.3042501306721159

The 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.9926399933502194
0.9012631042579775
0.952204827317816
0.9834795087243372
0.574978288371474
0.5452000220863134
0.8197091716086747
0.3042501306721159

which can be accessed later:

ft.a
4-element 2×2-pixel 1.0′-resolution LambertMap{Array{Float64, 2}}:
0.9926399933502194
0.9012631042579775
0.952204827317816
0.9834795087243372

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

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 LambertMap{Array{Float64, 2}}:
0.28714518682097934
0.10293834258407608
0.9970927110615834
0.7756430887202712
g = Fourier(f)
4-element 2×2-pixel 1.0′-resolution LambertFourier{Array{ComplexF64, 2}}:
2.16281932918691 + 0.0im
0.4056564665782154 + 0.0im
-1.382652270376799 + 0.0im
-0.0372427781044089 + 0.0im

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

f + g
4-element 2×2-pixel 1.0′-resolution LambertMap{Array{Float64, 2}}:
0.5742903736419587
0.20587668516815222
1.9941854221231665
1.5512861774405424

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

Diagonal(f) * g
4-element 2×2-pixel 1.0′-resolution LambertMap{Array{Float64, 2}}:
0.08245235831445515
0.010596302373956618
0.994193874452138
0.6016222010795225

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

f * g
MethodError: no method matching *(::BaseField{Map, ProjLambert{Float64, Vector{Float64}, Matrix{Float64}}, Float64, Matrix{Float64}}, ::BaseField{Fourier, ProjLambert{Float64, Vector{Float64}, Matrix{Float64}}, ComplexF64, Matrix{ComplexF64}})

Closest candidates are:
*(::Any, ::Any, ::Any, ::Any...)
@ Base operators.jl:578
*(::StaticArraysCore.StaticArray{Tuple{2}, F, 1} where F<:Union{Field, FieldOp}, ::Field)
@ CMBLensing ~/CMBLensing/src/field_vectors.jl:32
*(::Union{StaticArraysCore.StaticArray{Tuple{2}, F, 1}, StaticArraysCore.StaticArray{Tuple{2, 2}, F, 2}, LinearAlgebra.Adjoint{<:Any, <:StaticArraysCore.StaticArray{Tuple{2}, F, 1}}} where F<:Union{Field, FieldOp}, ::Field)
@ CMBLensing ~/CMBLensing/src/field_vectors.jl:24
...

Stacktrace:

 top-level scope

@ In:1

## 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 LambertMap{Array{Float64, 2}}:
0.28714518682097934
0.10293834258407608
0.9970927110615834
0.7756430887202712
f, f, f, f
(0.28714518682097934, 0.10293834258407608, 0.9970927110615834, 0.7756430887202712)
f
BoundsError: attempt to access 2×2 Matrix{Float64} at index 

Stacktrace:

 getindex

@ ./essentials.jl:13 [inlined]

 getindex(f::BaseField{Map, ProjLambert{Float64, Vector{Float64}, Matrix{Float64}}, Float64, Matrix{Float64}}, I::Int64)

@ CMBLensing ~/CMBLensing/src/base_fields.jl:36

 top-level scope

@ In:1

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.28714518682097934, 0.10293834258407608, 0.9970927110615834, 0.7756430887202712)

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{BaseField{Map, ProjLambert{Float64, Vector{Float64}, Matrix{Float64}}, Float64, Matrix{Float64}}, BaseField{Map, ProjLambert{Float64, Vector{Float64}, Matrix{Float64}}, Float64, Matrix{Float64}}}:
0.9926399933502194
0.9012631042579775
0.952204827317816
0.9834795087243372
0.574978288371474
0.5452000220863134
0.8197091716086747
0.3042501306721159
ft
4-element 2×2-pixel 1.0′-resolution LambertMap{Array{Float64, 2}}:
0.9926399933502194
0.9012631042579775
0.952204827317816
0.9834795087243372
ft
4-element 2×2-pixel 1.0′-resolution LambertMap{Array{Float64, 2}}:
0.574978288371474
0.5452000220863134
0.8197091716086747
0.3042501306721159
ft
BoundsError: attempt to access NamedTuple{(: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}}}} at index 

Stacktrace:

 getindex

@ ./namedtuple.jl:136 [inlined]

 getindex(f::FieldTuple{NamedTuple{(: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}}}}, CMBLensing.BasisProd{Tuple{Map, Map}}, Float64}, i::Int64)

@ CMBLensing ~/CMBLensing/src/field_tuples.jl:33

 top-level scope

@ In:1

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

f.Ix
2×2 view(::Matrix{Float64}, :, :) with eltype Float64:
0.287145  0.997093
0.102938  0.775643

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 LambertMap{SubArray{Float64, 2, Array{Float64, 3}, Tuple{Base.Slice{Base.OneTo{Int64}}, Base.Slice{Base.OneTo{Int64}}, Int64}, true}}:
0.9926399933502194
0.9012631042579775
0.952204827317816
0.9834795087243372

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 view(::Matrix{ComplexF64}, :, :) with eltype ComplexF64:
2.16282+0.0im    -1.38265+0.0im
0.405656+0.0im  -0.0372428+0.0im

For 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.16282+0.0im    -1.38265+0.0im
0.405656+0.0im  -0.0372428+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{ComplexF64, 3}, :, :, 1) with eltype ComplexF64:
-3.82959-0.0im  0.0417812+0.0im
0.0601022+0.0im   0.485681+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.