Reputation: 25
I want to write a python matrix that looks like:
[P1^3,p2^3,p3^3,p4^3 ...]
[p1^2,p2^2,p3^2,p4^2 ...]
[p1^1,p2^1,p3^1,p4^1 ...]
[p1^0,p2^0,p3^0,p4^0 ...]
The number of columns and the index of p
is determined by the input i
of pi
I tried many ways, but it doesn't work.
Someone please help me.
Upvotes: 0
Views: 990
Reputation: 114781
If you really meant **
and not ^
, you can do this with a single function, numpy.vander
(for Vandermonde) from the numpy
library (http://www.numpy.org/):
In [13]: p = numpy.array([2, 3, 5, 10])
In [14]: numpy.vander(p, 4).T
Out[14]:
array([[ 8, 27, 125, 1000],
[ 4, 9, 25, 100],
[ 2, 3, 5, 10],
[ 1, 1, 1, 1]])
The .T
after the function call transposes the array, since the array created by numpy.vander
is the transpose of what you want.
Upvotes: 4
Reputation: 1136
[[y^x for y in [p1, p2, p3, p4]] for x in [3, 2, 1, 0]]
is probably what you want.
This expands to
[[y^3 for y in [p1, p2, p3, p4]],
[y^2 for y in [p1, p2, p3, p4]],
[y^1 for y in [p1, p2, p3, p4]],
[y^0 for y in [p1, p2, p3, p4]]]
Note that ^
is xor in python.
I'm not really sure what you are trying to get here...
Also, do you mean numpy matrix/array or nested list?
Upvotes: 0