Problem compiling a function from Boost Library - c++

I'm trying to use the function norm_2_vector from boost.
But I'm getting the error ‘norm_2_square’ was not declared in this scope.
To compile I used the command below, where testNormSquare is the name of the program:
g++ -o testNorm2Square testNorm2Square.cpp
Question: Is there anything I'm missing? What should I do to make the code compilable?
A toy example that is not working is given below.
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/io.hpp>
int main () {
using namespace boost::numeric::ublas;
vector<double> v (3);
for (unsigned i = 0; i < v.size (); ++ i)
v(i) = i;
std::cout << 2.0 * v << std::endl;
std::cout << v * 2.0 << std::endl;
std::cout << norm_2_square(v);
}
The error message is the following:
testNorm2Square.cpp:12:18: error: ‘norm_2_square’ was not declared in this scope
Another problem happens if I specify explicitly that norm_2_square belongs to boost::numeric::ublas:
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/io.hpp>
int main () {
using namespace boost::numeric::ublas;
vector<double> v (3);
for (unsigned i = 0; i < v.size (); ++ i)
v(i) = i;
std::cout << 2.0 * v << std::endl;
std::cout << v * 2.0 << std::endl;
std::cout << boost::numeric::ublas::norm_2_square(v);
}
In this case the error message is:
testNorm2SquareV2.cpp:12:41: error: ‘norm_2_square’ is not a member of ‘boost::numeric::ublas’

Related

Boost::multiprecision and cardinal_cubic_b_spline

