annotate liboctave/PermMatrix.cc @ 9708:6f3ffe11d926

implement luupdate
author Jaroslav Hajek <highegg@gmail.com>
date Thu, 08 Oct 2009 16:05:53 +0200
parents 9fba7e1da785
children c6b97d218345
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8367
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
1 /*
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
2
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 8524
diff changeset
3 Copyright (C) 2008, 2009 Jaroslav Hajek
8367
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
4
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
5 This file is part of Octave.
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
6
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
7 Octave is free software; you can redistribute it and/or modify it
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
9 Free Software Foundation; either version 3 of the License, or (at your
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
10 option) any later version.
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
11
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
12 Octave is distributed in the hope that it will be useful, but WITHOUT
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
15 for more details.
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
16
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
18 along with Octave; see the file COPYING. If not, see
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
19 <http://www.gnu.org/licenses/>.
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
20
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
21 */
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
22
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
23 #ifdef HAVE_CONFIG_H
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
24 #include <config.h>
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
25 #endif
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
26
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
27 #include "PermMatrix.h"
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
28 #include "idx-vector.h"
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
29 #include "error.h"
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
30 #include "Array-util.h"
9695
9fba7e1da785 correct algorithm for perm matrix det (sign)
Jaroslav Hajek <highegg@gmail.com>
parents: 8958
diff changeset
31 #include "oct-locbuf.h"
8367
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
32
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
33 static void
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
34 gripe_invalid_permutation (void)
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
35 {
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
36 (*current_liboctave_error_handler)
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
37 ("PermMatrix: invalid permutation vector");
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
38 }
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
39
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
40 PermMatrix::PermMatrix (const Array<octave_idx_type>& p, bool colp, bool check)
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
41 : Array<octave_idx_type> (p), _colp(colp)
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
42 {
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
43 if (check)
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
44 {
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
45 if (! idx_vector (p).is_permutation (p.length ()))
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
46 {
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
47 gripe_invalid_permutation ();
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
48 Array<octave_idx_type>::operator = (Array<octave_idx_type> ());
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
49 }
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
50 }
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
51 }
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
52
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
53 PermMatrix::PermMatrix (const idx_vector& idx, bool colp, octave_idx_type n)
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
54 : Array<octave_idx_type> (), _colp(colp)
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
55 {
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
56 octave_idx_type len = idx.length (n);
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
57 if (! idx.is_permutation (len))
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
58 gripe_invalid_permutation ();
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
59 else
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
60 {
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
61 Array<octave_idx_type> idxa (len);
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
62 for (octave_idx_type i = 0; i < len; i++) idxa(i) = idx(i);
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
63 Array<octave_idx_type>::operator = (idxa);
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
64 }
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
65 }
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
66
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
67 PermMatrix::PermMatrix (octave_idx_type n)
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
68 : Array<octave_idx_type> (n), _colp (false)
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
69 {
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
70 for (octave_idx_type i = 0; i < n; i++) xelem (i) = i;
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
71 }
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
72
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
73 octave_idx_type
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
74 PermMatrix::checkelem (octave_idx_type i, octave_idx_type j) const
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
75 {
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
76 octave_idx_type len = Array<octave_idx_type>::length ();
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
77 if (i < 0 || j < 0 || i > len || j > len)
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
78 {
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
79 (*current_liboctave_error_handler) ("index out of range");
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
80 return 0;
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
81 }
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
82 else
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
83 return elem (i, j);
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
84 }
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
85
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
86
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
87 PermMatrix
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
88 PermMatrix::transpose (void) const
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
89 {
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
90 PermMatrix retval (*this);
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
91 retval._colp = ! retval._colp;
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
92 return retval;
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
93 }
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
94
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
95 PermMatrix
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
96 PermMatrix::inverse (void) const
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
97 {
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
98 return transpose ();
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
99 }
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
100
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
101 octave_idx_type
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
102 PermMatrix::determinant (void) const
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
103 {
9695
9fba7e1da785 correct algorithm for perm matrix det (sign)
Jaroslav Hajek <highegg@gmail.com>
parents: 8958
diff changeset
104 // Determine the sign of a permutation in linear time.
9fba7e1da785 correct algorithm for perm matrix det (sign)
Jaroslav Hajek <highegg@gmail.com>
parents: 8958
diff changeset
105 // Is this widely known?
9fba7e1da785 correct algorithm for perm matrix det (sign)
Jaroslav Hajek <highegg@gmail.com>
parents: 8958
diff changeset
106
9fba7e1da785 correct algorithm for perm matrix det (sign)
Jaroslav Hajek <highegg@gmail.com>
parents: 8958
diff changeset
107 octave_idx_type len = perm_length ();
9fba7e1da785 correct algorithm for perm matrix det (sign)
Jaroslav Hajek <highegg@gmail.com>
parents: 8958
diff changeset
108 const octave_idx_type *pa = data ();
9fba7e1da785 correct algorithm for perm matrix det (sign)
Jaroslav Hajek <highegg@gmail.com>
parents: 8958
diff changeset
109
9fba7e1da785 correct algorithm for perm matrix det (sign)
Jaroslav Hajek <highegg@gmail.com>
parents: 8958
diff changeset
110 OCTAVE_LOCAL_BUFFER (octave_idx_type, p, len);
9fba7e1da785 correct algorithm for perm matrix det (sign)
Jaroslav Hajek <highegg@gmail.com>
parents: 8958
diff changeset
111 OCTAVE_LOCAL_BUFFER (octave_idx_type, q, len);
9fba7e1da785 correct algorithm for perm matrix det (sign)
Jaroslav Hajek <highegg@gmail.com>
parents: 8958
diff changeset
112
8367
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
113 for (octave_idx_type i = 0; i < len; i++)
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
114 {
9695
9fba7e1da785 correct algorithm for perm matrix det (sign)
Jaroslav Hajek <highegg@gmail.com>
parents: 8958
diff changeset
115 p[i] = pa[i];
9fba7e1da785 correct algorithm for perm matrix det (sign)
Jaroslav Hajek <highegg@gmail.com>
parents: 8958
diff changeset
116 q[p[i]] = i;
9fba7e1da785 correct algorithm for perm matrix det (sign)
Jaroslav Hajek <highegg@gmail.com>
parents: 8958
diff changeset
117 }
9fba7e1da785 correct algorithm for perm matrix det (sign)
Jaroslav Hajek <highegg@gmail.com>
parents: 8958
diff changeset
118
9fba7e1da785 correct algorithm for perm matrix det (sign)
Jaroslav Hajek <highegg@gmail.com>
parents: 8958
diff changeset
119 bool neg = false;
9fba7e1da785 correct algorithm for perm matrix det (sign)
Jaroslav Hajek <highegg@gmail.com>
parents: 8958
diff changeset
120
9fba7e1da785 correct algorithm for perm matrix det (sign)
Jaroslav Hajek <highegg@gmail.com>
parents: 8958
diff changeset
121 for (octave_idx_type i = 0; i < len; i++)
9fba7e1da785 correct algorithm for perm matrix det (sign)
Jaroslav Hajek <highegg@gmail.com>
parents: 8958
diff changeset
122 {
9fba7e1da785 correct algorithm for perm matrix det (sign)
Jaroslav Hajek <highegg@gmail.com>
parents: 8958
diff changeset
123 octave_idx_type j = p[i], k = q[i];
8367
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
124 if (j != i)
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
125 {
9695
9fba7e1da785 correct algorithm for perm matrix det (sign)
Jaroslav Hajek <highegg@gmail.com>
parents: 8958
diff changeset
126 p[k] = p[i];
9fba7e1da785 correct algorithm for perm matrix det (sign)
Jaroslav Hajek <highegg@gmail.com>
parents: 8958
diff changeset
127 q[j] = q[i];
8367
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
128 neg = ! neg;
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
129 }
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
130 }
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
131
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
132 return neg ? -1 : 1;
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
133 }
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
134
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
135 PermMatrix
8958
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
136 PermMatrix::power (octave_idx_type m) const
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
137 {
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
138 octave_idx_type n = rows ();
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
139 bool res_colp = _colp;
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
140 if (m < 0)
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
141 {
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
142 res_colp = ! res_colp;
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
143 m = -m;
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
144 }
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
145 else if (m == 0)
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
146 return PermMatrix (n);
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
147
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
148 const octave_idx_type *p = data ();
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
149 Array<octave_idx_type> res_pvec (n, -1);
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
150 octave_idx_type *q = res_pvec.fortran_vec ();
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
151
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
152 for (octave_idx_type ics = 0; ics < n; ics++)
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
153 {
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
154 if (q[ics] > 0)
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
155 continue;
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
156
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
157 // go forward m steps or until a cycle is found.
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
158 octave_idx_type ic, j;
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
159 for (j = 1, ic = p[ics]; j != m && ic != ics; j++, ic = p[ic]) ;
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
160 if (ic == ics)
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
161 {
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
162 // reduce power.
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
163 octave_idx_type mm = m % j;
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
164 // go forward mm steps.
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
165 for (j = 0, ic = ics; j != mm; j++, ic = p[ic]) ;
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
166 }
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
167
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
168 // now ic = p^m[ics]. Loop through the whole cycle.
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
169 octave_idx_type jcs = ics;
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
170 do
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
171 {
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
172 q[jcs] = ic;
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
173 jcs = p[jcs]; ic = p[ic];
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
174 }
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
175 while (jcs != ics);
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
176
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
177 }
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
178
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
179 return PermMatrix (res_pvec, res_colp, false);
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
180 }
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
181
9708
6f3ffe11d926 implement luupdate
Jaroslav Hajek <highegg@gmail.com>
parents: 9695
diff changeset
182 PermMatrix
6f3ffe11d926 implement luupdate
Jaroslav Hajek <highegg@gmail.com>
parents: 9695
diff changeset
183 PermMatrix::eye (octave_idx_type n)
6f3ffe11d926 implement luupdate
Jaroslav Hajek <highegg@gmail.com>
parents: 9695
diff changeset
184 {
6f3ffe11d926 implement luupdate
Jaroslav Hajek <highegg@gmail.com>
parents: 9695
diff changeset
185 Array<octave_idx_type> p(n);
6f3ffe11d926 implement luupdate
Jaroslav Hajek <highegg@gmail.com>
parents: 9695
diff changeset
186 for (octave_idx_type i = 0; i < n; i++)
6f3ffe11d926 implement luupdate
Jaroslav Hajek <highegg@gmail.com>
parents: 9695
diff changeset
187 p(i) = i;
6f3ffe11d926 implement luupdate
Jaroslav Hajek <highegg@gmail.com>
parents: 9695
diff changeset
188
6f3ffe11d926 implement luupdate
Jaroslav Hajek <highegg@gmail.com>
parents: 9695
diff changeset
189 return PermMatrix (p, false, false);
6f3ffe11d926 implement luupdate
Jaroslav Hajek <highegg@gmail.com>
parents: 9695
diff changeset
190 }
6f3ffe11d926 implement luupdate
Jaroslav Hajek <highegg@gmail.com>
parents: 9695
diff changeset
191
8958
6ccc12cc65ef implement raising a permutation matrix to integer power
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
192 PermMatrix
8367
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
193 operator *(const PermMatrix& a, const PermMatrix& b)
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
194 {
8375
e3c9102431a9 fix design problems of diag & perm matrix classes
Jaroslav Hajek <highegg@gmail.com>
parents: 8367
diff changeset
195 const Array<octave_idx_type> ia = a.pvec (), ib = b.pvec ();
8367
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
196 PermMatrix r;
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
197 octave_idx_type n = a.columns ();
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
198 if (n != b.rows ())
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
199 gripe_nonconformant ("operator *", n, n, b.rows (), b.rows ());
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
200 else if (a._colp == b._colp)
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
201 {
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
202 r = PermMatrix ((a._colp
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
203 ? ia.index (idx_vector (ib))
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
204 : ib.index (idx_vector (ia))), a._colp, false);
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
205 }
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
206 else
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
207 {
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
208 Array<octave_idx_type> ra (n);
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
209 if (a._colp)
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
210 ra.assign (idx_vector (ib), ia);
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
211 else
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
212 ra.assign (idx_vector (ia), ib);
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
213 r = PermMatrix (ra, a._colp, false);
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
214 }
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
215
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
216 return r;
445d27d79f4e support permutation matrix objects
Jaroslav Hajek <highegg@gmail.com>
parents:
diff changeset
217 }