dot operations

Discussion in 'Python' started by jm.suresh@no.spam.gmail.com, Jan 11, 2007.

  1. Guest

    Hi,
    Frequently I get to do like this:
    a = (1, 2, 3, 4) # some dummy values
    b = (4, 3, 2, 1)
    import operator
    c = map(operator.add, a, b)

    I am finding the last line not very readable especially when I combine
    couple of such operations into one line. Is it possible to overload
    operators, so that, I can use .+ for element wise addition, as,
    c = a .+ b
    which is much more readable.

    Similarly, I want to use .- , .*, ./ . Is it possible to do?

    thanks.

    -
    Suresh
     
    , Jan 11, 2007
    #1
    1. Advertising

  2. robert Guest

    wrote:
    > Hi,
    > Frequently I get to do like this:
    > a = (1, 2, 3, 4) # some dummy values
    > b = (4, 3, 2, 1)
    > import operator
    > c = map(operator.add, a, b)
    >
    > I am finding the last line not very readable especially when I combine
    > couple of such operations into one line. Is it possible to overload
    > operators, so that, I can use .+ for element wise addition, as,
    > c = a .+ b
    > which is much more readable.
    >
    > Similarly, I want to use .- , .*, ./ . Is it possible to do?


    import numpy

    You'll not even need dots
     
    robert, Jan 11, 2007
    #2
    1. Advertising

  3. Paddy Guest

    wrote:
    > Hi,
    > Frequently I get to do like this:
    > a = (1, 2, 3, 4) # some dummy values
    > b = (4, 3, 2, 1)
    > import operator
    > c = map(operator.add, a, b)
    >
    > I am finding the last line not very readable especially when I combine
    > couple of such operations into one line. Is it possible to overload
    > operators, so that, I can use .+ for element wise addition, as,
    > c = a .+ b
    > which is much more readable.
    >
    > Similarly, I want to use .- , .*, ./ . Is it possible to do?
    >
    > thanks.
    >
    > -
    > Suresh


    List comprehensions?

    >>> a = (1, 2, 3, 4)
    >>> b = (4, 3, 2, 1)
    >>> import operator
    >>> c = map(operator.add, a, b)
    >>> c

    [5, 5, 5, 5]
    >>> c1 = [a1+b1 for a1,b1 in zip(a,b)]
    >>> c1

    [5, 5, 5, 5]
    >>>



    - Paddy.
     
    Paddy, Jan 11, 2007
    #3
  4. Guest

    Paddy wrote:
    > wrote:
    > > Hi,
    > > Frequently I get to do like this:
    > > a = (1, 2, 3, 4) # some dummy values
    > > b = (4, 3, 2, 1)
    > > import operator
    > > c = map(operator.add, a, b)
    > >
    > > I am finding the last line not very readable especially when I combine
    > > couple of such operations into one line. Is it possible to overload
    > > operators, so that, I can use .+ for element wise addition, as,
    > > c = a .+ b
    > > which is much more readable.
    > >
    > > Similarly, I want to use .- , .*, ./ . Is it possible to do?
    > >
    > > thanks.
    > >
    > > -
    > > Suresh

    >
    > List comprehensions?
    >
    > >>> a = (1, 2, 3, 4)
    > >>> b = (4, 3, 2, 1)
    > >>> import operator
    > >>> c = map(operator.add, a, b)
    > >>> c

    > [5, 5, 5, 5]
    > >>> c1 = [a1+b1 for a1,b1 in zip(a,b)]
    > >>> c1

    > [5, 5, 5, 5]
    > >>>

    >


    I just found this from :
    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/384122

    class Infix(object):
    def __init__(self, function):
    self.function = function
    def __ror__(self, other):
    return Infix(lambda x, self=self, other=other:
    self.function(other, x))
    def __or__(self, other):
    return self.function(other)
    def __rlshift__(self, other):
    return Infix(lambda x, self=self, other=other:
    self.function(other, x))
    def __rshift__(self, other):
    return self.function(other)
    def __call__(self, value1, value2):
    return self.function(value1, value2)

    import operator
    dotplus = Infix(lambda x,y: map(operator.add, x, y))

    a = range(4)
    b = range(4)

    c = a |dotplus| b

    >
    > - Paddy.
     
    , Jan 11, 2007
    #4
  5. On Thursday, Jan 11th 2007 at 11:41 +0100, quoth robert:

    => wrote:
    =>> Hi,
    =>> Frequently I get to do like this:
    =>> a = (1, 2, 3, 4) # some dummy values
    =>> b = (4, 3, 2, 1)
    =>> import operator
    =>> c = map(operator.add, a, b)
    =>>
    =>> I am finding the last line not very readable especially when I combine
    =>> couple of such operations into one line. Is it possible to overload
    =>> operators, so that, I can use .+ for element wise addition, as,
    =>> c = a .+ b
    =>> which is much more readable.
    =>>
    =>> Similarly, I want to use .- , .*, ./ . Is it possible to do?
    =>
    =>import numpy
    =>
    =>You'll not even need dots

    I'm very new so my plea to be gentle is still on.

    I just looked at the numpy package. Seems very cool, but for the life of
    me I didn't understand the method by which python allows for creation of
    infix operators. Can someone please explain or point me to a reference?

    TIA

    --
    Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
    happened but none stranger than this. Does your driver's license say Organ ..0
    Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
    individuals! What if this weren't a hypothetical question?
    steveo at syslang.net
     
    Steven W. Orr, Jan 11, 2007
    #5
  6. On Thu, 11 Jan 2007 06:06:39 -0800, wrote:

    >> wrote:
    >> > Hi,
    >> > Frequently I get to do like this:
    >> > a = (1, 2, 3, 4) # some dummy values
    >> > b = (4, 3, 2, 1)
    >> > import operator
    >> > c = map(operator.add, a, b)
    >> >
    >> > I am finding the last line not very readable especially when I combine
    >> > couple of such operations into one line. Is it possible to overload
    >> > operators, so that, I can use .+ for element wise addition, as,
    >> > c = a .+ b
    >> > which is much more readable.


    [snip]

    > I just found this from :
    > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/384122
    >
    > class Infix(object):


    [snip code]

    > c = a |dotplus| b



    Personally, I find that to be the least readable of all the alternatives.
    It is also slow, so slow that using it is (in my opinion) a pessimation
    rather than an optimization.

    Let me make the usual warnings about premature optimization being
    the root of all evil, blah blah blah. Readability counts, and nobody cares
    if you shave one hundredth of a millisecond off some code that your
    program runs once. But, having said that, map is *fast*, especially with
    the functions from operator.

    >>> import timeit
    >>> timeit.Timer("map(operator.add, a, b)",

    .... "import operator; a = (1, 2, 3, 4); b = (4, 3, 2, 1)").timeit()
    4.0072550773620605

    It is even faster if you get rid of the dot lookup:

    >>> timeit.Timer("map(add, a, b)", "from operator import add; "

    .... "a = (1, 2, 3, 4); b = (4, 3, 2, 1)").timeit()
    3.6557090282440186

    Using lambda can be much slower:

    >>> timeit.Timer("map(lambda x,y: x+y, a, b)",

    .... "a = (1, 2, 3, 4); b = (4, 3, 2, 1)").timeit()
    6.1221940517425537

    A list comprehension is in the middle, speed-wise:

    >>> timeit.Timer("[x+y for x,y in zip(a,b)]",

    .... "a = (1, 2, 3, 4); b = (4, 3, 2, 1)").timeit()
    5.0318419933319092

    But the Infix recipe is slower than a wet week:

    >>> timeit.Timer("a|dotplus|b",

    .... "a = (1, 2, 3, 4); b = (4, 3, 2, 1); "
    .... "from __main__ import dotplus").timeit()
    15.195909976959229


    Speaking for myself, I find list comprehensions to be the most readable of
    the alternatives and the most Pythonic.



    --
    Steven.
     
    Steven D'Aprano, Jan 11, 2007
    #6
  7. Dan Bishop Guest

    On Jan 11, 10:21 am, "Steven W. Orr" <> wrote:
    > On Thursday, Jan 11th 2007 at 11:41 +0100, quoth robert:
    >
    > => wrote:=>> Hi,
    > =>> Frequently I get to do like this:
    > =>> a = (1, 2, 3, 4) # some dummy values
    > =>> b = (4, 3, 2, 1)
    > =>> import operator
    > =>> c = map(operator.add, a, b)
    > =>>
    > =>> I am finding the last line not very readable especially when I combine
    > =>> couple of such operations into one line. Is it possible to overload
    > =>> operators, so that, I can use .+ for element wise addition, as,
    > =>> c = a .+ b
    > =>> which is much more readable.
    > =>>
    > =>> Similarly, I want to use .- , .*, ./ . Is it possible to do?
    > =>
    > =>import numpy
    > =>
    > =>You'll not even need dots
    >
    > I'm very new so my plea to be gentle is still on.
    >
    > I just looked at the numpy package. Seems very cool, but for the life of
    > me I didn't understand the method by which python allows for creation of
    > infix operators. Can someone please explain or point me to a reference?


    Python doesn't allow the creation of new operators, but you can
    overload the existing ones (except for "and" and "or"). This is done
    by implementing the methods __add__, __sub__, __mul__, etc.

    http://docs.python.org/ref/specialnames.html
     
    Dan Bishop, Jan 12, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Samuël van Laere

    To dot or not to dot?

    Samuël van Laere, Oct 16, 2003, in forum: HTML
    Replies:
    8
    Views:
    443
    Samuël van Laere
    Oct 16, 2003
  2. Christopher M. Lusardi

    volatile struct in dot h vs dot c

    Christopher M. Lusardi, May 11, 2004, in forum: C Programming
    Replies:
    3
    Views:
    488
    Peter Shaggy Haywood
    May 15, 2004
  3. Nathan Sokalski
    Replies:
    11
    Views:
    720
    AAaron123
    Aug 14, 2009
  4. krishnan

    Dot Net Project Execution without Dot Net and Framework....

    krishnan, Jan 7, 2006, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    200
    krishnan
    Jan 7, 2006
  5. Replies:
    6
    Views:
    268
    Thomas 'PointedEars' Lahn
    Dec 12, 2005
Loading...

Share This Page