Ask Your Question

Revision history [back]

Besides the suggestion by @Emmanuel Charpentier and @FrédéricC ...

... it seems you are trying to reimplement the dot product of vectors.

Python provides more efficient ways to sum elementwise products of lists.

Sage also has a built-in dot product for vectors.

With a and d defined as lists:

sage: a = [2, 2, 3, 4]
sage: d = [2, 4, 5, 6]

Python:

sage: sum(ak * dk for ak, dk in zip(a, d))
51

Sage:

sage: vector(a) * vector(d)
51

With a and d defined directly as vectors:

sage: a = vector([2, 2, 3, 4])
sage: d = vector([2, 4, 5, 6])
sage: a * d
51