Skip to content

Operators ​

In this page a and b are vectors of one kind, s is a number of any of the kinds below, m a 3 x 3 real matrix, and b^=b/|b|.

NumbersKinds
integersI1P, I2P, I4P, I8P
realsR4P, R8P, R16P

A number is converted to the kind of the vector before the operation, with real(s, kind). Two vectors must have the same kind (and the same type: vector and vector_R8P do not mix, see Precision and kinds).

Assignment ​

StatementResult
a = ba copy of b
a = s(s, s, s)

Arithmetic ​

ExpressionResultNote
+a(ax,ay,az)
-a(−ax,−ay,−az)
a + b, a - b(ax±bx,ay±by,az±bz)
a * b(axbx,ayby,azbz)component by component, not the dot product
a / b(ax/bx,ay/by,az/bz)component by component
s * a, a * s(sax,say,saz)
a / s(ax/s,ay/s,az/s)no s / a
a + s, s + a(ax+s,ay+s,az+s)the number on every component
a - s(ax−s,ay−s,az−s)
s - a(s−ax,s−ay,s−az)

Products ​

ExpressionResultType
a .dot. baxbx+ayby+azbzreal, the kind of the vectors
a .cross. b(aybz−azby,azbx−axbz,axby−aybx)vector
m .matrix. ama, the matrix on the leftvector

m is a real array of shape (3, 3) of the kind of the vector, in Fortran's column-major order: m(i, j) is row i, column j. Rotation and mirror matrices come from rotation_matrix and mirror_matrix.

Parallel and orthogonal components ​

ExpressionResult
a .paral. b(a⋅b^)b^, the component of a along b
a .ortho. ba−(a⋅b^)b^, the component of a orthogonal to b

Only the direction of b matters. If b is the zero vector both are NaN.

Precedence ​

Fortran gives every user-defined binary operator (.dot., .cross., .matrix., .paral., .ortho.) the lowest precedence, below +, -, *, / and the comparisons, and evaluates a chain of them from left to right:

WrittenMeans
a .dot. b + ca .dot. (b + c)
2 * a .cross. b(2 * a) .cross. b
a .cross. b .dot. c(a .cross. b) .dot. c
a .dot. b > 0a .dot. (b > 0): does not compile

Parenthesise products: (a .dot. b) > 0._R8P.

Comparisons ​

ExpressionTrue when
a < b, a <= b, a > b, a >= bthe lengths compare so: |a|<|b|, ...
a == bsame length, and the unit vectors have equal components
a /= bnot a == b
a < s, s < a, ... (all six)the length of a compares so with s

The comparisons between vectors order them by length, not component by component: 3 * ex <= 3 * ey and 3 * ex >= 3 * ey are both true, 3 * ex == 3 * ey is false. All comparisons are exact, without tolerance: for computed vectors compare normL2(a - b) with a tolerance of your own.

Elemental ​

All operators but .matrix. are elemental: with arrays of vectors (and arrays of numbers) they work element by element. The defined assignment of a vector to a vector is not elemental, but Fortran's intrinsic assignment copies arrays of vectors element by element anyway.

Released under the GPL v3, BSD 2-Clause, BSD 3-Clause or MIT License.