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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
// SPDX-License-Identifier: Apache-2.0
//
// Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
// Copyright 2008-2016 National ICT Australia (NICTA)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------
//! \addtogroup op_inv_spd
//! @{
template<typename T1>
inline
void
op_inv_spd_default::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_inv_spd_default>& X)
{
arma_extra_debug_sigprint();
const bool status = op_inv_spd_default::apply_direct(out, X.m);
if(status == false)
{
out.soft_reset();
arma_stop_runtime_error("inv_sympd(): matrix is singular or not positive definite");
}
}
template<typename T1>
inline
bool
op_inv_spd_default::apply_direct(Mat<typename T1::elem_type>& out, const Base<typename T1::elem_type,T1>& expr)
{
arma_extra_debug_sigprint();
return op_inv_spd_full::apply_direct<T1,false>(out, expr, uword(0));
}
//
template<typename T1>
inline
void
op_inv_spd_full::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_inv_spd_full>& X)
{
arma_extra_debug_sigprint();
const uword flags = X.aux_uword_a;
const bool status = op_inv_spd_full::apply_direct(out, X.m, flags);
if(status == false)
{
out.soft_reset();
arma_stop_runtime_error("inv_sympd(): matrix is singular or not positive definite");
}
}
template<typename T1, const bool has_user_flags>
inline
bool
op_inv_spd_full::apply_direct(Mat<typename T1::elem_type>& out, const Base<typename T1::elem_type,T1>& expr, const uword flags)
{
arma_extra_debug_sigprint();
typedef typename T1::elem_type eT;
typedef typename T1::pod_type T;
if(has_user_flags == true ) { arma_extra_debug_print("op_inv_spd_full: has_user_flags = true"); }
if(has_user_flags == false) { arma_extra_debug_print("op_inv_spd_full: has_user_flags = false"); }
const bool fast = has_user_flags && bool(flags & inv_opts::flag_fast );
const bool allow_approx = has_user_flags && bool(flags & inv_opts::flag_allow_approx);
const bool no_ugly = has_user_flags && bool(flags & inv_opts::flag_no_ugly );
if(has_user_flags)
{
arma_extra_debug_print("op_inv_spd_full: enabled flags:");
if(fast ) { arma_extra_debug_print("fast"); }
if(allow_approx) { arma_extra_debug_print("allow_approx"); }
if(no_ugly ) { arma_extra_debug_print("no_ugly"); }
arma_debug_check( (fast && allow_approx), "inv_sympd(): options 'fast' and 'allow_approx' are mutually exclusive" );
arma_debug_check( (fast && no_ugly ), "inv_sympd(): options 'fast' and 'no_ugly' are mutually exclusive" );
arma_debug_check( (no_ugly && allow_approx), "inv_sympd(): options 'no_ugly' and 'allow_approx' are mutually exclusive" );
}
if(no_ugly)
{
op_inv_spd_state<T> inv_state;
const bool status = op_inv_spd_rcond::apply_direct(out, inv_state, expr);
// workaround for bug in gcc 4.8
const uword local_size = inv_state.size;
const T local_rcond = inv_state.rcond;
if((status == false) || (local_rcond < ((std::max)(local_size, uword(1)) * std::numeric_limits<T>::epsilon())) || arma_isnan(local_rcond)) { return false; }
return true;
}
if(allow_approx)
{
op_inv_spd_state<T> inv_state;
Mat<eT> tmp;
const bool status = op_inv_spd_rcond::apply_direct(tmp, inv_state, expr);
// workaround for bug in gcc 4.8
const uword local_size = inv_state.size;
const T local_rcond = inv_state.rcond;
if((status == false) || (local_rcond < ((std::max)(local_size, uword(1)) * std::numeric_limits<T>::epsilon())) || arma_isnan(local_rcond))
{
const Mat<eT> A = expr.get_ref();
if(inv_state.is_diag) { return op_pinv::apply_diag(out, A, T(0)); }
return op_pinv::apply_sym(out, A, T(0), uword(0));
}
out.steal_mem(tmp);
return true;
}
out = expr.get_ref();
arma_debug_check( (out.is_square() == false), "inv_sympd(): given matrix must be square sized", [&](){ out.soft_reset(); } );
if((arma_config::debug) && (arma_config::warn_level > 0))
{
if(auxlib::rudimentary_sym_check(out) == false)
{
if(is_cx<eT>::no ) { arma_debug_warn_level(1, "inv_sympd(): given matrix is not symmetric"); }
if(is_cx<eT>::yes) { arma_debug_warn_level(1, "inv_sympd(): given matrix is not hermitian"); }
}
else
if((is_cx<eT>::yes) && (sym_helper::check_diag_imag(out) == false))
{
arma_debug_warn_level(1, "inv_sympd(): imaginary components on diagonal are non-zero");
}
}
const uword N = out.n_rows;
if(N == 0) { return true; }
if(is_cx<eT>::no)
{
if(N == 1)
{
const T a = access::tmp_real(out[0]);
out[0] = eT(T(1) / a);
return (a > T(0));
}
else
if(N == 2)
{
const bool status = op_inv_spd_full::apply_tiny_2x2(out);
if(status) { return true; }
}
// fallthrough if optimisation failed
}
if(is_op_diagmat<T1>::value || out.is_diagmat())
{
arma_extra_debug_print("op_inv_spd_full: detected diagonal matrix");
eT* colmem = out.memptr();
for(uword i=0; i<N; ++i)
{
eT& out_ii = colmem[i];
const T out_ii_real = access::tmp_real(out_ii);
if(out_ii_real <= T(0)) { return false; }
out_ii = eT(T(1) / out_ii_real);
colmem += N;
}
return true;
}
bool sympd_state_junk = false;
return auxlib::inv_sympd(out, sympd_state_junk);
}
template<typename eT>
inline
bool
op_inv_spd_full::apply_tiny_2x2(Mat<eT>& X)
{
arma_extra_debug_sigprint();
typedef typename get_pod_type<eT>::result T;
// NOTE: assuming matrix X is square sized
// NOTE: assuming matrix X is symmetric
// NOTE: assuming matrix X is real
constexpr T det_min = std::numeric_limits<T>::epsilon();
constexpr T det_max = T(1) / std::numeric_limits<T>::epsilon();
eT* Xm = X.memptr();
T a = access::tmp_real(Xm[0]);
T c = access::tmp_real(Xm[1]);
T d = access::tmp_real(Xm[3]);
const T det_val = (a*d - c*c);
// positive definite iff all leading principal minors are positive
// a = first leading principal minor (top-left 1x1 submatrix)
// det_val = second leading principal minor (top-left 2x2 submatrix)
if(a <= T(0)) { return false; }
// NOTE: since det_min is positive, this also checks whether det_val is positive
if((det_val < det_min) || (det_val > det_max) || arma_isnan(det_val)) { return false; }
d /= det_val;
c /= det_val;
a /= det_val;
Xm[0] = d;
Xm[1] = -c;
Xm[2] = -c;
Xm[3] = a;
return true;
}
//
template<typename T1>
inline
bool
op_inv_spd_rcond::apply_direct(Mat<typename T1::elem_type>& out, op_inv_spd_state<typename T1::pod_type>& out_state, const Base<typename T1::elem_type,T1>& expr)
{
arma_extra_debug_sigprint();
typedef typename T1::elem_type eT;
typedef typename T1::pod_type T;
out = expr.get_ref();
out_state.size = out.n_rows;
out_state.rcond = T(0);
arma_debug_check( (out.is_square() == false), "inv_sympd(): given matrix must be square sized", [&](){ out.soft_reset(); } );
if((arma_config::debug) && (arma_config::warn_level > 0))
{
if(auxlib::rudimentary_sym_check(out) == false)
{
if(is_cx<eT>::no ) { arma_debug_warn_level(1, "inv_sympd(): given matrix is not symmetric"); }
if(is_cx<eT>::yes) { arma_debug_warn_level(1, "inv_sympd(): given matrix is not hermitian"); }
}
else
if((is_cx<eT>::yes) && (sym_helper::check_diag_imag(out) == false))
{
arma_debug_warn_level(1, "inv_sympd(): imaginary components on diagonal are non-zero");
}
}
if(is_op_diagmat<T1>::value || out.is_diagmat())
{
arma_extra_debug_print("op_inv_spd_rcond: detected diagonal matrix");
out_state.is_diag = true;
eT* colmem = out.memptr();
T max_abs_src_val = T(0);
T max_abs_inv_val = T(0);
const uword N = out.n_rows;
for(uword i=0; i<N; ++i)
{
eT& out_ii = colmem[i];
const eT src_val = out_ii;
const eT inv_val = eT(1) / src_val;
if( (src_val == eT(0)) || (access::tmp_real(src_val) <= T(0)) ) { return false; }
out_ii = inv_val;
const T abs_src_val = std::abs(src_val);
const T abs_inv_val = std::abs(inv_val);
max_abs_src_val = (abs_src_val > max_abs_src_val) ? abs_src_val : max_abs_src_val;
max_abs_inv_val = (abs_inv_val > max_abs_inv_val) ? abs_inv_val : max_abs_inv_val;
colmem += N;
}
out_state.rcond = T(1) / (max_abs_src_val * max_abs_inv_val);
return true;
}
if(auxlib::crippled_lapack(out))
{
arma_extra_debug_print("op_inv_spd_rcond: workaround for crippled lapack");
Mat<eT> tmp = out;
bool sympd_state = false;
auxlib::inv_sympd(out, sympd_state);
if(sympd_state == false) { out.soft_reset(); out_state.rcond = T(0); return false; }
out_state.rcond = auxlib::rcond(tmp);
if(out_state.rcond == T(0)) { out.soft_reset(); return false; }
return true;
}
bool is_sympd_junk = false;
return auxlib::inv_sympd_rcond(out, is_sympd_junk, out_state.rcond);
}
//! @}
|