Here a
is a 1D array of integer indices. To give some context, a
is the atom indices of the 1st molecules. The return is the atom indices for n
identical molecules, each of which contains step
atoms. This function basically applies the same atom selection to many molecules
JavaScript
x
7
1
def f(a, step, n):
2
a.shape=1,-1
3
got = np.repeat(a, n, axis=0)
4
blocks = np.arange(0, n*step, step)
5
blocks.shape = n, -1
6
return got + blocks
7
For example
JavaScript
1
8
1
In [52]: f(np.array([1,3,4]), 10, 4)
2
Out[52]:
3
array([[ 1, 3, 4],
4
[11, 13, 14],
5
[21, 23, 24],
6
[31, 33, 34]])
7
8
Advertisement
Answer
It looks like broadcasting should be enough:
JavaScript
1
5
1
def f(a, step, n):
2
return a + np.arange(0, n*step, step)[:, None]
3
4
f(np.array([1,3,4]), 10, 4)
5
output:
JavaScript
1
5
1
array([[ 1, 3, 4],
2
[11, 13, 14],
3
[21, 23, 24],
4
[31, 33, 34]])
5