-
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.31 KB
/
matrix.cpp
File metadata and controls
142 lines (110 loc) · 3.31 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-2026 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 <Eigen/Core>
#include <Eigen/SparseCore>
#include <cassert>
#include <vector>
namespace Graph {
using Eigen::Index;
//! check if coeff(r,c)==1
bool IncidenceMatrix::isSet( const Index r, const Index c) const
{
assert( coeff(r,c)<2 );
return coeff(r,c)==1;
}
bool IncidenceMatrix::isSet( const Index r, const last_t /*unused*/) const
{
return coeff(r,cols()-1)==1;
}
bool IncidenceMatrix::isSet( const last_t /*unused*/, const Index c) const
{
return coeff(rows()-1,c)==1;
}
//! Biadjacency matrix B = [O, A; A',O]
SparseMatrix<int> IncidenceMatrix::biadjacency() const
{
const Index C = cols();
const Index R = rows();
// vector of triplets (i,j,value)
std::vector<Eigen::Triplet<int, Index> > tripletList;
for ( Index k=0; k<outerSize(); ++k) {
for (SparseMatrix<int>::InnerIterator it(*this,k); it; ++it)
{
tripletList.emplace_back( it.row(), it.col()+R, it.value() );
tripletList.emplace_back( it.col()+R, it.row(), it.value() );
}
}
// create sparse matrix
SparseMatrix<int> AA;
AA.resize( C+R, C+R );
AA.setFromTriplets( tripletList.begin(), tripletList.end() );
return AA;
}
//! remove column c
void IncidenceMatrix::remove_column( const Index c)
{
assert( c>=0 && c<cols() );
const Index C = cols()-1;
SparseMatrix<int> RR(C+1,C);
for (Index i=0; i<c; i++) {
RR.insert(i,i) = 1;
}
for (Index i=c; i<C; i++) {
RR.insert(i+1,i) = 1;
}
*this = (*this)*RR;
}
//! remove row r
void IncidenceMatrix::remove_row( const Index r)
{
assert( r>=0 && r<rows() );
const Index R = rows()-1;
SparseMatrix<int> LL(R,R+1);
for (Index i=0; i<r; i++) {
LL.insert(i,i) = 1;
}
for (Index i=r; i<R; i++) {
LL.insert(i,i+1) = 1;
}
*this = LL*(*this);
}
//! remove r-th column and c-th row
void IncidenceMatrix::reduce( const Index r, const Index c)
{
assert( r>=0 && r<rows() );
assert( c>=0 && c<cols() );
// selection matrix LL (left, rows)
SparseMatrix<int> LL( rows()-1,rows());
for ( Index i=0; i<r; i++) {
LL.insert(i,i) = 1;
}
for ( Index i=r; i<LL.rows(); i++) {
LL.insert(i,i+1) = 1;
}
// selection matrix RR (right, cols)
SparseMatrix<int> RR( cols(),cols()-1);
for ( Index i=0; i<c; i++) {
RR.insert(i,i) = 1;
}
for ( Index i=c; i<RR.cols(); i++) {
RR.insert(i+1,i) = 1;
}
*this = LL*(*this)*RR;
}
} // namespace Graph