Skip to main content
`Ctrl`+`K`

# cosmology_equal

astropy.cosmology.cosmology_equal(cosmo1, cosmo2, /, *, allow_equivalent=False)

  Return element-wise equality check on the cosmologies.

  Note

  Cosmologies are currently scalar in their parameters.

  Parameters:
    cosmo1, cosmo2|Cosmology|-like

      The objects to compare. Must be convertible to |Cosmology|, as specified by `format`.

    format bool or `python:None` or `python:str` or `python:tuple` thereof, optional keyword-only

      Whether to allow the arguments to be converted to a |Cosmology|. This allows, e.g. a |Table| to be given instead a |Cosmology|. `False` (default) will not allow conversion. `True` or `None` will, and will use the auto-identification to try to infer the correct format. A `str` is assumed to be the correct format to use when converting. Note `format` is broadcast as an object array to match the shape of `cosmos` so `format` cannot determine the output shape.

    allow_equivalent bool, optional keyword-only

      Whether to allow cosmologies to be equal even if not of the same class. For example, an instance of |LambdaCDM| might have \(\Omega_0=1\) and \(\Omega_k=0\) and therefore be flat, like |FlatLambdaCDM|.

  Examples

  Assuming the following imports

      >>> import astropy.units as u
      >>> from astropy.cosmology import FlatLambdaCDM

  Two identical cosmologies are equal.

      >>> cosmo1 = FlatLambdaCDM(70 * (u.km/u.s/u.Mpc), 0.3)
      >>> cosmo2 = FlatLambdaCDM(70 * (u.km/u.s/u.Mpc), 0.3)
      >>> cosmology_equal(cosmo1, cosmo2)
      True

  And cosmologies with different parameters are not.

      >>> cosmo3 = FlatLambdaCDM(70 * (u.km/u.s/u.Mpc), 0.4)
      >>> cosmology_equal(cosmo1, cosmo3)
      False

  Two cosmologies may be equivalent even if not of the same class. In these examples the |LambdaCDM| has `Ode0` set to the same value calculated in |FlatLambdaCDM|.

      >>> from astropy.cosmology import LambdaCDM
      >>> cosmo3 = LambdaCDM(70 * (u.km/u.s/u.Mpc), 0.3, 0.7)
      >>> cosmology_equal(cosmo1, cosmo3)
      False
      >>> cosmology_equal(cosmo1, cosmo3, allow_equivalent=True)
      True

  While in this example, the cosmologies are not equivalent.

      >>> cosmo4 = FlatLambdaCDM(70 * (u.km/u.s/u.Mpc), 0.3, Tcmb0=3 * u.K)
      >>> cosmology_equal(cosmo3, cosmo4, allow_equivalent=True)
      False

  Also, using the keyword argument, the notion of equality is extended to any Python object that can be converted to a |Cosmology|.

      >>> mapping = cosmo2.to_format("mapping")
      >>> cosmology_equal(cosmo1, mapping, format=True)
      True

  Either (or both) arguments can be |Cosmology|-like.

      >>> cosmology_equal(mapping, cosmo2, format=True)
      True

  The list of valid formats, e.g. the |Table| in this example, may be checked with `Cosmology.from_format.list_formats()`.

  As can be seen in the list of formats, not all formats can be auto-identified by `Cosmology.from_format.registry`. Objects of these kinds can still be checked for equality, but the correct format string must be used.

      >>> yml = cosmo2.to_format("yaml")
      >>> cosmology_equal(cosmo1, yml, format=(None, "yaml"))
      True

  This also works with an array of `format` matching the number of cosmologies.

      >>> cosmology_equal(mapping, yml, format=[True, "yaml"])
      True