summaryrefslogtreecommitdiffstats
path: root/src/armadillo/include/armadillo_bits/glue_solve_meat.hpp
blob: 1c3bbf0c0111bb7411debee32ed6ab39aee2bd28 (plain)
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
// 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 glue_solve
//! @{



//
// glue_solve_gen_default


template<typename T1, typename T2>
inline
void
glue_solve_gen_default::apply(Mat<typename T1::elem_type>& out, const Glue<T1,T2,glue_solve_gen_default>& X)
  {
  arma_extra_debug_sigprint();
  
  const bool status = glue_solve_gen_default::apply(out, X.A, X.B);
  
  if(status == false)
    {
    out.soft_reset();
    arma_stop_runtime_error("solve(): solution not found");
    }
  }



template<typename eT, typename T1, typename T2>
inline
bool
glue_solve_gen_default::apply(Mat<eT>& out, const Base<eT,T1>& A_expr, const Base<eT,T2>& B_expr)
  {
  arma_extra_debug_sigprint();
  
  return glue_solve_gen_full::apply<eT,T1,T2,false>( out, A_expr, B_expr, uword(0));
  }



//
// glue_solve_gen_full


template<typename T1, typename T2>
inline
void
glue_solve_gen_full::apply(Mat<typename T1::elem_type>& out, const Glue<T1,T2,glue_solve_gen_full>& X)
  {
  arma_extra_debug_sigprint();
  
  const bool status = glue_solve_gen_full::apply( out, X.A, X.B, X.aux_uword );
  
  if(status == false)
    {
    out.soft_reset();
    arma_stop_runtime_error("solve(): solution not found");
    }
  }



