Issue
Given an array [x1 x2 x3 ... xn]
containing n elements, it is desired to produce such following array containing K rows:
[[x1 x2 x3 ... xn],
[x1^2 x2^2 x3^2 ... xn^2],
[x1^3 x2^3 x3^3 ... xn^3],
...,
[x1^K x2^K x3^K ... xn^K]].
How to get this efficiently ?
Solution
A variation on the power.outer
using the ** operator and broadcasting:
In [223]: np.arange(1,5)**np.arange(1,4)[:,None]
Out[223]:
array([[ 1, 2, 3, 4],
[ 1, 4, 9, 16],
[ 1, 8, 27, 64]])
Answered By - hpaulj
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.