1
0
mirror of https://github.com/aportelli/LatAnalyze.git synced 2025-04-11 03:20:46 +01:00

fit: coordinate buffering forced to be const

This commit is contained in:
Antonin Portelli 2016-04-05 14:29:12 +01:00
parent f83ee5bf6c
commit 939520db24
6 changed files with 95 additions and 87 deletions

View File

@ -122,7 +122,7 @@ Index FitInterface::getYSize(const Index j) const
return static_cast<Index>(yDataIndex_[j].size()); return static_cast<Index>(yDataIndex_[j].size());
} }
Index FitInterface::getXFitSize(void) Index FitInterface::getXFitSize(void) const
{ {
Index size = 0; Index size = 0;
@ -134,7 +134,7 @@ Index FitInterface::getXFitSize(void)
return size; return size;
} }
Index FitInterface::getXFitSize(const Index i) Index FitInterface::getXFitSize(const Index i) const
{ {
set<Index> fitCoord; set<Index> fitCoord;
vector<Index> v; vector<Index> v;
@ -227,7 +227,7 @@ Index FitInterface::dataIndex(const vector<Index> &v) const
return k; return k;
} }
const vector<Index> & FitInterface::dataCoord(const Index k) const vector<Index> & FitInterface::dataCoord(const Index k) const
{ {
checkDataIndex(k); checkDataIndex(k);
@ -370,7 +370,7 @@ bool FitInterface::isXExact(const Index i) const
return xIsExact_[i]; return xIsExact_[i];
} }
bool FitInterface::isXUsed(const Index r, const Index i, const bool inFit) bool FitInterface::isXUsed(const Index r, const Index i, const bool inFit) const
{ {
vector<Index> v; vector<Index> v;
@ -463,16 +463,18 @@ void FitInterface::scheduleDataCoordInit(void)
initDataCoord_ = true; initDataCoord_ = true;
} }
void FitInterface::updateDataCoord(void) void FitInterface::updateDataCoord(void) const
{ {
FitInterface * modThis = const_cast<FitInterface *>(this);
if (initDataCoord_) if (initDataCoord_)
{ {
dataCoord_.clear(); modThis->dataCoord_.clear();
for (auto k: getDataIndexSet()) for (auto k: getDataIndexSet())
{ {
dataCoord_[k] = rowMajToCoord(k); modThis->dataCoord_[k] = rowMajToCoord(k);
} }
initDataCoord_ = false; modThis->initDataCoord_ = false;
} }
} }
@ -483,68 +485,70 @@ void FitInterface::scheduleLayoutInit(void)
scheduleFitVarMatInit(); scheduleFitVarMatInit();
} }
bool FitInterface::initVarMat(void) bool FitInterface::initVarMat(void) const
{ {
return initVarMat_; return initVarMat_;
} }
void FitInterface::updateLayout(void) void FitInterface::updateLayout(void) const
{ {
if (initLayout_) if (initLayout_)
{ {
Index size, ifit; FitInterface * modThis = const_cast<FitInterface *>(this);
vector<Index> v; Layout & l = modThis->layout;
Index size, ifit;
vector<Index> v;
layout.nXFitDim = 0; l.nXFitDim = 0;
layout.nYFitDim = 0; l.nYFitDim = 0;
layout.totalSize = 0; l.totalSize = 0;
layout.totalXSize = 0; l.totalXSize = 0;
layout.totalYSize = 0; l.totalYSize = 0;
layout.xSize.clear(); l.xSize.clear();
layout.ySize.clear(); l.ySize.clear();
layout.dataIndexSet.clear(); l.dataIndexSet.clear();
layout.xDim.clear(); l.xDim.clear();
layout.yDim.clear(); l.yDim.clear();
layout.xFitDim.clear(); l.xFitDim.clear();
layout.yFitDim.clear(); l.yFitDim.clear();
layout.x.clear(); l.x.clear();
layout.y.clear(); l.y.clear();
layout.xFit.clear(); l.xFit.clear();
layout.yFit.clear(); l.yFit.clear();
ifit = 0; ifit = 0;
for (Index i = 0; i < getNXDim(); ++i) for (Index i = 0; i < getNXDim(); ++i)
{ {
if (!xIsExact_[i]) if (!xIsExact_[i])
{ {
layout.nXFitDim++; l.nXFitDim++;
size = getXFitSize(i); size = getXFitSize(i);
layout.xSize.push_back(size); l.xSize.push_back(size);
layout.totalXSize += size; l.totalXSize += size;
layout.xDim.push_back(i); l.xDim.push_back(i);
layout.xFitDim.push_back(layout.xDim.size() - 1); l.xFitDim.push_back(layout.xDim.size() - 1);
layout.x.push_back(vector<Index>()); l.x.push_back(vector<Index>());
layout.xFit.push_back(vector<Index>()); l.xFit.push_back(vector<Index>());
for (Index r = 0; r < getXSize(i); ++r) for (Index r = 0; r < getXSize(i); ++r)
{ {
if (isXUsed(r, i)) if (isXUsed(r, i))
{ {
layout.x[ifit].push_back(r); l.x[ifit].push_back(r);
layout.xFit[i].push_back(layout.x[ifit].size() - 1); l.xFit[i].push_back(layout.x[ifit].size() - 1);
} }
else else
{ {
layout.xFit[i].push_back(-1); l.xFit[i].push_back(-1);
} }
} }
ifit++; ifit++;
} }
else else
{ {
layout.xFitDim.push_back(-1); l.xFitDim.push_back(-1);
layout.xFit.push_back(vector<Index>()); l.xFit.push_back(vector<Index>());
for (Index r = 0; r < getXSize(i); ++r) for (Index r = 0; r < getXSize(i); ++r)
{ {
layout.xFit[i].push_back(-1); l.xFit[i].push_back(-1);
} }
} }
} }
@ -552,46 +556,46 @@ void FitInterface::updateLayout(void)
{ {
Index s = 0; Index s = 0;
layout.nYFitDim++; l.nYFitDim++;
size = getYFitSize(j); size = getYFitSize(j);
layout.ySize.push_back(size); l.ySize.push_back(size);
layout.totalYSize += size; l.totalYSize += size;
layout.yDim.push_back(j); l.yDim.push_back(j);
layout.yFitDim.push_back(layout.yDim.size() - 1); l.yFitDim.push_back(layout.yDim.size() - 1);
layout.y.push_back(vector<Index>()); l.y.push_back(vector<Index>());
layout.yFit.push_back(vector<Index>()); l.yFit.push_back(vector<Index>());
layout.data.push_back(vector<Index>()); l.data.push_back(vector<Index>());
layout.yFitFromData.push_back(map<Index, Index>()); l.yFitFromData.push_back(map<Index, Index>());
for (auto &p: yDataIndex_[j]) for (auto &p: yDataIndex_[j])
{ {
if (p.second) if (p.second)
{ {
layout.dataIndexSet.insert(p.first); l.dataIndexSet.insert(p.first);
layout.y[j].push_back(s); l.y[j].push_back(s);
layout.yFit[j].push_back(layout.y[j].size() - 1); l.yFit[j].push_back(layout.y[j].size() - 1);
layout.data[j].push_back(p.first); l.data[j].push_back(p.first);
layout.yFitFromData[j][p.first] = layout.y[j].size() - 1; l.yFitFromData[j][p.first] = layout.y[j].size() - 1;
} }
else else
{ {
layout.yFit[j].push_back(-1); l.yFit[j].push_back(-1);
layout.yFitFromData[j][p.first] = -1; l.yFitFromData[j][p.first] = -1;
} }
s++; s++;
} }
} }
layout.totalSize = layout.totalXSize + layout.totalYSize; l.totalSize = layout.totalXSize + layout.totalYSize;
layout.nXFitDim = static_cast<Index>(layout.xSize.size()); l.nXFitDim = static_cast<Index>(layout.xSize.size());
layout.nYFitDim = static_cast<Index>(layout.ySize.size()); l.nYFitDim = static_cast<Index>(layout.ySize.size());
for (Index k: layout.dataIndexSet) for (Index k: layout.dataIndexSet)
{ {
v = dataCoord(k); v = dataCoord(k);
for (Index i = 0; i < getNXDim(); ++i) for (Index i = 0; i < getNXDim(); ++i)
{ {
layout.xIndFromData[k].push_back(indX(v[i], i)); l.xIndFromData[k].push_back(indX(v[i], i));
} }
} }
initLayout_ = false; modThis->initLayout_ = false;
} }
} }

