# Arithmetic¶

## Operations along one or more axes¶

In Cube objects, the `sum`

, `mean`

and
`median`

methods return the sum, mean and median values along
a given axis or axes of the cube:

```
In [1]: import matplotlib.pyplot as plt
In [2]: from mpdaf.obj import Cube
In [3]: cube = Cube('sdetect/minicube.fits')
In [4]: cube.info()
[INFO] 3681 x 40 x 40 Cube (sdetect/minicube.fits)
[INFO] .data(3681 x 40 x 40) (1e-20 erg / (Angstrom cm2 s)), .var(3681 x 40 x 40)
[INFO] center:(10:27:56.39623411,04:13:25.35875899) size:(8.000",8.000") step:(0.200",0.200") rot:-0.0 deg frame:FK5
[INFO] wavelength: min:4749.89 max:9349.89 step:1.25 Angstrom
In [5]: spe = cube.mean(axis=(1,2))
In [6]: spe.info()
[INFO] 3681 Spectrum (sdetect/minicube.fits)
[INFO] .data(3681) (1e-20 erg / (Angstrom cm2 s)), .var(3681)
[INFO] wavelength: min:4749.89 max:9349.89 step:1.25 Angstrom
In [7]: spe.plot()
# white image
In [8]: ima = cube.sum(axis=0)
In [9]: ima.info()
[INFO] 40 x 40 Image (sdetect/minicube.fits)
[INFO] .data(40 x 40) (1e-20 erg / (Angstrom cm2 s)), .var(40 x 40)
[INFO] center:(10:27:56.39623411,04:13:25.35875899) size:(8.000",8.000") step:(0.200",0.200") rot:-0.0 deg frame:FK5
In [10]: plt.figure()
Out[10]: <Figure size 640x480 with 0 Axes>
In [11]: ima.plot()
Out[11]: <matplotlib.image.AxesImage at 0x7fe700f35130>
In [12]: spe.sum(7100,7200)
Out[12]: (3344.121536058322, 1.6975161139416672)
In [13]: spe.integrate(7100, 7200)
Out[13]:
(<Quantity 4436.30591051 1e-20 erg / (cm2 s)>,
<Quantity 2.11729934 1e-20 erg / (cm2 s)>)
```

The `Spectrum.mean`

and `Spectrum.sum`

methods compute the mean and total flux
value over a given wavelength range. Similarly, `Spectrum.integrate`

integrates the flux value over a given
wavelength range.

## Arithmetic Operations between objects¶

Arithmetic operations can be performed between MPDAF objects and
scalar numbers, numpy arrays, masked arrays or other MPDAF
objects. When an operation is performed between two MPDAF objects or
between an MPDAF object and an array, their dimensions must either
match, or be broadcastable to the same dimensions via the usual numpy
rules. The broadcasting rules make it possible to perform arithmetic
operations between a `Cube`

and an `Image`

or a
`Spectrum`

, assuming that they have compatible spatial and
spectral world-coordinates. The following demonstration shows a cube
being multiplied by an image that has the same spatial dimensions and
world-coordinates as the cube, and also being divided by a spectrum
that has the same spectral dimensions and wavelengths as the spectral
axis of the cube.

```
In [14]: cube2 = cube * ima / spe
In [15]: cube2.info()
[INFO] 3681 x 40 x 40 Cube (sdetect/minicube.fits)
[INFO] .data(3681 x 40 x 40) (1e-20 erg / (Angstrom cm2 s)), .var(3681 x 40 x 40)
[INFO] center:(10:27:56.3962,04:13:25.3588) size in arcsec:(8.000,8.000) step in arcsec:(0.200,0.200) rot:-0.0 deg
[INFO] wavelength: min:4749.89 max:9349.89 step:1.25 Angstrom
```

## Generic object arithmetic:¶

Cube, Image and Spectrum objects are all derived from a base class
called `DataArray`

. This class implements a couple of
arithmetic functions that operate on the data and variance arrays of
these objects:

```
In [16]: ima2 = ima.sqrt()
In [17]: ima2.plot()
Out[17]: <matplotlib.image.AxesImage at 0x7fe703c37790>
```