File: subsindex.m

package info (click to toggle)
suitesparse-graphblas 7.4.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 67,112 kB
  • sloc: ansic: 1,072,243; cpp: 8,081; sh: 512; makefile: 503; asm: 369; python: 125; awk: 10
file content (57 lines) | stat: -rw-r--r-- 1,788 bytes parent folder | download | duplicates (2)
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
function I = subsindex (G)
%SUBSINDEX subscript index from a GraphBLAS matrix.
% I = subsindex (G) is an overloaded method used when the GraphBLAS
% matrix G is used to index into a non-GraphBLAS matrix A, for A(G).
%
% See also GrB/subsref, GrB/subsasgn.

% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2022, All Rights Reserved.
% SPDX-License-Identifier: Apache-2.0

% On input, G must contain integers in the range 1 to prod (size (A))-1.
% The dimensions of A are not provided to subsindex.

G = G.opaque ;

% As an extension to the expression A(G), prune zeros and negative
% values first.  The expression A(G) becomes A (G (find (G > 0))).
G = gbselect ('>0', G) ;

[m, n, type] = gbsize (G) ;
if (isinteger (m))
    % G is so huge that gbsize returns m and n as int64.  This means
    % that m or n (or both) are bigger than flintmax, so G cannot be full.
    G_is_full = false ;
else
    G_is_full = (m*n == gbnvals (G)) ;
end

if (isequal (type, 'double') || isequal (type, 'single'))
    % double or single: convert to int64
    I = gbextractvalues (G) ;
    if (~isequal (I, round (I)))
        error ('GrB:error', 'array indices must be integers') ;
    end
    I = int64 (I) ;
elseif (gb_contains (type, 'int'))
    % any integer: just extract the values
    I = gbextractvalues (G) ;
else
    % logical or complex
    error ('GrB:error', 'array indices must be integers') ;
end

% I must contain entries in range 0 to prod (size (A)) - 1,
% so subtract the offset
I = I - 1 ;

% reshape I as needed
if (m == 1)
    % I should be a row vector instead
    I = I' ;
elseif (n > 1 && G_is_full)
    % I is should be an m-by-n matrix, so reshape it.  But I cannot be
    % reshaped to m-by-n if G is sparse, so leave it as a column vector.
    I = reshape (I, m, n) ;
end