Is it possible to write (0 < x <5) in a conditonal statement. Where x is a variable. IN PYTHON - variables

I set variable x = 0. But under a while statement I changed it to x+=1. I'm trying to account for when x is in between 1 and 4 while also accounting for when x is over 4.

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.

Why is my conditional expression != ignored?

Here's my Fibonacci code using python 3.5
z = 0
x = 0
y = 1
while z != 317811:
x = x + y
z = x
print (z)
y = x + y
z = y
print (z)
I am wondering why this prints to infinity when setting the condition to
z != 317811
but works when it is below this number like
z != 196418
or a number greater than this like
z!= 514229
I tried a different approach (z <= 317811) but it prints up to 514229.
Thank you for your time.
KD
You're only testing alternate Fibonnaci numbers as the stopping condition: 317811 is getting missed.
One fix would be to test both x and y.
this is just logical problem
you are printing two
z != 317811
for this condition
"z"
is updated twice once in first z assignment i.e z = x
but "z" again get updated at second assignment z = y and then "z" is not sutisfying the condition(z != 317811) and not equal to 317811 but it is now 514229
Note: it will always work for number being printed at the second steps as this value of Z will be compared in while condition in loop
You are increasing z value twice a loop, but only checking once.
What actually is happening is that z is increasing with the fibonacci serie. Last values of z are:
196418
317811
514229
But you are only checking the stop condition once every two assignment. In this case you are checking that 196418 != 317811 and 514229 != 317811, thus never matching it.
One possible fix could be to test if z != 317811 after the first print. Even if in this case I would prefer testing "<" instead of "!="

What does a percentage sign (%) do mathematically in Objective C?

I am super confused what the percentage sign does in Objective C. Can someone explain to me in language that an average idiot like myself can understand?! Thanks.
% is the modulo operator, so for example 10 % 3 would result in 1.
If you have some numbers a and b, a % b gives you just the remainder of a divided by b.
So in the example 10 % 3, 10 divided by 3 is 3 with remainder 1, so the answer is 1.
If there is no remainder to a divided by b, the answer is zero, so for example, 4 % 2 = 0.
Here's a relevant SO question about modular arithmetic.
Same as what it does in C, it's "modulo" (also known as integer remainder).
% is the modulo operator. It returns the remainder of <number> / <number>. For example:
5 % 2
means 5 / 2, which equals 2 with a remainder of 1, so, 1 is the value that is returned. Here's some more examples:
3 % 3 == 0 //remainder of 3/3 is 0
6 % 3 == 0 //remainder of 6/3 is 0
5 % 3 == 2 //remainder of 5/3 is 2
15 % 4 == 3 //remainder of 15/4 is 3
99 % 30 == 9 //remainder of 99/30 is 9
The definition of modulo is:
mod·u·lo
(in number theory) with respect to or using a modulus of a specified number. Two numbers are congruent modulo a given number if they give the same remainder when divided by that number.
In Mathematics, The Percentage Sign %, Called Modulo (Or Sometimes The Remainder Operator) Is A Operator Which Will Find The Remainder Of Two Numbers x And y. Mathematically Speaking, If x/y = {(z, r) : y * z + r = x}, Where All x, y, and z Are All Integers, Then
x % y = {r: ∃z: x/y = (z, r)}. So, For Example, 10 % 3 = 1.
Some Theorems And Properties About Modulo
If x < y, Then x % y = x
x % 1 = 0
x % x = 0
If n < x, Then (x + n) % x = n
x Is Even If And Only If x % 2 = 0
x Is Odd If And Only If x % 2 = 1
And Much More!
Now, One Might Ask: How Do We Find x % y? Well, Here's A Fairly Simple Way:
Do Long Division. I Could Explain How To Do It, But Instead, Here's A Link To A Page Which Explains Long Division: https://www.mathsisfun.com/numbers/long-division-index.html
Stop At Fractions. Once We Reach The Part Where We Would Normally Write The Answer As A Fraction, We Should Stop. So, For Example, 101/2 Would Be 50.5, But, As We Said, We Would Stop At The Fractions, So Our Answer Ends Up Being 50.
Output What's Left As The Answer. Here's An Example: 103/3. First, Do Long Division. 103 - 90 = 13. 13 - 12 = 1. Now, As We Said, We Stop At The Fractions. So Instead Of Continuing The Process And Getting The Answer 34.3333333..., We Get 34. And Finally, We Output The Remainder, In This Case, 1.
NOTE: Some Mathematicians Write x mod y Instead Of x % y, But Most Programming Languages Only Understand %.

Counting occurrences of values in spss

I have 50 variables, named w1 to w50, and each holds a value from 1 to 20. I want to create variables showing the number of occurrences of each of these values. This is what I'd like to do, but SPSS seems to have a problem with me using #n in the COUNT command.
COMPUTE #n = 1 .
DO REPEAT x = num1 to num20 .
COMPUTE x = 0 .
COUNT x = w1 to w50 (#n) .
COMPUTE #n = #n + 1 .
END REPEAT .
This is the error message I get:
Error # 4772 in column 24. Text: #n
On the COUNT command, the parenthesized value list is syntactically invalid.
Execution of this command stops.
You cannot supply a variable as the value list in the COUNT command. Fortunately the work around for your example is quite simple - you can use a stand in increment on the DO REPEAT:
DO REPEAT x = num1 to num20 /#i = 1 to 20.
COUNT x = w1 to w50 (#i).
END REPEAT.
Full example below.
**********************************************.
*creating fake data.
data list free / ID.
begin data
1
2
end data.
vector w(50,F2.0).
loop #i = 1 to 50.
compute w(#i) = TRUNC(RV.UNIFORM(1,21)).
end loop.
vector num(20,F2.0).
execute.
*making new vector.
DO REPEAT x = num1 to num20 /#i = 1 to 20.
COUNT x = w1 to w50 (#i).
END REPEAT.
EXECUTE.
**********************************************.

Variable created from another variable in AMPL

In AMPL I have a set of variables x[e], for some calculations I need a binary variable w[e] which equals 1 when x[e] > 0 and 0 if x[e] = 0. I tried a lot of stuff to make this constraint, but I failed to come up with something. Is this possible?
I have solved your problem the following way:
var u binary;
this is our binary variable that will be 0 or 1.
Then we put following constraint:
subject to U_constraint :
x <= 999999 * u;
Now when x = 0 then AMPL will make u = 0 and when x != 0 obviously u = 1.