Currently, I've implemented:

template <class AE>
struct distance {
    typedef boost::mpl::if_c<boost::mpl::is_integral<typename AE::value_type>::value,
                             long double,
                             typename AE::value_type>::type type;
};

template <class AE>
typename distance<AE>::type mean_square(const boost::numeric::ublas::matrix_expression<AE> &me) {
    typename distance<AE>::type s(0);

    [...]

    return s / (me().size1() * me().size2());
}

However, to use 'distance' struct in compare_distance functions, I need the AE type, however in compare_distance function there are M1 and M2 matrices but not AE. Which expression I could use to get AE?

Joaquim Duran


2015-08-18 18:02 GMT+02:00 Nasos Iliopoulos <nasos_i@hotmail.com>:
Rethinking it it seems that if it is an integral type we should be using floating type (like long double) and if not we should be using the type itself. This would cover cases of using higher precision arithmetic (like using boost::multi-precision)

Something like:

#include <boost/mpl/if.hpp>
#include <boost/type_traits/is_integral.hpp>
...
typedef boost::mpl::if_c<boost::mpl::is_integral<typename AE::value_type>::value, long double,  typename AE::value_type>::type return_type;

-Nasos


On 08/18/2015 11:14 AM, Marco Guazzone wrote:
Don't know enough detail about the mean_square function, but what if
AE::value_type is a long double?

Maybe you could use type deduction like the following:

    typename ublas::promote_traits<typename AE::value_type,double>::promote_type

I've written the following example:

--- [snip] ---
#include <boost/numeric/ublas/traits.hpp>
#include <iostream>

void foo(long double x)
{
     (void)x;
     std::cout << "LONG DOUBLE" << std::endl;
}
void foo(double x)
{
     (void)x;
     std::cout << "DOUBLE" << std::endl;
}

int main()
{
     namespace ublas = boost::numeric::ublas;

     std::cout << "long double vs. double -> ";
     ublas::promote_traits<long double,double>::promote_type ld_d = 0;
     foo(ld_d);
     std::cout << std::endl;

     std::cout << "int vs. double -> ";
     ublas::promote_traits<int,double>::promote_type i_d = 0;
     foo(i_d);
     std::cout << std::endl;

     std::cout << "long vs. double -> ";
     ublas::promote_traits<long,double>::promote_type l_d = 0;
     foo(l_d);
     std::cout << std::endl;
}
--- [/snip] ---

I've compiled on my machine (Linux 64bit with GCC 5.1.1 20150618 (Red
Hat 5.1.1-4)) with flags "-Wall -Wextra -ansi -pedantic", and the
result is:

long double vs. double -> LONG DOUBLE

int vs. double -> DOUBLE

long vs. double -> DOUBLE


Marco

On Tue, Aug 18, 2015 at 4:35 PM, Nasos Iliopoulos <nasos_i@hotmail.com> wrote:
I was debating this myself. I think converting to double is a good idea.

-Nasos

you cast the type do double before.


On 08/10/2015 11:40 AM, Joaquim Duran Comas wrote:

Hello,

Few days ago, I patched the function 'mean_square' for vectors and matrices.
Currently, the current return type of the function is AE::value_type. To get
better accuracy, is it better return double type than AE::value_type
(specially if it is an integuer)?

Joaquim Duran



_______________________________________________
ublas mailing list
ublas@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: athanasios.iliopoulos.ctr.gr@nrl.navy.mil



_______________________________________________
ublas mailing list
ublas@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: marco.guazzone@gmail.com
_______________________________________________
ublas mailing list
ublas@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: athanasios.iliopoulos.ctr.gr@nrl.navy.mil

_______________________________________________
ublas mailing list
ublas@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: jdurancomas@gmail.com