GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
PermMatrix.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2008-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_PermMatrix_h)
27 #define octave_PermMatrix_h 1
28 
29 #include "octave-config.h"
30 
31 #include "Array.h"
32 #include "mx-defs.h"
33 
34 // Array<T> is inherited privately so that some methods, like index, don't
35 // produce unexpected results.
36 
37 class OCTAVE_API PermMatrix : protected Array<octave_idx_type>
38 {
39 public:
40 
41  PermMatrix (void) = default;
42 
43  PermMatrix (const PermMatrix& m) = default;
44 
45  PermMatrix& operator = (const PermMatrix& m) = default;
46 
47  ~PermMatrix (void) = default;
48 
50 
51  PermMatrix (const Array<octave_idx_type>& p, bool colp, bool check = true);
52 
53  PermMatrix (const idx_vector& idx, bool colp, octave_idx_type n = 0);
54 
55  octave_idx_type dim1 (void) const
56  { return Array<octave_idx_type>::numel (); }
57  octave_idx_type dim2 (void) const
58  { return Array<octave_idx_type>::numel (); }
59 
60  octave_idx_type rows (void) const { return dim1 (); }
61  octave_idx_type cols (void) const { return dim2 (); }
62  octave_idx_type columns (void) const { return dim2 (); }
63 
65  { return Array<octave_idx_type>::numel (); }
66  // FIXME: a dangerous ambiguity?
67  octave_idx_type length (void) const
68  { return perm_length (); }
69 
70  octave_idx_type numel (void) const { return dim1 () * dim2 (); }
71 
72  size_t byte_size (void) const
74 
75  dim_vector dims (void) const { return dim_vector (dim1 (), dim2 ()); }
76 
77  bool isempty (void) const { return numel () == 0; }
78 
79  int ndims (void) const { return 2; }
80 
82  { return *this; }
83 
86  {
87  return (Array<octave_idx_type>::elem (j) == i) ? 1 : 0;
88  }
89 
92 
95  {
96  return elem (i, j);
97  }
98 
99  // These are, in fact, super-fast.
100  PermMatrix transpose (void) const;
101  PermMatrix inverse (void) const;
102 
103  // Determinant, i.e., the sign of permutation.
104  octave_idx_type determinant (void) const;
105 
106  // Efficient integer power of a permutation.
107  PermMatrix power (octave_idx_type n) const;
108 
109  bool is_col_perm (void) const { return true; }
110  bool is_row_perm (void) const { return false; }
111 
112  void print_info (std::ostream& os, const std::string& prefix) const
113  { Array<octave_idx_type>::print_info (os, prefix); }
114 
115  static PermMatrix eye (octave_idx_type n);
116 
117 private:
118 
119  PermMatrix pos_power (octave_idx_type m) const;
120 
121  void setup (const Array<octave_idx_type>& p, bool colp, bool check);
122 
123  void setup (const idx_vector& idx, bool colp, octave_idx_type n);
124 };
125 
126 // Multiplying permutations together.
128 OCTAVE_API
129 operator * (const PermMatrix& a, const PermMatrix& b);
130 
131 #endif
PermMatrix OCTAVE_API operator*(const PermMatrix &a, const PermMatrix &b)
Definition: PermMatrix.cc:211
N Dimensional Array with copy-on-write semantics.
Definition: Array.h:128
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:377
Array< T > & operator=(const Array< T > &a)
Definition: Array.h:325
T & elem(octave_idx_type n)
Size of the specified dimension.
Definition: Array.h:499
T & operator()(octave_idx_type n)
Size of the specified dimension.
Definition: Array.h:513
size_t byte_size(void) const
Size of the specified dimension.
Definition: Array.h:449
T & checkelem(octave_idx_type n)
Size of the specified dimension.
Definition: Array.cc:192
Array< T > transpose(void) const
Size of the specified dimension.
Definition: Array.cc:1599
octave_idx_type dim2(void) const
Definition: Array.h:422
void print_info(std::ostream &os, const std::string &prefix) const
Size of the specified dimension.
Definition: Array.cc:2734
octave_idx_type dim1(void) const
Definition: Array.h:414
octave_idx_type perm_length(void) const
Definition: PermMatrix.h:64
const Array< octave_idx_type > & col_perm_vec(void) const
Definition: PermMatrix.h:81
octave_idx_type dim2(void) const
Definition: PermMatrix.h:57
bool is_row_perm(void) const
Definition: PermMatrix.h:110
octave_idx_type cols(void) const
Definition: PermMatrix.h:61
octave_idx_type columns(void) const
Definition: PermMatrix.h:62
octave_idx_type dim1(void) const
Definition: PermMatrix.h:55
octave_idx_type elem(octave_idx_type i, octave_idx_type j) const
Definition: PermMatrix.h:85
void print_info(std::ostream &os, const std::string &prefix) const
Definition: PermMatrix.h:112
size_t byte_size(void) const
Definition: PermMatrix.h:72
~PermMatrix(void)=default
octave_idx_type rows(void) const
Definition: PermMatrix.h:60
PermMatrix(const PermMatrix &m)=default
dim_vector dims(void) const
Definition: PermMatrix.h:75
PermMatrix(void)=default
octave_idx_type length(void) const
Definition: PermMatrix.h:67
octave_idx_type numel(void) const
Definition: PermMatrix.h:70
bool is_col_perm(void) const
Definition: PermMatrix.h:109
bool isempty(void) const
Definition: PermMatrix.h:77
int ndims(void) const
Definition: PermMatrix.h:79
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:95
T octave_idx_type m
Definition: mx-inlines.cc:773
octave_idx_type n
Definition: mx-inlines.cc:753