Return type of 'mean_square'

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Return type of 'mean_square'

Joaquim Duran Comas
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
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Return type of 'mean_square'

Nasos Iliopoulos
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
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: [hidden email]


_______________________________________________
ublas mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Return type of 'mean_square'

Marco Guazzone
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 <[hidden email]> 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
> [hidden email]
> http://lists.boost.org/mailman/listinfo.cgi/ublas
> Sent to: [hidden email]
>
>
>
> _______________________________________________
> ublas mailing list
> [hidden email]
> http://lists.boost.org/mailman/listinfo.cgi/ublas
> Sent to: [hidden email]
_______________________________________________
ublas mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Return type of 'mean_square'

Nasos Iliopoulos
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 <[hidden email]> 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
>> [hidden email]
>> http://lists.boost.org/mailman/listinfo.cgi/ublas
>> Sent to: [hidden email]
>>
>>
>>
>> _______________________________________________
>> ublas mailing list
>> [hidden email]
>> http://lists.boost.org/mailman/listinfo.cgi/ublas
>> Sent to: [hidden email]
> _______________________________________________
> ublas mailing list
> [hidden email]
> http://lists.boost.org/mailman/listinfo.cgi/ublas
> Sent to: [hidden email]

_______________________________________________
ublas mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Return type of 'mean_square'

Joaquim Duran Comas
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 <[hidden email]>:
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 <[hidden email]> 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
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: [hidden email]



_______________________________________________
ublas mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: [hidden email]
_______________________________________________
ublas mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: [hidden email]

_______________________________________________
ublas mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: [hidden email]


_______________________________________________
ublas mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Return type of 'mean_square'

Nasos Iliopoulos
I am not sure which part you are referring to but I believe it should should be something along those lines:

typedef ublas::promote_traits<typename M1::value_type, typename M1::value_type>::promote_type intermediate_t;

typedef boost::mpl::if_c<boost::mpl::is_integral<intermediate_t>::value,
                             long double,
                             intermediate_t>::type type;

-Nasos


On 09/16/2015 08:08 AM, Joaquim Duran Comas wrote:
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 <[hidden email]>:
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 <[hidden email]> 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
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: [hidden email]



_______________________________________________
ublas mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: [hidden email]
_______________________________________________
ublas mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: [hidden email]

_______________________________________________
ublas mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: [hidden email]



_______________________________________________
ublas mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: [hidden email]


_______________________________________________
ublas mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/ublas
Sent to: [hidden email]