Josh Pinto
Josh Pinto

Reputation: 1573

How to multiply a vector and scalar in C++?

I would like to multiply a vector with a scalar. This vector was created using the accepted answer to this question of mine namely:

std::vector<int> n(N + 1);
  std::iota(begin(n), end(n), 0);

and I would like to multiply this vector, n, with a scalar (specifically of type double, if it is relevant here) called npi.

I have seen this answer to a previous question here, but it wasn't all that helpful. The way I attempted to implement it was by adding:

std::transform(n.begin(), n.end(), n.begin(),
           std::bind1st(std::multiplies<T>(),pin));

to my C++ program. This returned the compile error:

error: ‘T’ was not declared in this scope
                std::bind1st(std::multiplies<T>(),pin));

I would like to call the vector created by multiplying this vector with a scalar npi, so please do not give me code that will call this new vector n (i.e., overwriting my existing n vector).

EDIT:

If it will placate whomever voted to close this question, here is my full program:

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <cmath>
#include <utility>
#include <unistd.h>
#include <algorithm>
#include <numeric>
/*#include <armadillo>*/

using namespace std;
/*using namespace arma;*/

double N  = 1000.0;
double x0 = 0;
double x1 = 100;
double pin = M_PI / double(N);

int main() {
  std::vector<int> n(N + 1);
  std::iota(begin(n), end(n), 0);
  std::transform(n.begin(), n.end(), n.begin(),
               std::bind1st(std::multiplies<T>(),pin));
  for(double i: n)
  {
    std::cout << i << '\n' << std::scientific;
  }
}

Upvotes: 1

Views: 15834

Answers (4)

Spandyie
Spandyie

Reputation: 945

You can pass the scalar in the capture clause of the Lambda function and do the multiplication inside the lambda function itself

    #include <algorithm>
    #include <vector>
    
    std::vector<int> foo; 
    std::vector<int> bar;
    auto npi=4.0;
    std::transform (foo.begin(), foo.end(), bar.begin(), foo.begin(), [&npi](auto& c){return c * npi;}

Upvotes: 0

M.M
M.M

Reputation: 141598

For vector<int> output, one way is:

auto npi = n;

for( auto& i: npi )
    i *= pin;

If npi should be vector<double> (not clear from the question) then replace the first line with:

std::vector<double> npi( n.begin(), n.end() );

Upvotes: 3

Jack Deeth
Jack Deeth

Reputation: 3357

You need to replace T by the type contained in the vector, in this case int. However you can probably simplify your code by using a lambda function here instead:

#include <algorithm> // for std::transform
#include <cmath>     // for M_PI
#include <iostream>  // for std::cout etc
#include <numeric>   // for std::iota
#include <vector>    // for awesome

int main() {
  std::vector<int> vec1(10);
  std::iota(vec1.begin(), vec1.end(), 0);

  int N = 42;

  std::vector<double> vec2(vec1.size()); // vec2 needs to be as big or bigger than vec1

  std::transform(vec1.begin(), vec1.end(), vec2.begin(),
                 [N](int i) { return i * M_PI / N; });

  for (auto a : vec1)
    std::cout << a << " ";
  std::cout << std::endl;

  for (auto a : vec2)
    std::cout << a << " ";
  std::cout << std::endl;
}

Here's an online example: http://melpon.org/wandbox/permlink/XrNxDND0steJmym8

Upvotes: 2

Vlad from Moscow
Vlad from Moscow

Reputation: 310990

If I have understood you correctly you need the following

std::vector<double> v;
v.reserve(n.size());

std::transform(n.begin(), n.end(), std::back_inserter( v ),
    std::bind1st(std::multiplies<double>(), pin));

Upvotes: 1

Related Questions