Boost logo

Boost Users :

Subject: Re: [Boost-users] apply std::pow to vector or matrix
From: Max S. Kaznady (max.kaznady_at_[hidden])
Date: 2010-07-06 22:30:04


Hi Robert,

Yep, the following works great. Not sure why the same code doesn't
work when I have it in my larger project, but I guess that's for me to
figure out.

There is still the problem with the std::for_each, see below:

#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include "boost/bind.hpp"
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/io.hpp>

int main( )
{
  boost::numeric::ublas::vector<double> v(10);
  double exponent = 2.1;
  std::fill(v.begin(), v.end(), 8.0);
  // The following works just fine:
  std::transform( v.begin(), v.end(), v.begin(), boost::bind( pow, _1,
exponent ) );
  // But the for_each doesn't apply the power function, just leaves
the vector unaltered
  std::for_each( v.begin(), v.end(), boost::bind( pow, _1, exponent ) );
  // Print the result
  std::cout<< v << std::endl;

  return 0;
}

Compiled with: g++ -O2 -Wfatal-errors -Wall -g -ansi
-I/usr/local/boost/include -o main main.cpp

Cheers,
Max

On Tue, Jul 6, 2010 at 3:56 AM, Robert Jones <robertgbjones_at_[hidden]> wrote:
> 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 mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>


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