Skip to content

Commit 7efa2e5

Browse files
committed
Added: Option to return the sum of a specified matrix column
1 parent 92c32b5 commit 7efa2e5

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

Diff for: src/ASM/LR/ASMu2D.C

+2-2
Original file line numberDiff line numberDiff line change
@@ -503,12 +503,12 @@ bool ASMu2D::evaluateBasis (int iel, FiniteElement& fe, int derivs) const
503503
dNdu.fillColumn(2,C*Bv);
504504

505505
#ifdef SP_DEBUG
506-
if (fabs(dNdu.getColumn(1).sum()) > 1.0e-10) {
506+
if (fabs(dNdu.sum(-1)) > 1.0e-10) {
507507
std::cerr <<"dNdu do not sum to zero at integration point #"
508508
<< fe.iGP << std::endl;
509509
return false;
510510
}
511-
else if (fabs(dNdu.getColumn(2).sum()) > 1.0e-10) {
511+
else if (fabs(dNdu.sum(-2)) > 1.0e-10) {
512512
std::cerr <<"dNdv do not sums to zero at integration point #"
513513
<< fe.iGP << std::endl;
514514
return false;

Diff for: src/ASM/LR/ASMu2Dnurbs.C

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ bool ASMu2D::evaluateBasisNurbs (int iel, FiniteElement& fe,
5757
if (fabs(fe.N.sum()-1.0) > 1.0e-10)
5858
std::cerr <<"fe.N do not sum to one at integration point #"
5959
<< fe.iGP << std::endl;
60-
else if (fabs(static_cast<const Vector&>(B).sum()-1.0) > 1.0e-10)
60+
else if (fabs(B.sum()-1.0) > 1.0e-10)
6161
std::cerr <<"Bezier basis do not sum to one at integration point #"
6262
<< fe.iGP << std::endl;
6363
else
@@ -107,10 +107,10 @@ bool ASMu2D::evaluateBasisNurbs (int iel, FiniteElement& fe,
107107
else if (fabs(B.sum()-1.0) > 1.0e-10)
108108
std::cerr <<"Bezier basis do not sum to one at integration point #"
109109
<< fe.iGP << std::endl;
110-
else if (fabs(dNdu.getColumn(1).sum()) > 1.0e-10)
110+
else if (fabs(dNdu.sum(-1)) > 1.0e-10)
111111
std::cerr <<"dNdu not sums to zero at integration point #"
112112
<< fe.iGP << std::endl;
113-
else if (fabs(dNdu.getColumn(2).sum()) > 1.0e-10)
113+
else if (fabs(dNdu.sum(-2)) > 1.0e-10)
114114
std::cerr <<"dNdv not sums to zero at integration point #"
115115
<< fe.iGP << std::endl;
116116
else if (fabs(Bu.sum()) > 1.0e-10 || fabs(Bv.sum()) > 1.0e-10)

Diff for: src/LinAlg/Test/TestMatrix.C

+11
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,17 @@ TEST(TestMatrix, AugmentCols)
122122
}
123123

124124

125+
TEST(TestMatrix, SumCols)
126+
{
127+
utl::matrix<int> a(5,3);
128+
std::iota(a.begin(),a.end(),1);
129+
std::cout <<"A:"<< a;
130+
EXPECT_EQ(a.sum(-1), 15);
131+
EXPECT_EQ(a.sum(-2), 40);
132+
EXPECT_EQ(a.sum(-3), 65);
133+
}
134+
135+
125136
TEST(TestMatrix, Multiply)
126137
{
127138
utl::vector<double> u(14), v(9), x, y;

Diff for: src/LinAlg/matrix.h

+15-2
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,21 @@ namespace utl //! General utility classes and functions.
414414
//! \param[in] inc Increment in the matrix element vector indices
415415
T asum(int inc = 1) const { return elem.asum(0,inc); }
416416
//! \brief Return the sum of the matrix elements.
417-
//! \param[in] inc Increment in the matrix element vector indices
418-
T sum(int inc = 1) const { return elem.sum(0,inc); }
417+
//! \param[in] inc Increment in the matrix element vector indices.
418+
//! If negative, the sum of matrix column \a -inc will be returned instead.
419+
T sum(int inc = 1) const
420+
{
421+
if (inc > 0)
422+
return elem.sum(0,inc);
423+
else if (inc == 0 || -inc > static_cast<int>(n[1]))
424+
return T(0);
425+
426+
T colsum = T(0);
427+
size_t ofs = n[0]*(-inc-1);
428+
for (size_t i = 0; i < n[0]; i++, ofs++)
429+
colsum += elem[ofs];
430+
return colsum;
431+
}
419432

420433
protected:
421434
size_t n[4]; //!< Dimension of the matrix

0 commit comments

Comments
 (0)