Signed division not working in z3py - z3

I was trying to work with z3py. I came across a strange problem. I read that the operation / is treated as signed division for bit vectors in z3py. I was trying to give the following commands:
a = z3.BitVec('a',2)
b = z3.BitVec('b',2)
solver = z3.Solver()
solver.add((a + b)/2 == 3)
solver.check()
The result z3 gives is unsat
In my views this is not correct because it has a solution a = 2, b = 0, a = 2 in 2's complement means a = -2, so (a+b)/2 must be equal to -1 i.e. 3 by signed representation.
Can anyone please help me what is wrong here?

I finally found the solution myself!
The problem is in expression (a+b)/2. Here a and b are Bit Vectors of size 2. So the denominator 2 of the expression is also treated as a 2-bit Bit Vector by z3. So the 2 in denominator is actually -2. Thus the constraint I was trying to solve was (a+b)/(-2) == -1 This is actually unsat.

Related

Does Z3 have troubles with division?

During a project, I encountered a phenomenon I can't explain. It is boiled down in the following minimal example (using Z3Py) which should be unsat but returns a model.
from z3 import *
solver = Solver()
x = Int("x")
# The next proposition is clearly unsatisfiable
solver.add(And(x > 0, (x + 1) * (1 / (x + 1)) != 1))
solver.add(x == 1)
# Still, Z3 provides a "model"
print(solver.check())
print(solver.model())
Is there anything I am missing? Is it forbidden to use division in certain situations?
Z3 version: 4.8.14
Python version: 3.9
Note that when you use / over integers, than the result is an integer itself. That is, we use integer division; which in SMTLib is defined to be the Euclidian one. See https://smtlib.cs.uiowa.edu/theories-Ints.shtml for details.
This means that the expression 1 / (1+1) reduces to 0, since division produces an integer. So you end up with 0 != 1, which is true, and thus your problem is satisfiable.
If you want division to produce reals, then you should either start with a real x (i.e., x = Real("x")) in which case your problem will be unsat; or convert the arguments to be over the reals, via an expression of the form ToReal(x); which again will produce unsat for your problem.
So, in the first case, your modification will be:
x = Real('x')
casting the problem over reals. Or, if you want to keep x an integer, yet the division to be over the reals, you should use:
solver.add(And(x > 0, ToReal(x + 1) * (1 / ToReal(x + 1)) != 1))
which does the conversion.
I should note that mixing integers and reals in this way can lead to hard problems in general, and the solver might end up reporting unknown. But that's besides the point for your particular question here.

Maxima numerical integration syntax

I'm trying to obtain a numerical solution to the following integral:
1
The correct answer is -0.324 + 0.382i but as seen below I am not getting a numerical answer and would appreciate help with the Maxima syntax.
2
Perhaps related to why I am not getting a numerical output are two specific questions:
I read that e and i in Maxima need to be preceded by % in input but should these also appear as %e and %i as seen in the Maxima output?
Why is dy missing at the end of the integral in the Maxima output?
Thank you!
Looks to me like your input is okay, however, the function to compute approximations to integrals is named quad_qags. (There are actually several related functions. See ?? quad_ for more info.) Also, a wrinkle here is that the integrand is a complex-valued function (of a real variable), and quad_qags can only work on real-valued integrands, so we'll have to work around it. Here's how I would arrange it.
myintegrand: exp(%i*(1 + %i*y))/(1 + %i*y + 1/(1 + %i*y));
result_realpart: quad_qags (realpart (myintegrand), y, 0, 6);
result_imagpart: quad_qags (imagpart (myintegrand), y, 0, 6);
result: result_realpart[1] + %i*result_imagpart[1];
I get 0.3243496676292901*%i + 0.3820529930785175 as the final result. That's a little different from what you said; maybe a minus sign went missing? or there's a missing or extra factor of %i?
A quick approximation
0.1 * lsum (x, x, float (rectform (makelist (ev (myintegrand, y = k/10), k, 0, 60))));
seems to show the result from quad_qags is reasonable.

Cannot solve this algebraic equation in Maxima?

