boost::multiprecision::number - c++

I'm new in using boost library, so I thanked every one that helps me.
#include "gmpxx.h"
#include <boost/multiprecision/gmp.hpp>
#include <boost/multiprecision/random.hpp>
#include <boost/multiprecision/number.hpp>
using namespace std;
using namespace boost::multiprecision;
using boost::multiprecision::mpz_int;
using boost::multiprecision::mpq_rational;
using boost::multiprecision::uint128_t;
using boost::multiprecision::uint256_t;
using boost::multiprecision::uint512_t;
using boost::multiprecision::uint1024_t;
using boost::multiprecision::mpf_float;
void testtmultiprecisions(){
mpq_rational b = boost::multiprecision::pow(mpf_float(1), mpf_float(2));
std::cout << typeid(b).name() << std::endl;
}
but i get compiler error like below :
error: conversion from boost::enable_if_c<true,
boost::multiprecision::detail::expression
<boost::multiprecision::detail::function,
boost::multiprecision::detail::pow_funct
<boost::multiprecision::backends::gmp_float<0u> >,
boost::multiprecision::number
<boost::multiprecision::backends::gmp_float<0u> >,
boost::multiprecision::number
<boost::multiprecision::backends::gmp_float<0u> >, void>
>::type {aka boost::multiprecision::detail::expression
<boost::multiprecision::detail::function,
boost::multiprecision::detail::pow_funct
<boost::multiprecision::backends::gmp_float<0u> >,
boost::multiprecision::number
<boost::multiprecision::backends::gmp_float<0u> >,
<boost::multiprecision::backends::gmp_float<0u> >, void>}
to non-scalar type â€کboost::multiprecision::mpq_rational {aka
boost::multiprecision::number
<boost::multiprecision::backends::gmp_rational,
(boost::multiprecision::expression_template_option)1u>}
requested
mpq_rational b = boost::multiprecision::pow(mpf_float(1), mpf_float(2));
i also changed above code :
mpq_rational b = boost::multiprecision::pow(mpf_float(1),
mpf_float(2)).covert_to<mpq_rational>();
and get new compiler errors :
[compiler error]

Rationals, by definition, have integral numerator and denominator.
Conversion of floating point to integral numbers is - again by definition - not lossless. The compiler will only compile conversions that are statically known to be lossless.
Also, conversions will not be chained (how would the library resolve ambiguous conversion paths?). In this case, you could help the compiler out using the intermediate step converting to the underlying type for mpq_rational (namely mpz_int):
#include <boost/multiprecision/gmp.hpp>
#include <boost/multiprecision/number.hpp>
#include <iostream>
using boost::multiprecision::mpq_rational;
using boost::multiprecision::mpz_int;
using boost::multiprecision::mpf_float;
int main() {
mpq_rational b = boost::multiprecision::pow(mpf_float(2), mpf_float(3)).convert_to<mpz_int>();
std::cout << b << "\n";
}
Prints
8

Related

boost multiprecision + numeric bindings + LAPACK

