Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Arithmetic & Operators

Scalar operators

OperatorOperationExample
+Addition3 + 47
-Subtraction / unary minus10 - 46, -5
*Multiplication3 * 721
/Division10 / 42.5
^Exponentiation (right-associative)2 ^ 101024

For modulo use the mod(a, b) function. % is a comment character.

Comparison operators

Return 1.0 (true) or 0.0 (false). Work element-wise on matrices.

OperatorMeaning
==Equal
~=Not equal
<Less than
>Greater than
<=Less or equal
>=Greater or equal

Logical operators

OperatorMeaning
~exprLogical NOT
&&Logical AND
||Logical OR

See Comparison & Logical Operators for full details.

Precedence (high → low)

  1. postfix ' — transpose
  2. ^, .^ — right-associative
  3. unary -, ~ — negation, logical NOT
  4. *, /, .*, ./, .^, implicit multiplication
  5. +, -
  6. : — range
  7. ==, ~=, <, >, <=, >= — comparison (non-associative)
  8. && — logical AND
  9. || — logical OR (lowest)

Use parentheses to override: (2 + 3) * 420.

Partial expressions

An expression starting with an operator uses ans as the left operand:

[ 100 ]: / 4
[ 25 ]: ^ 2
[ 625 ]:

Implicit multiplication

A number, variable, or closing parenthesis immediately before ( multiplies:

2(3 + 1)      →   8      (same as 2 * (3 + 1))
(2 + 1)(4)    →  12
2(3)(4)       →  24

Unary minus

-5
-(3 + 2)      →  -5
--5           →   5

Matrix operators

When one or both operands are matrices, the same operators apply with element-wise or broadcast semantics:

ExpressionSemantics
scalar + matrixAdd scalar to every element
matrix + matrixElement-wise (shapes must match)
scalar * matrixScale every element
matrix / scalarDivide every element
matrix ^ scalarRaise every element to the power

See Matrices for full details.