digitalmars.D.learn - Array operation with boolean operator
- n00b (6/6) Mar 14 2013 I tried to use a boolean operator for an array operation :
- John Colvin (2/9) Mar 14 2013 As far as I can tell, this is a bug.
- bearophile (6/9) Mar 14 2013 It's not implemented (and it's a bug that it returns something
- bearophile (15/16) Mar 14 2013 Code for a bug report:
- n00b (2/2) Mar 14 2013 This has already been reported
- Andrea Fontana (6/13) Mar 14 2013 Maybe this way:
- Andrea Fontana (12/17) Mar 14 2013 Yes i think there is a rational reason. Check this:
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (8/10) Mar 14 2013 I thought the same thing at first but note the brackets after b and c.
- Andrea Fontana (7/18) Mar 14 2013 Doesn't a[] means copy of a?
- bearophile (7/12) Mar 14 2013 Having a practice with NumPy and the like, I think X < Y as
- Andrea Fontana (5/18) Mar 14 2013 But does sort!"a
- bearophile (6/7) Mar 14 2013 See the difference in syntax:
- Marco Leise (8/19) Mar 14 2013 This is "just" a syntax ambiguity. a[] takes the complete
- bearophile (14/18) Mar 14 2013 Yet in D the only accepted syntax to perform a vector op sum is
I tried to use a boolean operator for an array operation : a[] = b[] < c[]; It compiles but seems to only fill a[] with the result of b[0] < c[0]. Is there any "rational" reason to that? And is there any way to use boolean operator for array operations?
Mar 14 2013
On Thursday, 14 March 2013 at 13:58:45 UTC, n00b wrote:I tried to use a boolean operator for an array operation : a[] = b[] < c[]; It compiles but seems to only fill a[] with the result of b[0] < c[0]. Is there any "rational" reason to that? And is there any way to use boolean operator for array operations?As far as I can tell, this is a bug.
Mar 14 2013
n00b:Is there any "rational" reason to that?It's not implemented (and it's a bug that it returns something different). Take a look in Bugzilla if it's already there.And is there any way to use boolean operator for array operations?I think the only supported boolean vec operation is assignment. Bye, bearophile
Mar 14 2013
Take a look in Bugzilla if it's already there.Code for a bug report: import core.stdc.stdio: printf; void main() { int[3] a = [1, 5, 1]; int[3] b = [1, 1, 5]; int[3] c; c[] = a[] < b[]; printf("%d %d %d", c[0], c[1], c[2]); } Output: 0 0 0 Expected output: 0 0 1 Bye, bearophile
Mar 14 2013
This has already been reported http://d.puremagic.com/issues/show_bug.cgi?id=5636
Mar 14 2013
On Thursday, 14 March 2013 at 13:58:45 UTC, n00b wrote:I tried to use a boolean operator for an array operation : a[] = b[] < c[]; It compiles but seems to only fill a[] with the result of b[0] < c[0]. Is there any "rational" reason to that? And is there any way to use boolean operator for array operations?Maybe this way: int a[] = [1,2,3]; int b[] = [3,2,1]; a.zip(b).map!"a[0]<a[1]".writeln; return;
Mar 14 2013
On Thursday, 14 March 2013 at 13:58:45 UTC, n00b wrote:I tried to use a boolean operator for an array operation : a[] = b[] < c[]; It compiles but seems to only fill a[] with the result of b[0] < c[0]. Is there any "rational" reason to that?Yes i think there is a rational reason. Check this: int a[] = [1,0,0]; int b[] = [0,1,0]; int c[] = [1,1,0]; bool ab = a[] < b[]; // False. a[0] > b[0] bool ac = a[] < c[]; // True. a[0] == c[0] but a[1] < c[1]; writeln(ab); writeln(ac); c[] = ab; // <-- assign! You see? Your code do this: a[] = (b[] < c[]);
Mar 14 2013
On 03/14/2013 10:45 AM, Andrea Fontana wrote:You see? Your code do this: a[] = (b[] < c[]);I thought the same thing at first but note the brackets after b and c. Those should make this an array-wise operation. For all elements of a to be the same value, one would not write the brackets: a[] = b < c; Some of the array-wise operations are confusing. :/ Ali
Mar 14 2013
On Thursday, 14 March 2013 at 17:56:07 UTC, Ali Çehreli wrote:On 03/14/2013 10:45 AM, Andrea Fontana wrote:Doesn't a[] means copy of a? So copyofa[] < copyofb[] == bool I don't think a boolean operator can be array-wise... I always think X < Y return a single bool, indipendently from X or Y type (class, array or what else). Math operations is a different topic in my mind.You see? Your code do this: a[] = (b[] < c[]);I thought the same thing at first but note the brackets after b and c. Those should make this an array-wise operation. For all elements of a to be the same value, one would not write the brackets: a[] = b < c; Some of the array-wise operations are confusing. :/ Ali
Mar 14 2013
Andrea Fontana:I always think X < Y return a single bool,Having a practice with NumPy and the like, I think X < Y as returning as many bools as the length of X and Y:array([False, False, True], dtype=bool) Programmers with experience of Matlab, Matcad, etc, feel the same. Bye, bearophilefrom numpy import * a = array([1, 5, 1]) b = array([1, 1, 5]) a < b
Mar 14 2013
On Thursday, 14 March 2013 at 18:41:25 UTC, bearophile wrote:Andrea Fontana:But does sort!"a<b" still work if it returns an array? It doesn't make sense for me :) if you ask me if a int vector is lesser than another one I don't mean every single component but the whole vector. Same goes for strings or any other range.I always think X < Y return a single bool,Having a practice with NumPy and the like, I think X < Y as returning as many bools as the length of X and Y:array([False, False, True], dtype=bool) Programmers with experience of Matlab, Matcad, etc, feel the same. Bye, bearophilefrom numpy import * a = array([1, 5, 1]) b = array([1, 1, 5]) a < b
Mar 14 2013
Andrea Fontana:But does sort!"a<b" still work if it returns an array?See the difference in syntax: a < b a[] < b[] Bye, bearophile
Mar 14 2013
Am Thu, 14 Mar 2013 22:15:11 +0100 schrieb "bearophile" <bearophileHUGS lycos.com>:Andrea Fontana:This is "just" a syntax ambiguity. a[] takes the complete slice of the array 'a'. And a dynamic array in D is a slice. So if you use a or a[] in an expression doesn't make much of a difference. -- MarcoBut does sort!"a<b" still work if it returns an array?See the difference in syntax: a < b a[] < b[] Bye, bearophile
Mar 14 2013
Marco Leise:This is "just" a syntax ambiguity. a[] takes the complete slice of the array 'a'. And a dynamic array in D is a slice. So if you use a or a[] in an expression doesn't make much of a difference.Yet in D the only accepted syntax to perform a vector op sum is to use add square brackets both operands: void main() { auto a = new int[5]; auto b = new int[5]; auto c = new int[5]; c[] = a[] + b[]; // OK c[] = a + b; // Error: invalid array operation c[] = a[] + b; // Error: invalid array operation c[] = a + b[]; // Error: invalid array operation } Bye, bearophile
Mar 14 2013