# Work with operators in Map Algebra

In Map Algebra, operators apply a mathematical operation on input rasters and numbers.

Operators are generally placed between two inputs (operands) to perform a mathematical operation (for example, outVar = 3 + 7). In Map Algebra, operands can be rasters or numbers. To use an operator with a raster, the raster must be a Raster object.

The table below provides a quick reference indicating how current Map Algebra operators have been implemented in relation to Python operators.

OperationPython operatorMap Algebra operatorGeoprocessing tool

Arithmetic

+

+

Plus

Division

/

/

Divide

Integer Division

//

//

N/A

Modulo

%

%

Mod

Multiplication

*

*

Times

Power

**

**

Power

Subtraction

-

-

Minus

Unary Minus

-

-

Negate

Unary Plus

+

+

N/A

Boolean

Boolean And

N/A

&

Boolean And

Boolean Complement

N/A

~

Boolean Not

Boolean Exclusive Or

N/A

^

Boolean XOr

Boolean Or

N/A

|

Boolean Or

Relational

Equal To

==

==

Equal To

Greater Than

>

>

Greater Than

Greater Than And Equal To

>=

>=

Greater Than Equal

Less Than

<

<

Less Than

Less Than And Equal To

<=

<=

Less Than Equal

Not Equal To

!=

!=

Not Equal

Bitwise

Bitwise And

&

N/A

Bitwise And

Bitwise Complement

~

N/A

Bitwise Not

Bitwise Exclusive Or

^

N/A

Bitwise XOr

Bitwise Left Shift

<<

<<

Bitwise Left Shift

Bitwise Or

|

N/A

Bitwise Or

Bitwise Right Shift

>>

>>

Bitwise Right Shift

## Operator rules

• When only numbers are used with operators, the result will be a number.
``````# outVar will be assigned 10
outVar = 3 + 7``````
• When using operators with rasters, the raster must be a Raster object.
``outRas = Raster("inraster1") + Raster("inraster2")``
• When a raster operand is used, the result is a Raster object.
``````# In the following statement, 4 is added to each cell value in inraster1
outRas = Raster("inraster1") + 4
outRas2 = Raster("inraster") + math.pi``````

In the above statement, pi is used from the Python math module. The math module also includes the base of the natural logarithm, math.e, which can also be used in a Map Algebra statement.

• Some operators can precede a Raster object or a number.
``outRas = -Raster("inraster")``
• The Boolean (~, &, ^, |) operators will perform a Boolean operation when one or more inputs (operand) is a raster. If both inputs (operands) are numbers, then these operators will perform Bitwise operations.

##### Tip:

Spaces are not necessary between operators but are recommended for readability.

Tools and operators can be nested to create complex statements.

### Multidimensional raster operator rules

• When an operator is used with a multidimensional raster and a number, the result is a multidimensional Raster object with the same variables and dimensionality as the input.
``````# In the following statement, 100 is added to each cell in
# each slice of in_multidem_raster
out_multidem_raster = in_multidem_raster + 100``````
• When an operator is used with a multidimensional raster and a non-multidimensional raster, the result will be a multidimensional Raster object with the same variables and dimensionality as the input.

If the input Raster objects do not overlap, no result is returned.

``````# In the following statement, the cell values from in_raster are added to
# the cell values in each slice of in_multidem_raster.
out_multidem_raster = in_multidem_raster + in_raster``````
• When an operator is used with two multidimensional rasters, the result will be a multidimensional Raster object containing only variables and dimensionality that are common between the inputs. For example, if one multidimensional raster contains daily temperature data for 1995–2015, and another multidimensional raster contains daily temperature data for 2001–2005, the result of an operator used with both multidimensional rasters will include only daily temperature data for 2001–2005.

If the input multidimensional Raster objects do not have common variables and dimensionality, no result is returned.

``````# In the following statement, the cell values in each slice from in_multidem_raster1
# are added to the cell values in each slice from in_multidem_raster2, only where variables and
# dimensionality overlap.
out_multidem_raster = in_multidem_raster1 + in_multidem_raster2``````

## Operator precedence

The precedence value determines the order in which operators are executed. The operator with the higher precedence will be processed first. If two operators have the same precedence value, then they will be processed in a left-to-right order in an expression.

You can use parentheses to override the precedence priority, with the operation in the deepest parentheses being processed first no matter what operator is specified.

The following table lists all the Map Algebra operators in order from lowest precedence to highest precedence. Each of the operators that are listed in the same row have the same precedence.

Map Algebra operatorReference

<, <=, >, >=, ==, !=

|

Boolean Or

^

Boolean XOr

&

Boolean And

<<, >>

+, -

*, /, //, %

+, -, ~

**

Power