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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
|
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-2022, All Rights Reserved.
% SPDX-License-Identifier: Apache-2.0
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
opname = op ;
if (nargin == 1 && GB_spec_is_positional (opname))
% optype_default is ignored
optype = 'int64' ;
else
optype = optype_default ;
end
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 = test_contains (optype, 'single') || test_contains (optype, 'double') ;
is_complex = test_contains (optype, 'complex') ;
is_int = test_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', 'oneb', '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', 'cbrt' }
% 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' ;
%--------------------------------------------------------------------------
% binary positional ops
%--------------------------------------------------------------------------
case { 'firsti' , 'firsti1' , 'firstj' , 'firstj1', ...
'secondi', 'secondi1', 'secondj', 'secondj1' } ;
if (~(isequal (ztype, 'int64') || isequal (ztype, 'int32')))
error ('invalid op') ;
end
xtype = optype ;
ytype = optype ;
%--------------------------------------------------------------------------
% unary positional ops
%--------------------------------------------------------------------------
case { 'positioni', 'positioni1', 'positionj', 'positionj1' }
if (~(isequal (ztype, 'int64') || isequal (ztype, 'int32')))
error ('invalid op') ;
end
ytype = optype ;
%--------------------------------------------------------------------------
% idxunop
%--------------------------------------------------------------------------
case { 'rowindex', 'colindex', 'diagindex' }
s = strcmp (optype, 'int64') || strcmp (optype, 'int32') ;
if (~s)
error ('invalid op') ;
end
xtype = '' ;
ytype = optype ;
ztype = optype ;
case { 'tril', 'triu', 'diag', 'offdiag', ...
'colle', 'colgt', 'rowle', 'rowgt' }
s = strcmp (optype, 'int64') ;
if (~s)
error ('invalid op') ;
end
xtype = '' ;
ytype = optype ;
ztype = 'logical' ;
case { 'valuene', 'valueeq' }
s = true ;
xtype = optype ;
ytype = optype ;
ztype = 'logical' ;
case { 'valuelt', 'valuele', 'valuegt', 'valuege' }
s = ~test_contains (optype, 'complex') ;
if (~s)
error ('invalid op') ;
end
xtype = optype ;
ytype = optype ;
ztype = 'logical' ;
otherwise
error ('unknown op') ;
end
|