template<typename eT, typename T1, typename T2, const bool has_user_flags>
inline
bool
glue_solve_gen_full::apply(Mat<eT>& actual_out, const Base<eT,T1>& A_expr, const Base<eT,T2>& B_expr, const uword flags)
  {
  arma_extra_debug_sigprint();
  
  typedef typename get_pod_type<eT>::result T;
  
  if(has_user_flags == true )  { arma_extra_debug_print("glue_solve_gen_full::apply(): has_user_flags = true" ); }
  if(has_user_flags == false)  { arma_extra_debug_print("glue_solve_gen_full::apply(): has_user_flags = false"); }
  
  const bool fast         = has_user_flags && bool(flags & solve_opts::flag_fast        );
  const bool equilibrate  = has_user_flags && bool(flags & solve_opts::flag_equilibrate );
  const bool no_approx    = has_user_flags && bool(flags & solve_opts::flag_no_approx   );
  const bool no_band      = has_user_flags && bool(flags & solve_opts::flag_no_band     );
  const bool no_sympd     = has_user_flags && bool(flags & solve_opts::flag_no_sympd    );
  const bool allow_ugly   = has_user_flags && bool(flags & solve_opts::flag_allow_ugly  );
  const bool likely_sympd = has_user_flags && bool(flags & solve_opts::flag_likely_sympd);
  const bool refine       = has_user_flags && bool(flags & solve_opts::flag_refine      );
  const bool no_trimat    = has_user_flags && bool(flags & solve_opts::flag_no_trimat   );
  const bool force_approx = has_user_flags && bool(flags & solve_opts::flag_force_approx);
  
  if(has_user_flags)
    {
    arma_extra_debug_print("glue_solve_gen_full::apply(): enabled flags:");
    
    if(fast        )  { arma_extra_debug_print("fast");         }
    if(equilibrate )  { arma_extra_debug_print("equilibrate");  }
    if(no_approx   )  { arma_extra_debug_print("no_approx");    }
    if(no_band     )  { arma_extra_debug_print("no_band");      }
    if(no_sympd    )  { arma_extra_debug_print("no_sympd");     }
    if(allow_ugly  )  { arma_extra_debug_print("allow_ugly");   }
    if(likely_sympd)  { arma_extra_debug_print("likely_sympd"); }
    if(refine      )  { arma_extra_debug_print("refine");       }
    if(no_trimat   )  { arma_extra_debug_print("no_trimat");    }
    if(force_approx)  { arma_extra_debug_print("force_approx"); }
    
    arma_debug_check( (fast     && equilibrate ), "solve(): options 'fast' and 'equilibrate' are mutually exclusive"      );
    arma_debug_check( (fast     && refine      ), "solve(): options 'fast' and 'refine' are mutually exclusive"           );
    arma_debug_check( (no_sympd && likely_sympd), "solve(): options 'no_sympd' and 'likely_sympd' are mutually exclusive" );
    }
  
  Mat<eT> A = A_expr.get_ref();
  
  if(force_approx)
    {
    arma_extra_debug_print("glue_solve_gen_full::apply(): forced approximate solution");
    
    arma_debug_check( no_approx, "solve(): options 'no_approx' and 'force_approx' are mutually exclusive" );
    
    if(fast)          { arma_debug_warn_level(2,  "solve(): option 'fast' ignored for forced approximate solution"         ); }
    if(equilibrate)   { arma_debug_warn_level(2,  "solve(): option 'equilibrate' ignored for forced approximate solution"  ); }
    if(refine)        { arma_debug_warn_level(2,  "solve(): option 'refine' ignored for forced approximate solution"       ); }
    if(likely_sympd)  { arma_debug_warn_level(2,  "solve(): option 'likely_sympd' ignored for forced approximate solution" ); }
    
    return auxlib::solve_approx_svd(actual_out, A, B_expr.get_ref());  // A is overwritten
    }
  
  // A_expr and B_expr can be used more than once (sympd optimisation fails or approximate solution required),
  // so ensure they are not overwritten in case we have aliasing
  
  bool is_alias = true;  // assume we have aliasing until we can prove otherwise
  
  if(is_Mat<T1>::value && is_Mat<T2>::value)
    {
    const quasi_unwrap<T1> UA( A_expr.get_ref() );
    const quasi_unwrap<T2> UB( B_expr.get_ref() );
    
    is_alias = UA.is_alias(actual_out) || UB.is_alias(actual_out);
    }
  
  Mat<eT>  tmp;
  Mat<eT>& out = (is_alias) ? tmp : actual_out;
  
  T    rcond  = T(0);
  bool status = false;
  
  if(A.n_rows == A.n_cols)
    {
    arma_extra_debug_print("glue_solve_gen_full::apply(): detected square system");
    
    uword KL = 0;
    uword KU = 0;
    
    const bool is_band  = arma_config::optimise_band && ((no_band || auxlib::crippled_lapack(A)) ? false : band_helper::is_band(KL, KU, A, uword(32)));
    
    const bool is_triu = (no_trimat || refine || equilibrate || likely_sympd || is_band           ) ? false : trimat_helper::is_triu(A);
    const bool is_tril = (no_trimat || refine || equilibrate || likely_sympd || is_band || is_triu) ? false : trimat_helper::is_tril(A);
    
    const bool try_sympd = arma_config::optimise_sym && ((no_sympd || auxlib::crippled_lapack(A) || is_band || is_triu || is_tril) ? false : (likely_sympd ? true : sym_helper::guess_sympd(A, uword(16))));
    
    if(fast)
      {
      // fast mode: solvers without refinement and without rcond estimate
      
      arma_extra_debug_print("glue_solve_gen_full::apply(): fast mode");
      
      if(is_band)
        {
        if( (KL == 1) && (KU == 1) )
          {
          arma_extra_debug_print("glue_solve_gen_full::apply(): fast + tridiagonal");
          
          status = auxlib::solve_tridiag_fast(out, A, B_expr.get_ref());
          }
        else
          {
          arma_extra_debug_print("glue_solve_gen_full::apply(): fast + band");
          
          status = auxlib::solve_band_fast(out, A, KL, KU, B_expr.get_ref());
          }
        }
      else
      if(is_triu || is_tril)
        {
        if(is_triu)  { arma_extra_debug_print("glue_solve_gen_full::apply(): fast + upper triangular matrix"); }
        if(is_tril)  { arma_extra_debug_print("glue_solve_gen_full::apply(): fast + lower triangular matrix"); }
        
        const uword layout = (is_triu) ? uword(0) : uword(1);
        
        status = auxlib::solve_trimat_fast(out, A, B_expr.get_ref(), layout);
        }
      else
      if(try_sympd)
        {
        arma_extra_debug_print("glue_solve_gen_full::apply(): fast + try_sympd");
        
        status = auxlib::solve_sympd_fast(out, A, B_expr.get_ref());  // A is overwritten
        
        if(status == false)
          {
          // auxlib::solve_sympd_fast() may have failed because A isn't really sympd
          
          arma_extra_debug_print("glue_solve_gen_full::apply(): auxlib::solve_sympd_fast() failed; retrying");
          
          A = A_expr.get_ref();
          
          status = auxlib::solve_square_fast(out, A, B_expr.get_ref());  // A is overwritten
          }
        }
      else
        {
        arma_extra_debug_print("glue_solve_gen_full::apply(): fast + dense");
        
        status = auxlib::solve_square_fast(out, A, B_expr.get_ref());  // A is overwritten
        }
      }
    else
    if(refine || equilibrate)
      {
      // refine mode: solvers with refinement and with rcond estimate
      
      arma_extra_debug_print("glue_solve_gen_full::apply(): refine mode");
      
      if(is_band)
        {
        arma_extra_debug_print("glue_solve_gen_full::apply(): refine + band");
        
        status = auxlib::solve_band_refine(out, rcond, A, KL, KU, B_expr, equilibrate);
        }
      else
      if(try_sympd)
        {
        arma_extra_debug_print("glue_solve_gen_full::apply(): refine + try_sympd");
        
        status = auxlib::solve_sympd_refine(out, rcond, A, B_expr.get_ref(), equilibrate);  // A is overwritten
        
        if( (status == false) && (rcond == T(0)) )
          {
          // auxlib::solve_sympd_refine() may have failed because A isn't really sympd;
          // in that case rcond is set to zero
          
          arma_extra_debug_print("glue_solve_gen_full::apply(): auxlib::solve_sympd_refine() failed; retrying");
          
          A = A_expr.get_ref();
          
          status = auxlib::solve_square_refine(out, rcond, A, B_expr.get_ref(), equilibrate);  // A is overwritten
          }
        }
      else
        {
        arma_extra_debug_print("glue_solve_gen_full::apply(): refine + dense");
        
        status = auxlib::solve_square_refine(out, rcond, A, B_expr, equilibrate);  // A is overwritten
        }
      }
    else
      {
      // default mode: solvers without refinement but with rcond estimate
      
      arma_extra_debug_print("glue_solve_gen_full::apply(): default mode");
      
      if(is_band)
        {
        arma_extra_debug_print("glue_solve_gen_full::apply(): rcond + band");
        
        status = auxlib::solve_band_rcond(out, rcond, A, KL, KU, B_expr.get_ref());
        }
      else
      if(is_triu || is_tril)
        {
        if(is_triu)  { arma_extra_debug_print("glue_solve_gen_full::apply(): rcond + upper triangular matrix"); }
        if(is_tril)  { arma_extra_debug_print("glue_solve_gen_full::apply(): rcond + lower triangular matrix"); }
        
        const uword layout = (is_triu) ? uword(0) : uword(1);
        
        status = auxlib::solve_trimat_rcond(out, rcond, A, B_expr.get_ref(), layout);
        }
      else
      if(try_sympd)
        {
        bool sympd_state = false;
        
        status = auxlib::solve_sympd_rcond(out, sympd_state, rcond, A, B_expr.get_ref());  // A is overwritten
        
        if( (status == false) && (sympd_state == false) )
          {
          arma_extra_debug_print("glue_solve_gen_full::apply(): auxlib::solve_sympd_rcond() failed; retrying");
          
          A = A_expr.get_ref();
          
          status = auxlib::solve_square_rcond(out, rcond, A, B_expr.get_ref());  // A is overwritten
          }
        }
      else
        {
        status = auxlib::solve_square_rcond(out, rcond, A, B_expr.get_ref());  // A is overwritten
        }
      }
    }
  else
    {
    arma_extra_debug_print("glue_solve_gen_full::apply(): detected non-square system");
    
    if(equilibrate)   { arma_debug_warn_level(2,  "solve(): option 'equilibrate' ignored for non-square matrix"  ); }
    if(refine)        { arma_debug_warn_level(2,  "solve(): option 'refine' ignored for non-square matrix"       ); }
    if(likely_sympd)  { arma_debug_warn_level(2,  "solve(): option 'likely_sympd' ignored for non-square matrix" ); }
    
    if(fast)
      {
      status = auxlib::solve_rect_fast(out, A, B_expr.get_ref());  // A is overwritten
      }
    else
      {
      status = auxlib::solve_rect_rcond(out, rcond, A, B_expr.get_ref());  // A is overwritten
      }
    }
  
  
  if( (status == true) && (fast == false) && (allow_ugly == false) && ((rcond < std::numeric_limits<T>::epsilon()) || arma_isnan(rcond)) )
    {
    status = false;
    }
  
  
  if( (status == false) && (no_approx == false) )
    {
    arma_extra_debug_print("glue_solve_gen_full::apply(): solving rank deficient system");
    
    if(rcond == T(0))
      {
      arma_debug_warn_level(2, "solve(): system is singular; attempting approx solution");
      }
    else
      {
      arma_debug_warn_level(2, "solve(): system is singular (rcond: ", rcond, "); attempting approx solution");
      }
    
    // TODO: conditionally recreate A: have a separate state flag which indicates whether A was previously overwritten
    
    A = A_expr.get_ref();  // as A may have been overwritten
    
    status = auxlib::solve_approx_svd(out, A, B_expr.get_ref());  // A is overwritten
    }
  
  if(is_alias)  { actual_out.steal_mem(out); }
  
  return status;
  }



