PermMatrix.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2008-2012 Jaroslav Hajek
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #if !defined (octave_PermMatrix_h)
00024 #define octave_PermMatrix_h 1
00025 
00026 #include "Array.h"
00027 #include "mx-defs.h"
00028 
00029 // Array<T> is inherited privately so that some methods, like index, don't
00030 // produce unexpected results.
00031 
00032 class OCTAVE_API PermMatrix : protected Array<octave_idx_type>
00033 {
00034 
00035 public:
00036 
00037   PermMatrix (void) : Array<octave_idx_type> (), _colp (false) { }
00038 
00039   PermMatrix (octave_idx_type n);
00040 
00041   PermMatrix (const Array<octave_idx_type>& p, bool colp = false,
00042               bool check = true);
00043 
00044   PermMatrix (const PermMatrix& m)
00045     : Array<octave_idx_type> (m), _colp(m._colp) { }
00046 
00047   PermMatrix (const idx_vector& idx, bool colp = false, octave_idx_type n = 0);
00048 
00049   octave_idx_type dim1 (void) const
00050     { return Array<octave_idx_type>::length (); }
00051   octave_idx_type dim2 (void) const
00052     { return Array<octave_idx_type>::length (); }
00053 
00054   octave_idx_type rows (void) const { return dim1 (); }
00055   octave_idx_type cols (void) const { return dim2 (); }
00056   octave_idx_type columns (void) const { return dim2 (); }
00057 
00058   octave_idx_type perm_length (void) const
00059     { return Array<octave_idx_type>::length (); }
00060   // FIXME: a dangerous ambiguity?
00061   octave_idx_type length (void) const
00062     { return perm_length (); }
00063   octave_idx_type nelem (void) const { return dim1 () * dim2 (); }
00064   octave_idx_type numel (void) const { return nelem (); }
00065 
00066   size_t byte_size (void) const
00067     { return Array<octave_idx_type>::byte_size (); }
00068 
00069   dim_vector dims (void) const { return dim_vector (dim1 (), dim2 ()); }
00070 
00071   Array<octave_idx_type> pvec (void) const
00072     { return *this; }
00073 
00074   octave_idx_type
00075   elem (octave_idx_type i, octave_idx_type j) const
00076     {
00077       return (_colp
00078               ? ((Array<octave_idx_type>::elem (j) == i) ? 1 : 0)
00079               : ((Array<octave_idx_type>::elem (i) == j) ? 1 : 0));
00080     }
00081 
00082   octave_idx_type
00083   checkelem (octave_idx_type i, octave_idx_type j) const;
00084 
00085   octave_idx_type
00086   operator () (octave_idx_type i, octave_idx_type j) const
00087     {
00088 #if defined (BOUNDS_CHECKING)
00089       return checkelem (i, j);
00090 #else
00091       return elem (i, j);
00092 #endif
00093     }
00094 
00095   // These are, in fact, super-fast.
00096   PermMatrix transpose (void) const;
00097   PermMatrix inverse (void) const;
00098 
00099   // Determinant, i.e. the sign of permutation.
00100   octave_idx_type determinant (void) const;
00101 
00102   // Efficient integer power of a permutation.
00103   PermMatrix power (octave_idx_type n) const;
00104 
00105   bool is_col_perm (void) const { return _colp; }
00106   bool is_row_perm (void) const { return !_colp; }
00107 
00108   friend OCTAVE_API PermMatrix operator *(const PermMatrix& a, const PermMatrix& b);
00109 
00110   const octave_idx_type *data (void) const
00111     { return Array<octave_idx_type>::data (); }
00112 
00113   const octave_idx_type *fortran_vec (void) const
00114     { return Array<octave_idx_type>::fortran_vec (); }
00115 
00116   octave_idx_type *fortran_vec (void)
00117     { return Array<octave_idx_type>::fortran_vec (); }
00118 
00119   void print_info (std::ostream& os, const std::string& prefix) const
00120     { Array<octave_idx_type>::print_info (os, prefix); }
00121 
00122   static PermMatrix eye (octave_idx_type n);
00123 
00124 private:
00125   bool _colp;
00126 };
00127 
00128 // Multiplying permutations together.
00129 PermMatrix
00130 OCTAVE_API
00131 operator *(const PermMatrix& a, const PermMatrix& b);
00132 
00133 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines