Boost logo

Boost Users :

Subject: Re: [Boost-users] apply std::pow to vector or matrix
From: Robert Jones (robertgbjones_at_[hidden])
Date: 2010-07-06 03:56:10


On Mon, Jul 5, 2010 at 9:18 PM, Max S. Kaznady <max.kaznady_at_[hidden]>wrote:

> I followed the UBLAS extensions here:
>
> http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl?Examples_-_How_To_Extend_UBLAS
> and I figured out how to extend functions like "exp" and "log" to act
> on a vector.
>
> But, those are functions of one argument. What I want is to be able to
> apply a function with one fixed argument, like std::pow, to a vector
> for some exponent exp, so I would like the code to look like:
> vector<double> v( 100 );
> double exponent = 2.1; // some exponent value
> // ... fill v ...
> std::cout << apply_to_all( v, functor::my_pow<double>(exponent) ) <<
> std::endl;
> std::cout << apply_to_all<functor::my_pow<double>(exponent) >( v ) <<
> std::endl;
> OR
> std::cout << apply_to_all( v, functor::my_pow<double>() , exponent) <<
> std::endl;
>
> How can I do this? I tried overloading apply_to_all and writing
> constructor code, but it didn't work because the return type of the
> apply function is static.
>
> P.S. I tried posting this to ublas lists, but got a message saying
> that my e-mail address is not registered. I guess that list is only
> for developers.
>

Hi Max

I'm not familiar with Ublas, but does this achieve the effect you're after

#include <vector>
#include <algorithm>
#include <math.h>
#include "boost/bind.hpp"

int main( )
{
    std::vector<double> v;
    double exponent = 2.1;
    std::transform( v.begin(), v.end(), v.begin(), boost::bind( pow, _1,
exponent ) );
}

Regards,

- Rob.



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net