Skip to content

Commit 342fd71

Browse files
committed
symbolic indexing for last row/column
1 parent 0df5074 commit 342fd71

File tree

3 files changed

+56
-34
lines changed

3 files changed

+56
-34
lines changed

src/matrix.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ bool IncidenceMatrix::isSet( const Index r, const Index c) const
4545
}
4646

4747

48+
bool IncidenceMatrix::isSet( const Index r, const last_t /*unused*/) const
49+
{
50+
return coeff(r,cols()-1)==1;
51+
}
52+
53+
54+
bool IncidenceMatrix::isSet( const last_t /*unused*/, const Index c) const
55+
{
56+
return coeff(rows()-1,c)==1;
57+
}
58+
59+
60+
4861
//! Biadjacency matrix A = [O, B; B',O]
4962
SparseMatrix<int> IncidenceMatrix::biadjacency() const
5063
{

src/matrix.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ namespace Graph {
3030
using Eigen::ColMajor;
3131
using Eigen::SparseMatrix;
3232
using Eigen::VectorXi;
33+
using Eigen::placeholders::last_t;
3334

3435

3536
//! Sparse incidence matrix to encode relationships
@@ -45,10 +46,18 @@ class IncidenceMatrix : public SparseMatrix<int, ColMajor, int>
4546
}
4647

4748
[[nodiscard]] bool isSet( Index r, Index c) const; //!< Check if r and c are related
49+
[[nodiscard]] bool isSet( Index r, last_t last) const; //!< Check if r and c are related
50+
[[nodiscard]] bool isSet( last_t last, Index c) const; //!< Check if r and c are related
51+
4852
[[nodiscard]] SparseMatrix<int> biadjacency() const; //!< Create biadjacency matrix [O, A; A', O]
4953

50-
void set( const Index r, const Index c) { coeffRef(r,c) = 1; } //!< Set relation (row r, column c)
54+
void set( const Index r, const Index c) { coeffRef(r,c) = 1; } //!< Set relation (row r, column c)
55+
void set( const Index r, const last_t /*unused*/) { coeffRef(r,cols()-1) = 1; } //!< Set relation (row r, column c)
56+
void set( const last_t /*unused*/, const Index c) { coeffRef(rows()-1,c) = 1; } //!< Set relation (row r, column c)
57+
5158
void unset( const Index r, const Index c) { coeffRef(r,c) = 0; } //!< Delete relation (row r, column c)
59+
void unset( const Index r, const last_t /*unused*/) { coeffRef(r,cols()-1) = 0; } //!< Delete relation (row r, column c)
60+
void unset( const last_t /*unused*/, const Index c) { coeffRef(rows()-1,c) = 0; } //!< Delete relation (row r, column c)
5261

5362
void remove_row( Index r ); //!< Remove r-th row
5463
void remove_column( Index c ); //!< Remove c-th column

src/state.cpp

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ using Eigen::Matrix3d;
6666
using Eigen::Index;
6767
using Eigen::SparseMatrix;
6868
using Eigen::ColMajor;
69+
using Eigen::indexing::last;
6970

7071
using Constraint::ConstraintBase;
7172
using Constraint::Parallel;
@@ -459,28 +460,28 @@ void impl::find_adjacencies_of_latest_segment(const Quantiles::Snapping &snap)
459460
if ( m_segm.last()->touchedBy( m_segm.at(i)->ux(),
460461
snap.quantile_stdNormDistr(),
461462
snap.quantile_chi2_1dof()) ) {
462-
x_touches_l.set( i, N-1);
463+
x_touches_l.set(i,last);
463464
are_adjacent = true;
464465
}
465466

466467
if ( m_segm.last()->touchedBy( m_segm.at(i)->uy(),
467468
snap.quantile_stdNormDistr(),
468469
snap.quantile_chi2_1dof()) ) {
469-
y_touches_l.set(i,N-1);
470+
y_touches_l.set(i,last);
470471
are_adjacent = true;
471472
}
472473

473474
if ( m_segm.at(i)->touchedBy( m_segm.last()->ux() ,
474475
snap.quantile_stdNormDistr(),
475476
snap.quantile_chi2_1dof() ) ) {
476-
x_touches_l.set(N-1,i);
477+
x_touches_l.set(last,i);
477478
are_adjacent = true;
478479
}
479480

480481
if ( m_segm.at(i)->touchedBy( m_segm.last()->uy(),
481482
snap.quantile_stdNormDistr(),
482483
snap.quantile_chi2_1dof() ) ) {
483-
y_touches_l.set(N-1,i);
484+
y_touches_l.set(last,i);
484485
are_adjacent = true;
485486
}
486487

@@ -490,8 +491,8 @@ void impl::find_adjacencies_of_latest_segment(const Quantiles::Snapping &snap)
490491
}
491492

