Monadic Operators


Common Error Reports

Multiple errors elicit but one report. If an error report in the following list is issued, then the ones preceding it do not apply. Five reports are common to all monadic operators:

Definitions of Monadic Operators

Apply gx and y gx

   Arguments
g is a function scalar; that is, g is <{f} for some function f (other than Assignment). The arguments y and x are suitable arguments for f.
   Definition
The function f described in the arguments section is evaluated, as f x in the monadic case or as y f x in the dyadic case. Indeed, although the two infix forms are shown for definiteness, f{...} is evaluated for any valence of f, zero through nine.

Warning! If instead of a function scalar you give a function expression, (e.g., instead of (+;-;)[1]x or (<{f})x you give -x or fx), you will get a wrong result, namely, the result for the Each operator.

   Additional Error Report
The following report is issued only if there is no parse or value error (see "Common Error Reports"):

   Examples
     fns(;;)    Equivalent to <{},<{},<{}

     2 3 fns[0] 4
 8 12

     2 3 fns[1] 4
 0.5 0.75

     2 3 fns[2] 4
 4 4 4
 4 4 4

Bitwise fx and y fx

   Arguments
The operand f is Cast, And, Or, Not, or one of the six relational functions (< = > ). The arguments y and x are suitable arguments for f.

   Definition
The Bitwise operator applies Not to each bit of the argument and applies all the other functions except Cast to each pair of corresponding bits of the arguments.

The derived function Bitwise Not is a monadic scalar function and all the others except Bitwise Cast are dyadic scalar functions, obeying the usual rules of conformability.

Examples for Bitwise Not, And, and Unequal

     ~ 16 15 123 122
 15 16 122 123        
One's complement.

     5 ^ 10
 0 1 0 1 4 5 4 5 0 1

     5  10
 5 4 7 6 1 0 3 2 13 12

And and Or, without the Bitwise operator, give boolean results for all valid arguments - see above.

Bitwise Cast x

Bitwise Cast leaves the data part of the variable unchanged but changes the type indicator and (usually) shape. The conversion of symbol to character, for example, is quite different from plain Cast: for a symbol, the data part is a four-byte pointer, which may differ from process to process but is guaranteed unique and invariant within a process. The data part of a character is, of course, its ASCII code.

The left argument can be `int, `float, or `char; a Bitwise Cast to `sym is not allowed since not all integers are valid symbols, i.e., not all point to symbols. The type of the right argument can be of any of the four.

Since scalars of various types are represented using various numbers of bytes, the length of the last dimension of the result may differ from that of the right argument. There is no padding or truncation. If rb bytes are required to represent the result type and ab to represent the right argument type and rb>ra, then the last dimension of the right argument must be divisible by rbab. The table below exhibits the relation between the shape of the right argument and that of the result.

The rank is changed only when a scalar is cast bitwise to a type that is represented in fewer bytes.

Shape of the Result of Bitwise Cast, for Argument Shape s

From `intFrom `float From `charFrom `sym
To `ints (1s), 211,s (1s), (1s)4 s
To `float (1s), (1s)2s (1s), (1s)8 (1s), (1s)2
To `char (1s), 411,s (1s), 811,s s (1s), 411,s

Examples for Bitwise Cast
     `int  `
 4163890

     `int  1.2 3.4
 1072902963 858993459 1074475827 858993459

     `char  `int  'abcdefgh'
dh   Plain Cast converts just the last byte of an integer to character

     `char  `int  'abcdefgh'
