ILNumerics: compund test for ILLogical: AND two logical conditions - ilnumerics

I am trying to create an ILLogical array that selects data between upper and lower limits like this:
ILLogical ix = a > limit[0] & a < limit[1]
where a and limit are ILArray< double >. I get an ILArgumentException about "Nonscalar logical to bool conversion" asking me to see ILSettings.LogicalArrayToBoolConversion. Changing the '&' to '&&' doesn't help. Is there no way to set up a compound test resulting in an ILLogical? What are my alternatives?

There is no (convenient) way to overload the binary logical operators as & in C# in the way required here. Also, if such way would exist it would badly act against common intuition and likely cause more confusion than convenience.
Instead use the functional interface with ILMath.and() & Co:
ILLogical ix = ILMath.and(a > limit[0], a < limit[1]);
See: API class documentation on and

Related

Why is "reduce" an infix operator in Chapel?

According to this manual page, we can use reduce to perform reduction like
summation (+):
var a = (+ reduce A) / num;
var b = + reduce abs(A);
var c = sqrt(+ reduce A**2);
and maximum value/location:
var (maxVal, maxLoc) = maxloc reduce zip(A, A.domain);
Here, Chapel defines reduce to be an infix operator rather than a function (e.g., reduce( A, + )). IMHO, the latter form seems to be a bit more readable because the arguments are always separated by parentheses. So I am wondering if there is some reason for this choice (e.g., to simplify some parallel syntax) or just a matter of history (convention)?
I'd say the answer is a matter of history / convention. A lot of Chapel's array and domain features were heavily inspired by the ZPL language from the University of Washington, and I believe this syntax was taken reasonably directly from ZPL.
At the time, we didn't have a notion of passing things like functions and operators around in Chapel, which is probably one of the reasons that we didn't consider more of a function-based approach. (Even now, first-class function support in Chapel is still somewhat in its infancy, and I don't believe we have a way to pass operators around).
I'd also say that Chapel is a language that generally favors syntax for key patterns rather than taking more of a "make everything look like a function / method call" approach (e.g., ranges are supported via a literal syntax and several key operators rather than using an object type with methods).
None of this is to say that the choice was obviously right or couldn't be reconsidered.

systemverilog - how multiple increment operators in a is a single statement work

Would the increment happen Left to right or right to left in the following case:
desc.src_addr = {rdata[i++],rdata[i++],rdata[i++],rdata[i++],rdata[i++],rdata[i++],rdata[i++],rdata[i++]};
Assuming i = 0 at the start of the above statement, would the end resolution be which of the two below:
desc.src_addr = {rdata[0],rdata[1],rdata[2],rdata[3],rdata[4],rdata[5],rdata[6],rdata[7]};
desc.src_addr = {rdata[7],rdata[6],rdata[5],rdata[4],rdata[3],rdata[2],rdata[1],rdata[0]};
Is there anything in LRM that talks about this?
Obviously, my aim is to avoid hardcoding the indices in the above statement to avoid typo and oversight issues.
Yes, it would be good to know what the SystemVerilog LRM says when writing SystemVerilog code. Do you have a copy?
See section 11.4.2 Increment and decrement operators and look for the word undefined.
A much better way of writing this code would be using the streaming operator, section 11.4.14 Streaming operators (pack/unpack)
desc.src_addr = {<<{rdata}}; //reverses bit order

Is the uplus function useful?

This is a rhetorical question about the uplus function in MATLAB, or its corresponding operator, the unary plus +.
Is there a case where this operator is useful? Even better, is there a case where this operator is necessary?
It is not necessary, a language without a unary plus does not allow to write +1. Obviously you could also write 1 but when importing data which always writes the + or - it's very nice to have.
Searching some source codes, I found a curious use of +
A=+A
which replaced the code:
if ~isnumeric(A)
A=double(A);
end
It casts chars and logicals to double, but all numeric data types remain untouched.
It can be useful when defining new numeric types.
Suppose you define quaternion and overload uplus:
classdef quaternion
...
end
Then in your code you can write:
x = quaternion(...);
y = [+x, -x];
z = +quaternion.Inf;
t = -quaternion.Inf;
If you don't you cannot have same syntax as for other numeric.
PS: To the question "is it useful" (in the sence mandatory for some syntaxes) ... well I can't find any reason ... but sometimes writting '+x' make things clearer when reading back the code.
I'm not sure if this fully constitutes "useful" or if it's the best programming practice, but in some cases, one may wish to use the unary + for symmetry/clarity reasons. There's probably a better example, but I'm thinking of something like this:
A = [+1 -1 +1;
-1 +1 -1;
+1 -1 +1];
As for the uplus function, it's kind of a NOOP for numeric operations. If one writes a function that requires a function handle input to specify an operation to perform, it might be useful to have do nothing option.
Lastly, numeric operators can be overloaded for other classes. The uplus function could have more use in other built-in classes or even one you might want write yourself.

How does one overload / redefine binary operators in Julia?

I keep losing my reference on how to redefine say the || ("or") or && ("and") binary operators. I read somewhere that one has to first do importall Base . Then I tried
Base.||( x::MyType, y::MyType ) = dosomething( x, y )
and also
Base.or( x::MyType, y::MyType ) = dosomething( x, y )
But none of these worked. I would appreciate if somebody could give a reference explaining the basics of how to do this... I was unable to find one with queries such as "redefine binary operator in Julia"...
As && and || are short circuit operations, they can't be overloaded, without adding a special construction separate from functions. To call a function, you need to evaluate all the arguments, and that would not be a short circuit evaluation.
You might be able to overload & and | instead, but it is hard to tell without an example use case.

Need an explanation for a confusing way the AND boolean works

I am tutoring someone in basic search and sorts. In insertion sort I iterate negatively when I have a value that is greater than the one previous to it in numerical terms. Now of course this approach can cause issues because there is a check which calls for array[-1] which does not exist.
As underlined in bold below, adding the and x > 0 boolean prevents the index issue.
My question is how is this the case? Wouldn't the call for array[-1] still be made to ensure the validity of both booleans?
the_list = [10,2,4,3,5,7,8,9,6]
for x in range(1,len(the_list)):
value = the_list[x]
while value < the_list[x-1] **and x > 0**:
the_list[x] = the_list[x-1]
x=x-1
the_list[x] = value
print the_list
I'm not sure I completely understand the question, and I don't know what programming language this is, but most modern programming languages use so-called short-circuit Boolean evaluation by default so that the logical expression isn't evaluated further once the outcome is known.
You can use that to guard against range overflow, like this:
while x > 0 and value < the_list[x-1]
but the check of x's range here must come before the use.
AND operation returns true if and only if both arguments are true, so if one of arguments is false there's no point of checking others as the final value is already known at that point. As for your example, usually evaluation goes from left to right but it is not a principle and it looks the language you used is not following that rule (othewise it still should crash on array lookup). But ut may be, this particular implementation optimizes this somehow (which IMHO is not good idea) and evaluates "simpler" things first (like checking if x > 0) before it look up the array. check the specs why this exact order works for you as in most popular languages you would still crash if test x > 0 wouldn't be evaluated before lookup