/* * This file is part of the GreasePad distribution (https://github.com/FraunhoferIOSB/GreasePad). * Copyright (c) 2022 Jochen Meidow, Fraunhofer IOSB * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "global.h" #include "matrix.h" #include namespace Graph { // using Eigen::IOFormat; // using Eigen::DontAlignCols; using Eigen::Triplet; using Eigen::Index; // const IOFormat IncidenceMatrix::fmt = IOFormat( 4, DontAlignCols, ", ", "\n", "| ", "|", "", ".\n"); void IncidenceMatrix::serialize( QDataStream &out ) const { // qDebug() << Q_FUNC_INFO out << uint( rows() ); out << uint( cols() ); out << uint( nonZeros() ); for( Index c=0; c::InnerIterator it( *this, c); for( ; it; ++it) { out << int(it.row()) << int(it.col()); } } } bool IncidenceMatrix::deserialize( QDataStream &in ) { // qDebug() << Q_FUNC_INFO; uint nrows; uint ncols; uint nnz; int r; int c; in >> nrows >> ncols >> nnz; if ( nnz> UINT_MAX ) { return false; } std::vector< Triplet > tripletList; tripletList.reserve( nnz ); for ( uint i=0; i> r >> c; tripletList.emplace_back( Triplet(r, c, 1) ); } if ( in.status() != 0 ) { return false; } resize( int(nrows), int(ncols) ); setFromTriplets( tripletList.begin(), tripletList.end() ); return true; } bool IncidenceMatrix::isSet( const Index r, const Index c) const { Q_ASSERT_X( r>=0 && r=0 && c IncidenceMatrix::biadjacency() const { // compile A = [O, B; B',O] Index C_ = cols(); Index R_ = rows(); SparseMatrix AA; AA.resize(C_+R_,C_+R_); std::vector > tripletList; for ( Index k = 0; k < outerSize(); ++k) { for (SparseMatrix::InnerIterator it(*this,k); it; ++it) { tripletList.emplace_back( Triplet( it.row(), it.col() +R_, it.value() )); tripletList.emplace_back( Triplet( it.col() +R_, it.row(), it.value() )); } } AA.setFromTriplets( tripletList.begin(), tripletList.end() ); return AA; } VectorXi IncidenceMatrix::findInColumn( const int c ) const { Eigen::Index nnz = innerVector(c).nonZeros(); VectorXi idx( nnz ); int i=0; for ( SparseMatrix::InnerIterator it(*this,c); it; ++it) { idx(i++) = it.index(); } return idx; } void IncidenceMatrix::remove_column( const int c) { // qDebug() << Q_FUNC_INFO; Q_ASSERT( c>=0); Q_ASSERT( c TT(C+1,C); Index c2; for (c2=0; c2=0 ); Q_ASSERT( r SS(R,R+1); Index r2; for (r2=0; r2 SS( rows()-1,rows()); for ( int c=0; c