Home > utilities > testmexsumsparse.m

testmexsumsparse

PURPOSE ^

TESTMEXSUMSPARSE tests that the behaviour of mexsumsparse() is correct

SYNOPSIS ^

function [status] = testmexsumsparse(verbose,tol)

DESCRIPTION ^

 TESTMEXSUMSPARSE tests that the behaviour of mexsumsparse() is correct

 RETURN
   status ...  0 failed, 1 all ok
 INPUT
   verbose ... 0 print only what failed, 1 print progress,
               optional, default 1
   tol ....... tolerance when comparing the results
               optional, default 1e-8

 See also mexsumsparse, penlabtest, penlabstresstest

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [status] = testmexsumsparse(verbose,tol)
0002 % TESTMEXSUMSPARSE tests that the behaviour of mexsumsparse() is correct
0003 %
0004 % RETURN
0005 %   status ...  0 failed, 1 all ok
0006 % INPUT
0007 %   verbose ... 0 print only what failed, 1 print progress,
0008 %               optional, default 1
0009 %   tol ....... tolerance when comparing the results
0010 %               optional, default 1e-8
0011 %
0012 % See also mexsumsparse, penlabtest, penlabstresstest
0013 %
0014 
0015 % This file is a part of PENLAB package distributed under GPLv3 license
0016 % Copyright (c) 2013 by  J. Fiala, M. Kocvara, M. Stingl
0017 % Last Modified: 5 Dec 2013
0018 
0019   if (nargin<2)
0020     tol = 1e-8;
0021   end
0022   if (nargin<1)
0023     verbose = 1;
0024   end
0025   status = 1;
0026 
0027   if (verbose)
0028     disp(' testing functionality of mexsumsparse()...')
0029   end
0030 
0031   % is it possible to call it?
0032   try
0033     mexsumsparse(3,{sparse([1,2;2,1])});
0034   catch
0035     disp(' Error: mexsumsparse - cannot call')
0036     status = 0;
0037     return;
0038   end
0039 
0040   % some of Si will be empty...
0041   %status = status && onerandtest(verbose,tol,'n=1, a few',1,20,0.8);
0042 
0043   status = status && onerandtest(verbose,tol,'n=5, a few',5,20,0.8);
0044 
0045   status = status && onerandtest(verbose,tol,'n=30 x1, a few',30,1,0.3);
0046   status = status && onerandtest(verbose,tol,'n=30 x2, a few',30,2,0.3);
0047   status = status && onerandtest(verbose,tol,'n=30 x50, a few',30,50,0.1);
0048 
0049   status = status && onerandtest(verbose,tol,'n=401 x50, a few',401,50,0.1);
0050 
0051   %status = status && onerandtest(verbose,tol,testname,n,m,density);
0052 
0053 end
0054 
0055 function [status] = onerandtest(verbose,tol,testname,n,m,density)
0056 % ONERANDTEST run one randomly generated test on mexsumsparse()
0057 %   n - dimension of matrices
0058 %   m - number of matrices
0059 %   density (0..1) - density of the matrices
0060 
0061   status = 1;
0062 
0063   % generate the problem data
0064   S = cell(m,1);
0065   w = rand(m,1);
0066   for i=1:m
0067     S{i} = sprandsym(n,density);
0068   end
0069 
0070   % compute reference result (as dense)
0071   ref = zeros(n,n);
0072   for i=1:m
0073     ref = ref + w(i)*S{i};
0074   end
0075   nrm = norm(ref,inf);
0076 
0077   % call mex to generate result as dense
0078   try
0079     rd = mexsumsparse(w,S,-1);
0080     err = norm(ref-rd,inf);
0081   catch
0082     err = [];
0083   end
0084 
0085   if (isempty(err))
0086     fprintf(' test (dense) %-30s: HARD FAILED\n', testname);
0087     status = 0;
0088   elseif (err>tol*nrm)
0089     fprintf(' test (dense) %-30s: FAILED  %15.6e %15.6e\n', testname,err,nrm);
0090     status = 0;
0091   elseif (verbose)
0092     fprintf(' test (dense) %-30s: ok (tol)  %15.6e %15.6e\n', testname,err,nrm);
0093   end
0094 
0095   % call mex to generate result as sparse (auto)
0096   try
0097     rd = mexsumsparse(w,S);
0098     err = norm(ref-rd,inf);
0099   catch
0100     err = [];
0101   end
0102 
0103   if (isempty(err))
0104     fprintf(' test (auto) %-30s: HARD FAILED\n', testname);
0105     status = 0;
0106   elseif (err>tol*nrm)
0107     fprintf(' test (auto)  %-30s: FAILED  %15.6e %15.6e\n', testname,err,nrm);
0108     status = 0;
0109   elseif (verbose)
0110     fprintf(' test (auto)  %-30s: ok (tol)  %15.6e %15.6e\n', testname,err,nrm);
0111   end
0112 
0113 end
0114

Generated on Mon 26-Aug-2019 10:22:08 by m2html © 2005