文章目录
前言
一般情况集成了一个线性求解器(Ax=b),我们需要验证其性能和精度,这时需要大量数据来做验证, 尤其是有不同性质的矩阵 A A A 的数据,例如:稀疏性, 对称性, 正定性, 对角占优等。
Matrix Market
Matrix Market
Matrix Market 网站提供了友好的接口和数据, 方便我们验证求解器的精度和性能,尤其是对各种性质的矩阵 A A A 都有很多数据, 包含不同维数。
Matlab IO
Read data
function [A,rows,cols,entries,rep,field,symm] = mmread(filename)
%
% function [A] = mmread(filename)
%
% function [A,rows,cols,entries,rep,field,symm] = mmread(filename)
%
% Reads the contents of the Matrix Market file 'filename'
% into the matrix 'A'. 'A' will be either sparse or full,
% depending on the Matrix Market format indicated by
% 'coordinate' (coordinate sparse storage), or
% 'array' (dense array storage). The data will be duplicated
% as appropriate if symmetry is indicated in the header.
%
% Optionally, size information about the matrix can be
% obtained by using the return values rows, cols, and
% entries, where entries is the number of nonzero entries
% in the final matrix. Type information can also be retrieved
% using the optional return values rep (representation), field,
% and symm (symmetry).
%
mmfile = fopen(filename,'r');
if ( mmfile == -1 )
disp(filename);
error('File not found');
end;
header = fgets(mmfile);
if (header == -1 )
error('Empty file.')
end
% NOTE: If using a version of Matlab for which strtok is not
% defined, substitute 'gettok' for 'strtok' in the
% following lines, and download gettok.m from the
% Matrix Market site.
[head0,header] = strtok(header); % see note above
[head1,header] = strtok(header);
[rep,header] = strtok(header);
[field,header] = strtok(header);
[symm,header] = strtok(header);
head1 = lower(head1);
rep = lower(rep);
field = lower(field);
symm = lower(symm);
if ( length(symm) == 0 )
disp(['Not enough words in header line of file ',filename])
disp('Recognized format: ')
disp('%%MatrixMarket matrix representation field symmetry')
error('Check header line.')
end
if ( ~ strcmp(head0,'%%MatrixMarket') )
error('Not a valid MatrixMarket header.')
end
if ( ~ strcmp(head1,'matrix') )
disp(['This seems to be a MatrixMarket ',head1,' file.']);
disp('This function only knows how to read MatrixMarket matrix files.');
disp(' ');
error(' ');
end
% Read through comments, ignoring them
commentline = fgets(mmfile);
while length(commentline) > 0 & commentline(1) == '%',
commentline = fgets(mmfile);
end
% Read size information, then branch according to
% sparse or dense format
if ( strcmp(rep,'coordinate')) % read matrix given in sparse
% coordinate matrix format
[sizeinfo,count] = sscanf(commentline,'%d%d%d');
while ( count == 0 )
commentline = fgets(mmfile);
if (commentline == -1 )
error('End-of-file reached before size information was found.')
end
[sizeinfo,count] = sscanf(commentline,'%d%d%d');
if ( count > 0 & count ~= 3 )
error('Invalid size specification line.')
end
end
rows = sizeinfo(1);
cols = sizeinfo(2);
entries = sizeinfo(3);
if ( strcmp(field,'real') ) % real valued entries:
[T,count] = fscanf(mmfile,'%f',3);
T = [T; fscanf(mmfile,'%f')];
if ( size(T) ~= 3*entries )
message = ...
str2mat('Data file does not contain expected amount of data.',...
'Check that number of data lines matches nonzero count.');
disp(message);
error('Invalid data.');
end
T = reshape(T,3,entries)';
A = sparse(T(:,1), T(:,2), T(:,3), rows , cols);
elseif ( strcmp(field,'complex')) % complex valued entries:
T = fscanf(mmfile,'%f',4);
T = [T; fscanf(mmfile,'%f')];
if ( size(T) ~= 4*entries )
message = ...
str2mat('Data file does not contain expected amount of data.',...
'Check that number of data lines matches nonzero count.');
disp(message);
error('Invalid data.');
end
T = reshape(T,4,entries)';
A = sparse(T(:,1), T(:,2), T(:,3) + T(:,4)*sqrt(-1), rows , cols)

文章介绍了如何使用MatrixMarket网站获取不同性质的矩阵数据来验证线性求解器的性能。Matlab的mmread函数用于读取MatrixMarket格式的文件,mmwrite函数则用于写入数据。此外,还提供了C++的IO操作示例。文章提供了一个测试案例,涉及读取、写入矩阵数据并比较写入后数据的准确性。
最低0.47元/天 解锁文章
4034

被折叠的 条评论
为什么被折叠?



