1 // SPDX-License-Identifier: Apache-2.0
2 //
3 // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
4 // Copyright 2008-2016 National ICT Australia (NICTA)
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 // ------------------------------------------------------------------------
17 
18 
19 //! \addtogroup fn_pinv
20 //! @{
21 
22 
23 
24 template<typename T1>
25 arma_warn_unused
26 inline
27 typename enable_if2< is_real<typename T1::pod_type>::value, const Op<T1, op_pinv> >::result
pinv(const Base<typename T1::elem_type,T1> & X,const typename T1::pod_type tol=0.0,const char * method=nullptr)28 pinv
29   (
30   const Base<typename T1::elem_type,T1>& X,
31   const typename T1::pod_type            tol    = 0.0,
32   const char*                            method = nullptr
33   )
34   {
35   arma_extra_debug_sigprint();
36 
37   typedef typename T1::elem_type eT;
38 
39   uword method_id = 0;  // default setting
40 
41   if(method != nullptr)
42     {
43     const char sig = method[0];
44 
45     arma_debug_check( ((sig != 's') && (sig != 'd')), "pinv(): unknown method specified" );
46 
47     if(sig == 's')  { method_id = 1; }
48     if(sig == 'd')  { method_id = 2; }
49     }
50 
51   return Op<T1, op_pinv>(X.get_ref(), eT(tol), method_id, uword(0));
52   }
53 
54 
55 
56 template<typename T1>
57 inline
58 typename enable_if2< is_real<typename T1::pod_type>::value, bool >::result
pinv(Mat<typename T1::elem_type> & out,const Base<typename T1::elem_type,T1> & X,const typename T1::pod_type tol=0.0,const char * method=nullptr)59 pinv
60   (
61          Mat<typename T1::elem_type>&    out,
62   const Base<typename T1::elem_type,T1>& X,
63   const typename T1::pod_type            tol    = 0.0,
64   const char*                            method = nullptr
65   )
66   {
67   arma_extra_debug_sigprint();
68 
69   uword method_id = 0;  // default setting
70 
71   if(method != nullptr)
72     {
73     const char sig = method[0];
74 
75     arma_debug_check( ((sig != 's') && (sig != 'd')), "pinv(): unknown method specified" );
76 
77     if(sig == 's')  { method_id = 1; }
78     if(sig == 'd')  { method_id = 2; }
79     }
80 
81   const bool status = op_pinv::apply_direct(out, X.get_ref(), tol, method_id);
82 
83   if(status == false)
84     {
85     out.soft_reset();
86     arma_debug_warn_level(3, "pinv(): svd failed");
87     }
88 
89   return status;
90   }
91 
92 
93 
94 //! @}
95