LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
CheckMERepresentation.m
1% r = CheckMERepresentation(alpha, A, prec)
2%
3% Checks if the given vector and matrix define a valid matrix-
4% exponential representation.
5%
7% ----------
8% alpha : matrix, shape (1,M)
9% Initial vector of the matrix-exponential distribution
10% to check
11% A : matrix, shape (M,M)
12% Matrix parameter of the matrix-exponential distribution
13% to check
14% prec : double, optional
15% Numerical precision. The default value is 1e-14.
16%
17% Returns
18% -------
19% r : bool
20% True, if the matrix is a square matrix, the vector and
21% the matrix have the same size, the dominant eigenvalue
22% is negative and real
23%
24% Notes
25% -----
26% This procedure does not check the positivity of the density!
27% Call 'CheckMEPositiveDensity' if it is needed, but keep in
28% mind that it can be time-consuming, while this procedure
29% is fast.
30
31function r = CheckMERepresentation (alpha, A, prec)
32
33 global BuToolsVerbose;
34 global BuToolsCheckPrecision;
35 if isempty(BuToolsCheckPrecision)
36 BuToolsCheckPrecision = 1e-14;
37 end
38
39 if ~exist('prec','var')
40 prec = BuToolsCheckPrecision;
41 end
42
43 if size(A,1)~=size(A,2)
44 if BuToolsVerbose
45 fprintf ('CheckMERepresentation: The matrix is not a square matrix!\n');
46 end
47 r = false;
48 return;
49 end
50
51 if length(alpha)~=size(A,1)
52 if BuToolsVerbose
53 fprintf ('CheckMERepresentation: The vector and the matrix have different sizes!\n');
54 end
55 r = false;
56 return;
57 end
58
59 if sum(alpha)<-prec*length(alpha) || sum(alpha)>1+prec*length(alpha)
60 if BuToolsVerbose
61 fprintf ('CheckMERepresentation: The sum of the vector elements is less than zero or greater than one (precision: %g)!\n',prec);
62 end
63 r = false;
64 return;
65 end
66
67 if max(real(eig(A)))>=prec
68 if BuToolsVerbose
69 fprintf ('CheckMERepresentation: There is an eigenvalue of the matrix with non-negative real part (at precision %g)!\n',prec);
70 end
71 r = false;
72 return;
73 end
74
75 ev = eig(A);
76 [~,ix] = sort(abs(real(ev)));
77 maxev = ev(ix(1));
78
79 % The dominant real part need not be attained by a single eigenvalue, and
80 % the sort above breaks such a tie arbitrarily. A concentrated matrix
81 % exponential of order 2n+1 puts its whole spectrum on the line
82 % Re = -mu1, so the arbitrary pick returns a complex eigenvalue and the
83 % test below rejects a valid ME even though the real eigenvalue -mu1 is
84 % equally dominant. Among the eigenvalues attaining the dominant real
85 % part, prefer a real one: "the dominant eigenvalue is real" is a
86 % statement about the spectrum, not about which tied eigenvalue the sort
87 % happened to return. This only widens the accepted set, and only in the
88 % tied case, which BuTools itself reports as a warning below rather than
89 % as a rejection. Matches native Python butools.ph.check and
90 % jline.lib.butools.ph.CheckMERepresentation.
91 domre = abs(real(maxev));
92 domtol = max(prec, 1e-8*domre);
93 tied = ev(abs(abs(real(ev)) - domre) <= domtol);
94 realtied = tied(abs(imag(tied)) <= domtol);
95 if ~isempty(realtied)
96 maxev = real(realtied(1));
97 end
98
99 % Value-based rather than isreal(maxev): eig returns a complex array as
100 % soon as one eigenvalue is complex, so isreal() is false for a real-valued
101 % entry of that array and the dominant eigenvalue of any ME with an
102 % oscillating component would be declared non-real. jline.lib.butools
103 % CheckMERepresentation and native Python already test the imaginary part.
104 if abs(imag(maxev)) > prec
105 if BuToolsVerbose
106 fprintf ('CheckMERepresentation: The dominant eigenvalue of the matrix is not real!\n');
107 end
108 r = false;
109 return;
110 end
111
112 % Nested rather than a single && expression: BuToolsVerbose is a global that
113 % is [] until lineStart or BuToolsInit runs, and `scalar && []` is a hard
114 % error in MATLAB. The left operand here is true exactly when the spectrum
115 % has a repeated dominant modulus, which is the common case for an Erlang
116 % (ME.fromErlang(2,2) has eigenvalues -2,-2), so the short circuit that hides
117 % the problem for a generic ME does not fire and validity checking dies on a
118 % verbosity flag. `if []` is simply false, so the nested form is safe
119 % whether or not the global has been initialised.
120 if sum(abs(ev(1:end))==abs(maxev)) > 1
121 if BuToolsVerbose
122 fprintf ('CheckMERepresentation warning: There are more than one eigenvalue with the same absolute value as the largest eigenvalue!\n');
123 end
124 end
125
126 r = true;
127end
Definition Station.m:245