492493
if ( are_adjacent ) {
493-
Adj.set(i, N-1);
494-
Adj.set(N-1, i);
494+
Adj.set(i, last);
495+
Adj.set(last, i);
495496
}
496497
}
497498
}
@@ -912,10 +913,9 @@ void impl::establish_parallel( const Index a,
912913
m_qConstraint.append( QConstraint::QParallel::create() );
913914
m_constr.append( std::make_shared<Parallel>() );
914915

915-
Rel.conservativeResize( Rel.rows(),
916-
Rel.cols()+1); // append a column
917-
Rel.set( a, Rel.cols()-1 ); // B(a,end) = 1;
918-
Rel.set( b, Rel.cols()-1 ); // B(b,end) = 1;
916+
Rel.conservativeResize( Rel.rows(), Rel.cols()+1); // append a column
917+
Rel.set( a, last );
918+
Rel.set( b, last );
919919
}
920920

921921
void impl::establish_vertical( const Index a)
@@ -924,7 +924,7 @@ void impl::establish_vertical( const Index a)
924924
m_constr.append( std::make_shared<Vertical>() );
925925

926926
Rel.conservativeResize( Rel.rows(), Rel.cols()+1); // append a column
927-
Rel.set( a, Rel.cols()-1 ); // B(a,end) = 1;
927+
Rel.set( a, last );
928928
}
929929

930930
void impl::establish_horizontal( const Index a)
@@ -933,7 +933,7 @@ void impl::establish_horizontal( const Index a)
933933
m_constr.append( std::make_shared<Horizontal>() );
934934

935935
Rel.conservativeResize( Rel.rows(), Rel.cols()+1); // append a column
936-
Rel.set( a, Rel.cols()-1 ); // B(a,end) = 1;
936+
Rel.set( a, last );
937937
}
938938

939939
void impl::establish_diagonal( const Index a)
@@ -942,7 +942,7 @@ void impl::establish_diagonal( const Index a)
942942
m_constr.append( std::make_shared<Diagonal>() );
943943

944944
Rel.conservativeResize( Rel.rows(), Rel.cols()+1); // append a column
945-
Rel.set( a, Rel.cols()-1 ); // B(a,end) = 1;
945+
Rel.set( a, last );
946946
}
947947

