This simple test program...
#include <iostream>
#include <Eigen/Dense>
#include <unsupported/Eigen/MatrixFunctions>
using namespace Eigen;
int main()
{
Matrix<double,1,1> m11;
Matrix<double,2,2> m22;
Matrix<double,3,3> m33;
Matrix<double,4,4> m44;
m22.setZero();
m33.setZero();
m44.setZero();
std::cout << "Eigen version: " << EIGEN_WORLD_VERSION << "."
<< EIGEN_MAJOR_VERSION << "." << EIGEN_MINOR_VERSION << "\n";
std::cout << "11: " << m11.log() << "\n";
std::cout << "22: " << m22.log() << "\n";
std::cout << "33: " << m33.log() << "\n";
std::cout << "44: " << m44.log() << "\n";
}
hangs apparently while calculating m33.log(), giving the output:
Eigen version: 3.3.5
11: -728.932
22: -nan -nan
-nan -nan
[hangs here at 100% cpu]
with
g++ --version
g++ (GCC) 7.3.1 20180712 (Red Hat 7.3.1-6)
and also with
clang++ --version
clang version 5.0.2 (tags/RELEASE_502/final)
so I know it's invalid, but maybe a failure would be better than a hang, or do I really always need to check isInvertible() && all the eigenvalues before calling log() ?
Related
I'm using yaml-cpp with C++11. I can create a YAML file using something simple like this:
#include <yaml-cpp/yaml.h>
#include <iostream>
int main(void)
{
YAML::Node topNode;
topNode["one"]["two"]["A"] = "foo";
topNode["one"]["two"]["B"] = 42;
std::cout << "%YAML 1.2\n---\n" << topNode;
return 0;
}
That will produce a YAML file like this:
%YAML 1.2
---
one:
two:
A: foo
B: 42
Lovely!
I can also produce exactly the same YAML file like this:
#include <yaml-cpp/yaml.h>
#include <iostream>
int main(void)
{
YAML::Emitter out;
out << YAML::BeginMap // document {
<< "one"
<< YAML::BeginMap // one {
<< "two"
<< YAML::BeginMap // two {
<< YAML::Key << "A" << YAML::Value << "foo"
<< YAML::Key << "B" << YAML::Value << 42
<< YAML::EndMap // two }
<< YAML::EndMap // one }
<< YAML::EndMap // document }
;
std::cout << "%YAML 1.2\n---\n"
<< out.c_str();
return 0;
}
The nice thing about the second approach is that I can also add comments into the output file:
#include <yaml-cpp/yaml.h>
#include <iostream>
int main(void)
{
YAML::Emitter out;
out << YAML::BeginMap // document {
<< "one"
<< YAML::BeginMap // one {
<< "two"
<< YAML::BeginMap // two {
<< YAML::Key << "A" << YAML::Value << "foo"
<< YAML::Comment("A should be 'foo'")
<< YAML::Key << "B" << YAML::Value << 42
<< YAML::Comment("B is meaningful")
<< YAML::EndMap // two }
<< YAML::EndMap // one }
<< YAML::EndMap // document }
;
std::cout << "%YAML 1.2\n---\n"
<< out.c_str();
return 0;
}
to produce:
%YAML 1.2
---
one:
two:
A: foo # A should be 'foo'
B: 42 # B is meaningful
My question if there is a way to add comments into the first approach? Perhaps something like this:
topNode["one"]["two"]["A"] = "foo";
topNode["one"]["two"]["A"].addComment("A should be 'foo'");
I could subclass YAML::Node, adding my addComment() method, but I don't want to re-write all of YAML::Emitter to get my comment appended appropriately. The code is there, but I don't know how to get to it. How? Can you point me to an example or an approach?
I understand that the YAML specification says that comments are not an integral part of a YAML file, and can be discarded. My users find them useful, so I don't relish a debate that begins with "Your question is stupid." :-)
That is not possible with the current API. The Emitter uses an EventHandler which, as you can see, is not able to emit comments.
The Emit function that creates the events does not create any comment events via other means either.
Since operator<< on Node will internally use the Emitter class, there's no way to emit comments by adding them to a node, unless you rewrite the emitter yourself.
I'd like to use Boost.Fibers hosted by worker threads instead of just threads. I think I've done everything as in the manual while writing the code shown below, but it seems not to work – "<anonymous> called" is missing in the output.
Does anyone have an idea why?
#include <boost/fiber/all.hpp>
#include <boost/thread/barrier.hpp>
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <thread>
#define RIGHTNOW (std::cerr << "[thread " << std::this_thread::get_id() << "] ")
static inline
void run_worker(uint32_t threads, boost::barrier& barrier) {
RIGHTNOW << "run_worker(" << threads << ", " << (void*)&barrier << ") called" << std::endl;
boost::fibers::use_scheduling_algorithm<boost::fibers::algo::work_stealing>(threads);
barrier.wait();
RIGHTNOW << "run_worker(" << threads << ", " << (void*)&barrier << ") awaited everything else" << std::endl;
boost::fibers::mutex mutex;
boost::fibers::condition_variable_any cv;
mutex.lock();
RIGHTNOW << "run_worker(" << threads << ", " << (void*)&barrier << ") locked mutex" << std::endl;
cv.wait(mutex);
RIGHTNOW << "run_worker(" << threads << ", " << (void*)&barrier << ") awaited CV" << std::endl;
mutex.unlock();
}
void start_engine(uint32_t threads) {
RIGHTNOW << "start_engine(" << threads << ") called" << std::endl;
if (threads < 1u) {
threads = 1;
}
boost::barrier barrier (threads + 1u);
auto scheduler ([threads, &barrier]() { run_worker(threads, barrier); });
for (auto i (threads); i; --i) {
std::thread(scheduler).detach();
}
barrier.wait();
RIGHTNOW << "start_engine(" << threads << ") awaited everything else" << std::endl;
}
int main() {
RIGHTNOW << "main() called" << std::endl;
start_engine(1);
boost::fibers::fiber([]() {
RIGHTNOW << "<anonymous> called" << std::endl;
_Exit(1);
}).detach();
RIGHTNOW << "main() waiting" << std::endl;
for (;;) {
}
}
clang++ -std=c++11 -I ~/homebrew/Cellar/boost/1.70.0/include -L ~/homebrew/Cellar/boost/1.70.0/lib -l boost_fiber-mt -l boost_context-mt -l boost_thread-mt -o fibers fibers.cpp
./fibers
Actually I couldn't see what's my mistake in my code but std::binary_search returns false for the element that have already added to vec_barcode. For comparison I tried std::find method & it works properly.
std::find(vec_barcode.begin(), vec_barcode.end(), "LOGUS") != vec_barcode.end() ? std::cout << "yes" << std::endl : std::cout << "no" << std::endl;
std::binary_search(vec_barcode.begin(), vec_barcode.end(), "LOGUS") ? std::cout << "yes" << std::endl : std::cout << "no" << std::endl;
Any help is appreciated.
I am newbie to mfc, and I got struck over how to get the current operating system language (Ex: If it is English operating system I must get it as English and locale can be different. For English OS locale can be Japanese vice versa).
Current locale I am getting it through GetSystemDefaultLangID and the only thing I was left with is I need to get the current operating system language.
Can anyone kindly help me to resolve this issue.
Perhaps you need GetUserDefaultUILanguage. The system's settings and user settings may not be the same.
User Interface Language Management
int wmain()
{
wcout << "GetUserDefaultUILanguage: " << GetUserDefaultUILanguage() << "\n";
wcout << "GetSystemDefaultUILanguage: " << GetSystemDefaultUILanguage() << "\n";
wcout << "\n";
wcout << "GetUserDefaultLangID: " << GetUserDefaultLangID() << "\n";
wcout << "GetSystemDefaultLangID: " << GetSystemDefaultLangID() << "\n";
wcout << "\n";
wcout << "GetUserDefaultLCID: " << GetUserDefaultLCID() << "\n";
wcout << "GetSystemDefaultLCID: " << GetSystemDefaultLCID() << "\n";
wcout << "\n";
wchar_t buf[100];
LCID lcid = GetUserDefaultLCID();
cout << "GetUserDefaultLCID: " << "\n";
if (GetLocaleInfo(lcid, LOCALE_ILANGUAGE, buf, 100)) wcout << buf << "\n";
if (GetLocaleInfo(lcid, LOCALE_SENGLANGUAGE, buf, 100)) wcout << buf << "\n";
if (GetLocaleInfo(lcid, LOCALE_SISO639LANGNAME, buf, 100)) wcout << buf << "\n";
return 0;
}
For the following class, INTEL 2013 (update 3) and GCC 4.7.2 give different type_traits results. Which one is right?
#include <iostream>
#include <type_traits>
using namespace std;
class A
{
public:
A() = default;
private:
double t_;
};
int main()
{
cout << boolalpha;
cout << "is_trivial<A> : " << is_trivial<A>::value << endl;
cout << "is_compound<A> : " << is_compound<A>::value << endl;
cout << "is_pod<A> : " << is_pod<A>::value << endl;
cout << "is_standard_layout<A> : " << is_standard_layout<A>::value << endl;
cout << "is_literal_type<A> : " << is_literal_type<A>::value << endl;
return 0;
}
INTEL output:
is_trivial<A> : true
is_compound<A> : true
is_pod<A> : false
is_standard_layout<A> : true
is_literal_type<A> : false
GCC output:
is_trivial<A> : true
is_compound<A> : true
is_pod<A> : true
is_standard_layout<A> : true
is_literal_type<A> : true
I would say GCC is correct. is_pod is true if it's both is_trivial and is_standard_layout: http://en.cppreference.com/w/cpp/types/is_pod . Intel compiler doesn't comply with this. is_literal_type should also be true since all the conditions seem valid for A: http://en.cppreference.com/w/cpp/types/is_literal_type