What is the negation (complement) of:
i < A.Length
in the context of:
Do (i < A.Length ) // is it i == A.Length or i > A.Length ?
What is the negation of
i <= A.Length
in the context of:
Do (i <= A.Length ) // is it i >= A.Length ?
For some reason I can't find a source to confirm.
Try just to draw a line with 0 in the middle. Then, all that is to right of zero is > 0, so the complement is another part of the line. And it is < 0 and 0. So, the complement to > 0 is <= 0.
The complement of < is >= and the complement of > is <= though without understanding what i is, relative to the .length properties, that operation may not be quite what you want.
Negation of i < A.Length is i >= A.Length and negation of i <= A.Length is i > A.Length.
Related
There is a simple trick to convert a number to 1 or -1.
Just raise it to the power of 0.
So:
4^0 = 1
-4^0 = -1
However, in AS3:
Math.pow( 4, 0); // = 1
Math.pow(-4, 0); // = 1
Is there a way to get the right answer without an if else?
This could be done bitwise.
Given the number n (avg time: 0.0065ms):
1 + 2 * (n >> 31);
Or slightly slower (avg time: 0.0095ms):
(n < 0 && -1) || 1;
However, Marty's solution is the fastest (avg time: 0.0055ms)
n < 0 ? -1 : 1;
Not sure if without an if/else includes the ternary operator in your eyes, but if not:
// Where x is your input.
var r:int = x < 0 ? -1 : 1;
Will be more efficient than Math.pow() anyway.
I have a function like this:
y=-2 with x<=0
y=-2+3x^2 with 0=1
I need to compute this function on each element of the 1D matrix, without using a loop.
I thought it was possibile defining a function like this one:
function y= foo(x)
if x<=0
y=-2;
elseif x>=1
y=1;
else
y= -2+3*x.^2;
end
end
But this just produces a single result, how to operate on all elements? I know the . operator, but how to access the single element inside an if?
function b = helper(s)
if s<=0
b=-2;
elseif s>=1
b=1;
else
b= -2+3*s^2;
end
end
Then simply call
arrayfun(#helper, x)
to produce the behaviour you want of your function foo.
Another approach which doesn't need arrayfun() would be to multiply by the conditions:
y = -2*(x <= 0) + (-2+3*x.^2).*(x < 1).*(x > 0) + (x >= 1)
which you could also make a function. This will accept vector inputs for x e.g.
x = [1 4 0 -1 0.5];
y = -2*(x <= 0) + (-2+3*x.^2).*(x < 1).*(x > 0) + (x >= 1)
outputs
y =
1.0000 1.0000 -2.0000 -2.0000 -1.2500
I have written a decimal floating point unit for LaTeX3 (pure macros... that was tough). In particular, I have to decide how x < y < z should be parsed. I see three options:
Treat < as a left-associative binary operator, so x < y < z would be equivalent to (x < y) < z. This is what C does: -1 < 0 < 1 becomes (-1 < 0) < 1, thus 1 < 1, which is 0.
Treat < as a right-associative binary operator, so x<y<z would be equivalent to x < (y < z). I see no advantage to that option.
When encountering <, read ahead for more comparison operators, and treat x < y < z as equivalent to (x < y) && (y < z), where y would be evaluated only once. This is what most non-programmers would expect. And quite a few LaTeX users are non-programmers.
At the moment I am using the first option, but it does not seem very natural. I think that I can implement the second case whithout too much overhead. Should I?
Since that question is subjective, let me ask an objective question: what mainstream languages pick option 3? I'm interested in the details of what happens with mixed things like a < b > c == d < e != f. I'm also interested in other choices if they exist.
Short answer: it only makes sense to parse comparison sequences if they are "pointing into the same direction", and when you don't use !=.
Long answer: In Python, 3 > 2 > 1 evaluates to True. However, I have to say that the implementation used is overly simplistic, because it allows for expressions like a < b > c == d < e != f, which are nonsensical in my opinion. The expression would be interpreted as (a < b) and (b > c) and (c == d) and (d < e) and (e != f). It's an easy rule, but because it allows for surprising results, I don't like that interpretation.
I propose a more predictable option:
Consider a proposition xAyBzCw. If this proposition is "sensical", it is equivalent to xAy and yBz and zCw. For "sensicality", it is necessary that...
the values (x, y, z, w) are part of the same set X (or their types can be unified as such), and
the relations (A, B, C) are transitive binary relations on X, and
for every ordered pair of relations A and B, there exists a relation C, such that xAy and yBz implies xCz for all x, y, z; this relation is also subject to these restrictions.
Regarding the last rule, you want to be able to say that 1 < 2 = a < 4 is equivalent to 1<2 and 2=a and a<4, but also that 1<2 and 1<a and 1<4. To say the latter, you must know how = and < interact.
You can't use != in my option, because it isn't transitive. But you also can't say 1 < 3 > 2, 2 < 3 > 1, or 1 < 3 > 1, unless you have a relation ? such that 1?2, 2?1 and 1?1 (basically, it would be a relation allows any pair).
From a syntactical standpoint: you want to treat relational operators as special operators (+ is more of a functional operator), kind of like in your third option.
Python chains relational operators. Which gets interesting when you hit in and is, since they're considered relational as well.
>>> 1 < 2 in [True, False]
False
>>> 1 < 2 in [2, 4]
True
J evaluates statements right-to-left so that:
3 > 2 > 1
Becomes first
2 > 1
Which resolves to true, represented as 1, thus:
3 > 1
Which also resolves to true, thus 1. The opposite operator < would result in false, whereas the whole statement happens to be true. So you're no further with J.
Your main issue is that your initial representation:
3 > 2 > 1
is human shorthand for
(3 > 2) AND (2 > 1)
So while reading ahead seems icky, it's really what the representation needs. Unless of course there's some Python magic, as others have stated.
I am representing wind directions using integer values (an Enum) ranging from 0 for North, through to 15 for North-North-West.
I need to check if a given wind direction (integer value between 0 and 15) is within a certain range. I specify my WindDirectionFrom value first moving clockwise to WindDirectionTo to specify the range of allowable wind direction.
Obviously if WindDirectionFrom=0 and WindDirectionTo=4 (between N and E direction) and the wind direction is NE (2) the calculation is simply
int currentWindDirection = 2;
bool inRange = (WindDirectionFrom <= currentWindDirection && currentWindDirection <= WindDirectionTo);
//(0 <= 2 && 2 <= 4) simple enough...
However for a different case where say WindDirectionFrom=15, WindDirectionTo=4 and wind direction is NE (2) again, the calculation immediately breaks...
bool inRange = (WindDirectionFrom <= currentWindDirection && currentWindDirection <= WindDirectionTo);
//(15 <= 2 && 2 <= 4) oops :(
I'm sure this can't be too difficult, but I'm having a real mental block with this one.
What you want is modular arithmetic. Do your arithmetic mod 16, and check to see if the difference is from (say) at least 14 (the modular equivalent of -2) or at most 2.
How to do modular arithmetic will vary between languages. With C or C++, you would find x mod 16 as follows:
int xm = x % 16;
if (xm < 0) xm += 16;
(Thanks to msw for pointing out that arithmetic on enums is frequently not allowed, and for good reasons. An enum normally represents objects or conditions that are discrete and not related arithmetically.)
I would do it like this:
int normedDirection( int direction, int norm )
{
return (NumberOfDirections + currentDirection - norm) % NumberOfDirections;
}
int normed = normedDirection( currentWindDirection, WindDirectionFrom );
bool inRange = (0 <= normed && normed <= normedDirection( WindDirectionTo, WindDirectionFrom );
Out of curiosity, is there a (language independent*) way to make these two generic statements into one statement?
if (a < b) a += x;
else if (a > b) a -= x;
May also be used as
if (abs(b - a) < x) a = b;
else if (a < b) a += x;
else if (a > b) a -= x;
Oh, now that I'm writing this I'm thinking of something like this:
if (a != b) a += x * (a < b ? 1 : -1);
But that doesn't really benefit the readability. Other ways perhaps?
= Normal languages, not Brainfuck and the likes or really ancient ones
a += x * sgn(b-a)
Where sgn is the sign function:
sgn x | x > 0 = 1
| x < 0 = -1
| otherwise = 0
You should really wonder why you want this, the compiler will do fine optimizing and the given statement is very readable.
Language independent is a bit tricky, but if you have cmp or similar, you can use that:
a += x * cmp(b, a)
cmp(b, a) returns:
0 if b==a
1 if b > a
-1 if a < b
Also, there is a bug in your suggested implementation:
a = 9;
b = 10;
x = 2;
if (a < b) a += x;
if (a > b) a -= x;
print a;
Output: 9 (expected 11)
You need an else to fix this. Alternatively, if you want to prevent a going past b, do this:
if (a < b)
{
a = min(b, a + x);
}
else if (a > b)
{
a = max(b, a - x);
}
If you want to do this in one expression you can:
a = (a < b) ? min(b, a + x) : max(b, a - x);
I think the first way is clearer though.
a += (b - a) / abs(b - a) * x
But this is not language independent and does not really help readiness if you ask me....