File: ctmcchkQ.m

package info (click to toggle)
octave-queueing 1.2.8-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,288 kB
  • sloc: makefile: 56
file content (91 lines) | stat: -rw-r--r-- 2,471 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
## Copyright (C) 2012 Moreno Marzolla
##
## This file is part of the queueing toolbox.
##
## The queueing toolbox is free software: you can redistribute it and/or
## modify it under the terms of the GNU General Public License as
## published by the Free Software Foundation, either version 3 of the
## License, or (at your option) any later version.
##
## The queueing toolbox is distributed in the hope that it will be
## useful, but WITHOUT ANY WARRANTY; without even the implied warranty
## of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with the queueing toolbox. If not, see <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
##
## @deftypefn {Function File} {[@var{result} @var{err}] =} ctmcchkQ (@var{Q})
##
## @cindex Markov chain, continuous time
##
## If @var{Q} is a valid infinitesimal generator matrix, return
## the size (number of rows or columns) of @var{Q}. If @var{Q} is not
## an infinitesimal generator matrix, set @var{result} to zero, and
## @var{err} to an appropriate error string.
##
## @end deftypefn

## Author: Moreno Marzolla <moreno.marzolla(at)unibo.it>
## Web: http://www.moreno.marzolla.name/

function [result err] = ctmcchkQ( Q )

  persistent epsilon = 100*eps;

  if ( nargin != 1 )
    print_usage();
  endif

  result = 0;
  err = "";

  if ( !issquare(Q) )
    err = "P is not a square matrix";
    return;
  endif

  if (any(Q(~logical(eye(size(Q))))<0) || ... # there is any negative non-diagonal element
      norm( sum(Q,2), "inf" ) > epsilon )
    err = "Q is not an infinitesimal generator matrix";
    return;
  endif

  result = rows(Q);
endfunction
%!test
%! Q = [0];
%! [result err] = ctmcchkQ(Q);
%! assert( result, 1 );
%! assert( err, "" );

%!test
%! N = 10;
%! Q = ctmcbd(rand(1,N-1),rand(1,N-1));
%! [result err] = ctmcchkQ(Q);
%! assert( result, N );
%! assert( err, "" );

%!test
%! Q = [1 2 3; 4 5 6];
%! [result err] = ctmcchkQ(Q);
%! assert( result, 0 );
%! assert( index(err, "square") > 0 );

%!test
%! N = 10;
%! Q = ctmcbd(rand(1,N-1),rand(1,N-1));
%! Q(2,1) = -1;
%! [result err] = ctmcchkQ(Q);
%! assert( result, 0 );
%! assert( index(err, "infinitesimal") > 0 );

%!test
%! N = 10;
%! Q = ctmcbd(linspace(1,N-1,N-1),linspace(1,N-1,N-1));
%! Q(1,1) += 7;
%! [result err] = ctmcchkQ(Q);
%! assert( result, 0 );
%! assert( index(err, "infinitesimal") > 0 );