1
0
mirror of https://github.com/aportelli/LatAnalyze.git synced 2025-04-05 09:35:54 +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());
}
Index FitInterface::getXFitSize(void)
Index FitInterface::getXFitSize(void) const
{
Index size = 0;
@ -134,7 +134,7 @@ Index FitInterface::getXFitSize(void)
return size;
}
Index FitInterface::getXFitSize(const Index i)
Index FitInterface::getXFitSize(const Index i) const
{
set<Index> fitCoord;
vector<Index> v;
@ -227,7 +227,7 @@ Index FitInterface::dataIndex(const vector<Index> &v) const
return k;
}
const vector<Index> & FitInterface::dataCoord(const Index k)
const vector<Index> & FitInterface::dataCoord(const Index k) const
{
checkDataIndex(k);
@ -370,7 +370,7 @@ bool FitInterface::isXExact(const Index i) const
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;
@ -463,16 +463,18 @@ void FitInterface::scheduleDataCoordInit(void)
initDataCoord_ = true;
}
void FitInterface::updateDataCoord(void)
void FitInterface::updateDataCoord(void) const
{
FitInterface * modThis = const_cast<FitInterface *>(this);
if (initDataCoord_)
{
dataCoord_.clear();
modThis->dataCoord_.clear();
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();
}
bool FitInterface::initVarMat(void)
bool FitInterface::initVarMat(void) const
{
return initVarMat_;
}
void FitInterface::updateLayout(void)
void FitInterface::updateLayout(void) const
{
if (initLayout_)
{
Index size, ifit;
vector<Index> v;
FitInterface * modThis = const_cast<FitInterface *>(this);
Layout & l = modThis->layout;
Index size, ifit;
vector<Index> v;
layout.nXFitDim = 0;
layout.nYFitDim = 0;
layout.totalSize = 0;
layout.totalXSize = 0;
layout.totalYSize = 0;
layout.xSize.clear();
layout.ySize.clear();
layout.dataIndexSet.clear();
layout.xDim.clear();
layout.yDim.clear();
layout.xFitDim.clear();
layout.yFitDim.clear();
layout.x.clear();
layout.y.clear();
layout.xFit.clear();
layout.yFit.clear();
l.nXFitDim = 0;
l.nYFitDim = 0;
l.totalSize = 0;
l.totalXSize = 0;
l.totalYSize = 0;
l.xSize.clear();
l.ySize.clear();
l.dataIndexSet.clear();
l.xDim.clear();
l.yDim.clear();
l.xFitDim.clear();
l.yFitDim.clear();
l.x.clear();
l.y.clear();
l.xFit.clear();
l.yFit.clear();
ifit = 0;
for (Index i = 0; i < getNXDim(); ++i)
{
if (!xIsExact_[i])
{
layout.nXFitDim++;
l.nXFitDim++;
size = getXFitSize(i);
layout.xSize.push_back(size);
layout.totalXSize += size;
layout.xDim.push_back(i);
layout.xFitDim.push_back(layout.xDim.size() - 1);
layout.x.push_back(vector<Index>());
layout.xFit.push_back(vector<Index>());
l.xSize.push_back(size);
l.totalXSize += size;
l.xDim.push_back(i);
l.xFitDim.push_back(layout.xDim.size() - 1);
l.x.push_back(vector<Index>());
l.xFit.push_back(vector<Index>());
for (Index r = 0; r < getXSize(i); ++r)
{
if (isXUsed(r, i))
{
layout.x[ifit].push_back(r);
layout.xFit[i].push_back(layout.x[ifit].size() - 1);
l.x[ifit].push_back(r);
l.xFit[i].push_back(layout.x[ifit].size() - 1);
}
else
{
layout.xFit[i].push_back(-1);
l.xFit[i].push_back(-1);
}
}
ifit++;
}
else
{
layout.xFitDim.push_back(-1);
layout.xFit.push_back(vector<Index>());
l.xFitDim.push_back(-1);
l.xFit.push_back(vector<Index>());
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;
layout.nYFitDim++;
l.nYFitDim++;
size = getYFitSize(j);
layout.ySize.push_back(size);
layout.totalYSize += size;
layout.yDim.push_back(j);
layout.yFitDim.push_back(layout.yDim.size() - 1);
layout.y.push_back(vector<Index>());
layout.yFit.push_back(vector<Index>());
layout.data.push_back(vector<Index>());
layout.yFitFromData.push_back(map<Index, Index>());
l.ySize.push_back(size);
l.totalYSize += size;
l.yDim.push_back(j);
l.yFitDim.push_back(layout.yDim.size() - 1);
l.y.push_back(vector<Index>());
l.yFit.push_back(vector<Index>());
l.data.push_back(vector<Index>());
l.yFitFromData.push_back(map<Index, Index>());
for (auto &p: yDataIndex_[j])
{
if (p.second)
{
layout.dataIndexSet.insert(p.first);
layout.y[j].push_back(s);
layout.yFit[j].push_back(layout.y[j].size() - 1);
layout.data[j].push_back(p.first);
layout.yFitFromData[j][p.first] = layout.y[j].size() - 1;
l.dataIndexSet.insert(p.first);
l.y[j].push_back(s);
l.yFit[j].push_back(layout.y[j].size() - 1);
l.data[j].push_back(p.first);
l.yFitFromData[j][p.first] = layout.y[j].size() - 1;
}
else
{
layout.yFit[j].push_back(-1);
layout.yFitFromData[j][p.first] = -1;
l.yFit[j].push_back(-1);
l.yFitFromData[j][p.first] = -1;
}
s++;
}
}
layout.totalSize = layout.totalXSize + layout.totalYSize;
layout.nXFitDim = static_cast<Index>(layout.xSize.size());
layout.nYFitDim = static_cast<Index>(layout.ySize.size());
l.totalSize = layout.totalXSize + layout.totalYSize;
l.nXFitDim = static_cast<Index>(layout.xSize.size());
l.nYFitDim = static_cast<Index>(layout.ySize.size());
for (Index k: layout.dataIndexSet)
{
v = dataCoord(k);
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 getYSize(void) const;
Index getYSize(const Index j) const;
Index getXFitSize(void);
Index getXFitSize(const Index i);
Index getXFitSize(void) const;
Index getXFitSize(const Index i) const;
Index getYFitSize(void) const;
Index getYFitSize(const Index j) const;
Index getMaxDataIndex(void) const;
@ -81,11 +81,12 @@ public:
const VarName & xName(void) const;
VarName & yName(void);
const VarName & yName(void) const;
// Y dimension index helper
template <typename... Ts>
Index dataIndex(const Ts... is) 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
void fitPoint(const bool isFitPoint, const Index k, const Index j = 0);
// variance interface
@ -103,7 +104,7 @@ public:
bool pointExists(const Index k) const;
bool pointExists(const Index k, const Index j) 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;
// make correlation filter for fit variance matrix
DMat makeCorrFilter(void);
@ -122,11 +123,11 @@ protected:
virtual void createYData(const std::string name) = 0;
// coordinate buffering
void scheduleDataCoordInit(void);
void updateDataCoord(void);
void updateDataCoord(void) const;
// global layout management
void scheduleLayoutInit(void);
bool initVarMat(void);
void updateLayout(void);
bool initVarMat(void) const;
void updateLayout(void) const;
Index indX(const Index r, const Index i) const;
Index indY(const Index k, const Index j) const;
private:
@ -143,7 +144,8 @@ private:
std::vector<std::map<Index, bool>> yDataIndex_;
std::set<std::array<Index, 4>> xxCorr_, yyCorr_, xyCorr_;
Index maxDataIndex_{1};
bool initLayout_{true}, initVarMat_{true};
bool initLayout_{true};
bool initVarMat_{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");
}
PlotData::PlotData(XYStatData &data, const Index i, const Index j)
PlotData::PlotData(const XYStatData &data, const Index i, const Index j)
{
string usingCmd, tmpFileName;

View File

@ -94,7 +94,7 @@ public:
PlotData(const DMatSample &x, const DMatSample &y);
PlotData(const DVec &x, const DMatSample &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
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);
}
const DVec & XYStatData::x(const Index k)
const DVec & XYStatData::x(const Index k) const
{
checkDataIndex(k);
@ -215,7 +215,7 @@ DVec XYStatData::getYError(const Index j) const
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);
checkYDim(j);
@ -510,26 +510,28 @@ void XYStatData::updateFitVarMat(void)
chi2ModVec_.resize(layout.totalSize);
chi2Vec_.resize(layout.totalSize);
fitVar_ = fitVar_.cwiseProduct(makeCorrFilter());
fitVarInv_ = fitVar_.pInverse();
fitVarInv_ = fitVar_.pInverse(getSvdTolerance());
scheduleFitVarMatInit(false);
}
}
// buffer list of x vectors ////////////////////////////////////////////////////
void XYStatData::updateXMap(void)
void XYStatData::updateXMap(void) const
{
if (initXMap_)
{
xMap_.clear();
XYStatData * modThis = const_cast<XYStatData *>(this);
modThis->xMap_.clear();
for (auto k: getDataIndexSet())
{
xMap_[k] = DVec(getNXDim());
modThis->xMap_[k] = DVec(getNXDim());
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
virtual ~XYStatData(void) = default;
// data access
double & x(const Index r, const Index);
const double & x(const Index r, const Index) const;
const DVec & x(const Index k);
double & y(const Index k, const Index);
const double & y(const Index k, const Index) const;
double & x(const Index r, const Index i);
const double & x(const Index r, const Index i) const;
const DVec & x(const Index k) const;
double & y(const Index k, const Index j);
const double & y(const Index k, const Index j) const;
void setXXVar(const Index i1, const Index i2, 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);
@ -84,7 +84,7 @@ public:
const DMat & getXYVar(const Index i, const Index j) const;
DVec getXError(const Index i) 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
const DMat & getFitVarMat(void);
const DMat & getFitVarMatPInv(void);
@ -115,7 +115,7 @@ private:
// buffer total fit variance matrix
void updateFitVarMat(void);
// buffer list of x vectors
void updateXMap(void);
void updateXMap(void) const;
// buffer chi^2 vectors
void updateChi2DataVec(void);
void updateChi2ModVec(const DVec p,