Home > utilities > testmextrdsdsmat.m

# testmextrdsdsmat

## PURPOSE

TESTMEXTRDSDSMAT tests that the behaviour of mextrdsdsmat() is correct

## SYNOPSIS

function [status] = testmextrdsdsmat(verbose,tol)

## DESCRIPTION

     TESTMEXTRDSDSMAT tests that the behaviour of mextrdsdsmat() is correct
     mextrdsdsmat was the first version which is now superseeded by
     mextrcolumn which computest the vector of traces, not just one
     element. Thus mextrdsdsmat can retire at some point.

     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 mextrdsdsmat, penlabtest, penlabstresstest

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

- testmex TESTMEX tests that the behaviour of all the mex files is correct

## SUBFUNCTIONS

- function [status] = onematrixtest(verbose,tol,testname,A,S1,B,S2,ref)

## SOURCE CODE

    0001 function [status] = testmextrdsdsmat(verbose,tol)
    0002 % TESTMEXTRDSDSMAT tests that the behaviour of mextrdsdsmat() is correct
    0003 % mextrdsdsmat was the first version which is now superseeded by
    0004 % mextrcolumn which computest the vector of traces, not just one
    0005 % element. Thus mextrdsdsmat can retire at some point.
    0006 %
    0007 % RETURN
    0008 %   status ...  0 failed, 1 all ok
    0009 % INPUT
    0010 %   verbose ... 0 print only what failed, 1 print progress,
    0011 %               optional, default 1
    0012 %   tol ....... tolerance when comparing the results
    0013 %               optional, default 1e-8
    0014 %
    0015 % See also mextrdsdsmat, penlabtest, penlabstresstest
    0016 %
    0017
    0018 % This file is a part of PENLAB package distributed under GPLv3 license
    0019 % Copyright (c) 2013 by  J. Fiala, M. Kocvara, M. Stingl
    0020 % Last Modified: 5 Dec 2013
    0021
    0022   if (nargin<2)
    0023     tol = 1e-8;
    0024   end
    0025   if (nargin<1)
    0026     verbose = 1;
    0027   end
    0028   status = 1;
    0029
    0030   if (verbose)
    0031     disp(' testing functionality of mextrdsdsmat()...')
    0032   end
    0033
    0034   % is it possible to call it?
    0035   try
    0036     mextrdsdsmat(1,sparse(1),1,sparse(1));
    0037   catch
    0038     disp(' Error: mextrdsdsmat - cannot call')
    0039     status = 0;
    0040     return;
    0041   end
    0042
    0043   status = status && onematrixtest(verbose,tol,'n=0, all empty', [],[],[],[],0);
    0044   status = status && onematrixtest(verbose,tol,'n=1, all ones', 1,1,1,1,2);
    0045   status = status && onematrixtest(verbose,tol,'n=1, all nonzeros', 2,3,-4,5,-120*2);
    0046   status = status && onematrixtest(verbose,tol,'n=1, A=0', 0,3,-4,5,0);
    0047   status = status && onematrixtest(verbose,tol,'n=1, S1=0', 2,0,-4,5,0);
    0048   status = status && onematrixtest(verbose,tol,'n=1, B=0', 2,3,0,5,0);
    0049   status = status && onematrixtest(verbose,tol,'n=1, S2=0', 2,3,-4,0,0);
    0050   status = status && onematrixtest(verbose,tol,'n=2, all numbers, A diag', diag([2 3]),[-2.1,3.2;3.2,4.4],[4,6;6,5],[-13,-9;-9,-16]);
    0051   % don't forget that magic() is not symmetric
    0052   status = status && onematrixtest(verbose,tol,'n=2, all numbers, A diag, B=[]', diag([2 3]),[-2.1,3.2;3.2,4.4],magic(2)+magic(2)'+3,sparse(2,2),0);
    0053
    0054   % build special examples with very sparse matrices
    0055   n=5;
    0056   A=diag(1:n);
    0057   B=magic(n)+magic(n)';
    0058   S1=sparse(n,n); S1(2,1)=2;
    0059   S1=S1+S1';
    0060   S2=sparse(n,n); S2(4,4)=9;
    0061   status = status && onematrixtest(verbose,tol,'n=5, very sparse', A,S1,B,S2);
    0062
    0063   n=5;
    0064   A=diag(1:n);
    0065   B=magic(n)+magic(n)';
    0066   S1=sparse(n,n); S1(2,1)=2;
    0067   S1=S1+S1';
    0068   S2=sparse(n,n); S2(2,2)=9;
    0069   status = status && onematrixtest(verbose,tol,'n=5, very sparse2', A,S1,B,S2);
    0070
    0071   n=5;
    0072   A=zeros(n,n);
    0073   A(:)=[1:n^2]-10;
    0074   A=A+A';
    0075   B=magic(n)+magic(n)';
    0076   S1=sparse(n,n); S1(2,1)=2;
    0077   S1=S1+S1';
    0078   S2=sparse(n,n); S2(2,2)=9;
    0079   status = status && onematrixtest(verbose,tol,'n=5, very sparse3', A,S1,B,S2);
    0080
    0081   n=5;
    0082   A=zeros(n,n);
    0083   A(:)=[1:n^2]-10;
    0084   A=A+A';
    0085   B=magic(n)+magic(n)';
    0086   S1=sparse(n,n); S1(2,1)=2;
    0087   S1=S1+S1';
    0088   S2=sparse(diag([5:9]));
    0089   status = status && onematrixtest(verbose,tol,'n=5, sparse, S2 spdiag', A,S1,B,S2);
    0090
    0091   % random of different size
    0092   for n=[5 5 5 10 10 10 100 100 101 101]
    0093   %for n=[2 5 5]
    0094     A=rand(n,n);
    0095     A=A+A';
    0096     B=rand(n,n);
    0097     B=B+B';
    0098     S1=sprandsym(n,0.2);
    0099     S2=sprandsym(n,0.1);
    0100     testname=sprintf('n=%i all random',n);
    0101     status = status && onematrixtest(verbose,tol,testname, A,S1,B,S2);
    0102   end
    0103
    0104 end
    0105
    0106 function [status] = onematrixtest(verbose,tol,testname,A,S1,B,S2,ref)
    0107 % ONEMATRIXTEST - run one test with mextrdsdsmat(), S2 has just one element
    0108 %   testname - string to print out
    0109 %   A,B,S1,S2 - matrices for the test
    0110 %   ref [optional] - reference result, if not present, computed in Matlab
    0111
    0112   status = 1;
    0113
    0114   if (~issparse(S1))
    0115     S1=sparse(S1);
    0116   end
    0117   if (~issparse(S2))
    0118     S2=sparse(S2);
    0119   end
    0120
    0121   if (nargin==7)
    0122     %ref = trace(A*S1*B*S2);
    0123     ref = trace(A*S1*B*S2) + trace(A*S2*B*S1);
    0124   end
    0125
    0126   try
    0127     result = mextrdsdsmat(A,S1,B,S2);
    0128     err = abs(result-ref);
    0129   catch
    0130     err = [];
    0131   end
    0132
    0133   if (isempty(err))
    0134     fprintf(' test %-30s: HARD FAILED\n', testname);
    0135     status = 0;
    0136   elseif (err>tol*abs(ref))
    0137     fprintf(' test %-30s: FAILED  %15.6e %15.6e\n', testname,err,ref);
    0138     status = 0;
    0139   elseif (verbose)
    0140     fprintf(' test %-30s: ok (tol)  %15.6e %15.6e\n', testname,err,ref);
    0141   end
    0142
    0143 end
    0144