948948
void impl::establish_orthogonal( const Index a,
@@ -953,8 +953,8 @@ void impl::establish_orthogonal( const Index a,
953953

954954
Rel.conservativeResize( Rel.rows(),
955955
Rel.cols()+1); // append a column
956-
Rel.set( a, Rel.cols()-1 ); // B(a,end) = 1;
957-
Rel.set( b, Rel.cols()-1 ); // B(b,end) = 1;
956+
Rel.set( a, last );
957+
Rel.set( b, last );
958958
}
959959

960960

@@ -966,9 +966,9 @@ void impl::establish_copunctual( const Index a,
966966
m_constr.append( std::make_shared<Copunctual>() );
967967

968968
Rel.conservativeResize( Rel.rows(), Rel.cols()+1) ; // append a column
969-
Rel.set( a, Rel.cols()-1 ); // B(a,end) = 1;
970-
Rel.set( b, Rel.cols()-1 ); // B(b,end) = 1;
971-
Rel.set( c, Rel.cols()-1 ); // B(c,end) = 1;
969+
Rel.set( a, last );
970+
Rel.set( b, last );
971+
Rel.set( c, last );
972972
}
973973

974974

@@ -1082,36 +1082,36 @@ void impl::merge_segment( const Index a)
10821082
{
10831083
// qDebug() << Q_FUNC_INFO << a;
10841084

1085-
const Index last = m_segm.size()-1; // zero-based
1085+
const Index last1 = m_segm.size()-1; // zero-based
10861086

10871087
const QPolygonF merged_track
10881088
= m_qStroke.at(a)->polygon()
10891089
+ m_qStroke.last()->polygon();
10901090

10911091

1092-
m_qStroke.replace( last, std::make_shared<QEntity::QStroke>( merged_track ) );
1092+
m_qStroke.replace( last1, std::make_shared<QEntity::QStroke>( merged_track ) );
10931093

10941094
const std::pair<VectorXd, VectorXd> xiyi = trackCoords(merged_track); // {x_i, y_i}
10951095
const std::pair<uPoint, uPoint> uxuy = uEndPoints(xiyi.first, xiyi.second); // ux, uy
10961096

10971097
auto um = std::make_shared<uStraightLineSegment>( uxuy.first, uxuy.second);
1098-
m_segm.replace( last, um );
1098+
m_segm.replace( last1, um );
10991099

1100-
m_qUnconstrained.replace( last,
1100+
m_qUnconstrained.replace( last1,
11011101
std::make_shared<QEntity::QUnconstrained>(
11021102
m_segm.last()->ux(),
11031103
m_segm.last()->uy() ));
11041104

1105-
m_qConstrained.replace( last,
1105+
m_qConstrained.replace( last1,
11061106
std::make_shared<QEntity::QConstrained>(
11071107
m_segm.last()->ux(),
11081108
m_segm.last()->uy() ));
11091109

11101110
// inherit adjacencies of [a]
11111111
for ( Index i=0; i<Adj.cols()-1; i++) {
11121112
if ( Adj.isSet(i,a) ) { // column "a" fix
1113-
Adj.set( i, Adj.cols()-1 );
1114-
Adj.set( Adj.rows()-1, i );
1113+
Adj.set( i, last );
1114+
Adj.set( last, i );
11151115
}
11161116
}
11171117

@@ -1123,17 +1123,17 @@ void impl::merge_segment( const Index a)
11231123
}
11241124

11251125
for ( Index ii=0; ii<x_touches_l.cols(); ii++ ) { // but *ColMajor*...
1126-
if ( x_touches_l.isSet( x_touches_l.rows()-1,ii) ) {
1127-
x_touches_l.unset( x_touches_l.rows()-1,ii ); // explicit zero !
1126+
if ( x_touches_l.isSet( last,ii) ) {
1127+
x_touches_l.unset( last,ii ); // explicit zero !
11281128
}
1129-
if ( y_touches_l.isSet( y_touches_l.rows()-1,ii) ) {
1130-
y_touches_l.unset( y_touches_l.rows()-1,ii );
1129+
if ( y_touches_l.isSet( last,ii) ) {
1130+
y_touches_l.unset( last,ii );
11311131
}
1132-
if ( x_touches_l.isSet( ii,x_touches_l.cols()-1) ) {
1133-
x_touches_l.unset( ii,x_touches_l.cols()-1 );
1132+
if ( x_touches_l.isSet( ii,last) ) {
1133+
x_touches_l.unset( ii,last );
11341134
}
1135-
if ( y_touches_l.isSet( ii,y_touches_l.cols()-1) ) {
1136-
y_touches_l.unset( ii,y_touches_l.cols()-1 );
1135+
if ( y_touches_l.isSet( ii,last) ) {
1136+
y_touches_l.unset( ii,last );
11371137
}
11381138
}
11391139

0 commit comments

Comments
 (0)