Hello i am trying to solve a algebric equation in maxima, the equation has alpha, delta and psi as variable. I want the alpha in equation to be solved in terms of psi and delta. I tried using the solve command but i am getting alpha in terms of alpha.
Here is the equation to solve Equation to solve
And this is the output from maxima
.
This is the code i am trying -->
solve([(sqrt(-4*alpha*delta*psi-4*delta*psi+alpha^2*delta^2)/(delta^2+delta)-(alpha*delta/(delta^2+delta)))/2-sqrt(4*alpha^2*delta^2+6*alpha*delta^2+3*delta^2+2*alpha^2*delta+4*alpha*delta+2*delta)/(3*delta^2+2*delta)+alpha*delta/(3*delta^2+2*delta)=0],alpha);
Thank you
The problem with that algebraic equation is that involves square roots,or radicals and normal polynomial, and that that type of equation is not easy to solve, take a look at this equation:
(%i30) solve(x=sqrt(x+6),x);
(%o30) x = sqrt{x+6}
So Maxima doesn't return any value, but for example other software Mathematica does.
Let's square both sides of equation and try to solve it
(%i31) solve(x^2=x+6,x);
(%o31) x=3 , x=-2
we get two solutions, let's try with first equation:
3 = sqrt(3+6) => 3 = sqrt(9) => 3 = 3
-2 = sqrt(-2+6) => -2 = sqrt(4) => -2 = 2 ??????
So the second solution is not valid,
maxima solve program in Macsyma/Maxima generally avoids methods that
produce false solutions, like "square both sides". It may still
make errors based on dividing by expressions that appear to be
non-zero, but actually ARE zero, and maybe some other similar
situations.
from this mailing list
In your case I will factor the equation to get a simplified version but with those free variable this will be difficult so, try to assume some values for psi and delta:
(%i26) solve(factor((sqrt(-4*alpha*delta*psi-4*delta*psi+alpha^2*delta^2)/(delta^2+delta)-(alpha*delta/(delta^2+delta)))/2-sqrt(4*alpha^2*delta^2+6*alpha*delta^2+3*delta^2+2*alpha^2*delta+4*alpha*delta+2*delta)/(3*delta^2+2*delta)+alpha*delta/(3*delta^2+2*delta))=0,alpha);
(\%o26) \left[ \alpha=\ifrac{\left(3\,\delta+2\right)\,\isqrt{\left(-4\,\alpha-4\right)\,\delta\,\psi+\alpha^2\,\delta^2}+\left(-2\,\delta-2\right)\,\isqrt{\left(4\,\alpha^2+6\,\alpha+3\right)\,\delta^2+\left(2\,\alpha^2+4\,\alpha+2\right)\,\delta}}{\delta^2} \right]
Expand your Equation and try to remove square roots or some assumptions:
Equation : (sqrt(-4*alpha*delta*psi-4*delta*psi+alpha^2*delta^2)/(delta^2+delta)-(alpha*delta/(delta^2+delta)))/2-sqrt(4*alpha^2*delta^2+6*alpha*delta^2+3*delta^2+2*alpha^2*delta+4*alpha*delta+2*delta)/(3*delta^2+2*delta)+alpha*delta/(3*delta^2+2*delta);

Normalize a feature in this table

