File: mpower.m

package info (click to toggle)
suitesparse-graphblas 7.4.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 67,112 kB
  • sloc: ansic: 1,072,243; cpp: 8,081; sh: 512; makefile: 506; asm: 369; python: 125; awk: 10
file content (52 lines) | stat: -rw-r--r-- 1,368 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
function C = mpower (A, B)
%A^B matrix power.
% C = A^B computes the matrix power of A raised to the B. A must be a
% square matrix.  B must an integer >= 0.
%
% See also GrB/power.

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

if (isobject (A))
    A = A.opaque ;
end

if (isobject (B))
    B = B.opaque ;
end

[am, an, atype] = gbsize (A) ;
[bm, bn] = gbsize (B) ;
a_is_scalar = (am == 1) && (an == 1) ;
b_is_scalar = (bm == 1) && (bn == 1) ;

if (a_is_scalar && b_is_scalar)
    C = GrB (gb_power (A, B)) ;
else
    if (am ~= an)
        error ('GrB:error', 'For C=A^B, A must be square') ;
    end
    if (~b_is_scalar)
        error ('GrB:error', ...
            'For C=A^B, B must be a non-negative integer scalar') ;
    end
    b = gb_scalar (B) ;
    if (~(isreal (b) && isfinite (b) && round (b) == b && b >= 0))
        error ('GrB:error', ...
            'For C=A^B, B must be a non-negative integer scalar') ;
    end
    if (b == 0)
        % C = A^0 = I
        if (isequal (atype, 'single complex'))
            atype = 'single' ;
        elseif (isequal (atype, 'double complex'))
            atype = 'double' ;
        end
        C = GrB (gb_speye ('mpower', an, atype)) ;
    else
        % C = A^b where b > 0 is an integer
        C = GrB (gb_mpower (A, b)) ;
    end
end