Skip to content

BinaryOp

In mathematics, a binary operation or dyadic operation is a rule for combining two elements (called operands) to produce another element. More formally, a binary operation is an operation of arity two.

User Defined Binary Operators

User defined functions can be registered with OneSparse by inserting them into the onesparse.user_defined_binaryop table. They can then be JIT compiled into kernels and used in any function that takes a binaryop argument:

show onesparse.jit_control;  -- This must be set to 'on' in postgres config.
┌───────────────────────┐
│ onesparse.jit_control │
├───────────────────────┤
│ on                    │
└───────────────────────┘
(1 row)

Insert the expected function types and function body into the table. Here is an example function that computes the Hamming Distance between two elements. See the SuiteSparse User Guide for more details:

insert into user_defined_binaryop (name, ztype, xtype, ytype, func) VALUES
    ('binary_hamming_dist', 'int64', 'int64', 'int64',
    $$
    void binary_hamming_dist (int64_t *z, int64_t *x, int64_t *y) {
        (*z) = __builtin_popcountll((*x)^(*y));
       };
    $$);
The new operator can now be used in functions that take binaryop operators like eadd:
select eadd(random_vector('int64', 16, 'inf', 42),
            random_vector('int64', 16, 'inf', 43),
            'binary_hamming_dist');
┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                               eadd                                               │
├──────────────────────────────────────────────────────────────────────────────────────────────────┤
│ int64(16)[0:32 1:31 2:31 3:30 4:36 5:37 6:30 7:35 8:35 9:27 10:33 11:28 12:32 13:37 14:36 15:32] │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘
(1 row)

Built-in Operators