This has become quite a frustrating question, but I've asked in the Coursera discussions and they won't help. Below is the question:
I've gotten it wrong 6 times now. How do I normalize the feature? Hints are all I'm asking for.
I'm assuming x_2^(2) is the value 5184, unless I am adding the x_0 column of 1's, which they don't mention but he certainly mentions in the lectures when talking about creating the design matrix X. In which case x_2^(2) would be the value 72. Assuming one or the other is right (I'm playing a guessing game), what should I use to normalize it? He talks about 3 different ways to normalize in the lectures: one using the maximum value, another with the range/difference between max and mins, and another the standard deviation -- they want an answer correct to the hundredths. Which one am I to use? This is so confusing.
...use both feature scaling (dividing by the
"max-min", or range, of a feature) and mean normalization.
So for any individual feature f:
f_norm = (f - f_mean) / (f_max - f_min)
e.g. for x2,(midterm exam)^2 = {7921, 5184, 8836, 4761}
> x2 <- c(7921, 5184, 8836, 4761)
> mean(x2)
6676
> max(x2) - min(x2)
4075
> (x2 - mean(x2)) / (max(x2) - min(x2))
0.306 -0.366 0.530 -0.470
Hence norm(5184) = 0.366
(using R language, which is great at vectorizing expressions like this)
I agree it's confusing they used the notation x2 (2) to mean x2 (norm) or x2'
EDIT: in practice everyone calls the builtin scale(...) function, which does the same thing.
It's asking to normalize the second feature under second column using both feature scaling and mean normalization. Therefore,
(5184 - 6675.5) / 4075 = -0.366
Usually we normalize all of them to have zero mean and go between [-1, 1].
You can do that easily by dividing by the maximum of the absolute value and then remove the mean of the samples.
"I'm assuming x_2^(2) is the value 5184" is this because it's the second item in the list and using the subscript _2? x_2 is just a variable identity in maths, it applies to all rows in the list. Note that the highest raw mid-term exam result (i.e. that which is not squared) goes down on the final test and the lowest raw mid-term result increases the most for the final exam result. Theta is a fixed value, a coefficient, so somewhere your normalisation of x_1 and x_2 values must become (EDIT: not negative, less than 1) in order to allow for this behaviour. That should hopefully give you a starting basis, by identifying where the pivot point is.
I had the same problem, in my case the thing was that I was using as average the maximum x2 value (8836) minus minimum x2 value (4761) divided by two, instead of the sum of each x2 value divided by the number of examples.
For the same training set, I got the question as
Q. What is the normalized feature x^(3)_1?
Thus, 3rd training ex and 1st feature makes out to 94 in above table.
Now, normalized form is
x = (x - mean(x's)) / range(x)
Values are :
x = 94
mean(89+72+94+69) / 4 = 81
range = 94 - 69 = 25
Normalized x = (94 - 81) / 25 = 0.52
I'm taking this course at the moment and a really trivial mistake I made first time I answered this question was using comma instead of dot in the answer, since I did by hand and in my country we use comma to denote decimals. Ex:(0,52 instead of 0.52)
So in the second time I tried I used dot and works fine.

Minimum and maximum values of integer variable

Let's assume a very simple constraint: solve(x > 0 && x < 5).
Can Z3 (or any other SMT solver, or any other automatic technique)
compute the minimum and maximum values of (integer) variable x that satisfies the given constraints?
In our case, the minimum is 1 and the maximum is 4.
Z3 has not support for optimizing (maximizing/minimizing) objective functions or variables.
We plan to add this kind of capability, but it will not happen this year.
In the current version, we can "optimize" an objective function by solving several problems where in each iteration we add additional constraints. We know we found the optimal when the problem becomes unsatisfiable. Here is a small Python script that illustrates the idea. The script maximizes the value of a variable X. For minimization, we just have to replace s.add(X > last_model[X]) with s.add(X < last_model[X]). This script is very naive, it performs a "linear search". It can be improved in many ways, but it demonstrates the basic idea.
You can also try the script online at: http://rise4fun.com/Z3Py/KI1
See the following related question: Determine upper/lower bound for variables in an arbitrary propositional formula
from z3 import *
# Given formula F, find the model the maximizes the value of X
# using at-most M iterations.
def max(F, X, M):
s = Solver()
s.add(F)
last_model = None
i = 0
while True:
r = s.check()
if r == unsat:
if last_model != None:
return last_model
else:
return unsat
if r == unknown:
raise Z3Exception("failed")
last_model = s.model()
s.add(X > last_model[X])
i = i + 1
if (i > M):
raise Z3Exception("maximum not found, maximum number of iterations was reached")
x, y = Ints('x y')
F = [x > 0, x < 10, x == 2*y]
print max(F, x, 10000)
As Leonardo pointed out, this was discussed in detail before: Determine upper/lower bound for variables in an arbitrary propositional formula. Also see: How to optimize a piece of code in Z3? (PI_NON_NESTED_ARITH_WEIGHT related).
To summarize, one can either use a quantified formula, or go iteratively. Unfortunately, these techniques are not equivalent:
Quantified approach needs no iteration, and can find global min/max in a single call to the solver; at least in theory. However, it does give rise to harder formulas. So, the backend solver can time-out, or simply return "unknown".
Iterative approach creates simple formulas for the backend solver to deal with, but it can loop forever if there's no optimal value; simplest example being trying to find the largest Int value. Quantified version can solve this problem nicely by quickly telling you that there is no such value, while the iterative version would go on indefinitely. This can be a problem if you don't know ahead of time that your constraints do have an optimal solution. (Needless to say, the "sufficient" iteration count is typically hard to guess, and might depend on random factors, like the seed used by the solver.)
Also keep in mind that if there is a custom optimization algorithm for the problem domain at hand, it's unlikely that a general purpose SMT solver can outperform it.
z3 now supports optimization.
from z3 import *
o = Optimize()
x = Int( 'x' )
o.add(And(x > 0, x < 5))
o.maximize(x)
print(o.check()) # prints sat
print(o.model()) # prints [x = 4]
This particular problem is an integer program.

Resources