Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: sequence multiplied by -1

Reply
Thread Tools

Re: sequence multiplied by -1

 
 
Yingjie Lan
Guest
Posts: n/a
 
      09-25-2010
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






 
Reply With Quote
 
 
 
 
Stefan Schwarzer
Guest
Posts: n/a
 
      09-25-2010
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
 
Reply With Quote
 
 
 
 
Mel
Guest
Posts: n/a
 
      09-25-2010
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.
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      09-26-2010
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: sequence multiplied by -1 Thomas Jollans Python 45 10-06-2010 01:28 PM
Re: sequence multiplied by -1 Yingjie Lan Python 3 10-03-2010 03:14 PM
sequence multiplied by -1 Yingjie Lan Python 2 09-26-2010 12:41 PM
2 Multiplied clock sync. klior VHDL 0 07-31-2007 01:33 PM
problems at multiplication (Currency multiplied with float) abdul_n_khan@hotmail.com C++ 10 12-17-2005 01:59 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57