Skip to content

Commit

Permalink
Merge pull request #3069 from KratosMultiphysics/core/add-sum-to-matrix
Browse files Browse the repository at this point in the history
Adding sum to amatrix interface
  • Loading branch information
pooyan-dadvand authored Oct 18, 2018
2 parents b14db03 + 10bba93 commit 64e9010
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
14 changes: 14 additions & 0 deletions kratos/includes/amatrix_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,19 @@ template <typename TExpressionType, std::size_t TCategory>
return AMatrix::SubVector<TExpressionType>(TheExpression.expression(), From,To - From);
}


template <typename TExpressionType, std::size_t TCategory>
typename TExpressionType::data_type sum(
AMatrix::MatrixExpression<TExpressionType, TCategory> const& TheExpression) {
using data_type = typename TExpressionType::data_type;
auto& the_expression = TheExpression.expression();
data_type result = data_type();
for (std::size_t i = 0; i < the_expression.size(); ++i) {
result += the_expression[i];
}
return result;
}

template <typename TExpressionType, std::size_t TCategory>
typename TExpressionType::data_type norm_frobenius(
AMatrix::MatrixExpression<TExpressionType, TCategory> const& TheExpression) {
Expand All @@ -619,6 +632,7 @@ template <typename TExpressionType, std::size_t TCategory>
return std::sqrt(result);
}


template <typename TDataType>
class scalar_matrix
: public AMatrix::MatrixExpression<scalar_matrix<TDataType>, AMatrix::row_major_access> {
Expand Down
37 changes: 37 additions & 0 deletions kratos/tests/sources/test_amatrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// | / |
// ' / __| _` | __| _ \ __|
// . \ | ( | | ( |\__ `
// _|\_\_| \__,_|\__|\___/ ____/
// Multi-Physics
//
// License: BSD License
// Kratos default license: kratos/license.txt
//
// Main authors: Pooyan Dadvand
//
//

// System includes


// External includes

#ifdef KRATOS_USE_AMATRIX // This macro definition is for the migration period and to be removed afterward please do not use it

// Project includes
#include "testing/testing.h"
#include "includes/amatrix_interface.h"


namespace Kratos {
namespace Testing {

KRATOS_TEST_CASE_IN_SUITE(AMatrixSum, KratosCoreFastSuite)
{
DenseVector<double> a{ 1.0, 2.0, 3.1 };
KRATOS_CHECK_EQUAL(sum(a), 6.1);
}

}
} // namespace Kratos.
#endif // ifdef KRATOS_USE_AMATRIX

0 comments on commit 64e9010

Please sign in to comment.