simplify relational operators with logic? - sympy

How to simplify an expression with combined relational operators?
for example: x < 10 & x < 4 should return x < 4

There are a few ways to handle this. Here are two:
>>> var('x',real=True)
x
>>> solve((x<10, x<4))
x < 4
>>> And(x<10, x<4).as_set().as_relational(x)
x < 4

Related

Why is the output 0 (=false)?

I'm exercising opreators in C++ and I don't understand the output of the code bellow
int x = 21, z = 33, y = 43;
cout << (!(z < y&& x < z) || !(x = z - y)) << endl;
I wrote it with the thought to be true and I understand it as "it's not the case z is less than y and x is less than z (which is false) or it's not the case x is equal to the difference of z and y (which is true)" so I expected output 1 (=true) and I'm confused that's not the case. Can you explain me where I'm making a mistake?
edit: Thanks for the answers, it's funny how I made such trivial mistakes I actually read about.
The part that you misinterpreted:
!(x = z - y))
x = z - y is assignment. It yields -10 as result. -10 is not 0, hence negating it yields false.
Now, first part of the expression:
!(z < y&& x < z)
!(33 < 43 && 21 < 33)
!(true && true)
!(true)
false
Putting it together:
(false || false) == false
This = is an assignment operator. It assigns values.
This == is a comparison operator. It is used to compare two values.
int value = 5, value2 = 12;
if(value == value2)
{
// do something if value and value2 are EQUAL (which they are not)
}
See this link for more information on operators in C++.

Python One line loop two functions

I am currently running the code below in Python. What I need to know is how I can store the value of y in the line V=max(u(j) + y for y in set2). This line gets the maximum sum of each value for set1 and set2. In other words for the first iteration of the code, where the maximum value is 3 (1 for set1 and 2 for set2) then I would like to store the number 2 somewhere.
set1=[1,2,3]
set2=[1,2]
u=lambda c: c**(1)
for j in set1:
V=max(u(j) + y for y in set2)
print (V)
else:
V=0
I know one solution is to do an additional loop, I prefer to do it in one line.
Regards,
You can keep y along in some container (a tuple is the natural choice here):
>>> set1 = [1,2,3]
>>> set2 = [1,2]
>>> def u(c): return c**(1)
...
>>> for j in set1:
... V, y = max((u(j) + y, y) for y in set2)
... print(V, y)
...
3 2
4 2
5 2
As an aside, I have no idea what your else-clause is suppose to do, but I hope you know it is always executed, so V will always be 0.
Also, note that the y on the left-hand side of the assignment doesn't have anything to do with the y in the generator expression, which has it's own scope. I could have easily called it whatever I wanted:
>>> for j in set1:
... V, fish = max((u(j) + y, y) for y in set2)
... print(V, fish)
...
3 2
4 2
5 2

Mixing integer arithmetic with boolean - Z3 prover

Before reading this question please consider that it is intended for use with the Z3 solver tool and it's c++ api (everything is redefined so it's not normal c++ syntax)
Can someone explain how do I mix boolean logic with integers (programing wise)?
Example:
y = (x > 10 and x < 100) //y hsould be true or false (boolean)
z = (y == true and k > 20 and k < 200)
m = (z or w) //suppose w takes true of false (boolean)
I tried with the examples given in the c++ file but I can't figure out how it works when mixing integer arithmetic and boolean.
Writing answer assuming you a beginner of c++.
May be you are looking for this.
bool y,z,m,w;
int x, k;
y = (x>10 && x<100);
z = (y == true && k > 20 && k < 200);
m = (z || w);
Let see what this line means:
y = (x>10 && x<100);
here if x is greater than 10 x>10 results true. In the same way if x is less than 100 x<100 results true. if both of them are true, the right side results true, which will be assigned to y.
|| means or.

Python My programming Lab

I have a problem doing one of the questions from my programming lab.
The question was like "Given the variables x, y, and z, each associated with an int, write a fragment of code that assigns the smallest of these to min."
And my work area looks like
if x < y and x < z:
x = min
if y < x and y < z:
y = min
if z < x and z < y:
z = min
When I turned in, the feedback said:
Remarks:
⇒ Unexpected identifiers: and
More Hints:
⇒ Solutions with your approach don't usually use: <
although I tried may ways to figure it out but none of them worked.
Plz help.
Thx.
For the first reply, the system said:
Remarks:
⇒ Unexpected identifiers: and
More Hints:
⇒ Solutions with your approach don't usually use: <
Problems Detected:
⇒ Exception occurred(, TypeError('unorderable types: int() < builtin_function_or_method()',), )
⇒ Exception occurred(, TypeError('unorderable types: int() < builtin_function_or_method()',), )
⇒ Exception occurred(, TypeError('unorderable types: int() < builtin_function_or_method()',), )
⇒ Exception occurred(, TypeError('unorderable types: int() < builtin_function_or_method()',), )
⇒ min does not contain the correct value
⇒ y was modified
⇒ z was modified
For the second reply, the system said:
Remarks:
⇒ Unexpected identifiers: and, def, minist
⇒ You have to use the min variable .
⇒ You should use an assignment operator (=) in this exercise.
More Hints:
⇒ You almost certainly should be using: =
⇒ You almost certainly should be using: min
⇒ I haven't yet seen a correct solution that uses: , (comma)
I see three issues with the code as you've shown it in the question.
The first is that your indentation is a bit messed up. You probably want all your if statements to be at the same level, with the corresponding assignment statements indented under each one:
if something:
foo = bar
if something_else:
foo = baz
# etc
You could also use elif statements instead of the second and third if, since you never expect to have more than one of the conditions true at the same time.
The second issue is that you're doing your assignment statements backwards. If you want to assign the value in a variable x to a new variable named min, you should put min on the left side and x on the right:
min = x
This is part of the reason you're getting lots of confusing errors. Before your code runs, min is the name of a builtin function (which I suspect you'll learn about later in your class). When you did x = min you were replacing the old x value with a reference to the function.
The last thing is a logic issue. Your comparisons are all using the < operator to test if one value is smaller than the others. The issue is what happens if two of the values are equal. If x and y have the same value, both x < y and y < x will be False. That's not very good for your code, and so you probably want to use the <= operator instead. It tests if the left side is less than or equal to the right side. With this version, if you're using elifs, you can actually do away with the last condition and just use else, since if neither x or y is less than or equal to the other values, z must be the smallest value by process of elimination.
Anyway, here's the code with all three issues fixed:
if x <= y and x <= z:
min = x
elif y <= x and y <= z:
min = y
else: # no "z <= x and z <= y" check needed here, will always be true if reached
min = z
Maybe the tab problem.
You can try this:
if x < y and x < z:
x = min
if y < x and y < z:
y = min
if z < x and z < y:
z = min
I make a sample:
#-*- coding:utf-8 -*-
def minist(x,y,z):
if x < y and x < z:
return x
if y < x and y < z:
return y
if z < x and z < y:
return z
x = 1
y = 2
z = 3
test = -1
test = minist(x,y,z)
print(test)
This sample will print 1.

Calculate series: x = (x*7 + y) >> 3

I'm reading the UDT source. That is a excellent project, but it is so hard to understand some expressions:
m_iRTT = (m_iRTT * 7 + rtt) >> 3;
m_iBandwidth = (m_iBandwidth * 7 + *((int32_t *)ctrlpkt.m_pcData + 5)) >> 3;
In short expression form:
x = (x*7 + y) >> 3
Can someone explain what this expression does ?
To spell out x = (x*7 + y) >> 3:
x is multiplied by 7, then the result added to y, then that result shiftted 3 bits to the right and assigned to x.