I'm trying to implement some numeric procedures using the boost multiprecision template library in combination with numeric bindings for a lapack backend.
However, the template resolution doesn't seem to work as intended. I've been able to narrow it down to this minimal example:
#include <boost/numeric/ublas/symmetric.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/numeric/bindings/traits/ublas_matrix.hpp>
#include "boost/numeric/bindings/lapack/syev.hpp"
#include "boost/numeric/bindings/lapack/workspace.hpp"
typedef boost::multiprecision::number<boost::multiprecision::cpp_dec_float<100> > SuperFloat;
typedef std::numeric_limits<boost::multiprecision::cpp_dec_float<100> > SuperFloatPrecision;
typedef boost::numeric::ublas::matrix<SuperFloat> Matrix;
typedef boost::numeric::ublas::symmetric_matrix<SuperFloat> MatrixSym;
inline int diagonalize(Matrix& eigenvectors, boost::numeric::ublas::vector<SuperFloat>& eigenvalues) {
int r = boost::numeric::bindings::lapack::syev( 'U', eigenvectors, eigenvalues, boost::numeric::bindings::lapack::minimal_workspace() );
return r;
}
int main(){
std::cout << "hello!" << std::endl;
return 0;
}
which yields this error message:
boost/numeric/bindings/lapack/syev.hpp: In instantiation of ‘int boost::numeric::bindings::lapack::syev(char, A&, W&, boost::numeric::bindings::lapack::minimal_workspace) [with A = boost::numeric::ublas::matrix<boost::multiprecision::number<boost::multiprecision::backends::cpp_dec_float<100u> > >; W = boost::numeric::ublas::vector<boost::multiprecision::number<boost::multiprecision::backends::cpp_dec_float<100u> > >]’:
test.cxx:13:137: required from here
boost/numeric/bindings/lapack/syev.hpp:163:8: error: invalid application of ‘sizeof’ to incomplete type ‘boost::STATIC_ASSERTION_FAILURE<false>’
BOOST_STATIC_ASSERT( (boost::mpl::or_< boost::is_same< matrix_structure, traits::symmetric_t >
^
In file included from ./boost/numeric/bindings/traits/traits.hpp:18:0,
from ./boost/numeric/bindings/traits/ublas_matrix.hpp:17,
from test.cxx:3:
./boost/numeric/bindings/traits/matrix_traits.hpp: In instantiation of ‘char boost::numeric::bindings::traits::matrix_uplo_tag(SymmM&) [with SymmM = boost::numeric::ublas::matrix<boost::multiprecision::number<boost::multiprecision::backends::cpp_dec_float<100u> > >]’:
boost/numeric/bindings/lapack/syev.hpp:181:47: required from ‘int boost::numeric::bindings::lapack::syev(char, A&, W&, boost::numeric::bindings::lapack::minimal_workspace) [with A = boost::numeric::ublas::matrix<boost::multiprecision::number<boost::multiprecision::backends::cpp_dec_float<100u> > >; W = boost::numeric::ublas::vector<boost::multiprecision::number<boost::multiprecision::backends::cpp_dec_float<100u> > >]’
test.cxx:13:137: required from here
./boost/numeric/bindings/traits/matrix_traits.hpp:141:56: error: no type named ‘uplo_type’ in ‘struct boost::numeric::bindings::traits::matrix_traits<boost::numeric::ublas::matrix<boost::multiprecision::number<boost::multiprecision::backends::cpp_dec_float<100u> > > >’
typedef typename matrix_traits<SymmM>::uplo_type uplo_t;
Is it at all possible to use lapack as a backend for boost UBLAS templated to multiprecision, or is this a futile endeavour?
Are there any other ways to do, say, an eigenvector decomposition of a ublas multiprecision matrix?

What's wrong of this use of boost::lambda::bind?

I'm trying to use boost::lambda::bind() to define a predicate that I pass to the find_if algorithm in Boost.Range. Specifically, I want to search a vector of structures to find the first entry where a particular member has a specified value. My example is as follows:
#include <boost/lambda/bind.hpp>
#include <boost/range/algorithm/find_if.hpp>
#include <vector>
using namespace std;
using namespace boost;
using namespace boost::lambda;
struct foo
{
string s;
int x;
};
int main()
{
// create list and add a couple entries
vector<foo> fooList;
foo f1 = {"abc", 1};
foo f2 = {"def", 2};
fooList.push_back(f1);
fooList.push_back(f2);
// search for a value with the desired member
// fails with a compile error!
range_iterator<vector<foo> > it = find_if(fooList, boost::lambda::bind(&foo::s, _1) == string("abc"));
return 0;
}
When I try to compile this (under gcc 4.7.2), I get the typical spew of template instantiation errors, indicating that there was no operator== found that is compatible with the type returned by bind() and a const char []. I've tried this with other types also, such as int, with the same result.
I must be missing some small detail of bind() usage, but I can't see it; it seems like this sort of thing should work based upon the documentation. Am I wrong there?
Edit: Here is the first part of the compiler output:
test.cc:24:92: error: no match for ‘operator==’ in ‘boost::lambda::bind(const Arg1&, const Arg2&) [with Arg1 = std::basic_string<char> foo::*; Arg2 = boost::lambda::lambda_functor<boost::lambda::placeholder<1> >; typename boost::lambda::detail::bind_tuple_mapper<const Arg1, const Arg2>::type = boost::tuples::tuple<std::basic_string<char> foo::* const, const boost::lambda::lambda_functor<boost::lambda::placeholder<1> >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type>]((* & boost::lambda::{anonymous}::_1)) == "abc"’
Turns out that I wasn't including the required headers. It appears that <boost/lambda/bind.hpp> only brings in bind functionality, and the operator overloads for the resulting type are not included. If I add #include <boost/lambda/lambda.hpp> to the above, then it resolves the compiler error that I referenced. The final revised code (fixing another error in the type of the return value from find_if()) is as follows:
#include <boost/lambda/bind.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/range/algorithm/find_if.hpp>
#include <string>
#include <vector>
using namespace std;
using namespace boost;
using namespace boost::lambda;
struct foo
{
string s;
int x;
};
int main()
{
// create list and add a couple entries
vector<foo> fooList;
foo f1 = {"abc", 1};
foo f2 = {"def", 2};
fooList.push_back(f1);
fooList.push_back(f2);
// search for a value with the desired member
typename range_iterator<vector<foo> >::type it = find_if(fooList, bind(&foo::s, _1) == "abc");
return 0;
}

no matching function for call to ‘find'

I have the following code:
#include <algorithm>
#include <vector>
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
typedef vector<int> IntContainer;
typedef IntContainer::iterator IntIterator;
IntContainer vw;
IntIterator i = find(vw.begin(), vw.end(), 5);
if (i != vw.end())
{
printf("Find 5 in vector\n"); // found it
}
else
{
printf("Couldn't find 5 in vector\n"); // couldn't found it
}
return 0;
}
I try to compile it on Ubuntu with gcc 4.7.1 and get the following error:
vec_test.cpp: In function ‘int main()’:
vec_test.cpp:27:46: error: no matching function for call to ‘find(std::vector<int>::iterator, std::vector<int>::iterator, int)’
vec_test.cpp:27:46: note: candidate is:
In file included from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.1/../../../../include/c++/4.7.1/bits/locale_facets.h:50:0,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.1/../../../../include/c++/4.7.1/bits/basic_ios.h:39,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.1/../../../../include/c++/4.7.1/ios:45,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.1/../../../../include/c++/4.7.1/ostream:40,
from /usr/local/lib/gcc/i686-pc-linux-gnu/4.7.1/../../../../include/c++/4.7.1/iostream:40,
from vec_test.cpp:3:
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.1/../../../../include/c++/4.7.1/bits/streambuf_iterator.h:371:5: note: template<class _CharT2> typename __gnu_cxx::__enable_if<std::__is_char<_CharT2>::__value, std::istreambuf_iterator<_CharT2, std::char_traits<_CharT> > >::__type std::find(std::istreambuf_iterator<_CharT2, std::char_traits<_CharT> >, std::istreambuf_iterator<_CharT2, std::char_traits<_CharT> >, const _CharT2&)
/usr/local/lib/gcc/i686-pc-linux-gnu/4.7.1/../../../../include/c++/4.7.1/bits/streambuf_iterator.h:371:5: note: template argument deduction/substitution failed:
vec_test.cpp:27:46: note: ‘__gnu_cxx::__normal_iterator<int*, std::vector<int> >’ is not derived from ‘std::istreambuf_iterator<_CharT2, std::char_traits<_CharT> >’
This code doesn't do anything since the vector is not initialized with any content but it should compile.
I suspect this is a gcc problem but after lots of digging I'm quit desperate.
Please let me know if anyone encountered this problem and knows how to solve it.
Maybe some file defines a function find in global namespace?
Have you tried specifying full-scope? That's to say, std::find instead of find. It could help deleting that weird line:
using namespace std;
However, it is not what I would expect. I would call it a bug.

Compiler error of incomplete variable type

Starting the the error, which is showed by building the following code:
Testfile.cpp:27:41: error: variable ‘boost::numeric::ublas::matrix_column<boost::numeric::ublas::bounded_matrix<double, 2u, 2u> > op1’ has initialiser but incomplete type.
Please condider the following code:
//! System includes
#include <iostream>
#include <fstream>
//! Boost includes
#include <boost/lexical_cast.hpp>
#include <boost/integer/static_min_max.hpp>
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/array.hpp>
int main( )
{
namespace ublas = boost::numeric::ublas;
typedef ublas::bounded_matrix<double,2,2> MatDofDdim;
typedef ublas::bounded_vector<double,2> VecDof;
MatDofDdim op;
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
op(i,j)=i+j;
}
}
//VecDof op1;
ublas::matrix_column<MatDofDdim> op1 ( op, 1 ) ;
//VecDof op1( ublas::matrix_column<MatDofDdim>( op, 1 ));
return 0;
}
I also tried an different approach with comment the line "ublas::matrix_column<MatDofDdim> op1 ( op, 1 ) ;" and with uncomment the preceeding and following line.
Then it says:
Testfile.cpp:29:48: error: redeclaration of ‘VecDof op1’
Testfile.cpp:26:9: error: ‘VecDof op1’ previously declared here
Testfile.cpp:29:54: error: invalid use of incomplete type ‘struct boost::numeric::ublas::matrix_column<boost::numeric::ublas::bounded_matrix<double, 2u, 2u> >’
/usr/include/boost/numeric/ublas/fwd.hpp:75:11: error: declaration of ‘struct boost::numeric::ublas::matrix_column<boost::numeric::ublas::bounded_matrix<double, 2u, 2u> >’
I use eclipse in ubuntu with g++.
matrix_column is declared in <boost/numeric/ublas/matrix_proxy.hpp>
http://www.boost.org/doc/libs/1_51_0/libs/numeric/ublas/doc/matrix_proxy.htm#matrix_column

