[英]Simple Vandermonde Matrix with for loop
This is a very simple question.My aim is to eventually create a Vandermonde matrix but first i need to: 这是一个非常简单的问题。我的目标是最终创建一个Vandermonde矩阵,但首先我需要:
Write a function that takes a real number α and an integer n as input, and returns a vector v=(1,α,α2,…,αn−1) 编写一个函数,该函数以实数α和整数n作为输入,并返回向量v =(1,α,α2,…,αn-1)
so far i have: 到目前为止,我有:
import numpy as np 将numpy导入为np
n =6
a= 3
for i in range(n):
v = np.array([1, a**2, a**(n-1)])
print v
I get: [ 1 9 243] which is not what i want. 我得到:[1 9 243]这不是我想要的。 Please can you help, thank you!
请您帮忙,谢谢!
Use a list comprehension : 使用列表理解 :
v = np.array([a**x for x in range(n)])
This is equivalent to: 这等效于:
v = []
for x in range(n):
v.append(a**x)
v = np.array(v)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.