Message292564
The '|' should be '&' to avoid useless summing of 0 products.
I think this should be rejected. Python's number and collections classes provide basic operations from which users can build application-specific functions. Or in the case of floats, we provide a separate module of specialized functions. Or in the case of itertools, a section of recipes that build on the basic iterators in the module.
In this case, it makes little sense to me to provide an 'inner product' of Counters (bags, multisets).
>>> a, b, c, d = 'a', 'b', 'c', 'd'
>>> c1 = C([a,a,a,b,b,c])
>>> c2 = C([a, c,c,c, d,d,d])
>>> sum(c1[x]*c2[x] for x in c1.keys() & c2.keys())
6
Even if the keys are counts and one thinks of the counters as sparse vectors, they are not really matrices. Hence @ does not exactly fit.
If one does want sparse vectors implemented as dicts, they do not always have to be Counters. A function would not require that.
def sparse_inner_prod(v1, v2):
return sum(v1[x]*v2[x] for x in v1.keys() & v2.keys())
This only requires that v1 and v2 both have keys and __getitem__ methods. |
|
Date |
User |
Action |
Args |
2017-04-29 05:04:56 | terry.reedy | set | recipients:
+ terry.reedy, rhettinger, levkivskyi, Jáchym Barvínek, Jim Fasarakis-Hilliard |
2017-04-29 05:04:56 | terry.reedy | set | messageid: <1493442296.6.0.171457208215.issue30196@psf.upfronthosting.co.za> |
2017-04-29 05:04:56 | terry.reedy | link | issue30196 messages |
2017-04-29 05:04:55 | terry.reedy | create | |
|