mirror of
https://github.com/aportelli/LatAnalyze.git
synced 2025-06-19 07:47:05 +01:00
Merge from AP's develop.
This commit is contained in:
@ -17,7 +17,7 @@
|
||||
* along with LatAnalyze. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
Derived pInverse(const double tolerance = 1.0e-10)
|
||||
Derived pInverse(const double tolerance = 1.0e-10) const
|
||||
{
|
||||
auto svd = jacobiSvd(Eigen::ComputeThinU|Eigen::ComputeThinV);
|
||||
const auto u = svd.matrixU();
|
||||
@ -52,7 +52,7 @@ Derived pInverse(const double tolerance = 1.0e-10)
|
||||
return v*s.asDiagonal()*u.transpose();
|
||||
}
|
||||
|
||||
Derived singularValues(void)
|
||||
Derived singularValues(void) const
|
||||
{
|
||||
auto svd = jacobiSvd();
|
||||
|
||||
|
@ -29,7 +29,8 @@ using namespace Latan;
|
||||
******************************************************************************/
|
||||
DMat MATH_NAMESPACE::varToCorr(const DMat &var)
|
||||
{
|
||||
DMat res = var, invDiag = res.diagonal();
|
||||
DMat res = var;
|
||||
DVec invDiag = res.diagonal();
|
||||
|
||||
invDiag = invDiag.cwiseInverse().cwiseSqrt();
|
||||
res = (invDiag*invDiag.transpose()).cwiseProduct(res);
|
||||
@ -37,6 +38,28 @@ DMat MATH_NAMESPACE::varToCorr(const DMat &var)
|
||||
return res;
|
||||
}
|
||||
|
||||
DMat MATH_NAMESPACE::corrToVar(const DMat &corr, const DVec &varDiag)
|
||||
{
|
||||
DMat res = corr;
|
||||
DVec varSqrtDiag = varDiag.cwiseSqrt();
|
||||
|
||||
res = (varSqrtDiag*varSqrtDiag.transpose()).cwiseProduct(res);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
double MATH_NAMESPACE::svdDynamicRange(const DMat &mat)
|
||||
{
|
||||
DVec s = mat.singularValues();
|
||||
|
||||
return s.maxCoeff()/s.minCoeff();
|
||||
}
|
||||
|
||||
double MATH_NAMESPACE::svdDynamicRangeDb(const DMat &mat)
|
||||
{
|
||||
return 10.*log10(svdDynamicRange(mat));
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
* Standard C functions *
|
||||
******************************************************************************/
|
||||
|
@ -70,6 +70,11 @@ namespace MATH_NAMESPACE
|
||||
|
||||
// convert variance matrix to correlation matrix
|
||||
DMat varToCorr(const DMat &var);
|
||||
DMat corrToVar(const DMat &corr, const DVec &varDiag);
|
||||
|
||||
// matrix SVD dynamic range
|
||||
double svdDynamicRange(const DMat &mat);
|
||||
double svdDynamicRangeDb(const DMat &mat);
|
||||
|
||||
// Constants
|
||||
constexpr double pi = 3.1415926535897932384626433832795028841970;
|
||||
|
@ -112,7 +112,7 @@ PlotHeadCommand::PlotHeadCommand(const string &command)
|
||||
}
|
||||
|
||||
// PlotData constructor ////////////////////////////////////////////////////////
|
||||
PlotData::PlotData(const DMatSample &x, const DMatSample &y)
|
||||
PlotData::PlotData(const DMatSample &x, const DMatSample &y, const bool abs)
|
||||
{
|
||||
if (x[central].rows() != y[central].rows())
|
||||
{
|
||||
@ -128,10 +128,17 @@ PlotData::PlotData(const DMatSample &x, const DMatSample &y)
|
||||
d.col(3) = y.variance().cwiseSqrt();
|
||||
tmpFileName = dumpToTmpFile(d);
|
||||
pushTmpFile(tmpFileName);
|
||||
setCommand("'" + tmpFileName + "' u 1:3:2:4 w xyerr");
|
||||
if (!abs)
|
||||
{
|
||||
setCommand("'" + tmpFileName + "' u 1:3:2:4 w xyerr");
|
||||
}
|
||||
else
|
||||
{
|
||||
setCommand("'" + tmpFileName + "' u 1:(abs($3)):2:4 w xyerr");
|
||||
}
|
||||
}
|
||||
|
||||
PlotData::PlotData(const DVec &x, const DMatSample &y)
|
||||
PlotData::PlotData(const DVec &x, const DMatSample &y, const bool abs)
|
||||
{
|
||||
if (x.rows() != y[central].rows())
|
||||
{
|
||||
@ -146,10 +153,17 @@ PlotData::PlotData(const DVec &x, const DMatSample &y)
|
||||
d.col(2) = y.variance().cwiseSqrt();
|
||||
tmpFileName = dumpToTmpFile(d);
|
||||
pushTmpFile(tmpFileName);
|
||||
setCommand("'" + tmpFileName + "' u 1:2:3 w yerr");
|
||||
if (!abs)
|
||||
{
|
||||
setCommand("'" + tmpFileName + "' u 1:2:3 w yerr");
|
||||
}
|
||||
else
|
||||
{
|
||||
setCommand("'" + tmpFileName + "' u 1:(abs($2)):3 w yerr");
|
||||
}
|
||||
}
|
||||
|
||||
PlotData::PlotData(const DMatSample &x, const DVec &y)
|
||||
PlotData::PlotData(const DMatSample &x, const DVec &y, const bool abs)
|
||||
{
|
||||
if (x[central].rows() != y.rows())
|
||||
{
|
||||
@ -164,14 +178,29 @@ PlotData::PlotData(const DMatSample &x, const DVec &y)
|
||||
d.col(1) = x.variance().cwiseSqrt();
|
||||
tmpFileName = dumpToTmpFile(d);
|
||||
pushTmpFile(tmpFileName);
|
||||
setCommand("'" + tmpFileName + "' u 1:3:2 w xerr");
|
||||
if (!abs)
|
||||
{
|
||||
setCommand("'" + tmpFileName + "' u 1:3:2 w xerr");
|
||||
}
|
||||
else
|
||||
{
|
||||
setCommand("'" + tmpFileName + "' u 1:($3):2 w xerr");
|
||||
}
|
||||
}
|
||||
|
||||
PlotData::PlotData(const XYStatData &data, const Index i, const Index j)
|
||||
PlotData::PlotData(const XYStatData &data, const Index i, const Index j, const bool abs)
|
||||
{
|
||||
string usingCmd, tmpFileName;
|
||||
|
||||
usingCmd = (data.isXExact(i)) ? "u 1:3:4 w yerr" : "u 1:3:2:4 w xyerr";
|
||||
if (!abs)
|
||||
{
|
||||
usingCmd = (data.isXExact(i)) ? "u 1:3:4 w yerr" : "u 1:3:2:4 w xyerr";
|
||||
}
|
||||
else
|
||||
{
|
||||
usingCmd = (data.isXExact(i)) ? "u 1:(abs($3)):4 w yerr" : "u 1:(abs($3)):2:4 w xyerr";
|
||||
}
|
||||
|
||||
tmpFileName = dumpToTmpFile(data.getTable(i, j));
|
||||
pushTmpFile(tmpFileName);
|
||||
setCommand("'" + tmpFileName + "' " + usingCmd);
|
||||
@ -195,6 +224,24 @@ PlotLine::PlotLine(const DVec &x, const DVec &y)
|
||||
setCommand("'" + tmpFileName + "' u 1:2 w lines");
|
||||
}
|
||||
|
||||
// PlotPoints constructor ////////////////////////////////////////////////////////
|
||||
PlotPoints::PlotPoints(const DVec &x, const DVec &y)
|
||||
{
|
||||
if (x.size() != y.size())
|
||||
{
|
||||
LATAN_ERROR(Size, "x and y vectors do not have the same size");
|
||||
}
|
||||
|
||||
DMat d(x.size(), 2);
|
||||
string usingCmd, tmpFileName;
|
||||
|
||||
d.col(0) = x;
|
||||
d.col(1) = y;
|
||||
tmpFileName = dumpToTmpFile(d);
|
||||
pushTmpFile(tmpFileName);
|
||||
setCommand("'" + tmpFileName + "' u 1:2");
|
||||
}
|
||||
|
||||
// PlotHLine constructor ///////////////////////////////////////////////////////
|
||||
PlotHLine::PlotHLine(const double y)
|
||||
{
|
||||
@ -217,7 +264,8 @@ PlotBand::PlotBand(const double xMin, const double xMax, const double yMin,
|
||||
|
||||
// PlotFunction constructor ////////////////////////////////////////////////////
|
||||
PlotFunction::PlotFunction(const DoubleFunction &function, const double xMin,
|
||||
const double xMax, const unsigned int nPoint)
|
||||
const double xMax, const unsigned int nPoint,
|
||||
const bool abs)
|
||||
{
|
||||
DMat d(nPoint, 2);
|
||||
string tmpFileName;
|
||||
@ -230,7 +278,14 @@ PlotFunction::PlotFunction(const DoubleFunction &function, const double xMin,
|
||||
}
|
||||
tmpFileName = dumpToTmpFile(d);
|
||||
pushTmpFile(tmpFileName);
|
||||
setCommand("'" + tmpFileName + "' u 1:2 w lines");
|
||||
if (!abs)
|
||||
{
|
||||
setCommand("'" + tmpFileName + "' u 1:2 w lines");
|
||||
}
|
||||
else
|
||||
{
|
||||
setCommand("'" + tmpFileName + "' u 1:(abs($2)) w lines");
|
||||
}
|
||||
}
|
||||
|
||||
// PlotPredBand constructor ////////////////////////////////////////////////////
|
||||
|
@ -89,10 +89,11 @@ class PlotData: public PlotObject
|
||||
{
|
||||
public:
|
||||
// constructor
|
||||
PlotData(const DMatSample &x, const DMatSample &y);
|
||||
PlotData(const DVec &x, const DMatSample &y);
|
||||
PlotData(const DMatSample &x, const DVec &y);
|
||||
PlotData(const XYStatData &data, const Index i = 0, const Index j = 0);
|
||||
PlotData(const DMatSample &x, const DMatSample &y, const bool abs = false);
|
||||
PlotData(const DVec &x, const DMatSample &y, const bool abs = false);
|
||||
PlotData(const DMatSample &x, const DVec &y, const bool abs = false);
|
||||
PlotData(const XYStatData &data, const Index i = 0, const Index j = 0,
|
||||
const bool abs = false);
|
||||
// destructor
|
||||
virtual ~PlotData(void) = default;
|
||||
};
|
||||
@ -115,6 +116,15 @@ public:
|
||||
virtual ~PlotLine(void) = default;
|
||||
};
|
||||
|
||||
class PlotPoints: public PlotObject
|
||||
{
|
||||
public:
|
||||
// constructor
|
||||
PlotPoints(const DVec &x, const DVec &y);
|
||||
// destructor
|
||||
virtual ~PlotPoints(void) = default;
|
||||
};
|
||||
|
||||
class PlotBand: public PlotObject
|
||||
{
|
||||
public:
|
||||
@ -130,7 +140,8 @@ class PlotFunction: public PlotObject
|
||||
public:
|
||||
// constructor
|
||||
PlotFunction(const DoubleFunction &function, const double xMin,
|
||||
const double xMax, const unsigned int nPoint = 1000);
|
||||
const double xMax, const unsigned int nPoint = 1000,
|
||||
const bool abs = false);
|
||||
// destructor
|
||||
virtual ~PlotFunction(void) = default;
|
||||
};
|
||||
@ -182,6 +193,11 @@ PlotRange(Axis::x, -.5, (m).cols() - .5) <<\
|
||||
PlotRange(Axis::y, (m).rows() - .5, -.5) <<\
|
||||
PlotMatrixNoRange(m)
|
||||
|
||||
#define PlotCorrMatrix(m)\
|
||||
PlotHeadCommand("set cbrange [-1:1]") <<\
|
||||
PlotHeadCommand("set palette defined (0 'blue', 1 'white', 2 'red')") <<\
|
||||
PlotMatrix(m)
|
||||
|
||||
/******************************************************************************
|
||||
* Plot modifiers *
|
||||
******************************************************************************/
|
||||
|
117
lib/Core/ThreadPool.cpp
Normal file
117
lib/Core/ThreadPool.cpp
Normal file
@ -0,0 +1,117 @@
|
||||
/*
|
||||
* ThreadPool.cpp, part of LatAnalyze 3
|
||||
*
|
||||
* Copyright (C) 2013 - 2021 Antonin Portelli
|
||||
*
|
||||
* LatAnalyze 3 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LatAnalyze 3 is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with LatAnalyze 3. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <LatAnalyze/Core/ThreadPool.hpp>
|
||||
#include <LatAnalyze/includes.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace Latan;
|
||||
|
||||
/******************************************************************************
|
||||
* ThreadPool implementation *
|
||||
******************************************************************************/
|
||||
// constructors ////////////////////////////////////////////////////////////////
|
||||
ThreadPool::ThreadPool(void)
|
||||
: ThreadPool(std::thread::hardware_concurrency())
|
||||
{}
|
||||
|
||||
ThreadPool::ThreadPool(const unsigned int nThreads)
|
||||
: nThreads_(nThreads)
|
||||
{
|
||||
for (unsigned int t = 0; t < nThreads_; ++t)
|
||||
{
|
||||
threads_.push_back(thread(&ThreadPool::workerLoop, this));
|
||||
}
|
||||
}
|
||||
|
||||
// destructor //////////////////////////////////////////////////////////////////
|
||||
ThreadPool::~ThreadPool(void)
|
||||
{
|
||||
terminate();
|
||||
}
|
||||
|
||||
// get the number of threads ///////////////////////////////////////////////////
|
||||
unsigned int ThreadPool::getThreadNum(void) const
|
||||
{
|
||||
return nThreads_;
|
||||
}
|
||||
|
||||
// get the pool mutex for synchronisation //////////////////////////////////////
|
||||
std::mutex & ThreadPool::getMutex(void)
|
||||
{
|
||||
return mutex_;
|
||||
}
|
||||
|
||||
// worker loop /////////////////////////////////////////////////////////////////
|
||||
void ThreadPool::workerLoop(void)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
Job job;
|
||||
{
|
||||
unique_lock<mutex> lock(mutex_);
|
||||
|
||||
condition_.wait(lock, [this](){
|
||||
return !queue_.empty() || terminatePool_;
|
||||
});
|
||||
if (terminatePool_ and queue_.empty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
job = queue_.front();
|
||||
queue_.pop();
|
||||
}
|
||||
job();
|
||||
}
|
||||
}
|
||||
|
||||
// add jobs ////////////////////////////////////////////////////////////////////
|
||||
void ThreadPool::addJob(Job newJob)
|
||||
{
|
||||
{
|
||||
unique_lock<mutex> lock(mutex_);
|
||||
|
||||
queue_.push(newJob);
|
||||
}
|
||||
condition_.notify_one();
|
||||
}
|
||||
|
||||
// critical section ////////////////////////////////////////////////////////////
|
||||
void ThreadPool::critical(Job fn)
|
||||
{
|
||||
unique_lock<mutex> lock(mutex_);
|
||||
|
||||
fn();
|
||||
}
|
||||
|
||||
// wait for completion /////////////////////////////////////////////////////////
|
||||
void ThreadPool::terminate(void)
|
||||
{
|
||||
{
|
||||
unique_lock<mutex> lock(mutex_);
|
||||
|
||||
terminatePool_ = true;
|
||||
}
|
||||
condition_.notify_all();
|
||||
for (auto &thread: threads_)
|
||||
{
|
||||
thread.join();
|
||||
}
|
||||
threads_.clear();
|
||||
}
|
56
lib/Core/ThreadPool.hpp
Normal file
56
lib/Core/ThreadPool.hpp
Normal file
@ -0,0 +1,56 @@
|
||||
/*
|
||||
* ThreadPool.hpp, part of LatAnalyze 3
|
||||
*
|
||||
* Copyright (C) 2013 - 2021 Antonin Portelli
|
||||
*
|
||||
* LatAnalyze 3 is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LatAnalyze 3 is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with LatAnalyze 3. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef Latan_ThreadPool_hpp_
|
||||
#define Latan_ThreadPool_hpp_
|
||||
|
||||
#include <LatAnalyze/Global.hpp>
|
||||
|
||||
class ThreadPool
|
||||
{
|
||||
public:
|
||||
typedef std::function<void(void)> Job;
|
||||
public:
|
||||
// constructors/destructor
|
||||
ThreadPool(void);
|
||||
ThreadPool(const unsigned int nThreads);
|
||||
virtual ~ThreadPool(void);
|
||||
// get the number of threads
|
||||
unsigned int getThreadNum(void) const;
|
||||
// get the pool mutex for synchronisation
|
||||
std::mutex & getMutex(void);
|
||||
// add jobs
|
||||
void addJob(Job newJob);
|
||||
// critical section
|
||||
void critical(Job fn);
|
||||
// wait for completion and terminate
|
||||
void terminate(void);
|
||||
private:
|
||||
// worker loop
|
||||
void workerLoop(void);
|
||||
private:
|
||||
unsigned int nThreads_;
|
||||
std::condition_variable condition_;
|
||||
std::vector<std::thread> threads_;
|
||||
bool terminatePool_{false};
|
||||
std::queue<Job> queue_;
|
||||
std::mutex mutex_;
|
||||
};
|
||||
|
||||
#endif
|
@ -24,6 +24,7 @@
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <complex>
|
||||
#include <condition_variable>
|
||||
#include <fstream>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
@ -40,6 +41,7 @@
|
||||
#include <stack>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <thread>
|
||||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
|
Reference in New Issue
Block a user