Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-06-02 08:48:24

0001 #ifndef INTERVAL_H
0002 #define INTERVAL_H
0003 
0004 /**
0005  * @file Interval.h
0006  * @author Bryan BERTHOU (SPhN / CEA Saclay)
0007  * @date July 23, 2014
0008  * @version 1.0
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  * @class Interval
0020  *
0021  * @brief Class defining an interval (with given bounds and step).
0022  */
0023 template<typename T>
0024 class Interval {
0025 public:
0026 
0027     /**
0028      * Type of interval.
0029      */
0030     enum StepMode {
0031         NORMAL = 0, ///< Linear interval.
0032         LOG = 1 ///< Logarithmic interval.
0033     };
0034 
0035     /**
0036      * Constructor.
0037      * @param lowerBound Lower bound of the interval.
0038      * @param upperBound Upper bound of the interval.
0039      * @param step Value of the step.
0040      * @param stepMode Type of interval.
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      * @return Nodes of interval.
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 //TODO implement
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      * Static function that computes the nodes of an interval for a chosen number of nodes.
0083      * @param lowerBound Lower bound of the interval.
0084      * @param upperBound Upper bound of the interval.
0085      * @param num Number of nodes.
0086      * @param stepMode Type of interval.
0087      * @return Nodes.
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      * @return Lower bound of the interval.
0120      */
0121     T getLowerBound() const {
0122         return m_lowerBound;
0123     }
0124 
0125     /**
0126      *
0127      * @param lowerBound Lower bound of the interval.
0128      */
0129     void setLowerBound(T lowerBound) {
0130         this->m_lowerBound = lowerBound;
0131     }
0132 
0133     /**
0134      *
0135      * @return Step.
0136      */
0137     T getStep() const {
0138         return m_step;
0139     }
0140 
0141     /**
0142      *
0143      * @param step Value of the step.
0144      */
0145     void setStep(T step) {
0146         this->m_step = step;
0147     }
0148 
0149     /**
0150      *
0151      * @return Upper bound of the interval.
0152      */
0153     T getUpperBound() const {
0154         return m_upperBound;
0155     }
0156 
0157     /**
0158      *
0159      * @param upperBound Upper bound of the interval.
0160      */
0161     void setUpperBound(T upperBound) {
0162         this->m_upperBound = upperBound;
0163     }
0164 
0165 private:
0166     T m_lowerBound; ///< Lower bound of the interval.
0167     T m_upperBound; ///< Upper bound of the interval.
0168     T m_step; ///< Step.
0169 
0170     Interval::StepMode m_stepMode; ///< Type of interval.
0171 };
0172 
0173 } /* namespace NumA */
0174 
0175 #endif /* INTERVAL_H */