Conditions | 5 |
Total Lines | 28 |
Code Lines | 12 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import numpy as np |
||
10 | def cartesian_(arrays, out=None): |
||
11 | """ |
||
12 | Generate a cartesian product of input arrays. |
||
13 | """ |
||
14 | |||
15 | # A re-entrant function |
||
16 | |||
17 | dtype = arrays[0].dtype |
||
18 | |||
19 | n = np.prod([x.size for x in arrays]) |
||
20 | |||
21 | # This happen only the first iteration |
||
22 | if out is None: |
||
23 | |||
24 | out = np.zeros([n, len(arrays)], dtype=dtype) |
||
25 | |||
26 | m = n / arrays[0].size |
||
27 | out[:,0] = np.repeat(arrays[0], m) |
||
28 | |||
29 | if arrays[1:]: |
||
30 | |||
31 | cartesian_(arrays[1:], out=out[0:m, 1:]) |
||
32 | |||
33 | for j in range(1, arrays[0].size): |
||
34 | |||
35 | out[j*m:(j+1)*m, 1:] = out[0:m, 1:] |
||
36 | |||
37 | return out |
||
38 | |||
51 |
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.