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 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
|
function C = GB_spec_op (op, A, B)
%GB_SPEC_OP apply a unary or binary operator
%
% Apply a binary operator z = f (x,y) element-wise to x and y, or a unary
% operator z = f(x) just x. The operator op is any built-in GraphBLAS
% operator.
%
% op or op.opname is a string with just the operator name. Valid names of
% binary operators are 'first', 'second', 'min', 'max', 'plus', 'minus',
% 'rminus', 'times', 'div', 'rdiv', 'eq', 'ne', 'gt', 'lt', 'ge', 'le', 'or',
% 'and', 'xor'. 'iseq', 'isne', 'isgt', 'islt', 'isge', 'le', 'pair' (same
% as 'oneb'), 'any', 'pow', ('bitget' or 'bget'), ('bitset' or 'bset'),
% ('bitclr' or 'bclr'), ('bitand' or 'band'), ('bitor' or 'bor'), ('bitxor' or
% 'bxor'), ('bitxnor', 'bxnor'), ('bitshift' or 'bshift'), ('bitnot' or
% 'bitcmp'), 'atan2', 'hypot', ('ldexp' or 'pow2'), ('complex', 'cmplx').
%
% Unary operators are 'one', 'identity', 'ainv', 'abs', 'minv', 'not', 'bnot',
% 'sqrt', 'log', 'exp', 'sin', 'cos', 'tan', 'asin', 'acos', 'atan', 'sinh',
% 'cosh', 'tanh', 'asinh', 'acosh', 'atanh', 'signum', 'ceil', 'floor',
% 'round', ('trunc' or 'fix'), 'exp2', 'expm1', 'log10', 'log2', ('lgamma' or
% 'gammaln'), ('tgamma' or 'gamma'), 'erf', 'erfc', 'frexpx', 'frexpe', 'conj',
% ('creal' or 'real'), ('cimag' or 'imag'), ('carg' or 'angle'), 'isinf',
% 'isnan', 'isfinite', 'cbrt'.
%
% op.optype: 'logical', 'int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32',
% 'int64', 'uint64', 'single', 'double', 'single complex' or 'double complex'.
%
% The class of z is the same as the class of the output of the operator, which
% is op.optype except for: (1) 'eq', 'ne', 'gt', 'lt', 'ge', 'le', in which
% case z is logical, (2) 'complex', where x and y are real and z is complex,
% (3) bitshift (where x has the optype and y is int8).
%
% Intrinsic built-in operators are used as much as possible, so as to test
% GraphBLAS operators. Some must be done in GraphBLAS because the typecasting,
% divide-by-zero and overflow rules for integers differs.
%
% Positional ops are not computed by this function.
% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2022, All Rights Reserved.
% SPDX-License-Identifier: Apache-2.0
% get the operator name and class
[opname optype ztype xtype ytype] = GB_spec_operator (op, GB_spec_type (A)) ;
if (GB_spec_is_positional (opname))
error ('positional op not supported by this funciton') ;
end
% cast the inputs A and B to the inputs of the operator
if (~isequal (GB_spec_type (A), xtype))
x = GB_mex_cast (A, xtype) ;
else
x = A ;
end
use_builtin = (isa (x, 'float') && ...
(test_contains (optype, 'single') || test_contains (optype, 'double'))) ;
if (nargin > 2 && ~ischar (B))
if (~isequal (GB_spec_type (B), ytype))
y = GB_mex_cast (B, ytype) ;
else
y = B ;
end
use_builtin = use_builtin && isa (y, 'float') ;
end
switch opname
% binary operators, result is ztype
case 'first'
z = x ;
case 'second'
z = y ;
case 'any'
z = y ;
case { 'pair', 'oneb' }
z = GB_spec_ones (size (x), ztype) ;
case 'min'
% min(x,y) in SuiteSparse:GraphBLAS is min(x,y,'omitnan') with built-in.
% see discussion in SuiteSparse/GraphBLAS/Source/GB.h
% z = min (x,y,'omitnan') ;
z = GB_mex_op (op, x, y) ;
case 'max'
% z = max (x,y,'omitnan') ;
z = GB_mex_op (op, x, y) ;
case 'plus'
if (use_builtin)
z = x + y ;
else
z = GB_mex_op (op, x, y) ;
end
case 'minus'
if (use_builtin)
z = x - y ;
else
z = GB_mex_op (op, x, y) ;
end
case 'rminus'
if (use_builtin)
z = y - x ;
else
z = GB_mex_op (op, x, y) ;
end
case 'times'
if (use_builtin)
z = x .* y ;
else
z = GB_mex_op (op, x, y) ;
end
case 'div'
if (use_builtin)
z = x ./ y ;
else
z = GB_mex_op (op, x, y) ;
end
case 'rdiv'
if (use_builtin)
z = y ./ x ;
else
z = GB_mex_op (op, x, y) ;
end
case 'pow'
if (use_builtin)
z = x .^ y ;
else
z = GB_mex_op (op, x, y) ;
end
% 6 binary comparators (result is ztype)
case 'iseq'
z = GB_mex_cast (x == y, ztype) ;
case 'isne'
z = GB_mex_cast (x ~= y, ztype) ;
case 'isgt'
z = GB_mex_cast (x > y, ztype) ;
case 'islt'
z = GB_mex_cast (x < y, ztype) ;
case 'isge'
z = GB_mex_cast (x >= y, ztype) ;
case 'isle'
z = GB_mex_cast (x <= y, ztype) ;
% 6 binary comparators (result is boolean)
case 'eq'
z = (x == y) ;
case 'ne'
z = (x ~= y) ;
case 'gt'
z = (x > y) ;
case 'lt'
z = (x < y) ;
case 'ge'
z = (x >= y) ;
case 'le'
z = (x <= y) ;
% 6 index_unop comparators (result is boolean)
case 'valueeq'
z = (x == y) ;
case 'valuene'
z = (x ~= y) ;
case 'valuegt'
z = (x > y) ;
case 'valuelt'
z = (x < y) ;
case 'valuege'
z = (x >= y) ;
case 'valuele'
z = (x <= y) ;
% 3 binary logical operators (result is ztype)
case 'or'
z = GB_mex_cast ((x ~= 0) | (y ~= 0), ztype) ;
case 'and'
z = GB_mex_cast ((x ~= 0) & (y ~= 0), ztype) ;
case 'xor'
z = GB_mex_cast ((x ~= 0) ~= (y ~= 0), ztype) ;
% bitwise operators
case { 'bitget', 'bget' }
bits = GB_spec_nbits (ztype) ;
m = (y > 0 & y <= bits) ;
t = bitget (x (m), y (m), ztype) ;
z = zeros (size (x), ztype) ;
z (m) = t ;
case { 'bitset', 'bset' }
bits = GB_spec_nbits (ztype) ;
m = (y > 0 & y <= bits) ;
t = bitset (x (m), y (m), 1, ztype) ;
z = x ;
z (m) = t ;
case { 'bitclr', 'bclr' }
bits = GB_spec_nbits (ztype) ;
m = (y > 0 & y <= bits) ;
t = bitset (x (m), y (m), 0, ztype) ;
z = x ;
z (m) = t ;
case { 'bitand', 'band' }
z = bitand (x, y, ztype) ;
case { 'bitor', 'bor' }
z = bitor (x, y, ztype) ;
case { 'bitxor', 'bxor' }
z = bitxor (x, y, ztype) ;
case { 'bitxnor', 'bxnor' }
z = bitcmp (bitxor (x, y, ztype), ztype) ;
case { 'bitshift', 'bshift' }
z = bitshift (x, y, ztype) ;
case { 'bitnot', 'bitcmp', 'bnot', 'bcmp' }
z = bitcmp (x, ztype) ;
case 'atan2'
z = atan2 (x,y) ;
case 'hypot'
z = hypot (x,y) ;
case { 'ldexp', 'pow2' }
z = pow2 (x,y) ;
case { 'fmod', 'rem' }
% see ANSI C11 fmod function
% the built-in rem differs slightly from the ANSI C11 fmod,
% if x/y is O(eps) smaller than an integer.
z = rem (x,y) ;
case { 'remainder' }
% see ANSI C11 remainder function
m = (y ~= 0 & x ~= y) ;
z = nan (size (x), ztype) ;
z (x == y) = 0 ;
z (m) = x (m) - round (x (m) ./ y (m)) .* y (m) ;
case { 'copysign' }
% see ANSI C11 copysign function
z = abs (x) .* (2 * double (y >= 0) - 1) ;
case { 'complex', 'cmplx' }
z = complex (x,y) ;
% unary operators (result is ztype)
case 'one'
z = GB_mex_cast (1, ztype) ;
case 'identity'
z = x ;
case 'ainv'
if (use_builtin)
z = -x ;
else
z = GB_mex_op (op, x) ;
end
case 'abs'
if (use_builtin)
z = abs (x) ;
else
z = GB_mex_op (op, x) ;
end
case 'minv'
if (use_builtin)
z = 1 ./ x ;
else
z = GB_mex_op (op, x) ;
end
case 'not'
z = GB_mex_cast (~(x ~= 0), ztype) ;
case 'bnot'
z = bitcmp (x) ;
case 'sqrt'
z = sqrt (x) ;
case 'log'
z = log (x) ;
case 'exp'
z = exp (x) ;
case 'sin'
z = sin (x) ;
case 'cos'
z = cos (x) ;
case 'tan'
z = tan (x) ;
case 'asin'
z = asin (x) ;
case 'acos'
z = acos (x) ;
case 'atan'
z = atan (x) ;
case 'sinh'
z = sinh (x) ;
case 'cosh'
z = cosh (x) ;
case 'tanh'
z = tanh (x) ;
case 'asinh'
z = asinh (x) ;
case 'acosh'
z = acosh (x) ;
case 'atanh'
z = atanh (x) ;
case { 'sign', 'signum' }
z = sign (x) ;
case 'ceil'
z = ceil (x) ;
case 'floor'
z = floor (x) ;
case 'round'
z = round (x) ;
case { 'trunc', 'fix' }
z = fix (x) ;
case { 'exp2' }
z = 2.^x ;
case 'expm1'
z = expm1 (x) ;
case 'log10'
z = log10 (x) ;
case 'log2'
z = log2 (x) ;
case 'log1p'
z = log1p (x) ;
case { 'lgamma', 'gammaln' }
z = gammaln (x) ;
case { 'tgamma', 'gamma' }
z = gamma (x) ;
case 'erf'
z = erf (x) ;
case 'erfc'
z = erfc (x) ;
case 'cbrt'
z = nthroot (x, 3) ;
case 'frexpx'
[z,~] = log2 (x) ;
case 'frexpe'
[~,z] = log2 (x) ;
case 'conj'
z = conj (x) ;
case { 'creal', 'real' }
z = real (x) ;
case { 'cimag', 'imag' }
z = imag (x) ;
case { 'carg', 'angle' }
z = angle (x) ;
case 'isinf'
z = isinf (x) ;
case 'isnan'
z = isnan (x) ;
case 'isfinite'
z = isfinite (x) ;
otherwise
opname
error ('unknown op') ;
end
if (~isequal (ztype, GB_spec_type (z)))
z = GB_mex_cast (z, ztype) ;
end
C = z ;
|