<commit_before>.. include:: links.inc

.. _compound-models-intro:

Combining Models
****************

Basics
======

While the Astropy modeling package makes it very easy to define :doc:`new
models <new-model>` either from existing functions, or by writing a
`~astropy.modeling.Model` subclass, an additional way to create new models is
by combining them using arithmetic expressions.  This works with models built
into Astropy, and most user-defined models as well.  For example, it is
possible to create a superposition of two Gaussians like so::

    >>> from astropy.modeling import models
    >>> g1 = models.Gaussian1D(1, 0, 0.2)
    >>> g2 = models.Gaussian1D(2.5, 0.5, 0.1)
    >>> g1_plus_2 = g1 + g2

The resulting object ``g1_plus_2`` is itself a new model.  Evaluating, say,
``g1_plus_2(0.25)`` is the same as evaluating ``g1(0.25) + g2(0.25)``::

    >>> g1_plus_2(0.25)  # doctest: +FLOAT_CMP
    0.5676756958301329
    >>> g1_plus_2(0.25) == g1(0.25) + g2(0.25)
    True
<commit_msg>Add clarification note on CompoundModel<commit_after>.. include:: links.inc

.. _compound-models-intro:

Combining Models
****************

Basics
======

While the Astropy modeling package makes it very easy to define :doc:`new
models <new-model>` either from existing functions, or by writing a
`~astropy.modeling.Model` subclass, an additional way to create new models is
by combining them using arithmetic expressions.  This works with models built
into Astropy, and most user-defined models as well.  For example, it is
possible to create a superposition of two Gaussians like so::

    >>> from astropy.modeling import models
    >>> g1 = models.Gaussian1D(1, 0, 0.2)
    >>> g2 = models.Gaussian1D(2.5, 0.5, 0.1)
    >>> g1_plus_2 = g1 + g2

The resulting object ``g1_plus_2`` is itself a new model.

.. note::
    The model ``g1_plus_2`` is a `~astropy.modeling.CompoundModel` which contains
    the models ``g1`` and ``g2`` without any parameter duplication. Meaning changes
    to the parameters of ``g1_plus_2`` will effect the parameters of ``g1`` or ``g2``
    and vice versa; if one does not want this to occur one can copy the models prior
    to adding them using the ``.copy()`` method ``g1.copy() + g2.copy()``. In
    general applies to any `~astropy.modeling.CompoundModel` constructed using a
    binary operation, so that `~astropy.modeling.CompoundModel` follows the Python
    convention for construction of container objects. For more information on this
    please see the `API Changes in astropy.modeling <https://docs.astropy.org/en/v4.0.x/whatsnew/4.0.html#whatsnew-4-0-modeling-api>`__

Evaluating, say, ``g1_plus_2(0.25)`` is the same as evaluating ``g1(0.25) + g2(0.25)``::

    >>> g1_plus_2(0.25)  # doctest: +FLOAT_CMP
    0.5676756958301329
    >>> g1_plus_2(0.25) == g1(0.25) + g2(0.25)
    True

{'fraction_non_alphanumeric': 0.12963617096432356, 'fraction_numerical': 0.053691275167785234, 'mean_word_length': 3.9166666666666665, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 42, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5543305', 'n_tokens_mistral': 1009, 'n_tokens_neox': 931, 'n_words': 327}