I'm new using the boost::multiprecision library and tried to use it combination with boost::math::interpolators::cardinal_cubic_b_spline however I can't compile the program.
The example code is
#include <boost/math/interpolators/cardinal_cubic_b_spline.hpp>
#include <iostream>
#include <boost/multiprecision/gmp.hpp>
using boost::multiprecision::mpf_float_50;
int main() {
std::vector<mpf_float_50> v(10);
mpf_float_50 step(0.01);
for (size_t i = 0; i < v.size(); ++i) {
v.at(i) = sin(i*step);
}
mpf_float_50 leftPoint(0.0);
boost::math::interpolators::cardinal_cubic_b_spline<mpf_float_50> spline(v.begin(), v.end(), leftPoint, step);
mpf_float_50 x(3.1);
mpf_float_50 tmpVal = spline(x);
std::cout << tmpVal << std::endl;
return 0;
}
When change the type of variables to boost::multiprecision::cpp_bin_float_50 the program is working. Also, boost::multiprecision::mpf_float_50 is working in all other examples I have tried.
The error I get is:
/home/..../main.cpp:19:31: required from here
/usr/include/boost/math/interpolators/detail/cardinal_cubic_b_spline_detail.hpp:50:10: error: conversion from ‘expression<boost::multiprecision::detail::function,boost::multiprecision::detail::abs_funct<boost::multiprecision::backends::gmp_float<50> >,boost::multiprecision::detail::expression<boost::multiprecision::detail::subtract_immediates, boost::multiprecision::number<boost::multiprecision::backends::gmp_float<50> >, long unsigned int, void, void>,[...],[...]>’ to non-scalar type ‘expression<boost::multiprecision::detail::subtract_immediates,boost::multiprecision::number<boost::multiprecision::backends::gmp_float<50> >,long unsigned int,[...],[...]>’ requested
The same error appeared for cpp_dec_float_50, mpfr_float_50 etc. I'm not sure what I'm doing wrong.
The selected type. is the GMP backend. To give it the usual operators, it is wrapped in the frontend template number<>:
Live On Coliru
using F = boost::multiprecision::mpf_float_50;
int main() {
F a = 3, b = 2;
F c = b - a;
std::cout << "a:" << a << ", b:" << b << ", c:" << c << std::endl;
b = abs(b - a);
std::cout << "a:" << a << ", b:" << b << ", c:" << c << std::endl;
}
Prints
a:3, b:2, c:-1
a:3, b:1, c:-1
However, the number<> enables expression templates by default. That means, typeof(F{} - F{}) is not necessarily F, but something like:
namespace mp = boost::multiprecision;
using F = mp::mpf_float_50;
int main() {
F a = 3, b = 2;
mp::detail::expression<mp::detail::subtract_immediates, F, F> //
c = b - a;
Template expressions can greatly optimize some code, e.g. by simplifying evaluation of complicated expressions.
However, some generic code doesn't deal well with the expression templates. Therefore you can turn them off:
namespace mp = boost::multiprecision;
using F = mp::number<mp::gmp_float<50>, mp::et_off>;
Now it all compiles, and probably works as it should.
Live On Coliru
#include <boost/math/interpolators/cardinal_cubic_b_spline.hpp>
#include <iostream>
#include <boost/multiprecision/gmp.hpp>
namespace mp = boost::multiprecision;
using F = mp::number<mp::gmp_float<50>, mp::et_off>;
int main() {
std::vector<F> v(10);
F step(0.01);
for (size_t i = 0; i < v.size(); ++i) {
v.at(i) = sin(i * step);
}
F leftPoint(0.0);
boost::math::interpolators::cardinal_cubic_b_spline<F> spline(v.begin(), v.end(), leftPoint, step);
F x(3.1);
F tmpVal = spline(x);
std::cout << tmpVal << std::endl;
}
Now printing:
0.0449663

What is an "exception handler state"?

Visual Studio's error C1509 says:
too many exception handler states in function
I'm curious as to exactly what an "exception handler state" is. It claims that the max number of states is: 215. However when I do not get this error when I run the file generated by this code, which has 215 + 1 try-catch blocks:
#include <fstream>
#include <iostream>
using namespace std;
int main() {
ofstream foo{ "main.cpp" };
const auto primer = "\tfoo = value(g);\n";
const auto body = "\ttry{\n"
"\t\tcout << 13.0F / foo << endl;\n"
"\t}catch(...){\n"
"\t\tcout << \"error\\n\";\n"
"\t}\n";
const auto count = (1 << 15) + 1;
if(foo)
{
foo << "#include <iostream>\n"
"#include <random>\n"
"\n"
"using namespace std;\n"
"\nint main(){\n"
"\tmt19937 g{ random_device()() };\n"
"\tuniform_int_distribution<> value{ 0, 13 };\n"
"\tauto foo = value(g);\n"
"\n"
<< body;
for(auto i = 1; i < count; ++i)
{
foo << endl << primer << body;
}
foo << '}' << endl;
}
}
So what is an exception handler state?

Why does cout not work when using uBLAS io?

I am using cygwin and I have boost 1.62.0 installed. I have compiled the following code using g++ and it functions as expected printing "Hi!" to the screen.
#include <iostream>
int main(){
std::cout << "Hi!" << std::endl;
}
When I try to compile the following code, I get nothing on the console. No "Hi!" and no printout of my vector 'v' that I created.
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/io.hpp>
int main () {
using namespace boost::numeric::ublas;
vector<double> v (3);
for (unsigned i = 0; i < v.size (); ++ i) {
v (i) = i;
}
std::cout << "Hi!" << std::endl;
std::cout << v << std::endl;
}
Even inserting #include <iostream> at the top doesn't help. Why is this happening?
UPDATE: I created a new, simplified test case with the following code:
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/matrix_proxy.hpp>
#include <boost/numeric/ublas/io.hpp>
int main(){
std::cout << "Hi!" << std::endl;
//boost::numeric::ublas::matrix<double> m (3, 3);
}
This program prints "Hi!" as expected and gives the return code "0" when I run $ echo $ at the prompt, but as soon as I uncomment the commented line, it doesn't output anything and the return code is "127".

GMP (GNU Multiple Precision) : "mpz_mod" function error

Can anyone help me with this?
The function "mpz_mod" is wrong but i don't know how to fix it.
#include <iostream>
#include <gmp.h>
#include <gmpxx.h>
using namespace std;
int main ()
{
mpz_class p;
mpz_class y;
mpz_class m;
for ( p=100 ; p<=500 ; p=p++)
{
for ( y=50 ; y<=60 ; y=y++)
{
mpz_mod (m,p,y);
}
if (m==0)
cout << p << "," << y << " ok " <<endl;
}
}
mpz_mod is for C code, and takes mpz_t * as arguments. Since you're using C++ and mpz_class, you want m = p % y;

c++ define vector in separate function

I want to define a vector with static content in a function that might be used by multiple programs. However I am facing some problems.
The main.cpp looked like this:
#include <iostream>
#include <boost/assign/std/vector.hpp>
using namespace std;
using namespace boost::assign;
int main (int argc, char* argv[]) {
vector< int > v;
v += 3,5,1;
for (int i=0; i<v.size(); i++) {
cout << "v: " << i << " " << v[i] << endl;
}
}
This works and I get as an output:
v: 0 3
v: 1 5
v: 2 1
However if I try to put the vector definition into a separate file it does not work.
main.cpp
#include <iostream>
#include "vector_test.hpp"
using namespace std;
int main (int argc, char* argv[]) {
vector< int > v;
for (int i=0; i<v.size(); i++) {
cout << "v: " << i << " " << v[i] << endl;
}
}
vector_test.hpp:
#include <boost/assign/std/vector.hpp>
#include <stdio.h>
using namespace std;
using namespace boost::assign;
static std::vector<int> v;
v += 3,5,1;
Trying to compile this gives me an error:
'v' does not name a type
and the qt creator also tells me:
expected a declaration
How do I fix this?
One way would be to have a function returning a reference to a static instance:
const std::vector<int>& get_magic_vector()
{
static std::vector<int> v{3, 5, 1};
return v;
}
Then
for (auto i : get_magic_vector())
std::cout << i << " ";
In your other files you must say that the variable exists but is defined in another file. That is, in otherFile.cpp you place the following line at file scope:
extern std::vector<int> v;