File: conditionMatrix.h

package info (click to toggle)
groops 0%2Bgit20250907%2Bds-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 11,140 kB
  • sloc: cpp: 135,607; fortran: 1,603; makefile: 20
file content (98 lines) | stat: -rw-r--r-- 2,659 bytes parent folder | download | duplicates (2)
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
/***********************************************/
/**
* @file conditionMatrix.h
*
* @brief Evaluate elements of a matrix based on an expression.
*
* @author Sebastian Strasser
* @date 2021-02-03
*
*/
/***********************************************/

#ifndef __GROOPS_CONDITIONMATRIX__
#define __GROOPS_CONDITIONMATRIX__

// Latex documentation
#ifdef DOCSTRING_Condition
static const char *docstringConditionMatrix = R"(
\subsection{Matrix}
Evaluate elements of a \configClass{matrix}{matrixGeneratorType} based on an expression.
If \config{all}=\verb|yes|, all elements of the matrix must evaluate to true
for the condition to be fulfilled, otherwise any element evaluating to true is sufficient.
)";
#endif

/***********************************************/

#include "base/import.h"
#include "classes/condition/condition.h"
#include "classes/matrixGenerator/matrixGenerator.h"

/***** CLASS ***********************************/

/** @brief Evaluate elements of a matrix based on an expression.
* @ingroup conditionGroup
* @see Condition */
class ConditionMatrix : public Condition
{
  ExpressionVariablePtr expr;
  MatrixGeneratorPtr matrixGenerator;
  Bool all;

public:
  ConditionMatrix(Config &config);

  Bool condition(const VariableList &varList) const;
};

/***********************************************/
/***** Inlines *********************************/
/***********************************************/

inline ConditionMatrix::ConditionMatrix(Config &config)
{
  try
  {
    readConfig(config, "matrix",     matrixGenerator, Config::MUSTSET, "",  "expression is evaluated for each element of resulting matrix");
    readConfig(config, "expression", expr,            Config::MUSTSET, "",  "(variable: data) evaluated for each element");
    readConfig(config, "all",        all,             Config::DEFAULT, "0", "all (=yes)/any (=no) elements must evaluate to true");
    if(isCreateSchema(config)) return;
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

inline Bool ConditionMatrix::condition(const VariableList &varList) const
{
  try
  {
    Matrix A = matrixGenerator->compute();

    VariableList varListMatrix = varList;
    for(UInt i=0; i<A.rows(); i++)
      for(UInt j=0; j<A.columns(); j++)
      {
        varListMatrix.setVariable("data", A(i,j));
        Bool result = (expr->evaluate(varListMatrix) != 0.);
        if(!all && result)
          return TRUE;
        else if(all && !result)
          return FALSE;
      }

    return all;
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

#endif