Re: sequence multiplied by -1

Discussion in 'Python' started by Yingjie Lan, Sep 25, 2010.

  1. Yingjie Lan

    Yingjie Lan Guest

    Hi,

    >
    > In my opinion this _isn't_ a situation where it's good. :)
    >
    >     L[::-1]
    >
    > is only marginally longer than
    >
    >     -1 * L
    >
    > I think this small gain doesn't justify "violating" this
    > "Python Zen" rule (from `import this`):
    >
    >     There should be one-- and preferably only one
    > --obvious way to do it.
    >


    Thanks for the insightful remarks. For the rule above,
    how about the case to reverse and multiply:

    >>> L*-3 #L reversed and repeated three times


    v.s.

    >>> L[::-1]*3 #L reversed and repeated three times


    The first one is simpler (4 chars v.s. 9 chars).
    I thought it was also intuitive because if you multiply
    a vector by -1, you should get a vector
    in the reversed direction. But, intuitiveness depends
    on who you are, what you do, etc....

    Regards,

    Yingjie
    Yingjie Lan, Sep 25, 2010
    #1
    1. Advertising

  2. Hi,

    On 2010-09-25 15:54, Yingjie Lan wrote:
    > The first one is simpler (4 chars v.s. 9 chars).


    One thing is whether a certain form is shorter, another
    thing to take into account is how often you need the
    functionality.

    > I thought it was also intuitive because if you multiply
    > a vector by -1, you should get a vector
    > in the reversed direction. But, intuitiveness depends
    > on who you are, what you do, etc....


    Sure ... in math, multiplying a vector by -1 gives a vector
    with all its elements negated:

    -1 * [1, 2, 3] -> [-1, -2, -3]

    :)

    Stefan
    Stefan Schwarzer, Sep 25, 2010
    #2
    1. Advertising

  3. Yingjie Lan

    Mel Guest

    Stefan Schwarzer wrote:
    > On 2010-09-25 15:54, Yingjie Lan wrote:
    >> The first one is simpler (4 chars v.s. 9 chars).


    >> I thought it was also intuitive because if you multiply
    >> a vector by -1, you should get a vector
    >> in the reversed direction. But, intuitiveness depends
    >> on who you are, what you do, etc....

    >
    > Sure ... in math, multiplying a vector by -1 gives a vector
    > with all its elements negated:
    >
    > -1 * [1, 2, 3] -> [-1, -2, -3]
    >
    > :)


    And math applies associativity

    v * (3 - 1) == v * 3 + v * -1


    Mel.
    Mel, Sep 25, 2010
    #3
  4. On Sat, 25 Sep 2010 06:54:36 -0700, Yingjie Lan wrote:

    > For the rule above, how about the
    > case to reverse and multiply:
    >
    >>>> L*-3 #L reversed and repeated three times

    >
    > v.s.
    >
    >>>> L[::-1]*3 #L reversed and repeated three times

    >
    > The first one is simpler (4 chars v.s. 9 chars). I thought it was also
    > intuitive because if you multiply a vector by -1, you should get a
    > vector in the reversed direction.


    Reversing the order of elements in a vector does NOT reverse the vector's
    direction. Vector multiplication by a scalar does elementwise
    multiplication. In mathematics and physics:

    [1, 2, 3]*5 => [5, 10, 15]
    [1, 2, 3]*-1 => [-1, -2, -3] is a reflection of the vector.

    It certainly doesn't reverse the order of elements!

    [1, 2, 3] => [3, 2, 1] is a rotation, not a reflection.


    Python doesn't treat sequences as vectors, since it's a programming
    language and sequences are more general. Instead it performs repetition.
    This is conceptually simple: multiplying a sequence by n *repeats* the
    sequence n times:

    L*n = L+L+L+...+L # n times

    where + is concatenation, not elementwise addition. If n is zero, you get
    the empty sequence:

    L*0 = an empty sequence of the same type as L

    just like you get nothing if you multiply a number by n. If you ask for
    zero copies of a sequence, you get nothing.

    The only tricky parts are what to do for fractional values of n, and
    negative values. For practical reasons, the current behaviour is the most
    useful thing to do.

    Once you see sequence multiplication as repetition, then the natural
    interpretation of multiplication by a negative value *cannot* be
    reversal, or any other re-ordering or permutation. How many repetitions
    of [1, 2, 3, 4] does it take to give [1, 4, 2, 3]? Answer -- you can't
    get any other permutation by repeating the original.

    Singling out reversal as a privileged permutation is just "tacking on"
    extra functionality, for no real benefit. It would be like making:

    sorted(my_list) => sequence in sorted order
    sorted(my_list, reversed=True) => sequence in reverse sorted order
    sorted(my_list, reversed=3) => sequence with every element multiplied by
    3, then sorted

    Yes, you could do it, and yes, it might save somebody a line of code
    somewhere, but WTF???

    But even if it wasn't, for reasons of backward compatibility, to say
    nothing of the moratorium, it isn't going to change.


    --
    Steven
    Steven D'Aprano, Sep 26, 2010
    #4
    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. Replies:
    10
    Views:
    532
    ma740988
    Dec 17, 2005
  2. klior

    2 Multiplied clock sync.

    klior, Jul 31, 2007, in forum: VHDL
    Replies:
    0
    Views:
    492
    klior
    Jul 31, 2007
  3. Yingjie Lan

    sequence multiplied by -1

    Yingjie Lan, Sep 25, 2010, in forum: Python
    Replies:
    2
    Views:
    254
    BartC
    Sep 26, 2010
  4. Thomas Jollans

    Re: sequence multiplied by -1

    Thomas Jollans, Sep 25, 2010, in forum: Python
    Replies:
    45
    Views:
    909
    Albert van der Horst
    Oct 6, 2010
  5. Yingjie Lan

    Re: sequence multiplied by -1

    Yingjie Lan, Sep 25, 2010, in forum: Python
    Replies:
    3
    Views:
    236
    Bruno Desthuilliers
    Oct 3, 2010
Loading...

Share This Page