-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.cpp
More file actions
142 lines (110 loc) · 3.63 KB
/
matrix.cpp
File metadata and controls
142 lines (110 loc) · 3.63 KB
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
/*
* This file is part of the GreasePad distribution (https://github.com/FraunhoferIOSB/GreasePad).
* Copyright (c) 2022-2025 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 <https://www.gnu.org/licenses/>.
*/
#include "matrix.h"
#include <QDataStream>
#include <QtCompilerDetection>
#include <qassert.h>
#include <Eigen/Core>
#include <Eigen/SparseCore>
#include <vector>
namespace Graph {
using Eigen::Triplet;
using Eigen::Index;
using Eigen::VectorXidx;
bool IncidenceMatrix::isSet( const Index r, const Index c) const
{
Q_ASSERT_X( r>=0 && r<rows(), Q_FUNC_INFO, "row index out of range" );
Q_ASSERT_X( c>=0 && c<cols(), Q_FUNC_INFO, "column index out of range" );
Q_ASSERT( coeff(r,c)<2 );
return coeff(r,c)==1;
}
SparseMatrix<int> IncidenceMatrix::biadjacency() const {
// compile A = [O, B; B',O]
Index const C_ = cols();
Index const R_ = rows();
SparseMatrix<int> AA;
AA.resize(C_+R_,C_+R_);
std::vector<Triplet<int, Index> > tripletList;
for ( Index k = 0; k < outerSize(); ++k) {
for (SparseMatrix<int>::InnerIterator it(*this,k); it; ++it)
{
// tripletList.emplace_back( Triplet<int, Index>( it.row(), it.col() +R_, it.value() ));
// tripletList.emplace_back( Triplet<int, Index>( it.col() +R_, it.row(), it.value() ));
tripletList.emplace_back( it.row(), it.col() +R_, it.value() );
tripletList.emplace_back( it.col() +R_, it.row(), it.value() );
}
}
AA.setFromTriplets( tripletList.begin(), tripletList.end() );
return AA;
}
VectorXidx IncidenceMatrix::findInColumn( const Index c ) const
{
Eigen::Index const nnz = innerVector(c).nonZeros();
VectorXidx idx( nnz );
int i=0;
for ( SparseMatrix<int>::InnerIterator it(*this,c); it; ++it) {
idx(i++) = it.index();
}
return idx;
}
void IncidenceMatrix::remove_column( const Index c) {
// qDebug() << Q_FUNC_INFO;
Q_ASSERT( c>=0);
Q_ASSERT( c<cols() );
Index const C = cols() - 1;
SparseMatrix<int> TT(C+1,C);
Index c2 = 0;
for (c2=0; c2<c; c2++) {
TT.insert(c2,c2) = 1;
}
for (c2=c; c2<C; c2++) {
TT.insert(c2+1,c2) = 1;
}
*this = (*this)*TT;
}
void IncidenceMatrix::remove_row( const Index r)
{
// qDebug() << QString("remove row #%1").arg(r);
Q_ASSERT( r>=0 );
Q_ASSERT( r<rows() );
Index const R = rows() - 1;
SparseMatrix<int> SS(R,R+1);
Index r2 = 0;
for (r2=0; r2<r; r2++) {
SS.insert(r2,r2) = 1; // i.e., true
}
for (r2=r; r2<R; r2++) {
SS.insert(r2,r2+1) = 1;
}
*this = SS*(*this);
}
// remove i-th column and i-th row.....................................
void IncidenceMatrix::reduce( const Index i)
{
// qDebug() << QString("reduce %1").arg(i);
// selection matrix S
SparseMatrix<int> SS( rows()-1,rows());
for ( int c=0; c<i; c++) {
SS.insert(c,c) = 1;
}
for ( Index c=i; c<SS.rows(); c++) {
SS.insert(c,c+1) = 1;
}
*this = SS*(*this)*SS.transpose();
}
} // namespace Graph