One of the fundamental building blocks of the NIFTy4 framework is the *domain*.

Its required capabilities are expressed by the abstract :class:`Domain` class.

A domain must be able to answer the following queries:

- its total number of data entries (pixels), which is accessible via the

:attr:`~Domain.size` property

- the shape of the array that is supposed to hold these data entries

(obtainable by means of the :attr:`~Domain.shape` property)

- equality comparison to another :class:`Domain` instance

Unstructured domains

--------------------

Domains can be either *structured* (i.e. there is geometrical information

associated with them, like position in space and volume factors),

or *unstructured* (meaning that the data points have no associated manifold).

Unstructured domains can be described by instances of NIFTy's

:class:`UnstructuredDomain` class.

Structured domains

------------------

In contrast to unstructured domains, these domains have an assigned geometry.

NIFTy requires them to provide the volume elements of their grid cells.

The additional methods are specified in the abstract class

:class:`StructuredDomain`:

- The attributes :attr:`~StructuredDomain.scalar_dvol`,

:attr:`~StructuredDomain.dvol`, and :attr:`~StructuredDomain.total_volume`

provide information about the domain's pixel volume(s) and its total volume.

- The property :attr:`~StructuredDomain.harmonic` specifies whether a domain

is harmonic (i.e. describes a frequency space) or not

- Iff the domain is harmonic, the methods

:meth:`~StructuredDomain.get_k_length_array`,

:meth:`~StructuredDomain.get_unique_k_lengths`, and

:meth:`~StructuredDomain.get_fft_smoothing_kernel_function` provide absolute

distances of the individual grid cells from the origin and assist with

Gaussian convolution.

NIFTy comes with several concrete subclasses of :class:`StructuredDomain`:

- :class:`RGSpace` represents a regular Cartesian grid with an arbitrary

number of dimensions, which is supposed to be periodic in each dimension.

- :class:`HPSpace` and :class:`GLSpace` describe pixelisations of the

2-sphere; their counterpart in harmonic space is :class:`LMSpace`, which

contains spherical harmonic coefficients.

- :class:`PowerSpace` is used to describe one-dimensional power spectra.

Among these, :class:`RGSpace` can be harmonic or not (depending on constructor arguments), :class:`GLSpace`, :class:`HPSpace`, and :class:`PowerSpace` are

pure position domains (i.e. nonharmonic), and :class:`LMSpace` is always

NIFTy4 enables the programming of grid and resolution independent algorithms.

This freedom is particularly desirable for signal inference algorithms, where

a continuous signal field is to be recovered.

It is achieved by means of an object-oriented infrastructure that comprises, among others, abstract classes for :ref:`Domains <domains>`, :ref:`Fields <fields>`, and :ref:`Operators <operators>`.

All those are covered in this tutorial.

You should be able to import NIFTy4 like this after a successful `installation <installation.html>`_.

>>> import nifty4 as ift

Technical bird's eye view

.........................

The fundamental building blocks required for IFT computations are best recognized from a large distance, ignoring all technical details.

From such a perspective,

- IFT problems largely consist of *minimization* problems involving a large number of equations.

- The equations are built mostly from the application of *linear operators*, but there may also be nonlinear functions involved.

- The unknowns in the equations represent either continuous physical *fields*, or they are simply individual measured *data* points.

- The locations and volume elements attached to discretized *field* values are supplied by *domain* objects. There are many variants of such discretized *domains* supported by NIFTy4, including Cartesian and spherical geometries and their harmonic counterparts. *Fields* can live on arbitrary products of such *domains*.

In the following sections, the concepts briefly presented here will be discussed in more detail; this is done in reversed order of their introduction, to avoid forward references.

.. _domains:

Domains

.......

One of the fundamental building blocks of the NIFTy4 framework is the *domain*.

Its required capabilities are expressed by the abstract :class:`Domain` class.

A domain must be able to answer the following queries:

- its total number of data entries (pixels), which is accessible via the

:attr:`~Domain.size` property

- the shape of the array that is supposed to hold these data entries

(obtainable by means of the :attr:`~Domain.shape` property)

- equality comparison to another :class:`Domain` instance

Unstructured domains

....................

There are domains (e.g. the data domain) which have no geometry associated to the individual data values.

In NIFTy4 they are represented by the :class:`UnstructuredDomain` class, which is derived from

