1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1994-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING.  If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_aepbalance_h)
27 #define octave_aepbalance_h 1
28 
29 #include "octave-config.h"
30 
31 #include <algorithm>
32 
33 namespace octave
34 {
35   namespace math
36   {
37     template <typename MT>
38     class aepbalance
39     {
40     public:
41 
42       typedef typename MT::real_column_vector_type VT;
43 
aepbalance(void)44       aepbalance (void) : balanced_mat (), scale (), ilo (), ihi (), job () { }
45 
46       aepbalance (const MT& a, bool noperm = false, bool noscal = false);
47 
aepbalance(const aepbalance & a)48       aepbalance (const aepbalance& a)
49         : balanced_mat (a.balanced_mat), scale (a.scale),
50           ilo(a.ilo), ihi(a.ihi), job(a.job)
51       { }
52 
53       aepbalance& operator = (const aepbalance& a)
54       {
55         if (this != &a)
56           {
57             balanced_mat = a.balanced_mat;
58             scale = a.scale;
59             ilo = a.ilo;
60             ihi = a.ihi;
61             job = a.job;
62           }
63 
64         return *this;
65       }
66 
67       virtual ~aepbalance (void) = default;
68 
69       MT balancing_matrix (void) const;
70 
balanced_matrix(void)71       MT balanced_matrix (void) const
72       {
73         return balanced_mat;
74       }
75 
permuting_vector(void)76       VT permuting_vector (void) const
77       {
78         octave_idx_type n = balanced_mat.rows ();
79 
80         VT pv (n);
81 
82         for (octave_idx_type i = 0; i < n; i++)
83           pv(i) = i+1;
84 
85         for (octave_idx_type i = n-1; i >= ihi; i--)
86           {
87             octave_idx_type j = scale(i) - 1;
88             std::swap (pv(i), pv(j));
89           }
90 
91         for (octave_idx_type i = 0; i < ilo-1; i++)
92           {
93             octave_idx_type j = scale(i) - 1;
94             std::swap (pv(i), pv(j));
95           }
96 
97         return pv;
98       }
99 
scaling_vector(void)100       VT scaling_vector (void) const
101       {
102         octave_idx_type n = balanced_mat.rows ();
103 
104         VT scv (n);
105 
106         for (octave_idx_type i = 0; i < ilo-1; i++)
107           scv(i) = 1;
108 
109         for (octave_idx_type i = ilo-1; i < ihi; i++)
110           scv(i) = scale(i);
111 
112         for (octave_idx_type i = ihi; i < n; i++)
113           scv(i) = 1;
114 
115         return scv;
116       }
117 
118     protected:
119 
120       MT balanced_mat;
121       VT scale;
122       octave_idx_type ilo;
123       octave_idx_type ihi;
124       char job;
125     };
126   }
127 }
128 
129 #endif
130