diff --git a/main.cpp b/main.cpp index 58661467..abe4c937 100644 --- a/main.cpp +++ b/main.cpp @@ -1,10 +1,44 @@ -#include "pip.h" +#include "picout.h" +#include "pivector2d.h" +#include "Eigen/Dense" + +typedef Eigen::MatrixXd PIMathMatrixd; +typedef Eigen::MatrixXf PIMathMatrixf; +typedef Eigen::MatrixXcf PIMathMatrixcf; +typedef Eigen::MatrixXcd PIMathMatrixcd; + +template +inline PICout operator <<(PICout s, const Eigen::DenseCoeffsBase & m) {s << '{'; for (uint r = 0; r < m.rows(); ++r) { for (uint c = 0; c < m.cols(); ++c) { s << m(r, c); if (c < m.cols() - 1 || r < m.rows() - 1) s << ", ";} if (r < m.rows() - 1) s << PICoutManipulators::NewLine << ' ';} s << '}'; return s;} +template +inline PIVector > toVectors(const Eigen::DenseCoeffsBase & m) { + PIVector > ret; + for(int i = 0; i < m.cols(); ++i) + ret << PIVector(&(m(i*m.rows())), m.rows()); + return ret; +} + +template +inline Eigen::Matrix fromVectors(const PIVector > & m) { + if (m.isEmpty()) return Eigen::Matrix(0, 0); + if (m[0].isEmpty()) return Eigen::Matrix(1, 0); + Eigen::Matrix ret(m[0].size(), m.size()); + for(int i = 0; i < m.size_s(); ++i) + for(int j = 0; j < m[i].size_s(); ++j) + ret(j,i) = m[i][j]; + return ret; +} +template +inline PIVector toPlainVector(const Eigen::DenseCoeffsBase & m) { + return PIVector(&(m(0)), m.size()); +} int main() { +// Eigen::MatrixXd; // PIMathMatrix m1(2,2, PIVector() << complexd(1.) << complexd(1.) << complexd(0.) << complexd(1.)); // PIMathMatrix m2(2,3, PIVector() << complexd(2., 3.) << complexd(2., 3.) << complexd(2., 3.) << complexd(2., 3.) << complexd(2., 3.) << complexd(2., 3.)); - PIMathMatrix m1(3,3, PIVector() << 1. << 2. << 3. << 4. << 6. << 5. << 9. << 8. << 7.); + PIMathMatrixd m1(3,3); + m1 << 1,2,3,4,6,5,9,8,7; // PIMathMatrix m2(2,3, PIVector() << 1. << 2. << complexd(2., 3.) << complexd(2., 3.) << complexd(2., 3.) << complexd(2., 3.)); // PIMathMatrix m3(vv); // piCout << m2 << PICoutManipulators::NewLine; @@ -18,6 +52,7 @@ int main() { // } // } piCout << m1 << PICoutManipulators::NewLine; + piCout << toPlainVector(m1); // piCout << m2 << PICoutManipulators::NewLine; // piCout << (PIVector2D)m2 << PICoutManipulators::NewLine; // piCout << m2.transposed() << PICoutManipulators::NewLine; @@ -27,17 +62,17 @@ int main() { // piCout << (m1*PIMathMatrix::matrixCol(v));``` // piCout << m2; // piCout << m3; - bool ok; - PIMathMatrix mi = m1.inverted(&ok); - piCout << mi << ok; - piCout << (mi * m1); - piCout << m1.row(0).toVector(); - piCout << m1.row(1).toVector(); - piCout << m1.row(2).toVector(); +// bool ok; + PIMathMatrixd mi = m1.inverse(); + piCout << mi; + piCout << PIMathMatrixd(mi * m1); + piCout << m1.row(0); + piCout << m1.row(1); + piCout << m1.row(2); piCout << ""; - piCout << m1.col(0).toVector(); - piCout << m1.col(1).toVector(); - piCout << m1.col(2).toVector(); + piCout << m1.col(0); + piCout << m1.col(1); + piCout << m1.col(2); //m2 = m2.transposed(); // piCout << m2 << PICoutManipulators::NewLine << hermitian(m2); // piCout << PICoutManipulators::NewLine << hermitian(m2) * mi; @@ -57,12 +92,21 @@ int main() { // v[r][c] = -v[r][c]; // } // } - PIMathMatrix v = PIMathMatrix(3,2, m1.toPlainVector().resize(6)); - PIMathMatrix v2 = PIMathMatrix(2,3, m1.toPlainVector().resize(6)); - piCout << v << PICoutManipulators::NewLine; - piCout << v.toVectors() << PICoutManipulators::NewLine; + piCout << "-------" << PICoutManipulators::NewLine; + PIMathMatrixd v = m1.block(0,0, 3,2); + m1 = v; +// PIMathMatrixd v2 = m1.block(0,0, 2,3); +// PIMathMatrixd v2(2,3, m1.toPlainVector().resize(6)); +// piCout << v << PICoutManipulators::NewLine; + piCout << m1 << PICoutManipulators::NewLine; + PIVector > vv = toVectors(m1); + piCout << vv << PICoutManipulators::NewLine; + piCout << fromVectors(vv) << PICoutManipulators::NewLine; +// piCout << toVectors(PIMathMatrixd(m1.transpose())) << PICoutManipulators::NewLine; // PIMathMatrix v2 = PIMathMatrix(v.toVectors()); - piCout << v2 << PICoutManipulators::NewLine; - piCout << v*v2 << PICoutManipulators::NewLine; +// piCout << v2 << PICoutManipulators::NewLine; +// piCout << v*v2 << PICoutManipulators::NewLine; +// piCout << v2*v << PICoutManipulators::NewLine; return 0; } + diff --git a/src_main/thread/pitimer.cpp b/src_main/thread/pitimer.cpp index 751e18a1..dddca2ca 100755 --- a/src_main/thread/pitimer.cpp +++ b/src_main/thread/pitimer.cpp @@ -148,13 +148,14 @@ public: private: class Pool: public PIThread { public: - Pool(); - ~Pool(); static Pool * instance(); void add(_PITimerImp_Pool * t); void remove(_PITimerImp_Pool * t); void run(); PIVector<_PITimerImp_Pool * > timers, to_remove; + private: + explicit Pool(); + virtual ~Pool(); }; virtual bool startTimer(double interval_ms); virtual bool stopTimer(bool wait); @@ -390,7 +391,7 @@ _PITimerImp_Pool::Pool * _PITimerImp_Pool::Pool::instance() { void _PITimerImp_Pool::Pool::add(_PITimerImp_Pool * t) { //piCout << "add ..."; lock(); - to_remove.removeOne(t); + to_remove.removeAll(t); if (!timers.contains(t)) timers << t; unlock(); @@ -408,14 +409,13 @@ void _PITimerImp_Pool::Pool::remove(_PITimerImp_Pool * t) { void _PITimerImp_Pool::Pool::run() { - piForeach (_PITimerImp_Pool * t, timers) - t->threadFunc(); if (!to_remove.isEmpty()) { piForeach (_PITimerImp_Pool * t, to_remove) - timers.removeOne(t); + timers.removeAll(t); to_remove.clear(); } - //while (t->threadFunc()); + piForeach (_PITimerImp_Pool * t, timers) + t->threadFunc(); } @@ -428,6 +428,9 @@ bool _PITimerImp_Pool::startTimer(double interval_ms) { bool _PITimerImp_Pool::stopTimer(bool wait) { Pool::instance()->remove(this); +// if (wait) { +// Pool::instance()->waitForDelete(this); +// } return true; }