OneSparse Name SuiteSparse Name
first_bool GrB_FIRST_BOOL
second_bool GrB_SECOND_BOOL
oneb_bool GrB_ONEB_BOOL
first_int8 GrB_FIRST_INT8
second_int8 GrB_SECOND_INT8
oneb_int8 GrB_ONEB_INT8
first_int16 GrB_FIRST_INT16
second_int16 GrB_SECOND_INT16
oneb_int16 GrB_ONEB_INT16
first_int32 GrB_FIRST_INT32
second_int32 GrB_SECOND_INT32
oneb_int32 GrB_ONEB_INT32
first_int64 GrB_FIRST_INT64
second_int64 GrB_SECOND_INT64
oneb_int64 GrB_ONEB_INT64
first_uint8 GrB_FIRST_UINT8
second_uint8 GrB_SECOND_UINT8
oneb_uint8 GrB_ONEB_UINT8
first_uint16 GrB_FIRST_UINT16
second_uint16 GrB_SECOND_UINT16
oneb_uint16 GrB_ONEB_UINT16
first_uint32 GrB_FIRST_UINT32
second_uint32 GrB_SECOND_UINT32
oneb_uint32 GrB_ONEB_UINT32
first_uint64 GrB_FIRST_UINT64
second_uint64 GrB_SECOND_UINT64
oneb_uint64 GrB_ONEB_UINT64
first_fp32 GrB_FIRST_FP32
second_fp32 GrB_SECOND_FP32
oneb_fp32 GrB_ONEB_FP32
first_fp64 GrB_FIRST_FP64
second_fp64 GrB_SECOND_FP64
oneb_fp64 GrB_ONEB_FP64
first_fc32 GxB_FIRST_FC32
second_fc32 GxB_SECOND_FC32
oneb_fc32 GxB_ONEB_FC32
first_fc64 GxB_FIRST_FC64
second_fc64 GxB_SECOND_FC64
oneb_fc64 GxB_ONEB_FC64
pow_bool GxB_POW_BOOL
pow_int8 GxB_POW_INT8
pow_int16 GxB_POW_INT16
pow_int32 GxB_POW_INT32
pow_int64 GxB_POW_INT64
pow_uint8 GxB_POW_UINT8
pow_uint16 GxB_POW_UINT16
pow_uint32 GxB_POW_UINT32
pow_uint64 GxB_POW_UINT64
pow_fp32 GxB_POW_FP32
pow_fp64 GxB_POW_FP64
pow_fc32 GxB_POW_FC32
pow_fc64 GxB_POW_FC64
plus_bool GrB_PLUS_BOOL
minus_bool GrB_MINUS_BOOL
times_bool GrB_TIMES_BOOL
div_bool GrB_DIV_BOOL
plus_int8 GrB_PLUS_INT8
minus_int8 GrB_MINUS_INT8
times_int8 GrB_TIMES_INT8
div_int8 GrB_DIV_INT8
plus_int16 GrB_PLUS_INT16
minus_int16 GrB_MINUS_INT16
times_int16 GrB_TIMES_INT16
div_int16 GrB_DIV_INT16
plus_int32 GrB_PLUS_INT32
minus_int32 GrB_MINUS_INT32
times_int32 GrB_TIMES_INT32
div_int32 GrB_DIV_INT32
plus_int64 GrB_PLUS_INT64
minus_int64 GrB_MINUS_INT64
times_int64 GrB_TIMES_INT64
div_int64 GrB_DIV_INT64
plus_uint8 GrB_PLUS_UINT8
minus_uint8 GrB_MINUS_UINT8
times_uint8 GrB_TIMES_UINT8
div_uint8 GrB_DIV_UINT8
plus_uint16 GrB_PLUS_UINT16
minus_uint16 GrB_MINUS_UINT16
times_uint16 GrB_TIMES_UINT16
div_uint16 GrB_DIV_UINT16
plus_uint32 GrB_PLUS_UINT32
minus_uint32 GrB_MINUS_UINT32
times_uint32 GrB_TIMES_UINT32
div_uint32 GrB_DIV_UINT32
plus_uint64 GrB_PLUS_UINT64
minus_uint64 GrB_MINUS_UINT64
times_uint64 GrB_TIMES_UINT64
div_uint64 GrB_DIV_UINT64
plus_fp32 GrB_PLUS_FP32
minus_fp32 GrB_MINUS_FP32
times_fp32 GrB_TIMES_FP32
div_fp32 GrB_DIV_FP32
plus_fp64 GrB_PLUS_FP64
minus_fp64 GrB_MINUS_FP64
times_fp64 GrB_TIMES_FP64
div_fp64 GrB_DIV_FP64
plus_fc32 GxB_PLUS_FC32
minus_fc32 GxB_MINUS_FC32
times_fc32 GxB_TIMES_FC32
div_fc32 GxB_DIV_FC32
plus_fc64 GxB_PLUS_FC64
minus_fc64 GxB_MINUS_FC64
times_fc64 GxB_TIMES_FC64
div_fc64 GxB_DIV_FC64
rminus_bool GxB_RMINUS_BOOL
rdiv_bool GxB_RDIV_BOOL
pair_bool GxB_PAIR_BOOL
any_bool GxB_ANY_BOOL
rminus_int8 GxB_RMINUS_INT8
rdiv_int8 GxB_RDIV_INT8
pair_int8 GxB_PAIR_INT8
any_int8 GxB_ANY_INT8
rminus_int16 GxB_RMINUS_INT16
rdiv_int16 GxB_RDIV_INT16
pair_int16 GxB_PAIR_INT16
any_int16 GxB_ANY_INT16
rminus_int32 GxB_RMINUS_INT32
rdiv_int32 GxB_RDIV_INT32
pair_int32 GxB_PAIR_INT32
any_int32 GxB_ANY_INT32
rminus_int64 GxB_RMINUS_INT64
rdiv_int64 GxB_RDIV_INT64
pair_int64 GxB_PAIR_INT64
any_int64 GxB_ANY_INT64
rminus_uint8 GxB_RMINUS_UINT8
rdiv_uint8 GxB_RDIV_UINT8
pair_uint8 GxB_PAIR_UINT8
any_uint8 GxB_ANY_UINT8
rminus_uint16 GxB_RMINUS_UINT16
rdiv_uint16 GxB_RDIV_UINT16
pair_uint16 GxB_PAIR_UINT16
any_uint16 GxB_ANY_UINT16
rminus_uint32 GxB_RMINUS_UINT32
rdiv_uint32 GxB_RDIV_UINT32
pair_uint32 GxB_PAIR_UINT32
any_uint32 GxB_ANY_UINT32
rminus_uint64 GxB_RMINUS_UINT64
rdiv_uint64 GxB_RDIV_UINT64
pair_uint64 GxB_PAIR_UINT64
any_uint64 GxB_ANY_UINT64
rminus_fp32 GxB_RMINUS_FP32
rdiv_fp32 GxB_RDIV_FP32
pair_fp32 GxB_PAIR_FP32
any_fp32 GxB_ANY_FP32
rminus_fp64 GxB_RMINUS_FP64
rdiv_fp64 GxB_RDIV_FP64
pair_fp64 GxB_PAIR_FP64
any_fp64 GxB_ANY_FP64
rminus_fc32 GxB_RMINUS_FC32
rdiv_fc32 GxB_RDIV_FC32
pair_fc32 GxB_PAIR_FC32
any_fc32 GxB_ANY_FC32
rminus_fc64 GxB_RMINUS_FC64
rdiv_fc64 GxB_RDIV_FC64
pair_fc64 GxB_PAIR_FC64
any_fc64 GxB_ANY_FC64
iseq_bool GxB_ISEQ_BOOL
isne_bool GxB_ISNE_BOOL
iseq_int8 GxB_ISEQ_INT8
isne_int8 GxB_ISNE_INT8
iseq_int16 GxB_ISEQ_INT16
isne_int16 GxB_ISNE_INT16
iseq_int32 GxB_ISEQ_INT32
isne_int32 GxB_ISNE_INT32
iseq_int64 GxB_ISEQ_INT64
isne_int64 GxB_ISNE_INT64
iseq_uint8 GxB_ISEQ_UINT8
isne_uint8 GxB_ISNE_UINT8
iseq_uint16 GxB_ISEQ_UINT16
isne_uint16 GxB_ISNE_UINT16
iseq_uint32 GxB_ISEQ_UINT32
isne_uint32 GxB_ISNE_UINT32
iseq_uint64 GxB_ISEQ_UINT64
isne_uint64 GxB_ISNE_UINT64
iseq_fp32 GxB_ISEQ_FP32
isne_fp32 GxB_ISNE_FP32
iseq_fp64 GxB_ISEQ_FP64
isne_fp64 GxB_ISNE_FP64
iseq_fc32 GxB_ISEQ_FC32
isne_fc32 GxB_ISNE_FC32
iseq_fc64 GxB_ISEQ_FC64
isne_fc64 GxB_ISNE_FC64
isgt_bool GxB_ISGT_BOOL
islt_bool GxB_ISLT_BOOL
isge_bool GxB_ISGE_BOOL
isle_bool GxB_ISLE_BOOL
isgt_int8 GxB_ISGT_INT8
islt_int8 GxB_ISLT_INT8
isge_int8 GxB_ISGE_INT8
isle_int8 GxB_ISLE_INT8
isgt_int16 GxB_ISGT_INT16
islt_int16 GxB_ISLT_INT16
isge_int16 GxB_ISGE_INT16
isle_int16 GxB_ISLE_INT16
isgt_int32 GxB_ISGT_INT32
islt_int32 GxB_ISLT_INT32
isge_int32 GxB_ISGE_INT32
isle_int32 GxB_ISLE_INT32
isgt_int64 GxB_ISGT_INT64
islt_int64 GxB_ISLT_INT64
isge_int64 GxB_ISGE_INT64
isle_int64 GxB_ISLE_INT64
isgt_uint8 GxB_ISGT_UINT8
islt_uint8 GxB_ISLT_UINT8
isge_uint8 GxB_ISGE_UINT8
isle_uint8 GxB_ISLE_UINT8
isgt_uint16 GxB_ISGT_UINT16
islt_uint16 GxB_ISLT_UINT16
isge_uint16 GxB_ISGE_UINT16
isle_uint16 GxB_ISLE_UINT16
isgt_uint32 GxB_ISGT_UINT32
islt_uint32 GxB_ISLT_UINT32
isge_uint32 GxB_ISGE_UINT32
isle_uint32 GxB_ISLE_UINT32
isgt_uint64 GxB_ISGT_UINT64
islt_uint64 GxB_ISLT_UINT64
isge_uint64 GxB_ISGE_UINT64
isle_uint64 GxB_ISLE_UINT64
isgt_fp32 GxB_ISGT_FP32
islt_fp32 GxB_ISLT_FP32
isge_fp32 GxB_ISGE_FP32
isle_fp32 GxB_ISLE_FP32
isgt_fp64 GxB_ISGT_FP64
islt_fp64 GxB_ISLT_FP64
isge_fp64 GxB_ISGE_FP64
isle_fp64 GxB_ISLE_FP64
min_bool GrB_MIN_BOOL
max_bool GrB_MAX_BOOL
min_int8 GrB_MIN_INT8
max_int8 GrB_MAX_INT8
min_int16 GrB_MIN_INT16
max_int16 GrB_MAX_INT16
min_int32 GrB_MIN_INT32
max_int32 GrB_MAX_INT32
min_int64 GrB_MIN_INT64
max_int64 GrB_MAX_INT64
min_uint8 GrB_MIN_UINT8
max_uint8 GrB_MAX_UINT8
min_uint16 GrB_MIN_UINT16
max_uint16 GrB_MAX_UINT16
min_uint32 GrB_MIN_UINT32
max_uint32 GrB_MAX_UINT32
min_uint64 GrB_MIN_UINT64
max_uint64 GrB_MAX_UINT64
min_fp32 GrB_MIN_FP32
max_fp32 GrB_MAX_FP32
min_fp64 GrB_MIN_FP64
max_fp64 GrB_MAX_FP64
lor_bool GxB_LOR_BOOL
land_bool GxB_LAND_BOOL
lxor_bool GxB_LXOR_BOOL
lor_int8 GxB_LOR_INT8
land_int8 GxB_LAND_INT8
lxor_int8 GxB_LXOR_INT8
lor_int16 GxB_LOR_INT16
land_int16 GxB_LAND_INT16
lxor_int16 GxB_LXOR_INT16
lor_int32 GxB_LOR_INT32
land_int32 GxB_LAND_INT32
lxor_int32 GxB_LXOR_INT32
lor_int64 GxB_LOR_INT64
land_int64 GxB_LAND_INT64
lxor_int64 GxB_LXOR_INT64
lor_uint8 GxB_LOR_UINT8
land_uint8 GxB_LAND_UINT8
lxor_uint8 GxB_LXOR_UINT8
lor_uint16 GxB_LOR_UINT16
land_uint16 GxB_LAND_UINT16
lxor_uint16 GxB_LXOR_UINT16
lor_uint32 GxB_LOR_UINT32
land_uint32 GxB_LAND_UINT32
lxor_uint32 GxB_LXOR_UINT32
lor_uint64 GxB_LOR_UINT64
land_uint64 GxB_LAND_UINT64
lxor_uint64 GxB_LXOR_UINT64
lor_fp32 GxB_LOR_FP32
land_fp32 GxB_LAND_FP32
lxor_fp32 GxB_LXOR_FP32
lor_fp64 GxB_LOR_FP64
land_fp64 GxB_LAND_FP64
lxor_fp64 GxB_LXOR_FP64
lor GrB_LOR
land GrB_LAND
lxor GrB_LXOR
lxnor GrB_LXNOR
atan2_fp32 GxB_ATAN2_FP32
hypot_fp32 GxB_HYPOT_FP32
fmod_fp32 GxB_FMOD_FP32
remainder_fp32 GxB_REMAINDER_FP32
atan2_fp64 GxB_ATAN2_FP64
hypot_fp64 GxB_HYPOT_FP64
fmod_fp64 GxB_FMOD_FP64
remainder_fp64 GxB_REMAINDER_FP64
ldexp_fp32 GxB_LDEXP_FP32
copysign_fp32 GxB_COPYSIGN_FP32
ldexp_fp64 GxB_LDEXP_FP64
copysign_fp64 GxB_COPYSIGN_FP64
bget_int8 GxB_BGET_INT8
bset_int8 GxB_BSET_INT8
bclr_int8 GxB_BCLR_INT8
bget_int16 GxB_BGET_INT16
bset_int16 GxB_BSET_INT16
bclr_int16 GxB_BCLR_INT16
bget_int32 GxB_BGET_INT32
bset_int32 GxB_BSET_INT32
bclr_int32 GxB_BCLR_INT32
bget_int64 GxB_BGET_INT64
bset_int64 GxB_BSET_INT64
bclr_int64 GxB_BCLR_INT64
bget_uint8 GxB_BGET_UINT8
bset_uint8 GxB_BSET_UINT8
bclr_uint8 GxB_BCLR_UINT8
bget_uint16 GxB_BGET_UINT16
bset_uint16 GxB_BSET_UINT16
bclr_uint16 GxB_BCLR_UINT16
bget_uint32 GxB_BGET_UINT32
bset_uint32 GxB_BSET_UINT32
bclr_uint32 GxB_BCLR_UINT32
bget_uint64 GxB_BGET_UINT64
bset_uint64 GxB_BSET_UINT64
bclr_uint64 GxB_BCLR_UINT64
bor_int8 GrB_BOR_INT8
band_int8 GrB_BAND_INT8
bxor_int8 GrB_BXOR_INT8
bxnor_int8 GrB_BXNOR_INT8
bor_int16 GrB_BOR_INT16
band_int16 GrB_BAND_INT16
bxor_int16 GrB_BXOR_INT16
bxnor_int16 GrB_BXNOR_INT16
bor_int32 GrB_BOR_INT32
band_int32 GrB_BAND_INT32
bxor_int32 GrB_BXOR_INT32
bxnor_int32 GrB_BXNOR_INT32
bor_int64 GrB_BOR_INT64
band_int64 GrB_BAND_INT64
bxor_int64 GrB_BXOR_INT64
bxnor_int64 GrB_BXNOR_INT64
bor_uint8 GrB_BOR_UINT8
band_uint8 GrB_BAND_UINT8
bxor_uint8 GrB_BXOR_UINT8
bxnor_uint8 GrB_BXNOR_UINT8
bor_uint16 GrB_BOR_UINT16
band_uint16 GrB_BAND_UINT16
bxor_uint16 GrB_BXOR_UINT16
bxnor_uint16 GrB_BXNOR_UINT16
bor_uint32 GrB_BOR_UINT32
band_uint32 GrB_BAND_UINT32
bxor_uint32 GrB_BXOR_UINT32
bxnor_uint32 GrB_BXNOR_UINT32
bor_uint64 GrB_BOR_UINT64
band_uint64 GrB_BAND_UINT64
bxor_uint64 GrB_BXOR_UINT64
bxnor_uint64 GrB_BXNOR_UINT64
bshift_int8 GxB_BSHIFT_INT8
bshift_int16 GxB_BSHIFT_INT16
bshift_int32 GxB_BSHIFT_INT32
bshift_int64 GxB_BSHIFT_INT64
bshift_uint8 GxB_BSHIFT_UINT8
bshift_uint16 GxB_BSHIFT_UINT16
bshift_uint32 GxB_BSHIFT_UINT32
bshift_uint64 GxB_BSHIFT_UINT64
eq_bool GrB_EQ_BOOL
ne_bool GrB_NE_BOOL
gt_bool GrB_GT_BOOL
lt_bool GrB_LT_BOOL
eq_int8 GrB_EQ_INT8
ne_int8 GrB_NE_INT8
gt_int8 GrB_GT_INT8
lt_int8 GrB_LT_INT8
eq_int16 GrB_EQ_INT16
ne_int16 GrB_NE_INT16
gt_int16 GrB_GT_INT16
lt_int16 GrB_LT_INT16
eq_int32 GrB_EQ_INT32
ne_int32 GrB_NE_INT32
gt_int32 GrB_GT_INT32
lt_int32 GrB_LT_INT32
eq_int64 GrB_EQ_INT64
ne_int64 GrB_NE_INT64
gt_int64 GrB_GT_INT64
lt_int64 GrB_LT_INT64
eq_uint8 GrB_EQ_UINT8
ne_uint8 GrB_NE_UINT8
gt_uint8 GrB_GT_UINT8
lt_uint8 GrB_LT_UINT8
eq_uint16 GrB_EQ_UINT16
ne_uint16 GrB_NE_UINT16
gt_uint16 GrB_GT_UINT16
lt_uint16 GrB_LT_UINT16
eq_uint32 GrB_EQ_UINT32
ne_uint32 GrB_NE_UINT32
gt_uint32 GrB_GT_UINT32
lt_uint32 GrB_LT_UINT32
eq_uint64 GrB_EQ_UINT64
ne_uint64 GrB_NE_UINT64
gt_uint64 GrB_GT_UINT64
lt_uint64 GrB_LT_UINT64
eq_fp32 GrB_EQ_FP32
ne_fp32 GrB_NE_FP32
gt_fp32 GrB_GT_FP32
lt_fp32 GrB_LT_FP32
eq_fp64 GrB_EQ_FP64
ne_fp64 GrB_NE_FP64
gt_fp64 GrB_GT_FP64
lt_fp64 GrB_LT_FP64
eq_fc32 GxB_EQ_FC32
ne_fc32 GxB_NE_FC32
eq_fc64 GxB_EQ_FC64
ne_fc64 GxB_NE_FC64
ge_bool GrB_GE_BOOL
le_bool GrB_LE_BOOL
ge_int8 GrB_GE_INT8
le_int8 GrB_LE_INT8
ge_int16 GrB_GE_INT16
le_int16 GrB_LE_INT16
ge_int32 GrB_GE_INT32
le_int32 GrB_LE_INT32
ge_int64 GrB_GE_INT64
le_int64 GrB_LE_INT64
ge_uint8 GrB_GE_UINT8
le_uint8 GrB_LE_UINT8
ge_uint16 GrB_GE_UINT16
le_uint16 GrB_LE_UINT16
ge_uint32 GrB_GE_UINT32
le_uint32 GrB_LE_UINT32
ge_uint64 GrB_GE_UINT64
le_uint64 GrB_LE_UINT64
ge_fp32 GrB_GE_FP32
le_fp32 GrB_LE_FP32
ge_fp64 GrB_GE_FP64
le_fp64 GrB_LE_FP64
cmplx_fp32 GxB_CMPLX_FP32
cmplx_fp64 GxB_CMPLX_FP64
firsti_int32 GxB_FIRSTI_INT32
firsti_int64 GxB_FIRSTI_INT64
firsti1_int32 GxB_FIRSTI1_INT32
firsti1_int64 GxB_FIRSTI1_INT64
firstj_int32 GxB_FIRSTJ_INT32
firstj_int64 GxB_FIRSTJ_INT64
firstj1_int32 GxB_FIRSTJ1_INT32
firstj1_int64 GxB_FIRSTJ1_INT64
secondi_int32 GxB_SECONDI_INT32
secondi_int64 GxB_SECONDI_INT64
secondi1_int32 GxB_SECONDI1_INT32
secondi1_int64 GxB_SECONDI1_INT64
secondj_int32 GxB_SECONDJ_INT32
secondj_int64 GxB_SECONDJ_INT64
secondj1_int32 GxB_SECONDJ1_INT32
secondj1_int64 GxB_SECONDJ1_INT64
ignore_dup GxB_IGNORE_DUP