2

I have 2 vectors and want to distribute one across the other to form a third vector like:

V1 = (a,b,c)
V2 = (d,e,f)

Result:

V3 = (ad,ae,af,bd,be,bf,...cf) 'nine total elements

The only way I know how to do it is by looping. I've tried searching a number of different ways, and can't find a 'one line of code' solution, to avoid looping.

If I've missed it, please point me to it. I may have not found the right search parameters.

If it is not possible, please spare me my misery and let me know :,,,(.

If there is an answer, please share it.

πάντα ῥεῖ
  • 83,259
  • 13
  • 96
  • 175
Michaelk87
  • 21
  • 1
  • 2
    Pick a specific language first please. – πάντα ῥεῖ Jun 16 '17 at 13:16
  • Are `ad` and the others multiplication of two real numbers, concatenation of two strings, joining into ordered pairs, or something else? – Rory Daulton Jun 16 '17 at 13:26
  • Considering, that `V3` is actually the matrix product of `V1` and `V2` (if matrix is stored row by row) and assuming that the elements are `int`, `float`, or `double`, you may have a look at the [**Eigen library**](http://eigen.tuxfamily.org/index.php?title=Main_Page). For Intels x86 architecture, it uses (or at least, can use) [SSE instructions](https://en.wikipedia.org/wiki/Streaming_SIMD_Extensions) to gain performance. It is implemented in **C++** and has bindings for a variety of languages among others **R** and **Python**. – Scheff's Cat Jun 16 '17 at 16:11

3 Answers3

1

You do not make clear what operation ab means. I'll assume here you want to multiply two real numbers.

In Python, you can use a comprehension. Here a complete code snippet.

v1 = (2, 3, 5)
v2 = (7, 11, 13)
v3 = tuple(x * y for x in v1 for y in v2)

The value for v3 is then

(14, 22, 26, 21, 33, 39, 35, 55, 65)

as wanted. If you want a Python list, the code looks easier: use

v3 = [x * y for x in v1 for y in v2]

It will be obvious how to change the operation to concatenation or anything else desired. Here is sample code for concatenation of strings:

v1 = ('a', 'b', 'c')
v2 = ('d', 'e', 'f')
v3 = tuple(x + y for x in v1 for y in v2)

which results in

('ad', 'ae', 'af', 'bd', 'be', 'bf', 'cd', 'ce', 'cf')

You could also use product() from the itertools module (which I used in the first version of this answer) but the above seems easier.

Rory Daulton
  • 19,351
  • 5
  • 34
  • 45
0

In R:
as.vector(sapply(V1, function(x) paste0(x, V2)))

emilliman5
  • 5,270
  • 3
  • 20
  • 33
0

In C++:

std::vector<decltype(v1.front() * v2.front())> v3 { v1[0] * v2[0], v1[0] * v2[1], v1[0] * v2[2], v1[1] * v2[0], v1[1] * v2[1], v1[1] * v2[2], v1[2] * v2[0], v1[2] * v2[1], v1[2] * v2[2] };

...though, I usually would format it in multiple lines.

As MCVE:

#include <iostream>
#include <vector>
using namespace std;

template <typename ELEM>
ostream& operator << (ostream &out, const vector<ELEM> &vec)
{
  const char *sep = "{ ";
  for (const ELEM &elem : vec) { out << sep << elem; sep = ", "; }
  return out << " }";
}

int main()
{
  vector<double> v1{ 1.0, 2.0, 3.0 }, v2{ 4.0, 5.0, 6.0 };
  // in one line:
  vector<decltype(v1.front() * v2.front())> v3 { v1[0] * v2[0], v1[0] * v2[1], v1[0] * v2[2], v1[1] * v2[0], v1[1] * v2[1], v1[1] * v2[2], v1[2] * v2[0], v1[2] * v2[1], v1[2] * v2[2] };
  // output:
  cout << v3 << endl;
  // done
  return 0;
}

Output:

{ 4, 5, 6, 8, 10, 12, 12, 15, 18 }

(Tested on ideone.)

Thanks, Rory Daulton, for the inspiration. In C++, I can do this also:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

string operator*(const string &str1, const string &str2)
{
  return str1 + str2;
}

template <typename ELEM>
ostream& operator << (ostream &out, const vector<ELEM> &vec)
{
  const char *sep = "{ ";
  for (const ELEM &elem : vec) { out << sep << elem; sep = ", "; }
  return out << " }";
}

int main()
{
  vector<string> v1{ "a", "b", "c" }, v2{ "d", "e", "f" };
  // in one line:
  vector<decltype(v1.front() * v2.front())> v3 { v1[0] * v2[0], v1[0] * v2[1], v1[0] * v2[2], v1[1] * v2[0], v1[1] * v2[1], v1[1] * v2[2], v1[2] * v2[0], v1[2] * v2[1], v1[2] * v2[2] };
  // output:
  cout << v3 << endl;
  // done
  return 0;
}

Output:

{ ad, ae, af, bd, be, bf, cd, ce, cf }

After a little improvement, it will even work for mixed types:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

string operator*(const string &arg1, int arg2)
{
  string ret; for (int i = 0; i < arg2; ++i) ret += arg1;
  return ret;
}

template <typename ELEM>
ostream& operator << (ostream &out, const vector<ELEM> &vec)
{
  const char *sep = "{ ";
  for (const ELEM &elem : vec) { out << sep << elem; sep = ", "; }
  return out << " }";
}

int main()
{
  vector<string> v1{ "a", "b", "c" }; vector<int> v2{ 1, 2, 3 };
  // in one line:
  vector<decltype(v1.front() * v2.front())> v3 { v1[0] * v2[0], v1[0] * v2[1], v1[0] * v2[2], v1[1] * v2[0], v1[1] * v2[1], v1[1] * v2[2], v1[2] * v2[0], v1[2] * v2[1], v1[2] * v2[2] };
  // output:
  cout << v3 << endl;
  // done
  return 0;
}

Output:

{ a, aa, aaa, b, bb, bbb, c, cc, ccc }
Scheff's Cat
  • 16,517
  • 5
  • 25
  • 45
  • Thank you all for your answers. I apologize for my lack of clarity, but I should have stated more specifically that the three vectors are strings requiring concatenation. – Michaelk87 Jun 20 '17 at 16:39
  • The extra info in various languages will also be useful to me, so time and effort were not entirely wasted. – Michaelk87 Jun 20 '17 at 16:40
  • @Michaelk87 I don't understand "not entirely wasted". The Python solution of Rory Daulton does mention the concatenation of strings as well as mine in C++ (2nd sample) and even although you didn't mention that the concatenation of strings is actually expected. (I wouldn't even exclude the solution in R because it might depend on `function(x)` what it actually does but this is speculation only.) – Scheff's Cat Jun 20 '17 at 17:00
  • No, I meant that I had actually figured it out before I read the posts, so I was afraid I wasted your time asking a question I found the solution for. I hate when I ask what I should already know or be able to reason out. I'm just beginning to write stuff and know that eventually I'll be expanding into other languages besides python, so I appreciated the answers that went beyond python. Sorry for the confusion and thanks for the help. – Michaelk87 Jun 27 '17 at 01:40
  • Also sorry I neglected to mention that I was working in Python. I have to slow down and pay more attention to details. LOL. Character flaw I can't seem to overcome. – Michaelk87 Jun 27 '17 at 01:44