1function [F, E] = log2 (G)
2%LOG2 base-2 logarithm.
3% C = log2 (G) is the base-2 logarithm of each entry of a GraphBLAS matrix
4% G.  Since log2 (0) is nonzero, the result is a full matrix.  If any entry
5% in G is negative, the result is complex.
6%
7% [F,E] = log2 (G) returns F and E so that G = F.*(2.^E), where entries in
8% abs (F) are either in the range [0.5,1), or zero if the entry in G is
9% zero.  F and E are both sparse, with the same pattern as G.  If G is
10% complex, [F,E] = log2 (real (G)).
11%
12% See also GrB/pow2, GrB/log, GrB/log1p, GrB/log10, GrB/exp.
13
14% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
15% SPDX-License-Identifier: GPL-3.0-or-later
16
17G = G.opaque ;
18
19if (nargout == 1)
20    % C = log2 (G)
21    F = GrB (gb_to_real_if_imag_zero (gb_trig ('log2', gbfull (G)))) ;
22else
23    % [F,E] = log2 (G)
24    type = gbtype (G) ;
25    switch (type)
26        case { 'logical', 'int8', 'int16', 'int32', 'int64', ...
27            'uint8', 'uint16', 'uint32', 'uint64', 'double complex' }
28            type = 'double' ;
29        case { 'single complex' }
30            type = 'single' ;
31        case { 'single', 'double' }
32            % type remains the same
33    end
34    F = GrB (gbapply (['frexpx.' type], G)) ;
35    E = GrB (gbapply (['frexpe.' type], G)) ;
36end
37
38