GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PermMatrix.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2008-2013 Jaroslav Hajek
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if !defined (octave_PermMatrix_h)
24 #define octave_PermMatrix_h 1
25 
26 #include "Array.h"
27 #include "mx-defs.h"
28 
29 // Array<T> is inherited privately so that some methods, like index, don't
30 // produce unexpected results.
31 
32 class OCTAVE_API PermMatrix : protected Array<octave_idx_type>
33 {
34 
35 public:
36 
37  PermMatrix (void) : Array<octave_idx_type> (), _colp (false) { }
38 
40 
41  PermMatrix (const Array<octave_idx_type>& p, bool colp = false,
42  bool check = true);
43 
45  : Array<octave_idx_type> (m), _colp(m._colp) { }
46 
47  PermMatrix (const idx_vector& idx, bool colp = false, octave_idx_type n = 0);
48 
49  octave_idx_type dim1 (void) const
50  { return Array<octave_idx_type>::length (); }
51  octave_idx_type dim2 (void) const
52  { return Array<octave_idx_type>::length (); }
53 
54  octave_idx_type rows (void) const { return dim1 (); }
55  octave_idx_type cols (void) const { return dim2 (); }
56  octave_idx_type columns (void) const { return dim2 (); }
57 
58  octave_idx_type perm_length (void) const
59  { return Array<octave_idx_type>::length (); }
60  // FIXME: a dangerous ambiguity?
61  octave_idx_type length (void) const
62  { return perm_length (); }
63  octave_idx_type nelem (void) const { return dim1 () * dim2 (); }
64  octave_idx_type numel (void) const { return nelem (); }
65 
66  size_t byte_size (void) const
68 
69  dim_vector dims (void) const { return dim_vector (dim1 (), dim2 ()); }
70 
71  Array<octave_idx_type> pvec (void) const
72  { return *this; }
73 
76  {
77  return (_colp
78  ? ((Array<octave_idx_type>::elem (j) == i) ? 1 : 0)
79  : ((Array<octave_idx_type>::elem (i) == j) ? 1 : 0));
80  }
81 
84 
87  {
88 #if defined (BOUNDS_CHECKING)
89  return checkelem (i, j);
90 #else
91  return elem (i, j);
92 #endif
93  }
94 
95  // These are, in fact, super-fast.
96  PermMatrix transpose (void) const;
97  PermMatrix inverse (void) const;
98 
99  // Determinant, i.e. the sign of permutation.
100  octave_idx_type determinant (void) const;
101 
102  // Efficient integer power of a permutation.
103  PermMatrix power (octave_idx_type n) const;
104 
105  bool is_col_perm (void) const { return _colp; }
106  bool is_row_perm (void) const { return !_colp; }
107 
108  friend OCTAVE_API PermMatrix operator *(const PermMatrix& a,
109  const PermMatrix& b);
110 
111  const octave_idx_type *data (void) const
112  { return Array<octave_idx_type>::data (); }
113 
114  const octave_idx_type *fortran_vec (void) const
116 
119 
120  void print_info (std::ostream& os, const std::string& prefix) const
121  { Array<octave_idx_type>::print_info (os, prefix); }
122 
123  static PermMatrix eye (octave_idx_type n);
124 
125 private:
126  bool _colp;
127 };
128 
129 // Multiplying permutations together.
131 OCTAVE_API
132 operator *(const PermMatrix& a, const PermMatrix& b);
133 
134 #endif