1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
function [opname optype ztype xtype ytype] = GB_spec_operator (op,optype_default)
%GB_SPEC_OPERATOR get the contents of an operator
%
% On input, op can be a struct with a string op.opname that gives the operator
% name, and a string op.optype with the operator type. Alternatively, op can
% be a string with the operator name, in which case the operator type is given
% by optype_default.
%
% ztype, xtype, and ytype are the types of z, x, and y for z = f(x,y), if
% f is a binary operator, or z = f(x) if f is a unary operator.
% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2020, All Rights Reserved.
% http://suitesparse.com See GraphBLAS/Doc/License.txt for license.
if (isempty (op))
% No operator has been defined; return an empty operator. GB_spec_accum
% uses this condition just like the (accum == NULL) condition in the C
% version of GraphBLAS. It means C<Mask>=T is to be done instead of
% C<Mask>=accum(C,T).
opname = '' ;
optype = '' ;
xtype = '' ;
ytype = '' ;
ztype = '' ;
return
elseif (isstruct (op))
% op is a struct with opname and optype
opname = op.opname ;
optype = op.optype ;
else
% op is a string, use the default optype unless the op is just logical
opname = op ;
optype = optype_default ;
end
% xtype is always the optype
xtype = optype ;
% for binary ops: ytype is usually the optype, except for bitshift.
% for unary ops: ytype is 'none'.
ytype = optype ;
% ztype is usually the optype, except for the cases below
ztype = optype ;
is_float = contains (optype, 'single') || contains (optype, 'double') ;
is_complex = contains (optype, 'complex') ;
is_int = contains (optype, 'int') ; % int or uint
is_logical = isequal (optype, 'logical') ;
is_real_float = is_float && ~is_complex ;
%-------------------------------------------------------------------------------
% boolean rename:
%-------------------------------------------------------------------------------
if (is_logical)
switch (opname)
case { 'div' }
opname = 'first' ;
case { 'rdiv' }
opname = 'second' ;
case { 'min', 'times' }
opname = 'and' ;
case { 'max', 'plus' }
opname = 'or' ;
case { 'minus', 'rminus', 'isne', 'ne' }
opname = 'xor' ;
case { 'iseq' }
opname = 'eq' ;
case { 'isgt' }
opname = 'gt' ;
case { 'islt' }
opname = 'lt' ;
case { 'isge', 'pow' }
opname = 'ge' ;
case { 'isle' }
opname = 'le' ;
otherwise
% op not renamed
end
end
%-------------------------------------------------------------------------------
% get the x,y,z types of the operator and check if valid
%-------------------------------------------------------------------------------
switch opname
%--------------------------------------------------------------------------
% binary ops for all 13 types
%--------------------------------------------------------------------------
case { 'first', 'second', 'pow', 'plus', 'minus', 'times', 'div', ...
'rminus', 'rdiv', 'pair', 'any', 'iseq', 'isne' }
% x,y,z types are all the same.
case { 'eq', 'ne' }
% x,y types the the same, z is logical
ztype = 'logical' ;
%--------------------------------------------------------------------------
% binary ops for 11 types (all but complex)
%--------------------------------------------------------------------------
case { 'max', 'min', 'isgt', 'islt', 'isge', 'isle', 'or', 'and', 'xor' }
% x,y,z types are all the same.
% available for 11 real types, not complex
if (is_complex)
error ('invalid op') ;
end
case { 'gt', 'lt', 'ge', 'le' }
% x,y types the the same, z is logical
% available for 11 real types, not complex
ztype = 'logical' ;
if (is_complex)
error ('invalid op') ;
end
%--------------------------------------------------------------------------
% binary ops for real floating point
%--------------------------------------------------------------------------
case { 'atan2', 'hypot', 'fmod', 'remainder', 'ldexp', 'copysign' }
% x,y,z types are all the same.
% available for real float and double only
if (~is_real_float)
error ('invalid op') ;
end
case { 'cmplx', 'complex' }
% x and y are real (float or double). z is the corresponding complex
if (~is_real_float)
error ('invalid op') ;
elseif (isequal (optype, 'single'))
ztype = 'single complex' ;
else
ztype = 'double complex' ;
end
opname = 'cmplx' ;
%--------------------------------------------------------------------------
% binary ops for integer only
%--------------------------------------------------------------------------
case { 'bitor', 'bitand', 'bitxor', 'bitxnor', ...
'bitget', 'bitset', 'bitclr', ...
'bor', 'band', 'bxor', 'bxnor', ...
'bget', 'bset', 'bclr' }
% x,y,z types are all the same.
% available for int and uint only
if (~(is_int))
error ('invalid op') ;
end
switch (opname)
case { 'bitor', 'bor' }
opname = 'bor' ;
case { 'bitand', 'band' }
opname = 'band' ;
case { 'bitxor', 'bxor' }
opname = 'bxor' ;
case { 'bitxnor', 'bxnor' }
opname = 'bxnor' ;
case { 'bitget', 'bget' }
opname = 'bget' ;
case { 'bitset', 'bset' }
opname = 'bset' ;
case { 'bitclr', 'bclr' }
opname = 'bclr' ;
end
case { 'bitshift' , 'bshift' }
% x,z types are the same. y is int8
% available for int and uint only
ytype = 'int8' ;
if (~(is_int))
error ('invalid op') ;
end
%--------------------------------------------------------------------------
% unary ops for all 13 types
%--------------------------------------------------------------------------
case { 'identity', 'one', 'ainv', 'minv' }
% x,y,z types are all the same.
case { 'abs' }
% x,y the same. z is the same as x, except if x is complex
if (isequal (optype, 'single complex'))
ztype = 'single' ;
elseif (isequal (optype, 'double complex'))
ztype = 'double' ;
else
ztype = xtype ;
end
%--------------------------------------------------------------------------
% unary ops for 11 real types
%--------------------------------------------------------------------------
case { 'not' }
% x and z have the same type
if (is_complex)
error ('invalid op') ;
end
ytype = 'none' ;
%--------------------------------------------------------------------------
% unary ops for integer only
%--------------------------------------------------------------------------
case { 'bitnot', 'bnot', 'bitcmp', 'bcmp' }
% x and z have the same type
if (~is_int)
error ('invalid op') ;
end
ytype = 'none' ;
opname = 'bnot' ;
%--------------------------------------------------------------------------
% unary ops for floating-point only (both real and complex)
%--------------------------------------------------------------------------
case { 'sqrt', 'log', 'exp', 'log2', ...
'sin', 'cos', 'tan', ...
'acos', 'asin', 'atan', ...
'sinh', 'cosh', 'tanh', ...
'acosh', 'asinh', 'atanh', ...
'ceil', 'floor', 'round', 'trunc', ...
'exp2', 'expm1', 'log10', 'log1p', ...
'signum' }
% x and z have the same type
if (~is_float)
error ('invalid op') ;
end
ytype = 'none' ;
case { 'isinf', 'isnan', 'isfinite' }
% z is logical
ztype = 'logical' ;
if (~is_float)
error ('invalid op') ;
end
ytype = 'none' ;
%--------------------------------------------------------------------------
% unary ops for real floating-point only
%--------------------------------------------------------------------------
case { 'lgamma', 'tgamma', 'erf', 'erfc', 'frexpx', 'frexpe' }
% x and z have the same type
if (~is_real_float)
error ('invalid op') ;
end
ytype = 'none' ;
%--------------------------------------------------------------------------
% unary ops for complex only
%--------------------------------------------------------------------------
case { 'conj' }
if (~is_complex)
error ('invalid op') ;
end
ytype = 'none' ;
case { 'real', 'imag', 'carg' }
if (~is_complex)
error ('invalid op') ;
end
if (isequal (optype, 'single complex'))
ztype = 'single' ;
else
ztype = 'double' ;
end
ytype = 'none' ;
%--------------------------------------------------------------------------
otherwise
error ('unknown op') ;
end
|