GNU Octave  9.1.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-2024 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 () = default;
42 
43  PermMatrix (const PermMatrix& m) = default;
44 
45  PermMatrix& operator = (const PermMatrix& m) = default;
46 
47  ~PermMatrix () = default;
48 
50 
51  OCTAVE_API PermMatrix (const Array<octave_idx_type>& p, bool colp,
52  bool check = true);
53 
54  OCTAVE_API PermMatrix (const octave::idx_vector& idx, bool colp,
55  octave_idx_type n = 0);
56 
58  { return Array<octave_idx_type>::numel (); }
60  { return Array<octave_idx_type>::numel (); }
61 
62  octave_idx_type rows () const { return dim1 (); }
63  octave_idx_type cols () const { return dim2 (); }
64  octave_idx_type columns () const { return dim2 (); }
65 
67  { return Array<octave_idx_type>::numel (); }
68  // FIXME: a dangerous ambiguity?
70  { return perm_length (); }
71 
72  octave_idx_type numel () const { return dim1 () * dim2 (); }
73 
74  std::size_t byte_size () const
76 
77  dim_vector dims () const { return dim_vector (dim1 (), dim2 ()); }
78 
79  bool isempty () const { return numel () == 0; }
80 
81  int ndims () const { return 2; }
82 
84  { return *this; }
85 
88  {
89  return (Array<octave_idx_type>::elem (j) == i) ? 1 : 0;
90  }
91 
93  checkelem (octave_idx_type i, octave_idx_type j) const;
94 
96  operator () (octave_idx_type i, octave_idx_type j) const
97  {
98  return elem (i, j);
99  }
100 
101  // These are, in fact, super-fast.
102  OCTAVE_API PermMatrix transpose () const;
103  OCTAVE_API PermMatrix inverse () const;
104 
105  // Determinant, i.e., the sign of permutation.
106  OCTAVE_API octave_idx_type determinant () const;
107 
108  // Efficient integer power of a permutation.
109  OCTAVE_API PermMatrix power (octave_idx_type n) const;
110 
111  bool is_col_perm () const { return true; }
112  bool is_row_perm () const { return false; }
113 
114  void print_info (std::ostream& os, const std::string& prefix) const
115  { Array<octave_idx_type>::print_info (os, prefix); }
116 
117  static OCTAVE_API PermMatrix eye (octave_idx_type n);
118 
119 private:
120 
121  PermMatrix pos_power (octave_idx_type m) const;
122 
123  void setup (const Array<octave_idx_type>& p, bool colp, bool check);
124 
125  void setup (const octave::idx_vector& idx, bool colp, octave_idx_type n);
126 };
127 
128 // Multiplying permutations together.
130 operator * (const PermMatrix& a, const PermMatrix& b);
131 
132 #endif
PermMatrix operator*(const PermMatrix &a, const PermMatrix &b)
Definition: PermMatrix.cc:211
N Dimensional Array with copy-on-write semantics.
Definition: Array.h:130
std::size_t byte_size() const
Size of the specified dimension.
Definition: Array.h:499
Array< T, Alloc > & operator=(const Array< T, Alloc > &a)
Definition: Array.h:361
void print_info(std::ostream &os, const std::string &prefix) const
Size of the specified dimension.
Definition: Array-base.cc:2747
octave_idx_type numel() const
Number of elements in the array.
Definition: Array.h:414
bool isempty() const
Definition: PermMatrix.h:79
bool is_row_perm() const
Definition: PermMatrix.h:112
~PermMatrix()=default
octave_idx_type elem(octave_idx_type i, octave_idx_type j) const
Definition: PermMatrix.h:87
octave_idx_type numel() const
Definition: PermMatrix.h:72
octave_idx_type rows() const
Definition: PermMatrix.h:62
void print_info(std::ostream &os, const std::string &prefix) const
Definition: PermMatrix.h:114
PermMatrix(const PermMatrix &m)=default
octave_idx_type dim2() const
Definition: PermMatrix.h:59
PermMatrix()=default
octave_idx_type dim1() const
Definition: PermMatrix.h:57
octave_idx_type cols() const
Definition: PermMatrix.h:63
const Array< octave_idx_type > & col_perm_vec() const
Definition: PermMatrix.h:83
octave_idx_type perm_length() const
Definition: PermMatrix.h:66
bool is_col_perm() const
Definition: PermMatrix.h:111
octave_idx_type columns() const
Definition: PermMatrix.h:64
int ndims() const
Definition: PermMatrix.h:81
std::size_t byte_size() const
Definition: PermMatrix.h:74
dim_vector dims() const
Definition: PermMatrix.h:77
octave_idx_type length() const
Definition: PermMatrix.h:69
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
octave::idx_vector idx_vector
Definition: idx-vector.h:1022
#define OCTAVE_API
Definition: main.cc:55
T octave_idx_type m
Definition: mx-inlines.cc:781
octave_idx_type n
Definition: mx-inlines.cc:761
T::size_type numel(const T &str)
Definition: oct-string.cc:74