/usr/include/linbox/matrix/sparsematrix/sparse-hyb-matrix.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 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 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 | /* linbox/matrix/sparse-csr-matrix.h
* Copyright (C) 2013 the LinBox
*
* Written by :
* Brice Boyer (briceboyer) <boyer.brice@gmail.com>
*
* ========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 matrix/sparsematrix/sparse-hyb-matrix.h
* @ingroup sparsematrix
*/
#ifndef __LINBOX_matrix_sparsematrix_sparse_hyb_matrix_H
#define __LINBOX_matrix_sparsematrix_sparse_hyb_matrix_H
#include <utility>
#include <iostream>
#include <algorithm>
#include "linbox/linbox-config.h"
#include "linbox/util/debug.h"
#include "sparse-domain.h"
#include "sparse-coo-matrix.h"
#include "sparse-csr-matrix.h"
#include "sparse-ellr-matrix.h"
/*! @todo benchmark me */
#define HYB_ELL_THRESHOLD 0.9
/*! @todo benchmark me */
#define HYB_ELL_COO_THRESHOLD 0.1
namespace LinBox
{
template<class Field>
class Stats {
const SparseMatrix<Field,SparseMatrixFormat::CSR> & _Mat;
const Field & _field ;
public:
size_t one ;
size_t mone ;
size_t avg ;
// size_t avg_one ;
// size_t avg_mone ;
std::vector<size_t> row ;
std::vector<size_t> row_one ;
std::vector<size_t> row_mone ;
std::vector<size_t> null_row ;
// size_t mean ;
size_t ell ;
size_t ell_nbnz ;
size_t ell_one ;
size_t ell_mone ;
public :
Stats( const SparseMatrix<Field,SparseMatrixFormat::CSR> & Mat) :
_Mat(Mat),_field(_Mat.field())
,one(0),mone(0)
,row(Mat.rowdim(),0)
,row_one(Mat.rowdim(),0)
,row_mone(Mat.rowdim(),0)
,null_row(0)
,avg(0)
,ell(0)
,ell_nbnz(0)
// ,_off_ell(Mat.rowdim(),0);
{
if (Mat.size() > 100) { /*! @todo what is small ? */
for (size_t i = 0 ; i < Mat.rowdim() ; ++i) {
if (Mat.getStart(i) == Mat.getEnd(i)) {
null_row.push_back(i);
}
for (size_t k = Mat.getStart(i) ; k < Mat.getEnd(i) ; ++k) {
if (_field.isOne(Mat.getEntry(k))) {
++one ;
row_one[i] += 1 ;
}
else if (_field.isMOne(Mat.getEntry(k))) {
++mone ;
row_mone[i] += 1 ;
}
row[i] += 1;
}
}
}
avg = std::ceil((Mat.rowdim()-null_row.size())/Mat.size());
}
void getOptimsedFormat(SparseMatrix<Field,SparseMatrixFormat::HYB> & hyb)
{
// std::sort(row.start(), row.end());
// std::vector<size_t>::iterator up;
// up= std::upper_bound (row.begin(), row.end(), 0); //
// linbox_check(null_row.size() == (size_t)(up-row.begin()));
size_t t1 = 0 , t2 = 0 , t3 = 0 ;
size_t e1 = avg-1 , e2 = avg , e3 = avg+1 ;
for (size_t i = 0 ; i < row.size() ; ++i) {
t1 += std::min(e1,row[i]);
t2 += std::min(e2,row[i]);
t3 += std::min(e3,row[i]);
}
double r1 = (double)t1/double(hyb.size()) ;
double r2 = (double)t2/double(hyb.size()) ;
double r3 = (double)t3/double(hyb.size()) ;
ell = 0 ;
if ( r1 > HYB_ELL_THRESHOLD) {
ell = e1 ;
ell_nbnz =t1;
}
if (r2 > std::min(r1, HYB_ELL_THRESHOLD)) { /*! @todo benchmark me */
ell = e2 ;
ell_nbnz =t2;
}
if (r3 > std::min( std::min(r2,r1), HYB_ELL_THRESHOLD) ) { /*! @todo benchmark me */
ell = e3 ;
ell_nbnz =t3;
}
size_t choose_ell = 0 ; // 0 is nothing, 1 is ell, 2 is ellr
size_t choose_coo = 0 ; // 0 is nothing, 1 is coo, 2 is csr
if (ell != 0) {
if (ell*hyb.rowdim() == hyb.size())
choose_ell = 1 ;
else
choose_ell = 2 ;
}
size_t rem = (hyb.size()-ell*hyb.rowdim()) ;
if (rem > 0) {
double r4 = rem/hyb.rowdim();
if (r4 < HYB_ELL_COO_THRESHOLD)
choose_coo = 1 ;
else
choose_coo = 2 ;
}
switch(choose_ell) {
case (1) :
// hyb.newELL();
case (2) :
hyb.newELL_R();
default :
{}
}
switch(choose_coo) {
case (1) :
hyb.newCOO();
case (2) :
hyb.newCSR();
default :
{}
}
//! @todo ±1 !
}
};
/** Sparse matrix, Coordinate storage.
*
* \ingroup matrix
* \ingroup sparse
*/
template<class _Field>
class SparseMatrix<_Field, SparseMatrixFormat::HYB > {
public :
typedef _Field Field ; //!< Field
typedef typename _Field::Element Element ; //!< Element
typedef const Element constElement ; //!< const Element
typedef SparseMatrixFormat::HYB Storage ; //!< Matrix Storage Format
typedef SparseMatrix<_Field,Storage> Self_t ; //!< Self type
// typedef Vector<_Field,VectorStorage::Sparse> Rep ;
/*! Constructors.
* @todo convert from other matrix.
*
*/
//@{
SparseMatrix<_Field, SparseMatrixFormat::HYB> () :
_rownb(0),_colnb(0) ,_nbnz(0)
,_coo(NULL),_csr(NULL),_ell_r(NULL)
, _field()
{
}
SparseMatrix<_Field, SparseMatrixFormat::HYB> (const _Field & F) :
_rownb(0),_colnb(0) ,_nbnz(0)
,_coo(NULL),_csr(NULL),_ell_r(NULL)
, _field(F)
{
}
SparseMatrix<_Field, SparseMatrixFormat::HYB> (const _Field & F, size_t m, size_t n) :
_rownb(m),_colnb(n) ,_nbnz(0)
,_coo(NULL),_csr(NULL),_ell_r(NULL)
, _field(F)
{
}
SparseMatrix<_Field, SparseMatrixFormat::HYB> (const SparseMatrix<_Field, SparseMatrixFormat::CSR> & S) :
_rownb(S._rownb),_colnb(S._colnb), _nbnz(S._nbnz)
,_coo(NULL),_csr(NULL),_ell_r(NULL)
,_field(S._field)
{
importe(S);
}
// XXX only for CSR
template<typename _Tp1, typename _Rw1 = SparseMatrixFormat::HYB>
struct rebind ;
template<typename _Tp1>
struct rebind<_Tp1 > {
typedef SparseMatrix<_Tp1, SparseMatrixFormat::HYB> other;
void operator() (other & Ap, const Self_t& A)
{
Ap.resize(A.rowdim(),A.coldim(),A.size());
// size might decrease...
if (A.have_reader()) {
Ap.newReader();
typename SparseMatrix<Field,SparseMatrixFormat::CSR>::template
rebind<_Tp1,SparseMatrixFormat::CSR>()(Ap.reader(),A.reader());
}
if (A.have_coo()) {
Ap.newCOO();
typename SparseMatrix<Field,SparseMatrixFormat::COO>::template
rebind<_Tp1,SparseMatrixFormat::COO>()(Ap.coo(),A.coo());
}
if (A.have_csr()) {
Ap.newCSR();
typename SparseMatrix<Field,SparseMatrixFormat::CSR>::template
rebind<_Tp1,SparseMatrixFormat::CSR>()(Ap.csr(),A.csr());
}
if (A.have_ell_r()) {
Ap.newELL_R();
typename SparseMatrix<Field,SparseMatrixFormat::ELL_R>::template
rebind<_Tp1,SparseMatrixFormat::ELL_R>()(Ap.ell_r(),A.ell_r());
}
}
};
template<typename _Tp1, typename _Rw1>
SparseMatrix (const SparseMatrix<_Tp1, _Rw1> &S, const Field& F) :
_rownb(S.rowdim()),_colnb(S.coldim()), _nbnz(S.size())
,_coo(NULL)
,_csr(NULL)
,_ell_r(NULL)
,_field(F)
{
typename SparseMatrix<_Tp1,_Rw1>::template
rebind<Field,SparseMatrixFormat::HYB>()(*this, S);
}
template<class VectStream>
SparseMatrix<_Field, SparseMatrixFormat::HYB> (const _Field & F, VectStream & stream) :
_rownb(stream.size()),_colnb(stream.dim()), _nbnz(0)
,_coo(NULL),_csr(NULL),_ell_r(NULL)
, _field(F)
{
_csr = (new SparseMatrix<Field,SparseMatrixFormat::CSR>(F,stream));
}
void resize(const size_t & mm, const size_t & nn, const size_t & zz)
{
_rownb = mm ;
_colnb = nn ;
_nbnz = zz;
}
/*! Default converter.
* @param S a sparse matrix in any storage.
*/
template<class _OtherStorage>
SparseMatrix<_Field, SparseMatrixFormat::HYB> (const SparseMatrix<_Field, _OtherStorage> & S) :
_rownb(S._rownb),_colnb(S._colnb),_nbnz(S._nbnz)
,_coo(NULL),_csr(NULL),_ell_r(NULL)
,_field(S._field)
{
this->importe(S); // convert Temp from anything
}
~SparseMatrix<_Field, SparseMatrixFormat::HYB>()
{
if(have_coo()) {
delete _coo ;
_coo = NULL ;
}
if(have_csr()) {
delete _csr;
_csr = NULL ;
}
if(have_reader()) {
delete _reader;
_reader = NULL ;
}
if(have_ell_r()) {
delete _ell_r;
_ell_r = NULL ;
}
}
//@}
/*! Conversions.
* Any sparse matrix has a converter to/from CSR.
* A specialisation can skip the temporary CSR matrix created.
*/
//@{
/*! Import a matrix in CSR format to HYB.
* @param S HYB matrix to be converted in HYB and optimsed
*/
void importe(const SparseMatrix<_Field,SparseMatrixFormat::CSR> &S)
{
optimise();
}
void optimise()
{
Stats<Field> stats(*this);
//! @todo ±1 !
if (have_ell_r()) {
ell_r().resize(rowdim(),coldim(),stats.ell_nbnz,stats.ell);
size_t l = 0 ;
for (size_t i = 0 ; i < rowdim() ;++i) {
size_t j = 0 ;
ell_r.setRowid(i,std::min(reader().rowLength(i),stats.ell));
for (size_t k = reader().getStart(i) ; k < reader().getStart(i)+ell_r.getRowid(i) ; ++k, ++j) {
ell_r().setColid(i,j,reader().getColid(k));
ell_r().setData (i,j,reader().getData (k));
}
for (size_t k = reader().getStart(i)+ell_r.getRowid(i) ; k < reader.getEnd(i) ; ++k , ++l) {
if (have_coo()) {
coo().setRowid(l,i);
coo().setColid(l,reader().getColid(k));
coo().setData (l,reader().getData (k));
}
else {
csr().setEnd(i,csr.getEnd(i)+1);
csr().setColid(l,reader().getColid(k));
csr().setData (l,reader().getData (k));
}
}
}
if (have_csr()) {
for (size_t i = 0 ; i < rowdim() ;++i) {
csr().setEnd(i,csr().getStart(i)+csr().getEnd(i));
}
csr().compress();
}
}
else if (have_csr()) {
csr().importe(reader());
}
else if (have_coo()) {
coo().importe(reader());
}
}
/*! Import a matrix in CSR format to CSR.
* @param S CSR matrix to be converted in CSR
*/
void importe(const SparseMatrix<_Field,SparseMatrixFormat::HYB> &A)
{
resize( A.rowdim(), A.coldim(), A.size() );
if (A.have_coo()) {
newCOO();
coo().importe(A.coo());
}
if (A.have_csr()) {
newCSR();
csr().importe(A.csr());
}
if (A.have_ell_r()) {
newELL_R();
ell_r().importe(A.ell_r());
}
}
/*! Export a matrix in CSR format from COO.
* @param S CSR matrix to be converted from COO
*/
SparseMatrix<_Field,SparseMatrixFormat::HYB > &
exporte(SparseMatrix<_Field,SparseMatrixFormat::HYB> &S) const
{
// could save memory by doing that iteratively and resize to 0,0,0
SparseMatrix<_Field,SparseMatrixFormat::CSR> S1(field());
SparseMatrix<_Field,SparseMatrixFormat::CSR> S2(field());
SparseMatrix<_Field,SparseMatrixFormat::CSR> S3(field());
if (have_coo()) {
S1.importe(coo());
}
if (have_csr()) {
S1.importe(csr());
}
if (have_ell_r()) {
S1.importe(ell_r());
}
S.merge(S1);
S.merge(S2);
S.merge(S3);
return S ;
}
//@}
/*! In place transpose. Not quite...
*/
void transposeIn()
{
if (have_coo()) {
coo().transposeIn();
}
if (have_csr()) {
csr().transposeIn();
}
if (have_ell_r()) {
ell_r().transposeIn();
}
if (have_reader()) {
reader().transposeIn();
}
}
/*! Transpose the matrix.
* @param S [out] transpose of self.
* @return a reference to \p S.
*/
SparseMatrix<_Field,SparseMatrixFormat::HYB> &
transpose(SparseMatrix<_Field,SparseMatrixFormat::HYB> &S)
{
if (have_coo()) {
S.newCoo();
coo().transpose(S.coo());
}
if (have_csr()) {
S.newCSR();
csr().transpose(S.csr());
}
if (have_ell_r()) {
S.newELLR();
ell_r().transpose(S.ell_r());
}
if (have_reader()) {
S.newCSR();
reader().transpose(S.reader());
}
return S;
}
/*! number of rows.
* @return row dimension.
*/
size_t rowdim() const
{
return _rownb ;
}
/*! number of columns.
* @return column dimension
*/
size_t coldim() const
{
return _colnb ;
}
/*! Number of non zero elements in the matrix.
* or at least the size of the _data if
* @return size of the _data.
*/
size_t size() const
{
// return _data.size();
return _nbnz ;
}
void setSize(const size_t & z)
{
_nbnz = z ;
}
/** Get a read-only individual entry from the matrix.
* @param i Row _colid
* @param j Column _colid
* @return Const reference to matrix entry
*/
constElement &getEntry(const size_t &i, const size_t &j) const
{
std::cout << " *** Warning/Error **** un optimised" << std::endl;
// get Entry in various opt, then reader
return reader().getEntry(i,j);
}
Element &getEntry (Element &x, size_t i, size_t j) const
{
std::cout << " *** Warning/Error **** un optimised" << std::endl;
return x = getEntry (i, j);
}
void finalize(){ optimise(); } // end construction after a sequence of setEntry calls.
/** Set an individual entry.
* Setting the entry to 0 will not remove it from the matrix
* @param i Row _colid of entry
* @param j Column _colid of entry
* @param value Value of the new entry
* @todo make it faster if i is 0 or m-1 ?
*/
void setEntry(const size_t &i, const size_t &j, const Element& e)
{
// only setentry on _reader, destroy other ?
// or add in coo/ wherever possible ? (seems doable)
std::cout << " *** Warning/Error **** un optimised" << std::endl;
return reader().setEntry(i,j,e);
}
/** Get a writeable reference to an entry in the matrix.
* If there is no entry at the position (i, j), then a new entry
* with a value of zero is inserted and a reference to it is
* returned.
* @param i Row _colid of entry
* @param j Column _colid of entry
* @return Reference to matrix entry
*/
Element &refEntry(const size_t &i, const size_t&j)
{
// dangerous if ref is 0, but doable.
std::cout << " *** Warning/Error **** un optimised" << std::endl;
return reader().refEntry(i,j);
}
/** Write a matrix to the given output stream using field read/write.
* @param os Output stream to which to write the matrix
* @param format Format with which to write
*/
template<class Format>
std::ostream & write(std::ostream &os,
Format = SparseFileFormat::CSR()) const
{
return this->writeSpecialized(os,Format());
}
std::ostream & write(std::ostream &os,
enum LINBOX_enum(Tag::FileFormat) ff = Tag::FileFormat::Maple) const
{
return this->writeSpecialized(os,ff);
}
/** Read a matrix from the given input stream using field read/write
* @param file Input stream from which to read the matrix
* @param format Format of input matrix
* @return ref to \p file.
*/
template<class Format>
std::istream& read (std::istream &file,
Format fmt = SparseFileFormat::CSR())
{
if ( ! have_reader() )
newReader();
return reader().read(file,fmt);
}
/*! @internal
* @brief Deletes the entry.
* Deletes \c A(i,j) if it exists.
* @param i row _colid
* @param j col _colid
*/
void clearEntry(const size_t &i, const size_t &j)
{
#if 0
linbox_check(i<_rownb);
linbox_check(j<_colnb);
size_t ibeg = _start[i];
size_t iend = _start[i+1];
if (ibeg == iend)
return ;
typedef typename std::vector<size_t>::iterator myIterator ;
myIterator beg = _colid.begin() ;
myIterator low = std::lower_bound (beg+(ptrdiff_t)ibeg, beg+(ptrdiff_t)iend, j);
if (low == beg+(ptrdiff_t)iend)
return ;
else {
// not sure
size_t la = low-beg ;
for (size_t k = i+1 ; k <= _rownb ; ++k) _start[k] -= 1 ;
_colid.erase(_colid.begin()+la);
_data. erase(_data. begin()+la);
return ;
}
#endif
}
/*! @internal
* @brief cleans 0 entries.
*/
void clean()
{
#if 0
size_t i = 0 ;
while(i < _data.size()) {
if ( field().isZero(_data[i]) ) {
for (size_t k = i+1 ; k <= _rownb ; ++k) _start[k] -= 1 ;
_colid.erase(_colid.begin()+i);
_data. erase(_data. begin()+i);
}
else
++i ;
}
return ;
#endif
}
// y= Ax
// y[i] = sum(A(i,j) x(j)
template<class Vector>
Vector& apply(Vector &y, const Vector& x, const Element & a ) const
{
linbox_check(consistent());
prepare(field(),y,a);
bool is_optimised = (have_ell_r() || have_coo() || have_csr());
if (is_optimised) {
if (have_ell_r())
ell_r().apply(y,x,field().one);
if (have_coo())
coo() .apply(y,x,field().one);
if (have_csr())
csr() .apply(y,x,field().one);
}
else {
reader(). apply(y,x,field().one);
}
return y;
}
class Helper ; // transpose
// y= A^t x
// y[i] = sum(A(j,i) x(j)
template<class Vector>
Vector& applyTranspose(Vector &y, const Vector& x, const Element & a ) const
{
linbox_check(consistent());
//! @bug if too big, create transpose.
prepare(field(),y,a);
bool is_optimised = (have_ell_r() || have_coo() || have_csr());
if (is_optimised) {
if (have_ell_r())
ell_r().applyTranspose(y,x,field().one);
if (have_coo())
coo() .applyTranspose(y,x,field().one);
if (have_csr())
csr() .applyTranspose(y,x,field().one);
}
else {
reader(). applyTranspose(y,x,field().one);
}
return y;
}
template<class Vector>
Vector& apply(Vector &y, const Vector& x ) const
{
return apply(y,x,field().zero);
}
template<class Vector>
Vector& applyTranspose(Vector &y, const Vector& x ) const
{
return apply(y,x,field().zero);
}
const Field & field() const
{
return _field ;
}
/** @todo Non element marker.
* We could end up a line with a marker.
* A field F would contain an element that does not belong to
* it. eg a nan for a Givaro::Modular<double>. It could act as a
* marker.
*/
bool consistent() const
{
size_t nbnz = 0 ;
size_t col = 0 ;
size_t row = 0 ;
if (have_coo()) {
if (!coo().consistent()) return false ;
nbnz += coo().size();
col = std::max(col,coo().coldim());
row = std::max(row,coo().rowdim());
}
if (have_csr()) {
if (!csr().consistent()) return false ;
nbnz += csr().size();
col = std::max(col,csr().coldim());
row = std::max(row,csr().rowdim());
}
if (have_ell_r()) {
if (!ell_r().consistent()) return false ;
nbnz += ell_r().size();
col = std::max(col,ell_r().coldim());
row = std::max(row,ell_r().rowdim());
}
if (nbnz != size())
return false ;
if (col > coldim())
return false;
if (row > rowdim())
return false;
return true ;
}
private :
std::ostream & writeSpecialized(std::ostream &os,
LINBOX_enum(Tag::FileFormat) format) const
{
if (have_reader()) {
return reader().write(os,format);
}
// SparseMatrix<Field,SparseMatrixFormat::CSR> Temp(field());
// this->exporte(Temp);
// Temp.write(os,format);
return os << "no export yet in HYB" << __FILE__ << ',' << __LINE__ ;
}
public:
void newCOO ()
{
linbox_check (!have_coo())
_coo= new SparseMatrix<Field,SparseMatrixFormat::COO>(_field);
}
void newCSR ()
{
linbox_check (!have_csr()) ;
_csr= new SparseMatrix<Field,SparseMatrixFormat::CSR>(_field);
}
void newELL_R ()
{
linbox_check (!have_ell_r()) ;
_ell_r= new SparseMatrix<Field,SparseMatrixFormat::ELL_R>(_field);
}
void newReader ()
{
linbox_check(!have_reader()) ;
_reader= new SparseMatrix<Field,SparseMatrixFormat::CSR>(_field);
}
bool have_coo() const
{
return _coo != NULL ;
}
bool have_csr() const
{
return _csr != NULL ;
}
bool have_ell_r() const
{
return _ell_r != NULL ;
}
bool have_reader() const
{
return _reader != NULL ;
}
SparseMatrix<Field,SparseMatrixFormat::COO> & coo(void)
{
linbox_check(have_coo());
return _coo[0] ;
}
const SparseMatrix<Field,SparseMatrixFormat::COO> & coo(void) const
{
linbox_check(have_coo());
return _coo[0] ;
}
SparseMatrix<Field,SparseMatrixFormat::CSR> & csr(void)
{
linbox_check(have_csr());
return _csr[0] ;
}
const SparseMatrix<Field,SparseMatrixFormat::CSR> & csr(void) const
{
linbox_check(have_csr());
return _csr[0] ;
}
SparseMatrix<Field,SparseMatrixFormat::ELL_R> & ell_r(void)
{
linbox_check(have_ell_r());
return _ell_r[0] ;
}
const SparseMatrix<Field,SparseMatrixFormat::ELL_R> & ell_r(void) const
{
linbox_check(have_ell_r());
return _ell_r[0] ;
}
SparseMatrix<Field,SparseMatrixFormat::CSR> & reader(void)
{
linbox_check(have_reader());
return _reader[0] ;
}
const SparseMatrix<Field,SparseMatrixFormat::CSR> & reader(void) const
{
linbox_check(have_reader());
return _reader[0] ;
}
protected :
size_t _rownb ;
size_t _colnb ;
size_t _nbnz ;
SparseMatrix<Field,SparseMatrixFormat::COO> * _coo ;
SparseMatrix<Field,SparseMatrixFormat::CSR> * _csr ;
SparseMatrix<Field,SparseMatrixFormat::ELL_R> * _ell_r ;
SparseMatrix<Field,SparseMatrixFormat::CSR> * _reader ;
const _Field & _field ;
};
} // namespace LinBox
#endif // __LINBOX_matrix_sparsematrix_sparse_hyb_matrix_H
// Local Variables:
// mode: C++
// tab-width: 8
// indent-tabs-mode: nil
// c-basic-offset: 8
// End:
// vim:sts=8:sw=8:ts=8:noet:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s
|