Dot product?

David C. Ullrich ullrich at math.okstate.edu
Mon Dec 13 13:37:01 EST 1999


Charles Boncelet wrote:

> Greg Ewing wrote:
> >
> > "Magnus L. Hetland" wrote:
> ...
> >
> > >
> > > And is
> > >
> > >   for x,y in list1, list2:
> > >
> > > ruled out because of anything except aesthetic preference?
> >
> > Yes. You're already allowed a comma-separated list of
> > target variables, in which case unpacking occurs.
> >
> How about the the mxTools solution:
>
> sum = 0.0
> for x,y in tuples(list1, list2):
>     sum = sum + x*y
>
> (On my soapbox): I like "tuples(list1, list2)" much better than
> "map(None, list1, list2)" because the former seems much clearer as to
> the intent of what the expression is supposed to do.

    Yes, if I'd seen "tuples" in the docs I woulda known that that was what
I was looking for. But the only reason I didn't know that map was what
I wanted was that I didn't _read_ the part about map when I was hunting
for "tuples". I didn't read it because I already knew what map did. (not)

    I don't think you're gonna have much luck designing a language so it
can be used by a bozo like me who skips part of the docs, is my point -
if I'd just read that paragraph I woulda known map was what I wanted.

> AFAIK, both "tuples" and "map" solutions create an intermediate object.
> However there seems to be no reason why the internals of Python couldn't
> be changed so that these could be computed "on the fly" as needed.

> Of course, if David (who started this thing) is really interested in
> computing dot products, he should look at Numeric.

    Thanks. I was actually aware of Numeric, at least dimly. The
actual things I'm doing are more abstract than that - in email someone
has confirmed my impression that Numeric is really more appropriate
for numeric things. Dot was not the best possible example, what I
really wanted was Transpose. (The _actual_ Dot looks more like

def Dot(X,Y):
    res = AbsoluteZero
    for (x,y) in Transpose((X,Y)):
        res = res + x*y
    return res

, where AbsoluteZero is a special object with the property that

AbsoluteZero + x = x

for absolutely any object x whatever.)


>
> --
> Charles Boncelet <boncelet at udel.edu>
> University of Delaware
> Newark DE 19716 USA
> http://www.eecis.udel.edu/~boncelet/




More information about the Python-list mailing list