:class:`Domain`.

Structured domains

..................

All domains defined on a geometrical manifold are derived from :class:`StructuredDomain` (which is in turn derived from :class:`Domain`).

In addition to the capabilities of :class:`Domain`, :class:`StructuredDomain` offers the following functionality:

- methods :meth:`~StructuredDomain.scalar_dvol`, :meth:`~StructuredDomain.dvol`, and :meth:`~StructuredDomain.total_volume` returning the pixel volume(s) and the total volume

- a :attr:`~StructuredDomain.harmonic` property

- (iff the domain is harmonic) some methods concerned with Gaussian convolution and the absolute distances of the individual grid cells from the origin

Examples for structured domains are

- :class:`RGSpace` (an equidistant Cartesian grid with a user-definable number of dimensions),

- :class:`GLSpace` (a Gauss-Legendre grid on the sphere), and

- :class:`LMSpace` (a grid storing spherical harmonic coefficients).

Among these, :class:`RGSpace` can be harmonic or not (depending on constructor arguments), :class:`GLSpace` is a pure position domain (i.e. nonharmonic), and :class:`LMSpace` is always harmonic.

Combinations of domains

.......................

A field can live on a single domain, but it can also live on a product of domains (or no domain at all, in which case it is a scalar).

The tuple of domain on which a field lives is described by the :class:`DomainTuple` class.

A :class:`DomainTuple` object can be constructed from

- a single instance of anything derived from :class:`Domain`

- a tuple of such instances (possibly empty)

- another :class:`DomainTuple` object

.. _fields:

Fields

......

A :class:`Field` object consists of the following components:

- a domain in form of a :class:`DomainTuple` object

- a data type (e.g. numpy.float64)

- an array containing the actual values

Fields support arithmetic operations, contractions, etc.

.. _operators:

Linear Operators

................

A linear operator (represented by NIFTy4's abstract :class:`LinearOperator` class) can be interpreted as an (implicitly defined) matrix.

It can be applied to :class:`Field` instances, resulting in other :class:`Field` instances that potentially live on other domains.

There are four basic ways of applying an operator :math:`A` to a field :math:`f`:

(For linear operators, inverse adjoint multiplication and adjoint inverse multiplication are equivalent.)

Operator classes defined in NIFTy may implement an arbitrary subset of these four operations.

If needed, the set of supported operations can be enhanced by iterative inversion methods;

for example, an operator defining direct and adjoint multiplication, could be enhanced to support the complete set by this method.

There are two domains associated with a :class:`LinearOperator`: a *domain* and a *target*.

Direct multiplication and adjoint inverse multiplication transform a field living on the operator's *domain* to one living on the operator's *target*, whereas adjoint multiplication and inverse multiplication transform from *target* to *domain*.

Operators with identical domain and target can be derived from :class:`EndomorphicOperator`;

typical examples for this category are the :class:`ScalingOperator`, which simply multiplies its input by a scalar value, and :class:`DiagonalOperator`, which multiplies every value of its input field with potentially different values.

Nifty4 allows simple and intuitive construction of combined operators.

As an example, if ``A``, ``B`` and ``C`` are of type :class:`LinearOperator` and ``f1`` and ``f2`` are :class:`Field` s, writing::

X = A*B.inverse*A.adjoint + C

f2 = X(f1)

will perform the operation suggested intuitively by the notation, checking domain compatibility while building the composed operator.

The combined operator infers its domain and target from its constituents, as well as the set of operations it can support.

.. _minimization:

Minimization

............

Most problems in IFT are solved by (possibly nested) minimizations of high-dimensional functions, which are often nonlinear.

In NIFTy4 such functions are represented by objects of type :class:`Energy`.

These hold the prescription how to calculate the function's value, gradient and (optionally) curvature at any given position.

Function values are floating-point scalars, gradients have the form of fields living on the energy's position domain, and curvatures are represented by linear operator objects.

Some examples of concrete energy classes delivered with NIFTy4 are :class:`QuadraticEnergy` (with position-independent curvature, mainly used with conjugate gradient minimization) and :class:`WienerFilterEnergy`.

Energies are classes that typically have to be provided by the user when tackling new IFT problems.

The minimization procedure can be carried out by one of several algorithms; NIFTy4 currently ships solvers based on

- the conjugate gradient method (for quadratic energies)