C++ Boost::MPL fold example - wrong number of arguments

I'd like to process some template arguments by using boost::mpl::fold. At the moment, I'm still stuck to the sample provided by Boost as even that does not work for me. I get the following error:
..\src\main.cpp:18:32: error: template argument 2 is invalid
..\src\main.cpp:18:37: error: wrong number of template arguments (4, should be 3)
The following code is taken from http://www.boost.org/doc/libs/1_48_0/libs/mpl/doc/refmanual/fold.html
#include <string>
#include <iostream>
#include <boost/mpl/fold.hpp>
#include <boost/mpl/plus.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/type_traits.hpp>
using namespace std;
using namespace boost;
using namespace boost::mpl;
using namespace boost::type_traits;
typedef vector<long,float,short,double,float,long,long double> types;
typedef fold<
types
, int_<0>
, if_< is_float<_2>,next<_1>,_1 >
>::type number_of_floats;
BOOST_MPL_ASSERT_RELATION( number_of_floats::value, ==, 4 );
int main(){
}
I'm running mingw 4.7.0 using the flag "-std=c++11". I found some other examples on the net but have not yet been successful in compiling anything useful. Any suggestions?
You are messing up the namespaces. Making a lot of symbols ambiguous.
Remove the using and the example works fine for me.
...
using namespace boost;
typedef mpl::vector<long,float,short,double,float,long,long double> types;
typedef mpl::fold<
types
, mpl::int_<0>
, mpl::if_< is_float<boost::mpl::_2>,boost::mpl::next<boost::mpl::_1>,boost::mpl::_1 >
>::type number_of_floats;
...