View File

@ -71,8 +71,8 @@ public:
Index getXSize(const Index i) const; Index getXSize(const Index i) const;
Index getYSize(void) const; Index getYSize(void) const;
Index getYSize(const Index j) const; Index getYSize(const Index j) const;
Index getXFitSize(void); Index getXFitSize(void) const;
Index getXFitSize(const Index i); Index getXFitSize(const Index i) const;
Index getYFitSize(void) const; Index getYFitSize(void) const;
Index getYFitSize(const Index j) const; Index getYFitSize(const Index j) const;
Index getMaxDataIndex(void) const; Index getMaxDataIndex(void) const;
@ -81,11 +81,12 @@ public:
const VarName & xName(void) const; const VarName & xName(void) const;
VarName & yName(void); VarName & yName(void);
const VarName & yName(void) const; const VarName & yName(void) const;
// Y dimension index helper // Y dimension index helper
template <typename... Ts> template <typename... Ts>
Index dataIndex(const Ts... is) const; Index dataIndex(const Ts... is) const;
Index dataIndex(const std::vector<Index> &v) const; Index dataIndex(const std::vector<Index> &v) const;
const 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 // variance interface
@ -103,7 +104,7 @@ public:
bool pointExists(const Index k) const; bool pointExists(const Index k) const;
bool pointExists(const Index k, const Index j) const; bool pointExists(const Index k, const Index j) const;
bool isXExact(const Index i) const; bool isXExact(const Index i) const;
bool isXUsed(const Index r, const Index i, const bool inFit = true); bool isXUsed(const Index r, const Index i, const bool inFit = true) const;
bool isFitPoint(const Index k, const Index j) const; bool isFitPoint(const Index k, const Index j) const;
// make correlation filter for fit variance matrix // make correlation filter for fit variance matrix
DMat makeCorrFilter(void); DMat makeCorrFilter(void);
@ -122,11 +123,11 @@ protected:
virtual void createYData(const std::string name) = 0; virtual void createYData(const std::string name) = 0;
// coordinate buffering // coordinate buffering
void scheduleDataCoordInit(void); void scheduleDataCoordInit(void);
void updateDataCoord(void); void updateDataCoord(void) const;
// global layout management // global layout management
void scheduleLayoutInit(void); void scheduleLayoutInit(void);
bool initVarMat(void); bool initVarMat(void) const;
void updateLayout(void); void updateLayout(void) const;
Index indX(const Index r, const Index i) const; Index indX(const Index r, const Index i) const;
Index indY(const Index k, const Index j) const; Index indY(const Index k, const Index j) const;
private: private:
@ -143,7 +144,8 @@ private:
std::vector<std::map<Index, bool>> yDataIndex_; std::vector<std::map<Index, bool>> yDataIndex_;
std::set<std::array<Index, 4>> xxCorr_, yyCorr_, xyCorr_; std::set<std::array<Index, 4>> xxCorr_, yyCorr_, xyCorr_;
Index maxDataIndex_{1}; Index maxDataIndex_{1};
bool initLayout_{true}, initVarMat_{true}; bool initLayout_{true};
bool initVarMat_{true};
bool initDataCoord_{true}; bool initDataCoord_{true};
}; };

