开发者

Python - how to write this in vector form [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reope开发者_JAVA百科ned, visit the help center. Closed 11 years ago.
def compute(c, r):
    s = 0;
    l = len(c);
    for i in range(l):
        s = s + c[i]*f(r[i]);
    return s


I don't know what you mean by vector form (unless you are using numpy?), but I would write your function like this:

def compute(c, r):
  return sum(x*f(y) for x,y in zip(c,r))

If you are using numpy you can use whole-array expressions instead of generator expressions, but in that case c and r must be numpy arrays:

def compute(c, r):
  return (c*f(r)).sum()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