cosmology_equal#
- astropy.cosmology.cosmology_equal(cosmo1: Any, cosmo2: Any, /, *, allow_equivalent: bool = False) bool [source]#
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 byformat
.- formatbool or
None
orstr
ortuple
thereof, optional keyword-only Whether to allow the arguments to be converted to a
Cosmology
. This allows, e.g. aTable
to be given instead aCosmology
.False
(default) will not allow conversion.True
orNone
will, and will use the auto-identification to try to infer the correct format. Astr
is assumed to be the correct format to use when converting. Noteformat
is broadcast as an object array to match the shape ofcosmos
soformat
cannot determine the output shape.- allow_equivalentbool, 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, likeFlatLambdaCDM
.
- cosmo1, cosmo2
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
hasOde0
set to the same value calculated inFlatLambdaCDM
.>>> 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 withCosmology.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