1 | // Copyright (C) 2009-2012 Conrad Sanderson |
---|
2 | // Copyright (C) 2009-2012 NICTA (www.nicta.com.au) |
---|
3 | // |
---|
4 | // This Source Code Form is subject to the terms of the Mozilla Public |
---|
5 | // License, v. 2.0. If a copy of the MPL was not distributed with this |
---|
6 | // file, You can obtain one at http://mozilla.org/MPL/2.0/. |
---|
7 | |
---|
8 | |
---|
9 | //! \addtogroup glue_solve |
---|
10 | //! @{ |
---|
11 | |
---|
12 | |
---|
13 | |
---|
14 | template<typename eT, typename T2> |
---|
15 | inline |
---|
16 | void |
---|
17 | glue_solve::solve_direct(Mat<eT>& out, Mat<eT>& A, const Base<eT,T2>& X, const bool slow) |
---|
18 | { |
---|
19 | arma_extra_debug_sigprint(); |
---|
20 | |
---|
21 | const uword A_n_rows = A.n_rows; |
---|
22 | const uword A_n_cols = A.n_cols; |
---|
23 | |
---|
24 | bool status = false; |
---|
25 | |
---|
26 | if(A_n_rows == A_n_cols) |
---|
27 | { |
---|
28 | status = auxlib::solve(out, A, X, slow); |
---|
29 | } |
---|
30 | else |
---|
31 | if(A_n_rows > A_n_cols) |
---|
32 | { |
---|
33 | arma_extra_debug_print("solve(): detected over-determined system"); |
---|
34 | status = auxlib::solve_od(out, A, X); |
---|
35 | } |
---|
36 | else |
---|
37 | { |
---|
38 | arma_extra_debug_print("solve(): detected under-determined system"); |
---|
39 | status = auxlib::solve_ud(out, A, X); |
---|
40 | } |
---|
41 | |
---|
42 | if(status == false) |
---|
43 | { |
---|
44 | out.reset(); |
---|
45 | arma_bad("solve(): solution not found"); |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | |
---|
50 | |
---|
51 | template<typename T1, typename T2> |
---|
52 | inline |
---|
53 | void |
---|
54 | glue_solve::apply(Mat<typename T1::elem_type>& out, const Glue<T1,T2,glue_solve>& X) |
---|
55 | { |
---|
56 | arma_extra_debug_sigprint(); |
---|
57 | |
---|
58 | typedef typename T1::elem_type eT; |
---|
59 | |
---|
60 | Mat<eT> A = X.A.get_ref(); |
---|
61 | |
---|
62 | glue_solve::solve_direct( out, A, X.B, (X.aux_uword == 1) ); |
---|
63 | } |
---|
64 | |
---|
65 | |
---|
66 | |
---|
67 | template<typename T1, typename T2> |
---|
68 | inline |
---|
69 | void |
---|
70 | glue_solve_tr::apply(Mat<typename T1::elem_type>& out, const Glue<T1,T2,glue_solve_tr>& X) |
---|
71 | { |
---|
72 | arma_extra_debug_sigprint(); |
---|
73 | |
---|
74 | typedef typename T1::elem_type eT; |
---|
75 | |
---|
76 | const unwrap_check<T1> A_tmp(X.A, out); |
---|
77 | const unwrap_check<T2> B_tmp(X.B, out); |
---|
78 | |
---|
79 | const Mat<eT>& A = A_tmp.M; |
---|
80 | const Mat<eT>& B = B_tmp.M; |
---|
81 | |
---|
82 | bool err_state = false; |
---|
83 | char* err_msg = 0; |
---|
84 | |
---|
85 | arma_debug_set_error( err_state, err_msg, ((&A) == (&B)), "solve(): A is an alias of B" ); |
---|
86 | arma_debug_set_error( err_state, err_msg, (A.n_rows != B.n_rows), "solve(): number of rows in A and B must be the same" ); |
---|
87 | arma_debug_set_error( err_state, err_msg, (A.is_square() == false), "solve(): A is not a square matrix" ); |
---|
88 | |
---|
89 | arma_debug_check(err_state, err_msg); |
---|
90 | |
---|
91 | const bool status = auxlib::solve_tr(out, A, B, X.aux_uword); |
---|
92 | |
---|
93 | if(status == false) |
---|
94 | { |
---|
95 | out.reset(); |
---|
96 | arma_bad("solve(): solution not found"); |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | |
---|
102 | //! @} |
---|