github ↗
docs menu

Module type Ops.Elementwise

Elementwise operations.

Nodes (Ops) and patterns (Upat) share them. On nodes, a binary operation first promotes its operands to their least upper type (Dtype.least_upper), a weak constant staying weak; the node's shape is their broadcast shape (broadcast_shape). On patterns, each builds the pattern of the node the operation builds.

#type t
#val alu : t -> Op.t -> t list -> t

alu x op rest is the operation op on x :: rest as given.

#val cast : t -> Dtype.t -> t

cast x dt is x converted to dt, or x if it is of type dt.

Arithmetic

add, mul, lt, eq, maximum, bitwise_and, bitwise_or, bitwise_xor, shl and shr are Op.t.Add, Op.t.Mul, Op.t.Cmplt, Op.t.Cmpeq, Op.t.Max, Op.t.And, Op.t.Or, Op.t.Xor, Op.t.Shl and Op.t.Shr of their promoted operands. reciprocal, trunc, sqrt, exp2 and log2 are Op.t.Recip, Op.t.Trunc, Op.t.Sqrt, Op.t.Exp2 and Op.t.Log2 of their operand. The others are built from these.

#val add : t -> t -> t
#val mul : t -> t -> t
#val lt : t -> t -> t
#val eq : t -> t -> t
#val maximum : t -> t -> t
#val bitwise_and : t -> t -> t
#val bitwise_or : t -> t -> t
#val bitwise_xor : t -> t -> t
#val shl : t -> t -> t
#val shr : t -> t -> t
#val reciprocal : t -> t
#val trunc : t -> t
#val sqrt : t -> t
#val exp2 : t -> t
#val log2 : t -> t
#val sub : t -> t -> t

sub x y is x + neg y.

#val neg : t -> t

neg x is logical_not x for booleans and x * -1 otherwise.

#val div : ?rounding:[ `Trunc | `Floor ] -> t -> t -> t

div ~rounding x y divides integers by Op.t.Truncdiv or Op.t.Div when rounding; otherwise it multiplies x, as a float, by y's reciprocal, then rounds as asked.

#val mod_ : t -> t -> t

mod_ x y is the remainder of div ~rounding:`Floor x y.

#val fmod : t -> t -> t

fmod x y is the remainder of div ~rounding:`Trunc x y.

#val floor : t -> t

floor x is trunc x, less one where that is above x.

#val pow : t -> t -> t

pow x y is Op.t.Pow of x and y: promoted to a common type, but for an integer x and a weak integer y, which keeps its value at no width (Op.t.Pow's exponent is a number).

Raises Invalid_argument if the promoted type is not a float and y is a negative integer constant.

#val gt : t -> t -> t

gt x y is lt y x.

#val le : t -> t -> t

le x y is logical_not (gt x y).

#val ge : t -> t -> t

ge x y is logical_not (lt x y).

#val ne : t -> t -> t

ne x y is logical_not (eq x y).

#val logical_not : t -> t

logical_not x is eq (cast x Bool) false.

#val bitwise_not : t -> t

bitwise_not x is logical_not x for booleans, and x xor all ones otherwise.

#val where : t -> t -> t -> t

where c x y is x where c holds and y elsewhere.

#val minimum : t -> t -> t

minimum x y is neg (maximum (neg x) (neg y)) for floats; for integers, the same through an order-reversing xor.

#module O : sig ... end

Operators, for local opening: O.(x + int 4 * y). Each is the function of the same meaning: + is add, // is div ~rounding:`Floor, % is mod_, <> is ne, land, lor, lxor and lnot are the bitwise operations, lsl and lsr the shifts.