/usr/include/linbox/algorithms/matrix-rank.h is in liblinbox-dev 1.4.2-5build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | /* Copyright (C) 2003 LinBox
* Author: Zhendong Wan
*
*
*
* ========LICENCE========
* This file is part of the library LinBox.
*
* LinBox is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
* ========LICENCE========
*/
/*! @file algorithms/matrix-rank.h
* @ingroup algorithms
* @ingroup rank
* @brief Computes the rank of a matrix by Gaussian Elimination, in place.
* @details NO DOC
*/
#ifndef __LINBOX_matrix_rank_H
#define __LINBOX_matrix_rank_H
#include "linbox/util/debug.h"
#include "linbox/matrix/sparse-matrix.h"
#include "linbox/solutions/rank.h"
#include "linbox/algorithms/matrix-hom.h"
#include <vector>
#include <algorithm>
#include "linbox/randiter/random-prime.h"
namespace LinBox
{
/** Compute the rank of an integer matrix in place over a finite field by Gaussian elimination.
* @bug there is no generic \c rankIn method.
*/
template<class _Ring, class _Field, class _RandomPrime = RandomPrimeIterator>
class MatrixRank {
public:
typedef _Ring Ring; //!< Ring ?
typedef _Field Field; //!< Field ?
Ring r; //!< Ring ?
mutable _RandomPrime rp; //!< Holds the random prime for Monte-Carlo rank
/*! Constructor.
* @param _r ring (default is Ring)
* @param _rp random prime generator (default is template provided)
*/
MatrixRank(const Ring& _r = Ring(), const _RandomPrime& _rp = _RandomPrime() ) :
r(_r), rp (_rp)
{}
~MatrixRank() {}
/*!compute the integer matrix A by modulo a random prime, Monto-Carlo.
* This is the generic method (mapping to a random modular matrix).
* @param A Any matrix
* @return the rank of A.
*/
template<class IMatrix>
long rank(const IMatrix& A) const
{
rp.template setBitsField<Field>();
Field F ((unsigned long)*rp);
BlasMatrix<Field> Ap(F, A.rowdim(), A.coldim());
MatrixHom::map(Ap, A);
long result;
result = rankIn(Ap);
return result;
}
/*!Specialisation for BlasMatrix.
* Computation done by mapping to a random modular matrix.
* @param A Any dense matrix
* @return the rank of A.
* @bug we suppose we can map IRing to Field...
*/
template<class IRing>
long rank(const BlasMatrix<IRing>& A) const
{
rp.template setBitsField<_Field>();
Field F ((integer)*rp);
//! bug the following should work :
// BlasMatrix<Field> Ap(F,A);
BlasMatrix<Field> Ap(F, A.rowdim(), A.coldim());
MatrixHom::map(Ap, A);
long result;
result = rankIn(Ap);
return result;
}
/*! Specialisation for SparseMatrix
* Computation done by mapping to a random modular matrix.
* @param A Any sparse matrix
* @return the rank of A.
* @bug we suppose we can map IRing to Field...
*/
template <class Row>
long rank(const SparseMatrix<Ring, Row>& A) const
{
rp.template setBitsField<Field>();
Field F (*rp);
typename SparseMatrix<Ring, Row>::template rebind<Field>::other Ap(A, F);
long result;
result = rankIn (Ap);
return result;
}
/*! Specialisation for BlasMatrix (in place).
* Generic (slow) elimination code.
* @param A a dense matrix
* @return its rank
* @warning The matrix is on the Field !!!!!!!
*/
long rankIn(BlasMatrix<Field>& Ap) const
{
typedef typename Field::Element Element;
Field F = Ap.field();
typename BlasMatrix<Field>::RowIterator cur_r, tmp_r;
typename BlasMatrix<Field>::ColIterator cur_c, tmp_c;
typename BlasMatrix<Field>::Row::iterator cur_rp, tmp_rp;
typename BlasMatrix<Field>::Col::iterator tmp_cp;
Element tmp_e;
std::vector<Element> tmp_v(Ap.coldim());
int offset_r = 0;
int offset_c = 0;
int R = 0;
for(cur_r = Ap. rowBegin(), cur_c = Ap. colBegin(); (cur_r != Ap. rowEnd())&&(cur_c != Ap.colEnd());) {
//try to find the pivot.
tmp_r = cur_r;
tmp_cp = cur_c -> begin() + offset_c;
while ((tmp_cp != cur_c -> end()) && F.isZero(*tmp_cp)) {
++ tmp_cp;
++ tmp_r;
}
// if no pivit found
if (tmp_cp == cur_c -> end()) {
++ offset_r;
++ cur_c;
continue;
}
//if swicth two row if nessary. Each row in dense matrix is stored in contiguous space
if (tmp_r != cur_r) {
std::copy (tmp_r -> begin(), tmp_r -> end(), tmp_v.begin());
std::copy (cur_r -> begin(), cur_r -> end(), tmp_r -> begin());
std::copy (tmp_v.begin(), tmp_v.end(), cur_r -> begin());
}
// continue gauss elimination
for(tmp_r = cur_r + 1; tmp_r != Ap.rowEnd(); ++ tmp_r) {
//see if need to update the row
if (!F.isZero(*(tmp_r -> begin() + offset_r ))) {
F.div (tmp_e, *(tmp_r -> begin() + offset_r), *(cur_r -> begin() + offset_r));
F.negin(tmp_e);
for ( cur_rp = cur_r ->begin() + offset_r,tmp_rp = tmp_r -> begin() + offset_r;
tmp_rp != tmp_r -> end(); ++ tmp_rp, ++ cur_rp )
F.axpyin ( *tmp_rp, *cur_rp, tmp_e);
}
}
++ cur_r;
++ cur_c;
++ offset_r;
++ offset_c;
++ R;
}
return R;
}
/** Specialisation for SparseMatrix, in place.
* solution rank is called. (is Elimination guaranteed as the doc says above ?)
* @param A a sparse matrix
* @return its rank
*/
template<class Field, class Row>
long rankIn(SparseMatrix<Field, Row>& A) const
{
unsigned long result;
LinBox::rank(result, A, A.field());
return (long)result;
}
};
} // end namespace LinBox
#endif //__LINBOX_matrix_rank_H
// vim:sts=8:sw=8:ts=8:noet:sr:cino=>s,f0,{0,g0,(0,:0,t0,+0,=s
// Local Variables:
// mode: C++
// tab-width: 8
// indent-tabs-mode: nil
// c-basic-offset: 8
// End:
|