//
// glue_solve_tri_default


template<typename T1, typename T2>
inline
void
glue_solve_tri_default::apply(Mat<typename T1::elem_type>& out, const Glue<T1,T2,glue_solve_tri_default>& X)
  {
  arma_extra_debug_sigprint();
  
  const bool status = glue_solve_tri_default::apply( out, X.A, X.B, X.aux_uword );
  
  if(status == false)
    {
    out.soft_reset();
    arma_stop_runtime_error("solve(): solution not found");
    }
  }



template<typename eT, typename T1, typename T2>
inline
bool
glue_solve_tri_default::apply(Mat<eT>& actual_out, const Base<eT,T1>& A_expr, const Base<eT,T2>& B_expr, const uword flags)
  {
  arma_extra_debug_sigprint();
  
  typedef typename get_pod_type<eT>::result T;
  
  const bool triu = bool(flags & solve_opts::flag_triu);
  const bool tril = bool(flags & solve_opts::flag_tril);
  
  arma_extra_debug_print("glue_solve_tri_default::apply(): enabled flags:");
  
  if(triu)  { arma_extra_debug_print("triu"); }
  if(tril)  { arma_extra_debug_print("tril"); }
  
  const quasi_unwrap<T1> UA(A_expr.get_ref());
  const Mat<eT>& A     = UA.M;
  
  arma_debug_check( (A.is_square() == false), "solve(): matrix marked as triangular must be square sized" );
  
  const uword layout = (triu) ? uword(0) : uword(1);
  
  bool is_alias = true;
  
  if(is_Mat<T2>::value)
    {
    const quasi_unwrap<T2> UB(B_expr.get_ref());
    
    is_alias = UA.is_alias(actual_out) || UB.is_alias(actual_out);
    }
  
  T    rcond  = T(0);
  bool status = false;
  
  Mat<eT>  tmp;
  Mat<eT>& out = (is_alias) ? tmp : actual_out;
  
  status = auxlib::solve_trimat_rcond(out, rcond, A, B_expr.get_ref(), layout);  // A is not modified
  
  
  if( (status == true) && ( (rcond < std::numeric_limits<T>::epsilon()) || arma_isnan(rcond) ) )
    {
    status = false;
    }
  
  
  if(status == false)
    {
    arma_extra_debug_print("glue_solve_tri_default::apply(): solving rank deficient system");
    
    if(rcond == T(0))
      {
      arma_debug_warn_level(2, "solve(): system is singular; attempting approx solution");
      }
    else
      {
      arma_debug_warn_level(2, "solve(): system is singular (rcond: ", rcond, "); attempting approx solution");
      }
    
    Mat<eT> triA = (triu) ? trimatu(A) : trimatl(A);  // trimatu() and trimatl() return the same type
    
    status = auxlib::solve_approx_svd(out, triA, B_expr.get_ref());  // triA is overwritten
    }
  
  
  if(is_alias)  { actual_out.steal_mem(out); }
  
  return status;
  }



