Bug 5636 – Array ops use lexicographic comparison instead of vector-style element-wise

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P4
Component
dlang.org
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-02-21T14:13:02Z
Last change time
2023-07-17T10:14:35Z
Keywords
pull, wrong-code
Assigned to
No Owner
Creator
Simen Kjaeraas

Comments

Comment #0 by simen.kjaras — 2011-02-21T14:13:02Z
void main( ) { auto a = [1,2,3,4,5]; auto b = [5,4,3,2,1]; bool[] c = new bool[a.length]; foreach ( i, ref e; c ) { e = a[i] < b[i]; } assert( c == [true, true, false, false, false] ); c[] = a[] < b[]; assert( c != [true, true, false, false, false] ); } One would believe the two results should be equal, but they're not.
Comment #1 by simen.kjaras — 2011-02-21T14:27:41Z
Actually, thinking about this some more, I see what is wrong. It is actually doing: bool tmp = a[] < b[]; c[] = tmp; I am not sure about the details of array comparison like that, a fact I feel makes this bug report more correct.
Comment #2 by bearophile_hugs — 2011-02-21T15:17:46Z
More context, links to examples from NumPy and AVX instructions that make some vectors operations even more useful: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=130273
Comment #3 by bearophile_hugs — 2011-02-21T15:18:58Z
Thank you for adding this bug report/enhancement request, I was going to add it myself in a day or two :-)
Comment #4 by yebblies — 2012-02-04T20:45:14Z
This is working as designed, although it's not particularly intuitive. It might be worth bringing this up on the newsgroup to see what kind of support it has.
Comment #5 by bearophile_hugs — 2012-03-06T20:05:50Z
A discussion thread: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=160067 One of the messages: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=160128 Vector operations like a[]<b[] are meant to return an array of bools. To see how this is useful you probably must think in terms of vector-style programming. In NumPy the use of arrays of booleans is common: >>> from numpy import * >>> a = array([3,6,8,9]) >>> a == 6 array([False, True, False, False], dtype=bool) >>> a >= 7 array([False, False, True, True], dtype=bool) >>> a < 5 array([ True, False, False, False], dtype=bool) >>> # count all the even numbers >>> sum( (a%2) == 0 ) 2 >>> b = array([2,6,7,10]) >>> a == b array([False, True, False, False], dtype=bool) >>> a < b array([False, False, False, True], dtype=bool) They are sometimes used as masks, it's useful if you have a Vector type that supports multi-index syntax: i = scipy.array([0,1,2,1]) # array of indices for the first axis j = scipy.array([1,2,3,4]) # array of indices for the second axis a[i,j] # return array([a[0,1], a[1,2], a[2,3], a[1,4]]) b = scipy.array([True, False, True, False]) a[b] # return array([a[0], a[2]]) since only b[0] and b[2] are True Using the new CPU AVX registers you are able to perform a loop and work on the items of an array in parallel until all the booleans of an array are false. See this, especially Listing 5: http://software.intel.com/en-us/articles/introduction-to-intel-advanced-vector-extensions/ http://www.cs.uaf.edu/2011/spring/cs641/lecture/04_12_AVX.html Vector comparisons have a natural hardware implementation with AVX/AVX2 instructions like _mm256_cmp_ps.
Comment #6 by nick — 2023-07-05T14:00:37Z
The digitalmars.com links are not working ATM. But because `a[] < b[]` has a defined meaning as an expression in itself, the vector op is ambiguous if allowed. So instead the docs* should clearly define this. To make this work, we need unique syntax for vector ops - see the end of this comment: https://issues.dlang.org/show_bug.cgi?id=6345#c5 * PR incoming.
Comment #7 by dlang-bot — 2023-07-05T14:04:29Z
@ntrel created dlang/dlang.org pull request #3654 "[spec/arrays] Improve vector operations docs" fixing this issue: - [spec/arrays] Improve vector operations docs Change title to Vector Operations, array operations is not precise enough IMO and hard to google for. Define which operations are supported. Rewrite example & make runnable. Add note warning about invalid expressions for vector op. Fixes Issue 5636 - Array ops use lexicographic comparison instead of vector-style element-wise. https://github.com/dlang/dlang.org/pull/3654
Comment #8 by dlang-bot — 2023-07-10T11:06:37Z
dlang/dlang.org pull request #3654 "[spec/arrays] Improve vector operations docs" was merged into master: - 53199ba50e78879f72401489bbbd38314768ed5e by Nick Treleaven: [spec/arrays] Improve vector operations docs Change title to Vector Operations, array operations is not precise enough IMO and hard to google for. Define which operations are supported. Rewrite example & make runnable. Add note warning about invalid expressions for vector op. Fixes Issue 5636 - Array ops use lexicographic comparison instead of vector-style element-wise. https://github.com/dlang/dlang.org/pull/3654