Elementwise Operators

Description


•

The elementwise operators in Maple are:

+~

addition or unary plus (prefix)

~

subtraction or unary minus (prefix)

*~

multiplication

/~

division

^~

exponentiation

mod~

modulo

!~

factorial (unary postfix)



<~

less than

<=~

less than or equal

>~

greater than

>=~

greater than or equal

=~

equal

<>~

not equal



@~

composition

@@~

repeated composition

~

concatenation operator

.~

noncommutative multiplication

::~

type operator



and~

logical and

or~

logical or

xor~

exclusive or

implies~

implication

not~

logical not (unary prefix)



union~

set union

subset~

subset

intersect~

set intersection

minus~

set difference

in~

set or list membership



&<name> ~

neutral operator

&name ~

neutral operator (unary prefix)



funct~

general elementwise operator (unary postfix)





•

An elementwise operation allows you to distribute the operation over the elements of a list, set, table, Array, Matrix, or Vector. The syntax for this is to use a tilde (~) after the given operator or function name.

 (1) 
 (2) 
•

Dimensioned container types: list, set, Array, Matrix, and Vector can be intermixed in a given operation as long as they are the same size. A table can only appear once in an argument list and can only be mixed with noncontainers. For the purpose of elementwise operations, records are not considered container objects.

>

[true,true,false,false] xor~ <true,false,true,false>;

 (3) 
•

Noncontainers are treated as single elements repeated sufficiently often to match the size of the other containers in the argument sequence.

 (4) 
•

Unlike map and zip, which usually apply to the operands of their given arguments, elementwise operations treat noncontainer data types as single elements. For example, f~(a=b) evaluates to f(a=b), whereas map(f,a=b) breaks apart the equation applying f to each side of the equation resulting in f(x)=f(b).

•

It is never an error to use a singleelement noncontainer in any part of an elementwise expression. If an error occurs it happens as a result of applying the base operator to one subset of the overall operation. In other words, when applying ^~, the only error that will ever be raised will come from ^, with the exception of mismatched container sizes.

•

The returned data structure will match the type of the given container. A call involving only lists will return a list. When mixed container types are present the return type will be determined according to the following precedence: rtable, list, set. A call involving rtables and arrays(deprecated) will result in an rtable. A call involving arrays and lists will result in an array.

 (5) 
•

Lists and sets are always considered to be 1dimensional. That is, a listoflists is not inferred as a 2D object.

 (6) 
•

Expression sequences are also treated as container types when they appear on either side of an elementwise operator. Due to the normal flattening rules expression sequences cannot normally be used with functional notation as they are interpreted as multiple arguments. The exception to this rule can be found when following the internal representation outlined for overloading elementwise operations. Elementwise operators can be overloaded by binding the ~ function. The ~ function gets called with the operation in square brackets as an index to the function name. In order to distinguish elementwise operator calls with an expression sequence on either side of the operator, the arguments are separated by a special fence token, $ (spacedollarsign). The statement, a +~ (b,c) is recast as ~[`+`](a,` $`, b, c). Similarly, f~(a,b,` $`,1,2) is interpreted as a elementwise function call with two expression sequence arguments, (a,b), and (1,2), resulting in the sequence f(a,1),f(b,2).

•

Because ~ is allowed at the beginning of a name in Maple, whitespace is required in some situations to make the meaning of a statement unambiguous. Consider the expression (a*~b)  this means (a *~ b), not a * (~b). Using parentheses or adding a space after the multiplication symbol is required to use the name ~b (tildeb) in an expression like this.

•

The neutral operators &*~ and &+~ are currently valid, so use of elementwise &* and &+ requires a space between the neutral operator and the tilde (i.e., (a &* ~ b) is elementwise &*, and (a &*~ b) is a use of the neutral operator &*~. This distinction is not something that can be enforced by the parser  be careful when using elementwise &* and &+.)

•

When a nonoperator expression involving tilde is not part of a function call, a symbolic representation is returned. The underlying representation of f~ is `~`[f]. These expressions retain their elementwise properties while being passed into other functions. For example, consider the difference between map(f,[[a,b]]) and map(f~,[[a,b]). In the first case f is applied to the sublist, [a,b], and in the second case f is applied in an elementwise fashion to the same sublist.



Examples of Elementwise Operators


>

A := <1,2,3;4,5,6;7,8,9>;

 (7) 
>

B := LinearAlgebra:IdentityMatrix(3);

 (8) 
 (9) 
 (10) 
 (11) 
 (12) 
 (13) 
>

myproc := proc(x) x^2; end:

 (14) 
 (15) 
 (16) 
 (17) 
 (18) 
 (19) 
 (20) 
>

[true,true,false,false] and~ <true,false,true,false>;

 (21) 
>

[{1,2},{3,4}] subset~ [{1,3,4,5},{1,3,4,5}];

 (22) 
 (23) 
 (24) 
 (25) 
 (26) 
 (27) 
 (28) 


See Also


Array, index/expression, LinearAlgebra[Zip], list, map, Matrix, neutral, operators/binary, operators/precedence, operators/unary, set, table, Vector, zip