View File

@ -167,7 +167,7 @@ PlotData::PlotData(const DMatSample &x, const DVec &y)
setCommand("'" + tmpFileName + "' u 1:3:2 w xerr"); setCommand("'" + tmpFileName + "' u 1:3:2 w xerr");
} }
PlotData::PlotData(XYStatData &data, const Index i, const Index j) PlotData::PlotData(const XYStatData &data, const Index i, const Index j)
{ {
string usingCmd, tmpFileName; string usingCmd, tmpFileName;

View File

@ -94,7 +94,7 @@ public:
PlotData(const DMatSample &x, const DMatSample &y); PlotData(const DMatSample &x, const DMatSample &y);
PlotData(const DVec &x, const DMatSample &y); PlotData(const DVec &x, const DMatSample &y);
PlotData(const DMatSample &x, const DVec &y); PlotData(const DMatSample &x, const DVec &y);
PlotData(XYStatData &data, const Index i = 0, const Index j = 0); PlotData(const XYStatData &data, const Index i = 0, const Index j = 0);
// destructor // destructor
virtual ~PlotData(void) = default; virtual ~PlotData(void) = default;
}; };

View File

@ -96,7 +96,7 @@ const double & XYStatData::x(const Index r, const Index i) const
return xData_[i](r); return xData_[i](r);
} }
const DVec & XYStatData::x(const Index k) const DVec & XYStatData::x(const Index k) const
{ {
checkDataIndex(k); checkDataIndex(k);
@ -215,7 +215,7 @@ DVec XYStatData::getYError(const Index j) const
return yyVar_(j, j).diagonal().cwiseSqrt(); return yyVar_(j, j).diagonal().cwiseSqrt();
} }
DMat XYStatData::getTable(const Index i, const Index j) DMat XYStatData::getTable(const Index i, const Index j) const
{ {
checkXDim(i); checkXDim(i);
checkYDim(j); checkYDim(j);
@ -510,26 +510,28 @@ void XYStatData::updateFitVarMat(void)
chi2ModVec_.resize(layout.totalSize); chi2ModVec_.resize(layout.totalSize);
chi2Vec_.resize(layout.totalSize); chi2Vec_.resize(layout.totalSize);
fitVar_ = fitVar_.cwiseProduct(makeCorrFilter()); fitVar_ = fitVar_.cwiseProduct(makeCorrFilter());
fitVarInv_ = fitVar_.pInverse(); fitVarInv_ = fitVar_.pInverse(getSvdTolerance());
scheduleFitVarMatInit(false); scheduleFitVarMatInit(false);
} }
} }
// buffer list of x vectors //////////////////////////////////////////////////// // buffer list of x vectors ////////////////////////////////////////////////////
void XYStatData::updateXMap(void) void XYStatData::updateXMap(void) const
{ {
if (initXMap_) if (initXMap_)
{ {
xMap_.clear(); XYStatData * modThis = const_cast<XYStatData *>(this);
modThis->xMap_.clear();
for (auto k: getDataIndexSet()) for (auto k: getDataIndexSet())
{ {
xMap_[k] = DVec(getNXDim()); modThis->xMap_[k] = DVec(getNXDim());
for (Index i = 0; i < getNXDim(); ++i) for (Index i = 0; i < getNXDim(); ++i)
{ {
xMap_[k](i) = xData_[i](dataCoord(k)[i]); modThis->xMap_[k](i) = xData_[i](dataCoord(k)[i]);
} }
} }
initXMap_ = false; modThis->initXMap_ = false;
} }
} }

