File: spdiag.m

package info (click to toggle)
rocsolver 6.4.3-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 17,876 kB
  • sloc: cpp: 151,850; python: 2,275; sh: 875; objc: 642; ansic: 402; makefile: 71; xml: 26
file content (34 lines) | stat: -rw-r--r-- 865 bytes parent folder | download
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
% ********************************************************************
% Copyright (C) 2023 Advanced Micro Devices, Inc. All rights reserved.
% ********************************************************************
function A = spdiag( d, idiag )
% A = spdiag( d, idiag )
%
% Generate a sparse matrix based on the diagonal
% This is similar to "diag" but returns
% a sparse matrix instead of a dense matrix

n = length(d);
m = n + abs(idiag);

if (idiag >= 0),
   % ----------------------
   % upper triangular part
   % ----------------------
   i1 = 1;
   i2 = min(m,i1 + n - 1);
   j1 = min(m,1 + idiag);
   j2 = min(m,j1 + n -1 );
else
   % -----------------------
   % lower triangular part
   % -----------------------
   i1 = min(m,1 + abs(idiag));
   i2 = min(m, i1 + n - 1);
   j1 = 1;
   j2 = min(m, j1 + n - 1);
end;


A = sparse( i1:i2, j1:j2, d, m,m );