This forum has been archived. All content is frozen. Please use KDE Discuss instead.

y=2^x where x and y are vectors

Tags: None
(comma "," separated)
denisfitzpatrick
Registered Member
Posts
3
Karma
0

y=2^x where x and y are vectors

Thu May 17, 2012 6:27 pm
Is the a non for loop way of doing:

y=2^x

where x and y are Vectors/ 1d Arrays

Thanks,
Denis
vak
Registered Member
Posts
23
Karma
0
OS
exp(x*log(2)) ?
manuels
Registered Member
Posts
47
Karma
0

Re: y=2^x where x and y are vectors

Thu May 17, 2012 10:21 pm
Code: Select all
#include <Eigen/Core>
#include <functional>
#include <cmath>

#include <iostream>

int main() {
  Eigen::Vector3d y;
  Eigen::Vector3d x = Eigen::Vector3d::LinSpaced(1,3);

  y = x.unaryExpr(std::bind1st( std::ptr_fun(pow), 2) );

  std::cout << x << "\n\n" << y << std::endl;
}


Or a bit better to read (c++0x):
Code: Select all
#include <Eigen/Core>
#include <functional>
#include <cmath>

#include <iostream>

int main() {
  Eigen::Vector3d y;
  Eigen::Vector3d x = Eigen::Vector3d::LinSpaced(1,3);

  auto powTo2 = std::bind1st(std::ptr_fun(pow), 2);
  y = x.unaryExpr( powTo2 );

  std::cout << x << "\n\n" << y << std::endl;
}
denisfitzpatrick
Registered Member
Posts
3
Karma
0

Re: y=2^x where x and y are vectors

Fri May 18, 2012 3:58 pm
Thanks to both replies! Two different approaches and would have liked to accept both.
Denis


Bookmarks



Who is online

Registered users: Bing [Bot], claydoh, Google [Bot], rblackwell, Yahoo [Bot]