Home > matpower6.0 > t > t_is.m

t_is

PURPOSE ^

T_IS Tests if two matrices are identical to some tolerance.

SYNOPSIS ^

function ok = t_is(got, expected, prec, msg)

DESCRIPTION ^

T_IS  Tests if two matrices are identical to some tolerance.
   T_IS(GOT, EXPECTED, PREC, MSG) increments the global test count
   and if the maximum difference between corresponding elements of
   GOT and EXPECTED is less than 10^(-PREC) then it increments the
   passed tests count, otherwise increments the failed tests count.
   Prints 'ok' or 'not ok' followed by the MSG, unless the global
   variable t_quiet is true. The input values can be real or complex,
   and they can be scalar, vector, or 2-d or higher matrices. If GOT
   is a vector or matrix and EXPECTED is a scalar or NaN, all elements
   must match the scalar. Intended to be called between calls to
   T_BEGIN and T_END.

   Optionally returns a true or false value indicating whether or
   not the test succeeded. NaN's are considered to be equal to each
   other.

   Example:
       quiet = 0;
       t_begin(5, quiet);
       t_ok(pi > 3, 'size of pi');
       t_skip(3, 'not yet written');
       t_is(2+2, 4, 12, '2+2 still equals 4');
       t_end;

   See also T_OK, T_SKIP, T_BEGIN, T_END, T_RUN_TESTS.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function ok = t_is(got, expected, prec, msg)
0002 %T_IS  Tests if two matrices are identical to some tolerance.
0003 %   T_IS(GOT, EXPECTED, PREC, MSG) increments the global test count
0004 %   and if the maximum difference between corresponding elements of
0005 %   GOT and EXPECTED is less than 10^(-PREC) then it increments the
0006 %   passed tests count, otherwise increments the failed tests count.
0007 %   Prints 'ok' or 'not ok' followed by the MSG, unless the global
0008 %   variable t_quiet is true. The input values can be real or complex,
0009 %   and they can be scalar, vector, or 2-d or higher matrices. If GOT
0010 %   is a vector or matrix and EXPECTED is a scalar or NaN, all elements
0011 %   must match the scalar. Intended to be called between calls to
0012 %   T_BEGIN and T_END.
0013 %
0014 %   Optionally returns a true or false value indicating whether or
0015 %   not the test succeeded. NaN's are considered to be equal to each
0016 %   other.
0017 %
0018 %   Example:
0019 %       quiet = 0;
0020 %       t_begin(5, quiet);
0021 %       t_ok(pi > 3, 'size of pi');
0022 %       t_skip(3, 'not yet written');
0023 %       t_is(2+2, 4, 12, '2+2 still equals 4');
0024 %       t_end;
0025 %
0026 %   See also T_OK, T_SKIP, T_BEGIN, T_END, T_RUN_TESTS.
0027 
0028 %   MATPOWER
0029 %   Copyright (c) 2004-2016, Power Systems Engineering Research Center (PSERC)
0030 %   by Ray Zimmerman, PSERC Cornell
0031 %
0032 %   This file is part of MATPOWER.
0033 %   Covered by the 3-clause BSD License (see LICENSE file for details).
0034 %   See http://www.pserc.cornell.edu/matpower/ for more info.
0035 
0036 global t_quiet;
0037 
0038 if nargin < 4
0039     msg = '';
0040 end
0041 if nargin < 3 || isempty(prec)
0042     prec = 5;
0043 end
0044 edims = size(expected);
0045 gdims = size(got);
0046 if (length(edims) == length(gdims) && all(edims == gdims)) || ...
0047         all(edims == 1) && ~any(gdims == 0)
0048     if all(edims == 0)
0049         condition = true;
0050     else
0051         %% check for NaNs!
0052         gNaN = find(isnan(got(:)));
0053         eNaN = find(isnan(expected(:)));
0054         if (~isscalar(expected) && ...
0055                 (length(gNaN) ~= length(eNaN) || sum(gNaN-eNaN) ~= 0)) || ...
0056             (isscalar(expected) && ...
0057                     (( isnan(expected) && ~all(isnan(got(:)))) || ...
0058                      (~isnan(expected) && any(isnan(got(:))))) )
0059             condition = false;
0060             max_diff = -1;
0061         elseif all(isnan(expected(:))) && all(isnan(got(:)))
0062             condition = true;
0063         else
0064             got_minus_expected = got - expected;
0065             max_diff = max(abs(got_minus_expected(:)));
0066             condition = ( max_diff < 10^(-prec) );
0067         end
0068     end
0069 else
0070     condition = false;
0071     max_diff = 0;
0072 end
0073 
0074 t_ok(condition, msg);
0075 if ~condition && ~t_quiet
0076     if max_diff > 0
0077         k = find(~(abs(got_minus_expected(:)) < 10^(-prec)));
0078         [vv, kk] = max(abs(got_minus_expected(k)));
0079         fprintf('    index              got             expected          got - exp\n');
0080         fprintf('---------------  ----------------  ----------------  ----------------');
0081         for u = 1:length(k)
0082             if isscalar(expected)
0083                 ex = expected;
0084             else
0085                 ex = expected(k(u));
0086             end
0087             if isscalar(got)
0088                 idxstr = '(1)';
0089             else
0090                 idx = cell(1, length(gdims));
0091                 [idx{:}] = ind2sub(gdims, k(u));
0092                 idxstr = sprintf('%d,', idx{1:end-1});
0093                 idxstr = sprintf('(%s%d)', idxstr, idx{end});
0094             end
0095             fprintf('\n%14s  %16g  %16g  %16g', ...
0096                 idxstr, got(k(u)), ex, got_minus_expected(k(u)));
0097             if u == kk
0098                 fprintf('  *');
0099                 idxstrkk = idxstr;
0100             end
0101         end
0102         fprintf('\nmax diff @ %s = %g > allowed tol of %g\n\n', ...
0103             idxstrkk, max_diff, 10^(-prec));
0104     elseif max_diff == -1
0105         fprintf('    mismatch in locations of NaNs\n');
0106     else
0107         gidxstr = sprintf('%d x ', gdims(1:end-1));
0108         gidxstr = sprintf('%s%d', gidxstr, gdims(end));
0109         eidxstr = sprintf('%d x ', edims(1:end-1));
0110         eidxstr = sprintf('%s%d', eidxstr, edims(end));
0111         fprintf('    dimension mismatch:\n');
0112         fprintf('             got: %s\n', gidxstr);
0113         fprintf('        expected: %s\n\n', eidxstr);
0114     end
0115 end
0116 if nargout
0117     ok = condition;
0118 end

Generated on Fri 16-Dec-2016 12:45:37 by m2html © 2005