Calculate minimum set of values which satisfies a conditional expression? - conditional-statements

I have a conditional expression:
if( (x > 0) && (x < 10) || (x == -2))
What I'm doing currently whenever there is a ">" symbol I'm just adding a value and in case of "<" symbol I'm subtracting a value.
So, in this case the x values are: 1, 9, -2. But, I want here is minimum number of values.
As per my logic: {1, 9, -2}
Minimum set = {1,-2}

Related

How to convert the following if conditions to Linear integer programming constraints?

These are the conditions:
if(x > 0)
{
y >= a;
z <= b;
}
It is quite easy to convert the conditions into Linear Programming constraints if x were binary variable. But I am not finding a way to do this.
You can do this in 2 steps
Step 1: Introduce a binary dummy variable
Since x is continuous, we can introduce a binary 0/1 dummy variable. Let's call it x_positive
if x>0 then we want x_positive =1. We can achieve that via the following constraint, where M is a very large number.
x < x_positive * M
Note that this forces x_positive to become 1, if x is itself positive. If x is negative, x_positive can be anything. (We can force it to be zero by adding it to the objective function with a tiny penalty of the appropriate sign.)
Step 2: Use the dummy variable to implement the next 2 constraints
In English: if x_positive = 1, then y >= a
However, if x_positive = 0, y can be anything (y > -inf)
y > a - M (1 - x_positive)
Similarly,
if x_positive = 1, then z <= b
z <= b + M * (1 - x_positive)
Both the linear constraints above will kick in if x>0 and will be trivially satisfied if x <=0.

Check a row for ascension in Numpy, but ignoring elements = 0

I have the code snippet below that searches each row/column in an array to see if all values are either ascending or descending. Ideally, this code would ignore zeros. For example, a row with (5, 0, 3, 1) would come up True for descending. The code below still looks at the zeros. If the masked technique is a dead end, maybe I could create a copy without zeros? I'm very new to Numpy so I would appreciate specific directions. Thanks!
np.ma.masked_equal(grid, 0)
for row in grid:
if np.all(np.diff(row) <= 0) or np.all(np.diff(row) >= 0):
monoScore += .5
for col in np.transpose(grid):
if np.all(np.diff(col) <= 0) or np.all(np.diff(col) >= 0):
monoScore += .5

100 <= x <= 150 as argument in if (), acting funny

I have an if statement followed by several else if statements. All of the if/else if statements have an argument structured something like this:
if (100 <= x <= 149) //do this
else if (150 <= x <= 199) //do that
else if ...etc...
However, for some reason only the first if statement ever gets executed. X can be 200 and only the first if statement will be recognized.
I'm not sure why it isn't moving on to the next else if statement when X doesn't fit the argument of the preceding statement. Does this not work in Obj-C? Any help is appreciated. Thanks
You need to rephrase the statements like:
if (x >= 100 && x <= 149) {
} else if (x >= 150 && x <= 199) {
} ...
Your first if is evaluated like:
if ((100 <= x) <= 149)
Let's have a look how that evaluates:
If x = 200, then (100 <= 200) is true and thus evaluates to the value 1 (which means true). And then 1 <= 149 is also true.
If x has a value smaller than 100, for example 10, then (100 <= 10) is false and thus evaluates to the value 0 (which means false). Again, 0 <= 149 is true.
So regardless of the value of x, the whole expression will always be true.
C is not math, so
if (100 <= x <= 149)
is NOT the same as
if (100 <= x && x <= 149)
Which is what you meant. The former will be true always, because 100 <= x <= 149 becomes
((100 <= x) <= 149)
leaving two possibilities: (1 <= 149) or (0 <= 149). Both are always true.
Chained comparisons like these don't work in C-based languages. Or rather, they do, but not how you'd expect.
100 <= x <= 149 gets evaluated as (100 <= x) <= 149. If x is over 100, then (100 <= x) will evaluate to true, or 1. If x is under 100, it's false, or 0. In either case, 0 <= 149 or 1 <= 149 is true, so the overall expression is true.
To fix this, change your conditions to look like this:
if (100 <= x && x <= 149)
That will make it work as you expect.
The compiler sees an expression formulated from binary operators. The <= symbol is a binary operator, as are =, >=, ||, &&, and so forth.
Just as with arithmetic, there is an order of precedence that the compiler must follow, evaluating the expression formed around each binary operator.
In arithmetic, you are probably familiar with this, as with these two examples:
2+5*7 This evaluates to 2+(5*7) or 37, because multiplication has precedence over addition.
2+3+21 is evaluated in the order the terms are read from left to right, since there is no other precedence rule. It becomes (2+3)+21.
So 100<=x<=150 is an expression of a similar type, where the binary operators are all the same, and thus have the same precedence. Once again, this is resolved by evaluating it from left to right, so it becomes (100<=x)<=150. If x>=100 the term in parentheses evaluates to TRUE, which has a numeric value of 1. Since 1 is less than 150, the rest evaluates to 1<=150, or TRUE, if x is greater than or equal to 100. On the other hand, it also evaluates to TRUE if x is less than 100, because the second comparison becomes 0<=150, which is TRUE.
The other recommendations to break this down with parentheses are correct if you aren't sure of the order of precedence for binary operators: (100<=x) && (x<=150). You can also write it as 100<=x && x<=150 since the order of precedence for value comparisons is higher than for logical operators.
Because if (100 <= x <= 149) is the same as if(1<=149) if you give 200 or another number to x. And that is correct always.
For example.
x=1
100<=1 is false so you get if(0<=149) which is true
x=200
100<=200 is true so you get if(1<=149) which is true
So you always get true for it.
So you must do it in another way, like this
if(x>=100 && x<=149) ...
Adding some additional parenthesis may help.
if ((100 <= x) && (x <= 149)) //do this
I don't think you can write math functions like this in objective-c... Try separating them and combining with an && statement:
if ( (100 <= x) && (x <= 149) ) { // "&&" = and, "||" = or, other math comparison operators are: <=, >=, <, >, ==, != (!= is does not equal))
//do this
} else if ( (150 <= x) && (x <= 199) ) {
//do that
} else if ...etc...
You've already got a lot of answers, but I'll add one more to cover one other possible point of confusion.
In C & Obj-C the boolean (and character) types are treated as integer types, which is not the case in call languages. So expressions like 'z' * true make perfect sense!
(Modern) C uses the type _Bool for boolean, which is defined to be large enough to hold 0 & 1. Cocoa uses the type BOOL for boolean, which is defined as signed char. CoreFoundation uses the type Boolean which is defined as unsigned char. All three define YES/true as 1 and NO/false as 0, while C itself treats any non-zero value as true.
The relation operators such as <, <= etc. are defined to return the int (yes, none of the booleans, not even _Bool) value 0 if the relation is false, and the int value 1 if the relation is true.
Given this and the left-to-right associativity of relational operators your:
if (100 <= x <= 149)
is parsed as:
if ((100 <= x) <= 149)
then 100 <= x evaluates to the int value 1 if x is greater than or equal to 100, otherwise it evaluates to the int value 0, so we get:
if (1 <= 149)
or
if (0 <= 149)
both of these evaluate to 1 so we get:
if (1)
and the if statement branches to the "then" branch if it's expression is non-zero.
It may be surprising, but the whole statement is evaluated without any use of booleans at all - it is all done with integers.
To achieve what you intended you need:
if((100 <= x) && (x <= 149))
etc. - which also doesn't use any booleans (&& is defined in terms of integers).

