Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-26 09:18:58

0001 #ifndef PROF_PARAMPOINTS_H
0002 #define PROF_PARAMPOINTS_H
0003 
0004 #include <string>
0005 #include <iostream>
0006 #include <sstream>
0007 #include <vector>
0008 #include <cassert>
0009 #include <stdexcept>
0010 
0011 namespace Professor {
0012 
0013 
0014   /// Throwable error
0015   struct ParamPointsError : public std::runtime_error {
0016     ParamPointsError(const std::string& reason) : std::runtime_error(reason) { }
0017   };
0018 
0019 
0020   /// Typedef for a list of parameters, defining a parameter point
0021   typedef std::vector<double> ParamPoint;
0022 
0023 
0024   /// @todo I think we need to rename the more structured object
0025   // typedef const std::vector< std::vector<double> > ParamPointVec;
0026 
0027 
0028   /// Class for the parametrisation hypercube, i.e. anchors
0029   class ParamPoints {
0030   public:
0031 
0032     /// @todo Also record the parameter names in this object
0033 
0034     /// Constructor, takes anchors as vector<vector<double>> = vector<ParamPoint>
0035     /// @todo Also take the parameter names as an arg
0036     ParamPoints(const std::vector< std::vector<double> >& ppoints);
0037 
0038     ~ParamPoints() {
0039       _parampoints.clear();
0040     }
0041 
0042     /// Implicit conversion operator to vector<vector<double>> = vector<ParamPoint>
0043     operator std::vector< std::vector<double> > () const {
0044       return _parampoints;
0045     }
0046 
0047 
0048     /// Overly complicated push_back
0049     void addParamPoint(const std::vector<double>& p) {
0050       if (_locked) {
0051         /// @todo Throw a ParamPointsError or similar rather than calling abort()
0052         std::cerr << "Adding point to locked collection not implemented, aborting" << std::endl;
0053         abort();
0054       }
0055       // This ensures that all ppoints are of the same dimension
0056       if (_parampoints.size() > 0)
0057         assert(p.size() == _parampoints[0].size()); ///< @todo Throw an exception instead of assert
0058       _parampoints.push_back(p);
0059     }
0060 
0061     /// Number of anchor points
0062     int numPoints() const { return _parampoints.size(); }
0063 
0064     /// Dimension of (anchor) points
0065     int dim() const {
0066       assert(!_parampoints.empty()); //< Emptiness should not be possible
0067       return _parampoints.front().size();
0068     }
0069 
0070     /// Centre of the anchor hyper cube
0071     std::vector<double> ptcenters() const;
0072 
0073     /// Lowest edge of anchor hyper cube
0074     std::vector<double> ptmins() const;
0075 
0076     /// Top edge of anchor hyper cube
0077     std::vector<double> ptmaxs() const;
0078 
0079     /// Edges of the anchor hyper cube
0080     std::vector< std::pair<double, double> > ptedges() const;
0081 
0082     /// print message: anchors
0083     /// @todo These non-redirectable print functions are a bad idea
0084     void printPoints() const;
0085 
0086     /// print message: meta info
0087     /// @todo These non-redirectable print functions are a bad idea
0088     void printMeta() const;
0089 
0090     /// @todo Add toString method(s) for the main logic in the print functions above, and to connect to Python's ParamPoints.__str__ function.
0091 
0092     /// Remove all anchors
0093     void reset() { _parampoints.clear(); _locked = false; };
0094 
0095     /// Header representation with metadata necesary to write out ProfDF
0096     std::string toString(const std::string& info="") const {
0097       std::stringstream ss;
0098       if (!info.empty()) ss << "# INFO " << info << "\n";
0099       ss << "# MINV ";
0100       for (const double& a : ptmins()) ss << a<< " ";
0101       ss << " \n";
0102       ss << "# MAXV ";
0103       for (const double& a : ptmaxs()) ss << a<< " ";
0104       ss << " \n";
0105       // Slightly redundant but nice to have as consistency check
0106       ss << "# DIM ";
0107       ss << dim();
0108       ss << "\n";
0109       // Write out parameter names if defined
0110       if (names().size()>0) {
0111         ss << "# PARAMS ";
0112         for (const std::string& s : names()) ss << s<< " ";
0113         ss << "\n";
0114       }
0115 
0116       return ss.str();
0117     }
0118 
0119     /// Get all anchor points
0120     const std::vector< std::vector<double> >& points() const { return _parampoints; }
0121 
0122     /// Get parameter names if set
0123     const std::vector< std::string >& names() const { return _names; }
0124 
0125     void setNames(std::vector<std::string >);
0126 
0127     /// Get one anchor point
0128     /// @todo Generalise to other sorts of key lookup? Needed? Bounds checking / checking key existence
0129     const std::vector<double>& point(size_t i) const { return points().at(i); }
0130 
0131 
0132   private:
0133 
0134     std::vector< std::vector<double> > _parampoints;
0135 
0136     std::vector< std::string > _names;
0137 
0138     bool _locked;
0139 
0140   };
0141 
0142 
0143 }
0144 
0145 #endif