mirror of
				https://github.com/aportelli/LatAnalyze.git
				synced 2025-10-31 23:04:31 +00:00 
			
		
		
		
	FitInterface (new): covariance matrix interface & layout
This commit is contained in:
		| @@ -21,14 +21,15 @@ int main(void) | |||||||
|     f.registerDataPoint(f.dataIndex(1,1,1), 1); |     f.registerDataPoint(f.dataIndex(1,1,1), 1); | ||||||
|     f.registerDataPoint(f.dataIndex(2,2,3), 1); |     f.registerDataPoint(f.dataIndex(2,2,3), 1); | ||||||
|     f.fitPoint(false, f.dataIndex(1,1,1), 1); |     f.fitPoint(false, f.dataIndex(1,1,1), 1); | ||||||
|  |     f.assumeXXCorrelated(true, 0, 0, 0, 1); | ||||||
|  |     f.assumeXXCorrelated(true, 1, 1, 0, 1); | ||||||
|  |     f.assumeXXCorrelated(true, 2, 2, 0, 1); | ||||||
|  |     f.assumeYYCorrelated(true, 0, 0, f.dataIndex(0,0,0), f.dataIndex(1,1,1)); | ||||||
|  |     f.assumeYYCorrelated(true, 1, 1, f.dataIndex(0,0,0), f.dataIndex(2,2,3)); | ||||||
|  |     f.assumeXYCorrelated(true, 0, 0, 0, f.dataIndex(1,1,1)); | ||||||
|     cout << f << endl; |     cout << f << endl; | ||||||
|     DEBUG_VAR(f.getYFitSize()); |     f.updateLayout(); | ||||||
|     DEBUG_VAR(f.getYFitSize(0)); |     DEBUG_MAT(f.makeCorrFilter()); | ||||||
|     DEBUG_VAR(f.getYFitSize(1)); |  | ||||||
|     DEBUG_VAR(f.getXFitSize()); |  | ||||||
|     DEBUG_VAR(f.getXFitSize(0)); |  | ||||||
|     DEBUG_VAR(f.getXFitSize(1)); |  | ||||||
|     DEBUG_VAR(f.getXFitSize(2)); |  | ||||||
|      |      | ||||||
|     return EXIT_SUCCESS; |     return EXIT_SUCCESS; | ||||||
| } | } | ||||||
|   | |||||||
| @@ -75,7 +75,8 @@ FitInterface::FitInterface(void) | |||||||
| {} | {} | ||||||
|  |  | ||||||
| // add dimensions ////////////////////////////////////////////////////////////// | // add dimensions ////////////////////////////////////////////////////////////// | ||||||
| void FitInterface::addXDim(const string name, const Index nData) | void FitInterface::addXDim(const string name, const Index nData, | ||||||
|  |                            const bool isExact) | ||||||
| { | { | ||||||
|     if (getYSize() != 0) |     if (getYSize() != 0) | ||||||
|     { |     { | ||||||
| @@ -86,6 +87,7 @@ void FitInterface::addXDim(const string name, const Index nData) | |||||||
|     { |     { | ||||||
|         xDimName_.push_back(name); |         xDimName_.push_back(name); | ||||||
|         xSize_.push_back(nData); |         xSize_.push_back(nData); | ||||||
|  |         xIsExact_.push_back(isExact); | ||||||
|         xDimIndex_[name]  = xDimName_.size(); |         xDimIndex_[name]  = xDimName_.size(); | ||||||
|         maxDataIndex_    *= nData; |         maxDataIndex_    *= nData; | ||||||
|         updateDataSize(); |         updateDataSize(); | ||||||
| @@ -248,6 +250,70 @@ void FitInterface::fitPoint(const bool isFitPoint, const Index k, const Index j) | |||||||
|     yDataIndex_[j][k] = isFitPoint; |     yDataIndex_[j][k] = isFitPoint; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // variance interface ////////////////////////////////////////////////////////// | ||||||
|  | void FitInterface::assumeXExact(const bool isExact, const Index i) | ||||||
|  | { | ||||||
|  |     checkXDim(i); | ||||||
|  |     xIsExact_[i] = isExact; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void FitInterface::addCorr(set<array<Index, 4>> &s, const bool isCorr, | ||||||
|  |                            const array<Index, 4> &c) | ||||||
|  | { | ||||||
|  |     if (isCorr) | ||||||
|  |     { | ||||||
|  |         s.insert(c); | ||||||
|  |     } | ||||||
|  |     else | ||||||
|  |     { | ||||||
|  |         auto it = s.find(c); | ||||||
|  |          | ||||||
|  |         if (it != s.end()) | ||||||
|  |         { | ||||||
|  |             s.erase(it); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void FitInterface::assumeXXCorrelated(const bool isCorr, const Index i1, | ||||||
|  |                                       const Index i2, const Index vi1, | ||||||
|  |                                       const Index vi2) | ||||||
|  | { | ||||||
|  |     array<Index, 4> c{{i1, i2, vi1, vi2}}; | ||||||
|  |      | ||||||
|  |     checkXIndex(vi1, i1); | ||||||
|  |     checkXIndex(vi2, i2); | ||||||
|  |     if ((i1 != i2) or (vi1 != vi2)) | ||||||
|  |     { | ||||||
|  |         addCorr(xxCorr_, isCorr, c); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void FitInterface::assumeYYCorrelated(const bool isCorr, const Index j1, | ||||||
|  |                                       const Index j2, const Index k1, | ||||||
|  |                                       const Index k2) | ||||||
|  | { | ||||||
|  |     array<Index, 4> c{{j1, j2, k1, k2}}; | ||||||
|  |      | ||||||
|  |     checkPoint(k1, j1); | ||||||
|  |     checkPoint(k2, j2); | ||||||
|  |     if ((j1 != j2) or (k1 != k2)) | ||||||
|  |     { | ||||||
|  |         addCorr(yyCorr_, isCorr, c); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void FitInterface::assumeXYCorrelated(const bool isCorr, const Index i, | ||||||
|  |                                       const Index j, const Index vi, | ||||||
|  |                                       const Index k) | ||||||
|  | { | ||||||
|  |     array<Index, 4> c{{i, j, vi, k}}; | ||||||
|  |      | ||||||
|  |     checkXIndex(vi, i); | ||||||
|  |     checkPoint(k, j); | ||||||
|  |     addCorr(xyCorr_, isCorr, c); | ||||||
|  | } | ||||||
|  |  | ||||||
| // tests /////////////////////////////////////////////////////////////////////// | // tests /////////////////////////////////////////////////////////////////////// | ||||||
| bool FitInterface::isXUsed(const Index k) const | bool FitInterface::isXUsed(const Index k) const | ||||||
| { | { | ||||||
| @@ -282,6 +348,108 @@ void FitInterface::registerDataPoint(const Index k, const Index j) | |||||||
|     yDataIndex_[j][k] = true; |     yDataIndex_[j][k] = true; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // global layout management //////////////////////////////////////////////////// | ||||||
|  | void FitInterface::updateLayout(void) | ||||||
|  | { | ||||||
|  |     Index size; | ||||||
|  |      | ||||||
|  |     layout_.totalSize  = 0; | ||||||
|  |     layout_.totalXSize = 0; | ||||||
|  |     layout_.totalYSize = 0; | ||||||
|  |     layout_.xSize.clear(); | ||||||
|  |     layout_.ySize.clear(); | ||||||
|  |     layout_.dataIndex.clear(); | ||||||
|  |     layout_.xTrans.clear(); | ||||||
|  |     layout_.dataTrans.clear(); | ||||||
|  |     for (Index i = 0; i < getNXDim(); ++i) | ||||||
|  |     { | ||||||
|  |         if (!xIsExact_[i]) | ||||||
|  |         { | ||||||
|  |             size = getXFitSize(i); | ||||||
|  |             layout_.xSize.push_back(size); | ||||||
|  |             layout_.totalXSize += size; | ||||||
|  |             layout_.xTrans[i] = static_cast<Index>(layout_.xSize.size() - 1); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     for (Index j = 0; j < getNYDim(); ++j) | ||||||
|  |     { | ||||||
|  |         size = getYFitSize(j); | ||||||
|  |         layout_.ySize.push_back(size); | ||||||
|  |         layout_.totalYSize += size; | ||||||
|  |     } | ||||||
|  |     layout_.totalSize = layout_.totalXSize + layout_.totalYSize; | ||||||
|  |     layout_.dataIndex.resize(layout_.ySize.size()); | ||||||
|  |     for (Index j = 0; j < getNYDim(); ++j) | ||||||
|  |     { | ||||||
|  |         for (auto &p: yDataIndex_[j]) | ||||||
|  |         { | ||||||
|  |             if (p.second) | ||||||
|  |             { | ||||||
|  |                 layout_.dataIndex[j].push_back(p.first); | ||||||
|  |                 layout_.dataTrans[p.first] = layout_.dataIndex[j].size() - 1; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // WARNING: NO INDEX CHECKS IN INDEXING FUNCTIONS | ||||||
|  | Index FitInterface::indX(const Index vi, const Index i) const | ||||||
|  | { | ||||||
|  |     Index ind, iTrans; | ||||||
|  |      | ||||||
|  |     iTrans = layout_.xTrans.at(i); | ||||||
|  |     ind    = layout_.totalYSize; | ||||||
|  |     for (Index a = 0; a < iTrans; ++a) | ||||||
|  |     { | ||||||
|  |         ind += layout_.xSize[a]; | ||||||
|  |     } | ||||||
|  |     ind += vi; | ||||||
|  |      | ||||||
|  |     return ind; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | Index FitInterface::indY(const Index k, const Index j) const | ||||||
|  | { | ||||||
|  |     Index ind = 0; | ||||||
|  |      | ||||||
|  |     for (Index a = 0; a < j; ++a) | ||||||
|  |     { | ||||||
|  |         ind += layout_.ySize[a]; | ||||||
|  |     } | ||||||
|  |     ind += layout_.dataTrans.at(k); | ||||||
|  |      | ||||||
|  |     return ind; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | DMat FitInterface::makeCorrFilter(void) const | ||||||
|  | { | ||||||
|  |     DMat f = DMat::Identity(layout_.totalSize, layout_.totalSize); | ||||||
|  |      | ||||||
|  |     for (auto &c: xxCorr_) | ||||||
|  |     { | ||||||
|  |         f(indX(c[2], c[0]), indX(c[3], c[1])) = 1.; | ||||||
|  |         f(indX(c[3], c[1]), indX(c[2], c[0])) = 1.; | ||||||
|  |     } | ||||||
|  |     for (auto &c: yyCorr_) | ||||||
|  |     { | ||||||
|  |         if (isFitPoint(c[2], c[0]) and isFitPoint(c[3], c[1])) | ||||||
|  |         { | ||||||
|  |             f(indY(c[2], c[0]), indY(c[3], c[1])) = 1.; | ||||||
|  |             f(indY(c[3], c[1]), indY(c[2], c[0])) = 1.; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     for (auto &c: xyCorr_) | ||||||
|  |     { | ||||||
|  |         if (isFitPoint(c[3], c[1])) | ||||||
|  |         { | ||||||
|  |             f(indX(c[2], c[0]), indY(c[3], c[1])) = 1.; | ||||||
|  |             f(indY(c[3], c[1]), indX(c[2], c[0])) = 1.; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |     return f; | ||||||
|  | } | ||||||
|  |  | ||||||
| // IO ////////////////////////////////////////////////////////////////////////// | // IO ////////////////////////////////////////////////////////////////////////// | ||||||
| ostream & Latan::operator<<(ostream &out, FitInterface &f) | ostream & Latan::operator<<(ostream &out, FitInterface &f) | ||||||
| { | { | ||||||
| @@ -306,6 +474,60 @@ ostream & Latan::operator<<(ostream &out, FitInterface &f) | |||||||
|             out << "\b) fit: " << (p.second ? "true" : "false") << endl; |             out << "\b) fit: " << (p.second ? "true" : "false") << endl; | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |     out << "X/X correlations (i1 i2 vi1 vi2): "; | ||||||
|  |     if (f.xxCorr_.empty()) | ||||||
|  |     { | ||||||
|  |         out << "no" << endl; | ||||||
|  |     } | ||||||
|  |     else | ||||||
|  |     { | ||||||
|  |         out << endl; | ||||||
|  |         for (auto &c: f.xxCorr_) | ||||||
|  |         { | ||||||
|  |             out << "  * "; | ||||||
|  |             for (auto i: c) | ||||||
|  |             { | ||||||
|  |                 out << i << " "; | ||||||
|  |             } | ||||||
|  |             out << endl; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     out << "Y/Y correlations (j1 j2 k1 k2): "; | ||||||
|  |     if (f.yyCorr_.empty()) | ||||||
|  |     { | ||||||
|  |         out << "no" << endl; | ||||||
|  |     } | ||||||
|  |     else | ||||||
|  |     { | ||||||
|  |         out << endl; | ||||||
|  |         for (auto &c: f.yyCorr_) | ||||||
|  |         { | ||||||
|  |             out << "  * "; | ||||||
|  |             for (auto i: c) | ||||||
|  |             { | ||||||
|  |                 out << i << " "; | ||||||
|  |             } | ||||||
|  |             out << endl; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     out << "X/Y correlations (i j vi k): "; | ||||||
|  |     if (f.xyCorr_.empty()) | ||||||
|  |     { | ||||||
|  |         out << "no"; | ||||||
|  |     } | ||||||
|  |     else | ||||||
|  |     { | ||||||
|  |         out << endl; | ||||||
|  |         for (auto &c: f.xyCorr_) | ||||||
|  |         { | ||||||
|  |             out << "  * "; | ||||||
|  |             for (auto i: c) | ||||||
|  |             { | ||||||
|  |                 out << i << " "; | ||||||
|  |             } | ||||||
|  |             out << endl; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|      |      | ||||||
|     return out; |     return out; | ||||||
| } | } | ||||||
|   | |||||||
| @@ -21,6 +21,7 @@ | |||||||
| #define Latan_FitInterface_hpp_ | #define Latan_FitInterface_hpp_ | ||||||
|  |  | ||||||
| #include <LatAnalyze/Global.hpp> | #include <LatAnalyze/Global.hpp> | ||||||
|  | #include <LatAnalyze/Mat.hpp> | ||||||
|  |  | ||||||
| BEGIN_LATAN_NAMESPACE | BEGIN_LATAN_NAMESPACE | ||||||
|  |  | ||||||
| @@ -29,13 +30,22 @@ BEGIN_LATAN_NAMESPACE | |||||||
|  ******************************************************************************/ |  ******************************************************************************/ | ||||||
| class FitInterface | class FitInterface | ||||||
| { | { | ||||||
|  | private: | ||||||
|  |     typedef struct | ||||||
|  |     { | ||||||
|  |         Index                           totalSize, totalXSize, totalYSize; | ||||||
|  |         std::vector<Index>              xSize, ySize; | ||||||
|  |         std::vector<std::vector<Index>> dataIndex; | ||||||
|  |         std::map<Index, Index>          xTrans, dataTrans; | ||||||
|  |     } Layout; | ||||||
| public: | public: | ||||||
|     // constructor |     // constructor | ||||||
|     FitInterface(void); |     FitInterface(void); | ||||||
|     // destructor |     // destructor | ||||||
|     virtual ~FitInterface(void) = default; |     virtual ~FitInterface(void) = default; | ||||||
|     // add dimensions |     // add dimensions | ||||||
|     void  addXDim(const std::string name, const Index nData); |     void  addXDim(const std::string name, const Index nData, | ||||||
|  |                   const bool isExact = false); | ||||||
|     void  addYDim(const std::string name); |     void  addYDim(const std::string name); | ||||||
|     // size access |     // size access | ||||||
|     Index getNXDim(void) const; |     Index getNXDim(void) const; | ||||||
| @@ -55,6 +65,14 @@ public: | |||||||
|     std::vector<Index> dataCoord(const Index k) const; |     std::vector<Index> dataCoord(const Index k) const; | ||||||
|     // enable fit points |     // enable fit points | ||||||
|     void fitPoint(const bool isFitPoint, const Index k, const Index j = 0); |     void fitPoint(const bool isFitPoint, const Index k, const Index j = 0); | ||||||
|  |     // variance interface | ||||||
|  |     void assumeXExact(const bool isExact, const Index i); | ||||||
|  |     void assumeXXCorrelated(const bool isCorr, const Index i1, const Index i2, | ||||||
|  |                             const Index vi1, const Index vi2); | ||||||
|  |     void assumeYYCorrelated(const bool isCorr, const Index j1, const Index j2, | ||||||
|  |                             const Index k1, const Index k2); | ||||||
|  |     void assumeXYCorrelated(const bool isCorr, const Index i, const Index j, | ||||||
|  |                             const Index vi, const Index k); | ||||||
|     // tests |     // tests | ||||||
|     bool isXUsed(const Index k) const; |     bool isXUsed(const Index k) const; | ||||||
|     bool isXUsed(const Index k, const Index j) const; |     bool isXUsed(const Index k, const Index j) const; | ||||||
| @@ -65,14 +83,25 @@ protected: | |||||||
| public: | public: | ||||||
|     // register a data point |     // register a data point | ||||||
|     void registerDataPoint(const Index k, const Index j = 0); |     void registerDataPoint(const Index k, const Index j = 0); | ||||||
|  |     // add correlation to a set | ||||||
|  |     static void addCorr(std::set<std::array<Index, 4>> &s, const bool isCorr, | ||||||
|  |                         const std::array<Index, 4> &c); | ||||||
|     // abstract method to update data container size |     // abstract method to update data container size | ||||||
|     virtual void updateDataSize(void) {}; |     virtual void updateDataSize(void) {}; | ||||||
|  |     // global layout management | ||||||
|  |     void  updateLayout(void); | ||||||
|  |     Index indX(const Index vi, const Index i) const; | ||||||
|  |     Index indY(const Index k, const Index j) const; | ||||||
|  |     DMat  makeCorrFilter(void) const; | ||||||
| private: | private: | ||||||
|     std::vector<std::string>           xDimName_, yDimName_; |     std::vector<std::string>           xDimName_, yDimName_; | ||||||
|     std::map<std::string, Index>       xDimIndex_, yDimIndex_; |     std::map<std::string, Index>       xDimIndex_, yDimIndex_; | ||||||
|     std::vector<Index>                 xSize_; |     std::vector<Index>                 xSize_; | ||||||
|  |     std::vector<bool>                  xIsExact_; | ||||||
|     std::vector<std::map<Index, bool>> yDataIndex_; |     std::vector<std::map<Index, bool>> yDataIndex_; | ||||||
|  |     std::set<std::array<Index, 4>>     xxCorr_, yyCorr_, xyCorr_; | ||||||
|     Index                              maxDataIndex_{1}; |     Index                              maxDataIndex_{1}; | ||||||
|  |     Layout                             layout_; | ||||||
| }; | }; | ||||||
|  |  | ||||||
| std::ostream & operator<<(std::ostream &out, FitInterface &f); | std::ostream & operator<<(std::ostream &out, FitInterface &f); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user