Mathematical operations

Earth Engine supports many basic mathematical operators. They share some common features. Earth Engine performs math operations per pixel. When an operator is applied to an image, it's applied to each unmasked pixel of each band. In the case of operations on two images, the operation is only applied at the locations where pixels in both images are unmasked. Earth Engine automatically matches bands between images. When an operator is applied to two images, the images are expected to have the same number of bands so they can be matched pairwise. However, if one of the images has only a single band, it is matched with all of the bands in the other image, essentially replicating that band enough times to match the other image.

For a simple example, consider the task of creating the Normalized Difference Vegetation Index (NDVI) using Landsat imagery:

// Load two 5-year Landsat 7 composites.
var landsat1999 = ee.Image('LANDSAT/LE7_TOA_5YEAR/1999_2003');
var landsat2008 = ee.Image('LANDSAT/LE7_TOA_5YEAR/2008_2012');

// Compute NDVI the hard way.
var ndvi1999 ='B4').subtract('B3'))

// Compute NDVI the easy way.
var ndvi2008 = landsat2008.normalizedDifference(['B4', 'B3']);

As shown in the previous example, math operators perform basic arithmetic operations on image bands. The normalized difference operation is so common in remote sensing, Earth Engine provides a shortcut method, as shown in the second part of the example. Subtracting the images in this example results in a “change vector” for each pixel. Bands are matched automatically to perform the difference:

// Compute the multi-band difference image.
var diff = landsat2008.subtract(landsat1999);
             {bands: ['B4', 'B3', 'B2'], min: -32, max: 32},

// Compute the squared difference in each band.
var squaredDifference = diff.pow(2);
             {bands: ['B4', 'B3', 'B2'], max: 1000},
             'squared diff.');

In the second part of this example, the squared difference is computed using image.pow(2). For the complete list of mathematical operators handling basic arithmetic, trigonometry, exponentiation, rounding, casting, bitwise operations and more, see the API documentation (in the Docs tab of the Earth Engine Code Editor).


To implement more complex mathematical expressions, it may be more convenient to use image.expression(), which parses a text representation of a math operation. The following example uses expression() to compute the Enhanced Vegetation Index (EVI):

// Load a Landsat 8 image.
var image = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318');

// Compute the EVI using an expression.
var evi = image.expression(
    '2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))', {

Map.centerObject(image, 9);
Map.addLayer(evi, {min: -1, max: 1, palette: ['FF0000', '00FF00']});

Observe that the first argument to expression is the textual representation of the math operation, the second argument is a dictionary where the keys are variable names used in the expression and the values are the image bands to which the variables should be mapped. Bands in the image may be referred to as b("band name") or b(index), for example b(0), instead of providing the dictionary. Note that division functions as it does in Python: dividing two integers results in an integer. For example 10 / 20 = 0. To change this behavior, multiply one of the operands by 1.0: 10 * 1.0 / 20 = 0.5. Supported expression operators are listed in the following table.

Operators for expression()
Arithmetic + - * / % ** Add, Subtract, Multiply, Divide, Modulus, Exponent
Comparison == != < > <= >= Equal, Not Equal, Less Than, Greater than, etc.
Logical && || ! ^ And, Or, Not, Xor
Ternary ? : If then else