1function C = gb_trig (op, G)
2%GB_TRIG inverse sine, cosine, log, sqrt, ... etc
3% Implements C = asin (G), C = acos (G), C = atanh (G), ... etc
4
5% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
6% SPDX-License-Identifier: GPL-3.0-or-later
7
8type = gbtype (G) ;
9
10if (~contains (type, 'complex'))
11
12    % determine if any entries are outside the domain for the real case
13    noutside = 0 ;  % default if no switch cases apply
14    switch (op)
15
16        case { 'asin', 'acos', 'atanh' }
17
18            % C is complex if any (abs (G) > 1)
19            switch (type)
20                case { 'int8', 'int16', 'int32', 'int64', 'single', 'double' }
21                    noutside = gbnvals (gbselect (gbapply ('abs', G), '>', 1)) ;
22                case { 'uint8', 'uint16', 'uint32', 'uint64' }
23                    noutside = gbnvals (gbselect (G, '>', 1)) ;
24            end
25
26        case { 'log', 'log10', 'sqrt', 'log2' }
27
28            % C is complex if any (G < 0)
29            switch (type)
30                case { 'int8', 'int16', 'int32', 'int64', 'single', 'double' }
31                    noutside = gbnvals (gbselect (G, '<', 0)) ;
32            end
33
34        case { 'log1p' }
35
36            % C is complex if any (G < -1)
37            switch (type)
38                case { 'int8', 'int16', 'int32', 'int64', 'single', 'double' }
39                    noutside = gbnvals (gbselect (G, '<', -1)) ;
40            end
41
42        case { 'acosh' }
43
44            % C is complex if any (G < 1)
45            noutside = gbnvals (gbselect (G, '<', 1)) ;
46    end
47
48    if (noutside > 0)
49        % G is real but C is complex
50        if (isequal (type, 'single'))
51            op = [op '.single complex'] ;
52        else
53            op = [op '.double complex'] ;
54        end
55    elseif (~gb_isfloat (type))
56        % G is integer or logical; use the op.double operator
57        op = [op '.double'] ;
58    end
59end
60
61% if G is already complex, gbapply will select a complex operator
62
63C = gbapply (op, G) ;
64
65