View File

@ -69,11 +69,11 @@ public:
// destructor // destructor
virtual ~XYStatData(void) = default; virtual ~XYStatData(void) = default;
// data access // data access
double & x(const Index r, const Index); double & x(const Index r, const Index i);
const double & x(const Index r, const Index) const; const double & x(const Index r, const Index i) const;
const DVec & x(const Index k); const DVec & x(const Index k) const;
double & y(const Index k, const Index); double & y(const Index k, const Index j);
const double & y(const Index k, const Index) const; const double & y(const Index k, const Index j) const;
void setXXVar(const Index i1, const Index i2, const DMat &m); void setXXVar(const Index i1, const Index i2, const DMat &m);
void setYYVar(const Index j1, const Index j2, const DMat &m); void setYYVar(const Index j1, const Index j2, const DMat &m);
void setXYVar(const Index i, const Index j, const DMat &m); void setXYVar(const Index i, const Index j, const DMat &m);
@ -84,7 +84,7 @@ public:
const DMat & getXYVar(const Index i, const Index j) const; const DMat & getXYVar(const Index i, const Index j) const;
DVec getXError(const Index i) const; DVec getXError(const Index i) const;
DVec getYError(const Index j) const; DVec getYError(const Index j) const;
DMat getTable(const Index i, const Index j); DMat getTable(const Index i, const Index j) const;
// get total fit variance matrix and its pseudo-inverse // get total fit variance matrix and its pseudo-inverse
const DMat & getFitVarMat(void); const DMat & getFitVarMat(void);
const DMat & getFitVarMatPInv(void); const DMat & getFitVarMatPInv(void);
@ -115,7 +115,7 @@ private:
// buffer total fit variance matrix // buffer total fit variance matrix
void updateFitVarMat(void); void updateFitVarMat(void);
// buffer list of x vectors // buffer list of x vectors
void updateXMap(void); void updateXMap(void) const;
// buffer chi^2 vectors // buffer chi^2 vectors
void updateChi2DataVec(void); void updateChi2DataVec(void);
void updateChi2ModVec(const DVec p, void updateChi2ModVec(const DVec p,