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
|
// 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 fn_spsolve
//! @{
//! Solve a system of linear equations, i.e., A*X = B, where X is unknown,
//! A is sparse, and B is dense. X will be dense too.
template<typename T1, typename T2>
inline
bool
spsolve_helper
(
Mat<typename T1::elem_type>& out,
const SpBase<typename T1::elem_type, T1>& A,
const Base<typename T1::elem_type, T2>& B,
const char* solver,
const spsolve_opts_base& settings,
const typename arma_blas_type_only<typename T1::elem_type>::result* junk = nullptr
)
{
arma_extra_debug_sigprint();
arma_ignore(junk);
typedef typename T1::pod_type T;
typedef typename T1::elem_type eT;
const char sig = (solver != nullptr) ? solver[0] : char(0);
arma_debug_check( ((sig != 'l') && (sig != 's')), "spsolve(): unknown solver" );
T rcond = T(0);
bool status = false;
superlu_opts superlu_opts_default;
// if(is_float <T>::value) { superlu_opts_default.refine = superlu_opts::REF_SINGLE; }
// if(is_double<T>::value) { superlu_opts_default.refine = superlu_opts::REF_DOUBLE; }
const superlu_opts& opts = (settings.id == 1) ? static_cast<const superlu_opts&>(settings) : superlu_opts_default;
arma_debug_check( ( (opts.pivot_thresh < double(0)) || (opts.pivot_thresh > double(1)) ), "spsolve(): pivot_thresh out of bounds" );
if(sig == 's') // SuperLU solver
{
if( (opts.equilibrate == false) && (opts.refine == superlu_opts::REF_NONE) )
{
status = sp_auxlib::spsolve_simple(out, A.get_ref(), B.get_ref(), opts);
}
else
{
status = sp_auxlib::spsolve_refine(out, rcond, A.get_ref(), B.get_ref(), opts);
}
}
else
if(sig == 'l') // brutal LAPACK solver
{
if( (settings.id != 0) && ((opts.symmetric) || (opts.pivot_thresh != double(1))) )
{
arma_debug_warn("spsolve(): ignoring settings not applicable to LAPACK based solver");
}
Mat<eT> AA;
bool conversion_ok = false;
try
{
Mat<eT> tmp(A.get_ref()); // conversion from sparse to dense can throw std::bad_alloc
AA.steal_mem(tmp);
conversion_ok = true;
}
catch(std::bad_alloc&)
{
arma_debug_warn("spsolve(): not enough memory to use LAPACK based solver");
}
if(conversion_ok)
{
arma_debug_check( (AA.n_rows != AA.n_cols), "spsolve(): matrix A must be square sized" );
uword flags = solve_opts::flag_none;
if(opts.refine != superlu_opts::REF_NONE) { flags |= solve_opts::flag_refine; }
if(opts.equilibrate == true ) { flags |= solve_opts::flag_equilibrate; }
if(opts.allow_ugly == true ) { flags |= solve_opts::flag_allow_ugly; }
status = glue_solve_gen::apply(out, AA, B.get_ref(), flags);
}
}
if(status == false)
{
if(rcond > T(0)) { arma_debug_warn("spsolve(): system seems singular (rcond: ", rcond, ")"); }
else { arma_debug_warn("spsolve(): system seems singular"); }
out.soft_reset();
}
if( (status == true) && (rcond > T(0)) && (rcond < auxlib::epsilon_lapack(out)) )
{
arma_debug_warn("solve(): solution computed, but system seems singular to working precision (rcond: ", rcond, ")");
}
return status;
}
template<typename T1, typename T2>
inline
bool
spsolve
(
Mat<typename T1::elem_type>& out,
const SpBase<typename T1::elem_type, T1>& A,
const Base<typename T1::elem_type, T2>& B,
const char* solver = "superlu",
const spsolve_opts_base& settings = spsolve_opts_none(),
const typename arma_blas_type_only<typename T1::elem_type>::result* junk = nullptr
)
{
arma_extra_debug_sigprint();
arma_ignore(junk);
const bool status = spsolve_helper(out, A.get_ref(), B.get_ref(), solver, settings);
return status;
}
template<typename T1, typename T2>
arma_warn_unused
inline
Mat<typename T1::elem_type>
spsolve
(
const SpBase<typename T1::elem_type, T1>& A,
const Base<typename T1::elem_type, T2>& B,
const char* solver = "superlu",
const spsolve_opts_base& settings = spsolve_opts_none(),
const typename arma_blas_type_only<typename T1::elem_type>::result* junk = nullptr
)
{
arma_extra_debug_sigprint();
arma_ignore(junk);
typedef typename T1::elem_type eT;
Mat<eT> out;
const bool status = spsolve_helper(out, A.get_ref(), B.get_ref(), solver, settings);
if(status == false)
{
arma_stop_runtime_error("spsolve(): solution not found");
}
return out;
}
//! @}
|