Assign value to specific bit of BitVecExpr in Z3 - z3

Here is my verilog statement:
reg[2:0] a; // Create register 'a' which is 3 bit.
assign a[1] = 1'b1; // Assigning value to 1st bit of register 'a'.
I have to implement the statement above in Z3.
For the 1st line of the verilog statement using BitVecExpr:
BitVecExpr a = ctx.mkBVConst("a",3);
I am a facing problem while implementing 2nd line of verilog statement.
Does anyone know how to implement this in Z3?

With Z3 you can't modify variables. In fact Z3 doesn't call this a variable, it's a constant.
You need to make a new constant that is related to the old constant. For example if you want to say y = x + 1 this would be
var y = ctx.MkBVAdd(x, 1);
If you want to say x = x + 1 you need to introduce a new name for the old and for the new x:
var x2 = ctx.MkBVAdd(x1, 1);

Related

Adding quadratic constraints in docplex

I have problem by writing quadratic constraints by docplex in python
This is my constraint:
A[i,j,k]=Z[i,j,k] *h[i,j,k]+Q[i,j,k]*d[i,j,k] ∀i,j,k
I wrote the constraint as below:
mdl.add_quadratic_constraints(z_qsd[i,j,k]*h_qsd[i,j,k]+Q_qsd[i,j,k]*d_qsd[i,j,k]==A_qsd[i,j,k] for i in q_ualification for j in s_hift for k in d_ay)
when I solve the model, I get this message:
Model is non-convex
The multiplication operator '*' has been overloaded in DOcplex to write quadratic expression. In other terms, you can multiply two variables.
Forgetting about indices, and assuming you have only four variables A,z,h,Q
the constraint can be written as:
mdl.add(A == Z *h + Q *d)
Now, for three-dimensional variables, you should use a variation of Model.continuous_var_cube (change the type), for example:
As = mdl.continous_var_cube(3,5,7, "A")
which builds a Python dictionary of variables, indexed by tuples of indices in the cartesian product of the
three ranges (1..3)x(1..5)x(1..7), that is 3x5x7 = 85 variables.
Each variable can be accessed by a collection of indices,
as in
a123 = As[1,2,3]
Here is a small quadratic-objective
problem example, inspired by the famous "zoo" examples from Alex Fleischer
(see https://www.linkedin.com/pulse/making-optimization-simple-python-alex-fleischer/ for more)
mdl = Model(name='buses')
nbbus40 = mdl.integer_var(name='nbBus40')
nbbus30 = mdl.integer_var(name='nbBus30')
mdl.add_constraint(nbbus40*40 + nbbus30*30 >= 300, 'kids')
# X**2 is the square of variable X
mdl.minimize(500 * nbbus40**2 + 400 * nbbus30**2)
mdl.solve()
for v in mdl.iter_integer_vars():
print(v," = ",v.solution_value)

Is there a way to return a pair of integers using the let construct in standard ML?

I am trying to return a pair of sums using the let construct in sml. Every way I have tried will only return one value. I have tried creating a list by using cons (::) and then returning the list, but that gives an error as well.
val t = [(3,4), (4,5), (5,6)];
fun sumPairs(nil) = 0
| sumPairs((x,y)::zs) =
let
val sumFirst = x + sumPairs(zs)
val sumSecond = y + sumPairs(zs)
in
(sumFirst, sumSecond) <how would I return this as a tuple or list?>
end;
sumPairs(t);
The problem is not with (sumFirst, sumSecond) or with let specifically, but with the rest of your code.
The base case and the recursions say that sumPairs produces an int, not a pair of ints.
Because of this, there is a conflict when you try produce a pair.
Your base case should be (0,0), not 0, since it must be a pair.
You also need to deconstruct the result from the recursion since that produces a pair, not an integer.
Like this
fun sumPairs nil = (0, 0)
| sumPairs ((x,y)::zs) =
let
val (sumFirst, sumSecond) = sumPairs zs
in
(x + sumFirst, y + sumSecond)
end;

Julia - Preallocating for sparse matrices

I was reading about preallocation from Performance Tips and it has this example:
function xinc!(ret::AbstractVector{T}, x::T) where T
ret[1] = x
ret[2] = x+1
ret[3] = x+2
nothing
end
function loopinc_prealloc()
ret = Array{Int}(3)
y = 0
for i = 1:10^7
xinc!(ret, i)
y += ret[2]
end
y
end
I see that the example is trying to change ret which is preallocated. However, when I tried the following:
function addSparse!(sp1, sp2)
sp1 = 2*sp2
nothing
end
function loopinc_prealloc()
sp1 = spzeros(3, 3)
y = 0
for i = 1:10^7
sp2 = sparse([1, 2], [1, 2], [2 * i, 2 * i], 3, 3)
addSparse!(sp1, sp2)
y += sp1[1,1]
end
y
end
I don't think sp1 is updated by addSparse!. In the example from Julia, function xinc! modifies ret one by one. How can I do the same to a sparse matrix?
In my actual code, I need to update a big sparse matrix in a loop for the sake of saving memory it makes sense for me to preallocate.
The issue is not that the Matrix is sparse. The issue is that when you use the assignment operator = you assign the name sp1 to a new object (with value 2sp2), rather than updating the sp1 matrix. Consider the example from performance tips: ret[1] = x does not reassign ret it just modifies it's elements.
Use the .= operator instead to overwrite all the elements of a container.

Swift Range Operator with two unknown values

If I have two unknown values, lets say x and y, what is the best way loop through all of the values between between those values?
For example, given the values x = 0 and y = 5 I would like to do something with the values 0, 1, 2, 3, 4, and 5. The result could exclude 0 and 5 if this is simpler.
Using Swift's Range operator, I could do something like this:
for i in x...y {
// Do something with i
}
Except I do not know if x or y is the greater value.
The Swift documentation for Range Operators states:
The closed range operator (a...b) defines a range that runs from a to b, and includes the values a and b. The value of a must not be greater than b.
There are a number of solutions here. A pretty straight forward one is:
let diff = y - x
for i in 0...abs(diff) {
let value = min(x, y) + i
// Do something with value
}
Is there a better, or more elegant way to achieve this?
I guess the most explicit way of writing it would be:
for i in min(a, b)...max(a, b) {
// Do something with i
}
To exclude the first and last value, you can increment your lower limit and use the Swift ..< syntax:
let lowerLimit = min(a, b) + 1
let upperLimit = max(a, b)
for i in lowerLimit..<upperLimit {
// Do something with i
}

Currying confusing about assignment

I have following code snippet that use currying:
let multiply x y = x * y
let double = multiply 2
let ten = double 5
I understand the above code, because I remember this code:
Currying is converting a single function of n arguments into n
functions with a single argument each
And then I encounter the following code:
let double2 z = multiply 2 z
double2 5
I do not understand this code at all. Why double2 can be a function?
What's happening in your latter example is really nothing special.
You can basically read it as:
define a function double2 with one argument z,
which is defined as multiply 2 z
Some people might refer to this as "currying" or "partial function evaluation" but really all that's happening here is that you're defining a function that uses another function in its function body.
let double2 z = multiply 2 z
// ^ here you define a parameter
This turns it into a function.

Resources