File indexing completed on 2026-06-02 08:48:24
0001 #ifndef INTERVAL_H
0002 #define INTERVAL_H
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <ElementaryUtils/logger/CustomException.h>
0012 #include <stddef.h>
0013 #include <cmath>
0014 #include <vector>
0015
0016 namespace NumA {
0017
0018
0019
0020
0021
0022
0023 template<typename T>
0024 class Interval {
0025 public:
0026
0027
0028
0029
0030 enum StepMode {
0031 NORMAL = 0,
0032 LOG = 1
0033 };
0034
0035
0036
0037
0038
0039
0040
0041
0042 Interval(T lowerBound, T upperBound, T step = 1,
0043 Interval::StepMode stepMode = NORMAL) :
0044 m_lowerBound(lowerBound), m_upperBound(upperBound), m_step(step), m_stepMode(
0045 stepMode) {
0046 }
0047
0048
0049
0050
0051
0052 std::vector<T> computeSteps() const {
0053 std::vector<T> steps;
0054
0055 switch (m_stepMode) {
0056 case NORMAL: {
0057
0058 T i = m_lowerBound;
0059 while (i < m_upperBound) {
0060 steps.push_back(i);
0061 i += m_step;
0062 }
0063
0064 steps.push_back(m_upperBound);
0065
0066 break;
0067 }
0068 case LOG: {
0069
0070 break;
0071 }
0072 default:
0073 throw ElemUtils::CustomException("Interval", __func__,
0074 "Unknown step mode");
0075 break;
0076 }
0077
0078 return steps;
0079 }
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089 static std::vector<T> computeNodes(T lowerBound, T upperBound, size_t num =
0090 10, Interval::StepMode stepMode = NORMAL) {
0091 T node = lowerBound;
0092 T step;
0093 std::vector<T> nodes(num, node);
0094 switch (stepMode) {
0095 case NORMAL:
0096 step = (upperBound - lowerBound) / (num - 1);
0097 for (size_t i = 1; i < num; i++) {
0098 node += step;
0099 nodes[i] = node;
0100 }
0101 break;
0102 case LOG:
0103 step = pow((upperBound / lowerBound), 1 / (num - 1));
0104 for (size_t i = 1; i < num; i++) {
0105 node *= step;
0106 nodes[i] = node;
0107 }
0108 break;
0109 default:
0110 throw ElemUtils::CustomException("Interval", __func__,
0111 "Unknown step mode");
0112 break;
0113 }
0114 return nodes;
0115 }
0116
0117
0118
0119
0120
0121 T getLowerBound() const {
0122 return m_lowerBound;
0123 }
0124
0125
0126
0127
0128
0129 void setLowerBound(T lowerBound) {
0130 this->m_lowerBound = lowerBound;
0131 }
0132
0133
0134
0135
0136
0137 T getStep() const {
0138 return m_step;
0139 }
0140
0141
0142
0143
0144
0145 void setStep(T step) {
0146 this->m_step = step;
0147 }
0148
0149
0150
0151
0152
0153 T getUpperBound() const {
0154 return m_upperBound;
0155 }
0156
0157
0158
0159
0160
0161 void setUpperBound(T upperBound) {
0162 this->m_upperBound = upperBound;
0163 }
0164
0165 private:
0166 T m_lowerBound;
0167 T m_upperBound;
0168 T m_step;
0169
0170 Interval::StepMode m_stepMode;
0171 };
0172
0173 }
0174
0175 #endif