Mathematica: how to find the max of an expression with exponents as parameters

I'm using Mathematica 8 to find an analytic solution to the max of an expression. When I use the Maximize command to try to find a solution, it just repeats what I entered, implying that Mathematica doesn't know how to do it. I've narrowed down the problem to this: it seems like if there is an exponent that is a parameter, Maximize doesn't work. Here's an example. This is the likelihood function from a Bernoulli trial, where a and b are the successes and failures.
Maximize[{t^a*(1 - t)^b, {t >= 0, t <= 1, a > 0, b > 0}}, {t}]
What I would like to get as a solution is a/(a+b) in this case. If I provide constants like 3 and 2 instead of a and b then it finds the solution.
Is there a different way to specify the expression or the constraints so that Mathematica can find a maximum to expressions with exponents that are parameters? I feel like there's something I'm missing because this is so simple.
I've been playing with it, i.e. moving conditions, changing expression form, removing conditions, and I can't get Maximize to behave, either. However, this can be solved directly, as follows
Solve[ D[ t^a (1 - t)^b, t ] == 0, t]
which gives, as you said, {{t -> a/(a + b)}}. Sometimes Reduce can be used to help understand why a function like Maximize misbehaves by giving a more complete picture of the solution space. It is invoked like Solve, as follows
Reduce[ D[ t^a (1 - t)^b, t ] == 0, t]
giving
((-1 + t) t != 0 && a == 0 && b == 0) ||
(a + b != 0 && a b != 0 && t == a/(a + b)) ||
(Re[b] > 1 && t == 1) ||
(Re[a] > 1 && t == 0)
which isn't all that helpful, in this case.
The Maximize function in Mathematica, applied on an exponential function, only works if you maximize with respect to all parameters (a, b and t in your case). Now, you only maximize with respect to t, which does not work.
Consider this easy example (using Mathematica 8.0):
Maximize[{Exp[a + b], a <= 1, b <= 1}, {a, b}]
Maximize[{Exp[a + b], a <= 1, b <= 1}, {a}]
Maximize[{a + b, a <= 1, b <= 1}, {a}]

Could abs(random()) % someNumberGreaterThanZero return zero?

SELECT foo FROM bar
WHERE id >= (abs(random()) % (SELECT max(id) FROM bar))
LIMIT 1;
I saw this in another answer as an alternative to ORDER BY random(). I need to make sure id would always be greater than zero. Do I have to change >= to >?
Since x % y returns 0 when x is a multiple of y, the answer is "Yes, your expression could return 0".
So, if id must be greater than 0, you need to use > rather than >=. Of course, if the modulo operator didn't return 0, you could still use > instead of >= and you'd get the same effect.
Yes. If abs(random()) returned the value of max(id), then the modulo's result would be zero. Since abs(random()) can return any value between 0 & 9223372036854775807, this is definitely possible.
Yes it can return 0 in two ways
Consider that 3 % 3 == 0, 6 % 3 == 0, etc. Then you would get 0 if random() happens to be max(id) or an even divider thereof.
random() can also return 0 and 0 % anything == 0, that is the other possibility.
Yes, it should be > because modulo division can return 0( a mod a ==0, 0 mod a == 0). Also, you might want to check if (SELECT max(id) is not null/0 (a mod 0 is undefined in some systems, or a)