Matlab read and write routines for YSMP format.
This commit is contained in:
parent
c97fa21d0c
commit
3abbeb61c2
47
tools/manalyze.m
Normal file
47
tools/manalyze.m
Normal file
@ -0,0 +1,47 @@
|
||||
function [nv,ia,ja,a] = manalyze(A);
|
||||
%-----------------------------------------
|
||||
%function [nv,ia,ja,a] = manalyze(A);
|
||||
% Analyzes matrix A, yields ysmp format
|
||||
%-----------------------------------------
|
||||
|
||||
% determine row/column indices of nonzero entries
|
||||
% add to diagonal while determining indices to
|
||||
% ensure that diagonal will be present even if 0
|
||||
|
||||
d = diag(A);
|
||||
mn = min(d);
|
||||
shf=mn+1;
|
||||
[ja, I] = find(A'+shf*speye(size(A)));
|
||||
t = find(A'+shf*speye(size(A)));
|
||||
AA=A';
|
||||
a=full(AA(t));
|
||||
nv = size(A, 1);
|
||||
na = size(a, 1);
|
||||
|
||||
% compute ia and put diagonal entry first on each row
|
||||
|
||||
ia = zeros(nv+1, 1);
|
||||
ia(1) = 1;
|
||||
for i = 1 : nv
|
||||
k = ia(i);
|
||||
while (k <= na)
|
||||
if (I(k) ~= i)
|
||||
break;
|
||||
end
|
||||
|
||||
% swap diagonal entry with first entry
|
||||
|
||||
if (ja(k) == i)
|
||||
tmp = a(ia(i));
|
||||
a(ia(i)) = a(k);
|
||||
a(k) = tmp;
|
||||
tmp = ja(ia(i));
|
||||
ja(ia(i)) = ja(k);
|
||||
ja(k) = tmp;
|
||||
end
|
||||
|
||||
k = k + 1;
|
||||
end
|
||||
ia(i+1) = k;
|
||||
end
|
||||
|
||||
41
tools/readysmp.m
Normal file
41
tools/readysmp.m
Normal file
@ -0,0 +1,41 @@
|
||||
function [A,ia,ja,a] = readysmp(filename)
|
||||
%-----------------------------------------------------------------------------
|
||||
% [A] = readysmp('filename'):
|
||||
% Reads from file 'filename' a matrix A in YSMP format.
|
||||
%
|
||||
% YSMP format:
|
||||
% First line is 'nv' the number of rows in matrix. Integer.
|
||||
%
|
||||
% Next 'nv+1' lines are ia(1:nv+1). ia(j) points to the location
|
||||
% in 'a' and 'ja' where the first entry for row 'j' lives. ia(nv+1)
|
||||
% points to the element one greater than length(a). Integers.
|
||||
%
|
||||
% Next 'ia(nv+1)-1' lines contain 'ja' the list of non-zero columns.
|
||||
% The numbers [ja(ia(j)):ja(ia(j+1)-1)] contain the nonzero columns in
|
||||
% row 'j', with 'j', the diagonal column, listed first. That is,
|
||||
% ja(ia(j))=j. Integers.
|
||||
%
|
||||
% Next 'ia(nv+1)-1' lines contain 'a' values.
|
||||
% The numbers [a(ia(j)):a(ia(j+1)-1)] contain the nonzero values in
|
||||
% row 'j', corresponding to columns [ja(ia(j)):ja(ia(j+1)-1)]. Reals.
|
||||
%
|
||||
%-----------------------------------------------------------------------------
|
||||
|
||||
fid=fopen(filename,'r');
|
||||
|
||||
nv = fscanf(fid,'%d',1); % number of variables (nv)
|
||||
[ia, count] = fscanf(fid,'%d ',nv+1);
|
||||
[ja, count] = fscanf(fid,'%d ',ia(nv+1)-1); % This is ja
|
||||
[a, count]=fscanf(fid,'%le ',ia(nv+1)-1); % This is the matrix a
|
||||
fclose(fid);
|
||||
|
||||
kz=zeros(length(ja),1);
|
||||
p=1;
|
||||
for j=1:nv,
|
||||
plen = ia(j+1)-ia(j);
|
||||
kz(p:p+plen-1)=j*ones(plen,1);
|
||||
p=p+plen;
|
||||
end
|
||||
|
||||
A = sparse(kz,ja,a);
|
||||
|
||||
22
tools/writeysmp.m
Normal file
22
tools/writeysmp.m
Normal file
@ -0,0 +1,22 @@
|
||||
function [nv,ia,ja,a] = writeysmp(filename,A,nv,ia,ja,a)
|
||||
% function [nv,ia,ja,a] = writeysmp(filename,A,nv,ia,ja,a)
|
||||
%
|
||||
% writes ysmp file in 'filename' (no qualifiers added,
|
||||
% eg. not 'filename.ysmp')
|
||||
%
|
||||
% arguments nv, ia, ja, and a are optional.
|
||||
%
|
||||
|
||||
if nargin < 6
|
||||
[nv,ia,ja,a] = manalyze(A);
|
||||
end;
|
||||
|
||||
outdat = [filename];
|
||||
fid=fopen(outdat,'w');
|
||||
|
||||
fprintf(fid,'%d\n',nv); % number of variables (nv)
|
||||
fprintf(fid,'%d\n',ia);
|
||||
fprintf(fid,'%d\n',ja);
|
||||
fprintf(fid,'%.15e\n',a);
|
||||
|
||||
fclose(fid);
|
||||
Loading…
Reference in New Issue
Block a user