OpenSCAD User Manual/Mathematical Operators

Scalar arithmetic operators
The scalar arithmetic operators take numbers as operands and produce a new number.

The  can also be used as prefix operator to negate a number.

Prior to version 2021.01, the builtin mathematical function  is used instead of the   exponent operator.

Relational operators
Relational operators produce a boolean result from two operands.

If both operands are simple numbers, the meaning is self-evident.

If both operands are strings, alphabetical sorting determines equality and order. E.g., "ab" > "aa" > "a".

If both operands are Booleans, true > false. In an inequality comparison between a Boolean and a number true is treated as 1 and false is treated as 0. Other inequality tests involving Booleans return false.

If both operands are vectors, an equality test returns true when the vectors are identical and false otherwise. Inequality tests involving one or two vectors always return false, so for example [1] < [2] is false.

Dissimilar types always test as unequal with '==' and '!='. Inequality comparisons between dissimilar types, except for Boolean and numbers as noted above, always result in false. Note that [1] and 1 are different types so [1] == 1 is false.

doesn't equal anything but undef. Inequality comparisons involving undef result in false.

doesn't equal anything (not even itself) and inequality tests all produce false. See Numbers.

Logical operators
All logical operators take Booleans as operands and produce a Boolean. Non-Boolean quantities are converted to Booleans before the operator is evaluated.

Since  is ,   is also.

Logical operators deal with vectors differently than relational operators:

is, but

is.

Conditional operator
The ?: operator can be used to conditionally evaluate one or another expression. It works like the ?: operator from the family of C-like programming languages.

Vector-number operators
The vector-number operators take a vector and a number as operands and produce a new vector.


 * Example

L = [1, [2, [3, "a"] ] ]; echo(5*L); // ECHO: [5, [10, [15, undef]]]

Vector operators
The vector operators take vectors as operands and produce a new vector.

The  can also be used as prefix operator to element-wise negate a vector.


 * Example

L1 = [1, [2, [3, "a"] ] ]; L2 = [1, [2, 3] ]; echo(L1+L1); // ECHO: [2, [4, [6, undef]]] echo(L1+L2); // ECHO: [2, [4, undef]] Using + or - with vector operands of different sizes produce a result vector that is the size of the smaller vector.

Vector dot-product operator
If both operands of multiplication are simple vectors, the result is a number according to the linear algebra rule for dot product. results in $$c = \sum u_iv_i$$. If the operands' sizes don't match, the result is.

Matrix multiplication
If one or both operands of multiplication are matrices, the result is a simple vector or matrix according to the linear algebra rules for matrix product. In the following, $A, B, C...$ are matrices, $u, v, w...$ are vectors. Subscripts $i, j$ denote element indices.

For $A$ a matrix of size $n × m$ and $B$ a matrix of size $m × p$, their product is a matrix of size $n × p$ with elements

$$C_{ij} = \sum_{k=0}^{m-1} A_{ik}B_{kj}$$.

results in  unless $n$ = $p$.

For $A$ a matrix of size $n × m$ and $v$ a vector of size $m$, their product is a vector of size $n$ with elements

$$u_{i} = \sum_{k=0}^{m-1} A_{ik}v_{k}$$.

In linear algebra, this is the product of a matrix and a column vector.

For $v$ a vector of size $n$ and $A$ a matrix of size $n × m$, their product is a vector of size $m$ with elements

$$u_{j} = \sum_{k=0}^{n-1} v_{k}A_{kj}$$.

In linear algebra, this is the product of a row vector and a matrix.

Matrix multiplication is not commutative: $$AB \neq BA$$, $$Av \neq vA$$.