abcdefgh

     `int  'ab'
[error]  : length   The shape is 2, which is not divisible by 4.
   Additional Report

Each fx and y fx

   Arguments and Result
f is any function expression (so Assignment is excluded, as it is for all operators). The result is nested. In the monadic case, the shape of the result is the shape of x. In the dyadic case, the shapes of the arguments and result follow the rules for dyadic scalar functions (see "Application, Conformability, and Result Shape"), except that only scalars are extended, not one-element arrays in general, and nonscalar arguments of differing ranks elicit a rank error report.

   Definition
The derived function, f, is a scalar function.

In the case of monadic f:

     (i#fx)  < f >i#x
for every valid index i. That is, each element of x is selected, f is applied to the Disclose of the selected element, and the result of f is enclosed and inserted in the result of f. (See "Disclose" for the definition of > and "Enclose" for <.) fx is equivalent to (f Each)x for the following defined operator Each:
     (f Each) x: {
          sx;
          x,x;
          z(x)<();
          (iz) do z[i]< f >x[i];
          sz}
In the dyadic case, if x and y have the same shape, then
     (i#y fx)  <(>i#y)f>i#x
for every valid index i. If x has one element, then
     (i#y fx)  <(>i#y)f 0x
for every valid index i. (See "Pick" for the definition of .) The definition is similar if y has one element, or both have one element.

Warning! If instead of a function expression you give a function scalar, (e.g., instead of -x or fx you give (+;-;)[1]x or (<{f})x), you will get a wrong result, namely, the result for the Apply operator.

   Additional Error Reports
Each of the following reports is issued only if there is no parse or value error (see "Common Error Reports") and none of the reports preceding it on this list applies:
   Examples
In the first expression, the Each operator is applied to a monadic function (Shape) to give the rank of a scalar, a vector, and a matrix. In the second, it is applied to a monadic (Count, to find the vector lengths) as well as to a dyadic (Take, to pad the vectors to equal lengths for the principal function, Disclose); notice the other use of Disclose to make the Count Each result simple for Max Reduce, which finds the greatest length.

     ҡҡ (2;2;2 3)
 0 1 2

     (/>#vs)١vs('ab';'----';'cde')
ab
----
cde

Reduce f/x

   Arguments and Result
f is one of +, , , , ^, . The argument x is any array whose items are suitable left and right arguments of f. The shape of the result is 1x.
   Definition
If #x is at least two, i.e., if x has at least two items, then f/x is defined to be:
     x[0] f x[1] f ... f x[1+#x]
If #x is one, then f/x is x; while if #x is zero, then f/x is (1x)identity, where identity is a scalar that depends on f, and whose type for + and depends on x. See the table "
Identity Scalars for Reduction".

As pointed out in "Operators and Derived Functions", Reduce is not in the strictest sense an operator, but for most purposes it can be regarded as one.

Identity Scalars for Reduction
FunctionIdentity
+0
1
FunctionIdentity
0
^1
FunctionIdentity
Inf
Inf

   Additional Error Reports
If there is no error reported as a parse error then a similar error may be reported:

The following report is issued only if there is no parse, token, or value error (see "
Common Error Reports"):

   Examples
     +/3 5 2
 6

     3+5+2
 6

     /6 2
 10 11

     +/0
 0

Scan f\x

   Arguments and Result
f is one of +, , , , ^, . The argument x is any array whose items are suitable left and right arguments of f. The shape of the result is the same as the shape for x.
   Definition
If #x is at least one, i.e., if x has at least one item, then:
     (f\x)[i]  f/x[i+1]
for every valid scalar index i. That is, Scan produces a sequence of moving reductions. If #x is zero, then f\x is empty, and its type is integer for ^ and and depends upon x for the rest.

As pointed out in "Operators and Derived Functions", Scan is not in the strictest sense an operator, but for most purposes it can be regarded as one.

   Additional Error Reports
If there is no error reported as a parse error then a similar error may be reported:

The following report is issued only if there is no parse, token, or value error (see "Common Error Reports"):

   Examples
     +\3 5 2
 3 8 6

     \3 4
 0 1 2 3
 0 1 2 3
 0 1 2 3

     \3 4
  0  1  2  3
  4  5  6  7
  8  9 10 11

doc@aplusdev.org© Copyright 1995–2008 Morgan Stanley Dean Witter & Co. All rights reserved.