//
// glue_solve_tri_full


template<typename T1, typename T2>
inline
void
glue_solve_tri_full::apply(Mat<typename T1::elem_type>& out, const Glue<T1,T2,glue_solve_tri_full>& X)
  {
  arma_extra_debug_sigprint();
  
  const bool status = glue_solve_tri_full::apply( out, X.A, X.B, X.aux_uword );
  
  if(status == false)
    {
    out.soft_reset();
    arma_stop_runtime_error("solve(): solution not found");
    }
  }



template<typename eT, typename T1, typename T2>
inline
bool
glue_solve_tri_full::apply(Mat<eT>& actual_out, const Base<eT,T1>& A_expr, const Base<eT,T2>& B_expr, const uword flags)
  {
  arma_extra_debug_sigprint();
  
  typedef typename get_pod_type<eT>::result T;
  
  const bool fast         = bool(flags & solve_opts::flag_fast        );
  const bool equilibrate  = bool(flags & solve_opts::flag_equilibrate );
  const bool no_approx    = bool(flags & solve_opts::flag_no_approx   );
  const bool triu         = bool(flags & solve_opts::flag_triu        );
  const bool tril         = bool(flags & solve_opts::flag_tril        );
  const bool allow_ugly   = bool(flags & solve_opts::flag_allow_ugly  );
  const bool likely_sympd = bool(flags & solve_opts::flag_likely_sympd);
  const bool refine       = bool(flags & solve_opts::flag_refine      );
  const bool no_trimat    = bool(flags & solve_opts::flag_no_trimat   );
  const bool force_approx = bool(flags & solve_opts::flag_force_approx);
  
  arma_extra_debug_print("glue_solve_tri_full::apply(): enabled flags:");
  
  if(fast        )  { arma_extra_debug_print("fast");         }
  if(equilibrate )  { arma_extra_debug_print("equilibrate");  }
  if(no_approx   )  { arma_extra_debug_print("no_approx");    }
  if(triu        )  { arma_extra_debug_print("triu");         }
  if(tril        )  { arma_extra_debug_print("tril");         }
  if(allow_ugly  )  { arma_extra_debug_print("allow_ugly");   }
  if(likely_sympd)  { arma_extra_debug_print("likely_sympd"); }
  if(refine      )  { arma_extra_debug_print("refine");       }
  if(no_trimat   )  { arma_extra_debug_print("no_trimat");    }
  if(force_approx)  { arma_extra_debug_print("force_approx"); }
  
  if(no_trimat || equilibrate || refine || force_approx)
    {
    const uword mask = ~(solve_opts::flag_triu | solve_opts::flag_tril);
    
    return glue_solve_gen_full::apply(actual_out, ((triu) ? trimatu(A_expr.get_ref()) : trimatl(A_expr.get_ref())), B_expr, (flags & mask));
    }
  
  if(likely_sympd)  { arma_debug_warn_level(2, "solve(): option 'likely_sympd' ignored for triangular matrix"); }
  
  const quasi_unwrap<T1> UA(A_expr.get_ref());
  const Mat<eT>& A     = UA.M;
  
  arma_debug_check( (A.is_square() == false), "solve(): matrix marked as triangular must be square sized" );
  
  const uword layout = (triu) ? uword(0) : uword(1);
  
  bool is_alias = true;
  
  if(is_Mat<T2>::value)
    {
    const quasi_unwrap<T2> UB(B_expr.get_ref());
    
    is_alias = UA.is_alias(actual_out) || UB.is_alias(actual_out);
    }
  
  T    rcond  = T(0);
  bool status = false;
  
  Mat<eT>  tmp;
  Mat<eT>& out = (is_alias) ? tmp : actual_out;
  
  if(fast)
    {
    status = auxlib::solve_trimat_fast(out, A, B_expr.get_ref(), layout);  // A is not modified
    }
  else
    {
    status = auxlib::solve_trimat_rcond(out, rcond, A, B_expr.get_ref(), layout);  // A is not modified
    }
  
  
  if( (status == true) && (fast == false) && (allow_ugly == false) && ((rcond < std::numeric_limits<T>::epsilon()) || arma_isnan(rcond)) )
    {
    status = false;
    }
  
  
  if( (status == false) && (no_approx == false) )
    {
    arma_extra_debug_print("glue_solve_tri_full::apply(): solving rank deficient system");
    
    if(rcond == T(0))
      {
      arma_debug_warn_level(2, "solve(): system is singular; attempting approx solution");
      }
    else
      {
      arma_debug_warn_level(2, "solve(): system is singular (rcond: ", rcond, "); attempting approx solution");
      }
    
    Mat<eT> triA = (triu) ? trimatu(A) : trimatl(A);  // trimatu() and trimatl() return the same type
    
    status = auxlib::solve_approx_svd(out, triA, B_expr.get_ref());  // triA is overwritten
    }
  
  
  if(is_alias)  { actual_out.steal_mem(out); }
  
  return status;
  }



//! @}