/usr/include/linbox/matrix/random-matrix.inl is in liblinbox-dev 1.3.2-1.1build2.
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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 | /* Copyright (C) 2011 LinBox
* Written by Brice Boyer <brice.boyer@imag.fr>
*
*
*
* ========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========
*/
#ifndef __LINBOX_matrix_random_matrix_INL
#define __LINBOX_matrix_random_matrix_INL
/** @file matrix/random-matrix.inl
* @ingroup matrix
* @brief Implementation of random matrices.
*
*/
namespace LinBox
{
//! This is the namespace all LinBox internal code is in.
namespace Protected {
template<class Randiter,class Field>
BlasMatrix<Field> &
random_lu_rank(const Field & F,
const Randiter & R,
BlasMatrix<Field> & A,
int & rank,
const RingCategories::ModularTag & tag)
{
size_t m = A.rowdim() ;
size_t n = A.coldim() ;
linbox_check(m != 0);
linbox_check(n != 0);
if (rank == -1)
rank = (int) ( (double)std::min(m,n)*drand48() );
linbox_check(!(rank<0 || rank>(int)std::min(m,n)));
typedef typename Field::Element Element ;
// be ready for PLUQ
//size_t * P = new size_t [m] ;
BlasPermutation<size_t> P(m);
//Element * L = new Element[m*m] ;
//TriangularBlasMatrix<Element> L(m,m,LinBoxTag::Lower,LinBoxTag::Unit);
//! @todo !!!
BlasMatrix<Field> L(F,m,m);
// Element * U = new Element[m*n] ;
// TriangularBlasMatrix<Element> U(m,n,LinBoxTag::Upper,LinBoxTag::NonUnit);
//
BlasMatrix<Field> U(F,m,n);
//size_t * Q = new size_t [n] ;
BlasPermutation<size_t> Q(n);
// be ready for random elements
NonzeroRandIter<Field> Rnz(F,R);
Element one,zero;
F.init(one,1UL);
F.init(zero,0UL);
/* Create L a random invertible lower unit triangular matrix (m x m format) */
for (size_t j=0 ; j<m ; ++j)
for (size_t i=j+1; i<m;++i)
R.random( L.refEntry( i,j ) );
#if 1
for (size_t i = 0; i < (size_t) m; ++i)
Rnz.random( L.refEntry( i,i ) ); // non zero diagonal
for (size_t j=0 ; j<m ; ++j)
for (size_t i=0; i<j;++i)
L.setEntry( i,j,zero );
#endif
/* Create U a random rank-r upper non-unit triangular matrix (m x n format) */
for (size_t i = 0 ; i < (size_t) rank; ++i)
for (size_t j = i+1; j < n; ++j)
R.random( U.refEntry( i,j ) ); // r random 'triangular' lines
for (size_t i = 0; i < (size_t) rank; ++i)
Rnz.random( U.refEntry( i,i ) ); // non zero diagonal on rank first lines
#if 1
for (size_t i = rank ; i < m ; ++i)
for (size_t j = i ; j < n ; ++j)
U.setEntry( i,j,zero ) ; // zero on remaining 'triangular' lines
#endif
/**
* @todo RandomPermutation avec P de type [Matrix-Blas]Permutation
*/
RandomBlasPermutation(P);
RandomBlasPermutation(Q);
BlasMatrixDomain< Field > BMD(F) ;
// LU
/**
* @todo : L = [[L1,0],[A,L2]] ;U = [[U1,B],[0,U2]] ; LU = [[ rec(L1,U1), ftrmm(L1,B)],[ftrmm(A,U1),fgemm(A,B)+rec(L2,U2) ]]
* de même UL
*/
// BlasMatrix<Field> A_ptr(A) ;
BMD.mul(A,L,U);
/*!
* @todo create BMD.applyP(A,P,LinBoxTag::Left) ;
* avec P : BlasPermutation
* ou P : MatrixPermutation
* @todo BlasPermutation a un ordre \p p et une taille \p r distinctes !!!
*/
BMD.mulin_left(A,Q);
BMD.mulin_right(P,A);
return A ;
}
//!@todo ZZ is A.field() !
template<class Randiter, class Ring>
BlasMatrix<Ring> &
random_lu_rank(const Ring & ZZ,
const Randiter & R,
BlasMatrix<Ring> &A,
int & rank,
const RingCategories::IntegerTag & tag)
{
typedef typename Ring::Element Int ;
size_t m = A.rowdim() ;
size_t n = A.coldim() ;
linbox_check(m != 0);
linbox_check(n != 0);
if (rank == -1)
rank = (int) ( (double)std::min(m,n)*drand48() );
linbox_check(!(rank<0 || rank>(int)std::min(m,n)));
// be ready for PLUQ
BlasPermutation<size_t> P(m);
BlasMatrix<Ring> L(ZZ,m,m);
BlasMatrix<Ring> U(ZZ,m,n);
BlasPermutation<size_t> Q(n);
// be ready for random elements
Randiter S_(R);
S_.setBits(R.getBits()-1);
RandomIntegerIter<false> T_(3);
NonzeroRandIter<Ring,RandomIntegerIter<false> > U_(ZZ,T_);
Int one(1),zero(0);
/* Create L a random invertible lower unit triangular matrix (m x m format) */
for (size_t j=0 ; j<m ; ++j)
for (size_t i=j+1; i<m;++i)
S_.random( L.refEntry( i,j ) );
#if 1
for (size_t i = 0; i < (size_t) m; ++i)
U_.random( L.refEntry( i,i ) ); // non zero diagonal
for (size_t j=0 ; j<m ; ++j)
for (size_t i=0; i<j;++i)
L.setEntry( i,j,zero );
#endif
/* Create U a random rank-r upper non-unit triangular matrix (m x n format) */
for (size_t i = 0 ; i < (size_t) rank; ++i)
for (size_t j = i+1; j < n; ++j)
T_.random( U.refEntry( i,j ) ); // r random 'triangular' lines
for (size_t i = 0; i < (size_t) rank; ++i)
U_.random( U.refEntry( i,i ) ); // non zero diagonal on rank first lines
#if 1
for (size_t i = rank ; i < m ; ++i)
for (size_t j = i ; j < n ; ++j)
U.setEntry( i,j,zero ) ; // zero on remaining 'triangular' lines
#endif
RandomBlasPermutation(P);
RandomBlasPermutation(Q);
BlasMatrixDomain< Ring > BMD(ZZ) ;
MatrixDomain< Ring > MD(ZZ) ;
// LU
// L.write(std::cout << "L:=",true ) << ';' << std::endl;
// L.write(std::cout << "U:=",true ) << ';' << std::endl;
typedef typename Ring::Element Element;
// BlasMatrix<Element> A_ptr(A) ;
MD.mul(A,L,U);
// A.write(std::cout << "pre A=",true) << std::endl;
BMD.mulin_left(A,Q);
BMD.mulin_right(P,A);
// P.write(std::cout<<"P:=",false) << std::endl;
// P.write(std::cout<<"Q:=",false) << std::endl;
// P.write(std::cout<<"Q:=",true) << std::endl;
return A ;
}
#if 0
template<class Randiter,class Field>
BlasMatrix<typename Field::Element> &
random_lu_rank(const Field & F,
const Randiter & R,
BlasMatrix<typename Field::Element> & A,
int & rank,
const RingCategories::IntegerTag & tag)
{
BlasBlackbox<Field> Alink(F,A);
random_lu_rank(F,R,Alink,rank,RingCategories::IntegerTag());
return A ;
}
#endif
#if 0 /* BlasMatrix spec. */
template<class Randiter, class Field>
BlasBlackbox<Field> &
random_rankupdate( const Field & F
,const Randiter & R
,BlasBlackbox<Field>& A
, int & rank
)
{
size_t m = A.rowdim();
size_t n = A.coldim();
BlasBlackbox<Field> D(F,m,rank) ;
BlasBlackbox<Field> G(F,rank,n) ;
RandomBlasBlackbox<Randiter, Field> RandMatGen(F,R);
RandMatGen.random(D) ;
RandMatGen.random(G) ;
MatrixDomain<Field> MD(F);
MD.mul(A,D,G);
return A ;
}
template<class Randiter>
BlasBlackbox<PID_integer> &
random_rankupdate( PID_integer & F //!@bug const !
,const Randiter & R
,BlasBlackbox<PID_integer> & A
, int & rank
)
{
//! @todo check randomness
size_t m = A.rowdim();
size_t n = A.coldim();
BlasBlackbox<PID_integer> D(F,(size_t)m,(size_t)rank) ;
BlasBlackbox<PID_integer> G(F,(size_t)rank,(size_t)n) ;
Randiter S_(R);
S_.setBits(R.getBits()-1);
RandomBlasBlackbox<Randiter,PID_integer > RandMatGen(F,S_);
RandMatGen.random(D) ;
RandomIntegerIter<false> T_(3);
RandomBlasBlackbox<RandomIntegerIter<false>,PID_integer > RandSmallMatGen(F,T_);
RandMatGen.random(G) ;
MatrixDomain<PID_integer> MD(F);
MD.mul(A,D,G);
return A ;
}
#endif
template<class Randiter,class Field>
BlasMatrix<Field> &
random_rankupdate( Field & F //!@bug const !
,const Randiter & R
,BlasMatrix<Field> & A
, int & rank
, const RingCategories::IntegerTag &tag
)
{
typedef typename Field::Element Int ;
size_t m = A.rowdim();
size_t n = A.coldim();
BlasMatrix<Field> D(F,(size_t)m,(size_t)rank) ;
BlasMatrix<Field> G(F,(size_t)rank,(size_t)n) ;
Randiter S_(R);
S_.setBits(R.getBits()-1);
RandomDenseMatrix<Randiter,Field > RandMatGen(F,S_);
RandMatGen.random(D) ;
RandomIntegerIter<false> T_(3);
RandomDenseMatrix<RandomIntegerIter<false>,Field > RandSmallMatGen(F,T_);
RandMatGen.random(G) ;
MatrixDomain<Field> MD(F);
MD.mul(A,D,G);
/// @bug do perms ?
#if 0 /* necessary */
BlasPermutation<size_t> P(m);
BlasPermutation<size_t> Q(n);
RandomBlasPermutation(P);
RandomBlasPermutation(Q);
PID_integer ZZ ;
BlasMatrixDomain< PID_integer > BMD(ZZ) ;
BMD.mulin_left (A,Q);
BMD.mulin_right(P,A);
#endif
return A ;
}
} // Protected
//////// Random Matrix
template<class Randiter, class Field>
template<class Matrix>
Matrix &
RandomDenseMatrix<Randiter, Field>::random( Matrix & A)
{
for (size_t i = 0 ; i < A.rowdim() ; ++i)
for (size_t j = 0 ; j < A.coldim() ; ++j)
R_.random(A.refEntry(i,j));
return A;
}
//////// Random Matrix with prescribed Rank
#if 0
template<class Randiter, class Field>
template<class Matrix,class Method>
Matrix &
RandomDenseMatrix<Randiter, Field>::randomRank(Matrix & A,
int rank
, const Method & meth )
{
throw NotImplementedYet(__func__,__FILE__,__LINE__);
}
#endif
template<class Randiter, class Field>
template<class Matrix>
Matrix &
RandomDenseMatrix<Randiter, Field>::randomRank(
Matrix & A
, int rank
)
{
//! @todo use CatergoryTag
return randomRank(A,rank,RankBuilder::LU_());
}
template<class Randiter, class Field>
template< class Matrix>
Matrix &
RandomDenseMatrix<Randiter, Field>::randomRank(
Matrix & A
, int rank
, const RankBuilder::LU_ & meth
)
{
Protected::random_lu_rank( F_,R_,A,rank,
typename FieldTraits<Field>::categoryTag());
return A ;
}
template<class Randiter, class Field>
template<class Matrix>
Matrix &
RandomDenseMatrix<Randiter, Field>::randomRank(
Matrix & A
, int rank
, const RankBuilder::Rank_update_ & meth
)
{
return Protected::random_rankupdate(F_,R_,A,rank,
typename FieldTraits<Field>::categoryTag());
}
/* dense matrix with random entries */
void RandomBlasPermutation(BlasPermutation<size_t> & P)
{
size_t * Pt = P.getWritePointer();
// size_t n = P.getSize();
size_t r = P.getOrder();
size_t n = r ; // no size given ?
for (size_t i = 0 ; i < r ; ++i) {
Pt[i] = i + size_t(double (n-i)*( drand48() ) ) ;
}
return ;
}
} // LinBox
#endif // __LINBOX_matrix_random_matrix_INL
// 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:
|