help m2 op_colon
-------------------------------------------------------------------------------
Title
[M-2] op_colon -- Colon operators
Syntax
a :+ b addition
a :- b subtraction
a :* b multiplication
a :/ b division
a :^ b power
a :== b equality
a :!= b inequality
a :> b greater than
a :>= b greater than or equal to
a :< b less than
a :<= b less than or equal to
a :& b and
a :| b or
Description
Colon operators perform element-by-element operations.
Remarks
Remarks are presented under the following headings:
C-conformability: element by element
Usefulness of colon logical operators
Use parentheses
C-conformability: element by element
The colon operators perform the indicated operation on each pair of
elements of a and b. For instance,
+- -+ +- -+ +- -+
| c d | | j k | | c*j d*k |
| f g | :* | l m | = | f*l g*m |
| h i | | n o | | h*n i*o |
+- -+ +- -+ +- -+
Also colon operators have a relaxed definition of conformability:
+- -+ +- -+ +- -+
| c | | j k | | c*j c*k |
| f | :* | l m | = | f*l f*m |
| g | | n o | | g*n g*o |
+- -+ +- -+ +- -+
+- -+ +- -+ +- -+
| c d | | j | | c*j d*j |
| f g | :* | l | = | f*l g*l |
| h i | | n | | h*n i*n |
+- -+ +- -+ +- -+
+- -+ +- -+
| j k | | c*j d*k |
[ c d ] :* | l m | = | c*l d*m |
| n o | | c*n d*o |
+- -+ +- -+
+- -+ +- -+
| c d | | c*l d*m |
| f g | :* [ l m ] = | f*l g*m |
| h i | | h*l i*m |
+- -+ +- -+
+- -+ +- -+
| j k | | c*j c*k |
c :* | l m | = | c*l c*m |
| n o | | c*n c*o |
+- -+ +- -+
+- -+ +- -+
| c d | | c*j d*j |
| f g | :* j = | f*j g*j |
| h i | | h*j i*j |
+- -+ +- -+
The matrices above are said to be c-conformable; the c stands for colon.
The matrices have the same number of rows and columns, or one or the
other is a vector with the same number of rows or columns as the matrix,
or one or the other is a scalar.
C-conformability is relaxed, but not everything is allowed. The
following is an error:
+- -+
| f |
(c d e) :* | g |
| h |
+- -+
Usefulness of colon logical operators
It is worth paying particular attention to the colon logical operators
because they can produce pattern vectors and matrices. Consider the
matrix
: x = (5, 0 \ 0, 2 \ 3, 8)
: x
1 2
+---------+
1 | 5 0 |
2 | 0 2 |
3 | 3 8 |
+---------+
Which elements of x contain 0?
: x:==0
1 2
+---------+
1 | 0 1 |
2 | 1 0 |
3 | 0 0 |
+---------+
How many zeros are there in x?
: sum(x:==0)
2
Use parentheses
Because of their relaxed conformability requirements, colon operators are
not associative even when the underlying operator is. For instance, you
expect (a+b)+c == a+(b+c), at least ignoring numerical roundoff error.
Nevertheless, (a:+b):+c == a:+(b:+c) does not necessarily hold. Consider
what happens when
a: 1 x 4
b: 5 x 1
c: 5 x 4
Then (a:+b):+c is an error because a:+b is not c-conformable.
Nevertheless, a:+(b:+c) is not an error and in fact produces a 5 x 4
matrix because b:+c is 5 x 4, which is c-conformable with a.
Conformability
a :op b:
a: r1 x c1
b: r2 x c2, a and b c-conformable
result: max(r1,r2) x max(c1,c2)
Diagnostics
The colon operators return missing and abort with error under the same
conditions that the underlying operator returns missing and aborts with
error.
Also see
Manual: [M-2] op_colon
Help: [M-2] exp; [M-2] intro