Implements a function in one line and without creating a list or tuples or any other collection that is not of fixed length and without using numpy using the given def signature

here what i did

def inner_product_c(c1: Iterable[complex], c2: Iterable[complex]) -> complex:
    return  list(map(lambda x, y: x*y.conjugate() , c1, c2)) ;

i define inner product space such that u,v are 2 vectors and i want the sum of all elements accordingly to the same position where u(i) * conjugate(v(i))

here is the only one line way that i came up with is there any other ways to do it in one line?

1

There are 1 best solutions below

3
On

In Python3:

def inner_product_c(c1: Iterable[complex], c2: Iterable[complex]) -> complex:                                                                                                                                                                                 
    return sum(list([c1[n]*c2[n].conjugate() for n in range(len(c1))]))

range() returns an iterator in Python3, with space-complexity O(1) Therefore the above does not create anything (other than the return list) of space-complexity O(n) (being n the length of the vectors)