1## Copyright (C) 2012 Moreno Marzolla
2##
3## This file is part of the queueing toolbox.
4##
5## The queueing toolbox is free software: you can redistribute it and/or
6## modify it under the terms of the GNU General Public License as
7## published by the Free Software Foundation, either version 3 of the
8## License, or (at your option) any later version.
9##
10## The queueing toolbox is distributed in the hope that it will be
11## useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13## General Public License for more details.
14##
15## You should have received a copy of the GNU General Public License
16## along with the queueing toolbox. If not, see <http://www.gnu.org/licenses/>.
17
18## -*- texinfo -*-
19##
20## @deftypefn {Function File} {[@var{result} @var{err}] =} ctmcchkQ (@var{Q})
21##
22## @cindex Markov chain, continuous time
23##
24## If @var{Q} is a valid infinitesimal generator matrix, return
25## the size (number of rows or columns) of @var{Q}. If @var{Q} is not
26## an infinitesimal generator matrix, set @var{result} to zero, and
27## @var{err} to an appropriate error string.
28##
29## @end deftypefn
30
31## Author: Moreno Marzolla <moreno.marzolla(at)unibo.it>
32## Web: http://www.moreno.marzolla.name/
33
34function [result err] = ctmcchkQ( Q )
35
36  persistent epsilon = 100*eps;
37
38  if ( nargin != 1 )
39    print_usage();
40  endif
41
42  result = 0;
43  err = "";
44
45  if ( !issquare(Q) )
46    err = "P is not a square matrix";
47    return;
48  endif
49
50  if (any(Q(~logical(eye(size(Q))))<0) || ... # there is any negative non-diagonal element
51      norm( sum(Q,2), "inf" ) > epsilon )
52    err = "Q is not an infinitesimal generator matrix";
53    return;
54  endif
55
56  result = rows(Q);
57endfunction
58%!test
59%! Q = [0];
60%! [result err] = ctmcchkQ(Q);
61%! assert( result, 1 );
62%! assert( err, "" );
63
64%!test
65%! N = 10;
66%! Q = ctmcbd(rand(1,N-1),rand(1,N-1));
67%! [result err] = ctmcchkQ(Q);
68%! assert( result, N );
69%! assert( err, "" );
70
71%!test
72%! Q = [1 2 3; 4 5 6];
73%! [result err] = ctmcchkQ(Q);
74%! assert( result, 0 );
75%! assert( index(err, "square") > 0 );
76
77%!test
78%! N = 10;
79%! Q = ctmcbd(rand(1,N-1),rand(1,N-1));
80%! Q(2,1) = -1;
81%! [result err] = ctmcchkQ(Q);
82%! assert( result, 0 );
83%! assert( index(err, "infinitesimal") > 0 );
84
85%!test
86%! N = 10;
87%! Q = ctmcbd(linspace(1,N-1,N-1),linspace(1,N-1,N-1));
88%! Q(1,1) += 7;
89%! [result err] = ctmcchkQ(Q);
90%! assert( result, 0 );
91%! assert( index(err, "infinitesimal") > 0 );
92