www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Array operation with boolean operator

reply "n00b" <adrienzoltan yahoo.fr> writes:
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
next sibling parent "John Colvin" <john.loughran.colvin gmail.com> writes:
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
prev sibling next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
 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
prev sibling parent "n00b" <adrienzoltan yahoo.fr> writes:
This has already been reported
http://d.puremagic.com/issues/show_bug.cgi?id=5636
Mar 14 2013
prev sibling next sibling parent "Andrea Fontana" <nospam example.com> writes:
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
prev sibling parent reply "Andrea Fontana" <nospam example.com> writes:
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
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
parent reply "Andrea Fontana" <nospam example.com> writes:
On Thursday, 14 March 2013 at 17:56:07 UTC, Ali Çehreli wrote:
 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
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.
Mar 14 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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:
 from numpy import *
 a = array([1, 5, 1])
 b = array([1, 1, 5])
 a < b
array([False, False, True], dtype=bool) Programmers with experience of Matlab, Matcad, etc, feel the same. Bye, bearophile
Mar 14 2013
parent reply "Andrea Fontana" <nospam example.com> writes:
On Thursday, 14 March 2013 at 18:41:25 UTC, bearophile wrote:
 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:
 from numpy import *
 a = array([1, 5, 1])
 b = array([1, 1, 5])
 a < b
array([False, False, True], dtype=bool) Programmers with experience of Matlab, Matcad, etc, feel the same. Bye, bearophile
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.
Mar 14 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
parent reply Marco Leise <Marco.Leise gmx.de> writes:
Am Thu, 14 Mar 2013 22:15:11 +0100
schrieb "bearophile" <bearophileHUGS lycos.com>:

 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
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. -- Marco
Mar 14 2013
parent "bearophile" <bearophileHUGS lycos.com> writes:
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