getting unknown result with a trivial forall - c++

I am using th z3 C++ API. if I create this simple false expression:
z3::expr x = C->int_const("x");
z3::expr p = z3::forall(x, x==0);
and try to solve, I get an unknown outcome. I am not an expert of strategies and tactics, but I am sure that z3 can solve this, if I use the right tactic.
I also tried
z3::expr p = !z3::forall(x, x==0);
with, of course, the same runknown esult.

I'm not familiar with z3, but from a general C++ perspective, wouldn't x==0 evaluate first, i.e. wouldn't your call be equivalent to implies(x, 1)? From a quick search it seems you may have to construct each piece of the statement as a z3 object, for example:
Z3_ast consequent = Z3_mk_eq(ctx, x, 0);
Z3_ast theorem = Z3_mk_implies(ctx, x, consequent);
But the above is not correct, either. I believe the parameters x and 0 themselves have to be instances of Z3_ast that encapsulate the statement you mean (as opposed to their interpolated values or references).

Related

Lua: Storing a logical operator in a variable?

I can't find anything about this through Google so I have to ask here. I want to do something like this (very pseudo code):
y = first_value
x={op_1 = >, op_2 = <, c = some_value}
if first_value x.op_1 x.c then
...
end
What that code says to me is that if first_value if greater than x's c value then do something. Now, I know I could set op_1 and op_2 to some value to differentiate between them and then compare values using separate if statements, but I would like to minimize the number of if statements used.
I was just wondering if something like this is possible, maybe even in a different form. Thanks in advance!
Not this way, an operator is a specific symbol that is part of the syntax. However, you can represent an operation using a function:
y = first_value
x={op_1 = function(a,b)return a>b end, op_2 = function(a,b)return a<b end, c = some_value}
if x.op1(first_value, x.c) then
...
end

Cython replacing list of tuples by C equivalent

I am trying to speed up an already very optimized function in Cython using list of two sized tuples of doubles as inputs and outputs. To do that I need to have it all in pure C first.
In python somehow cythonized the tuple syntax ressembles this:
def f(inputList1, inputList2):
cdef double p, i10,i11,i20,i21,a0,a1
a0,a1=inputList1[-1]
for i10,i11 in inputList1:
outputList=[]
#some more computations involving a0 and a1
for i20,i21 in inputList2:
p=f(i10,i11,a0,a1,i21,i20) #f returns a double
if p==0:
outputList.append((i10,i21))
else if p>0:
outputList.append(g(i10,i21,i20,i21)) #g returns two outputs that are parsed as a two sized tuples automatically
if len(outputList)<1:
return []
#some more computations
a0, a1=i10, i11
return outputList
The details are not relevant what is important is the speed and the syntax: it uses tuple unpacking in the for loops to break the tuples apart and can append whole tuples using append(), it can also fetch the last element of a list and it can return an empty list. It can also convert the two outputs of g to a tuple.
I am trying to change all the python to pure C to either increase speed or at least not damage the speed in my mind it should be doable (maybe I am wrong ?). So as g has to become pure C g has to return one object (I guess multiple outputs are out of the question ?)
My first idea was to use std::vectors and std::pairs lists of lists become vector[pair[double,double]] I modified g to return a pair[double,double] instead of two doubles
cdef vector[pair[double,double]] f(vector[pair[double,double]] inputList1, vector[pair[double,double]] inputList2):
cdef double p, i10,i11,i20,i21,a0,a1
#I have to add the pairs as I cannot use the for a,b in syntax
cdef pair[double,double] i1,i2,e
cdef vector[pair[double,double]] outputList, emptyList
a0,a1=inputList.back()
for i1 in inputList1:
i10=i1.first
i11=i1.second
outputList=emptyList
#some more computations involving a0 and a1
for i2 in inputList2:
i20=i2.first
i21=i2.second
p=f(i10,i11,a0,a1,i21,i20) #f returns a double
if p==0.:
outputList.push_back(i1) #I am now using push_back and not append
else if p>0.:
outputList.push_back(g(i10,i21,i20,i21)) #g now returns a pair
if outputList.size()<1:
return outputList
#some more computations
a0, a1=i10, i11
return outputList
Everything is pure C but it is 3 times slower !!!
I also tried std::list and list[list[double]] I am losing in speed by a factor of 3 also ! I use i1.back() and i1.front() instead of first and second I guess that makes me loose speed too. What is the reason for that ? Is there a better C object to use ? Is is the syntax I am using ? Doing explicitely i20=i2.first and so on is that what makes it so slow ?
Especially the syntax of g now seems really silly maybe the bottleneck comes from there:
cdef pair[double,double] g(double a, double b, double c, double d):
#looks ugly that I have to define res
cdef pair[double,double] res
cdef double res_int
res_1=computations1(a,b,c,d)
res_2=computations2(a,b,c,d)
#looks ugly
res.first=res_1
res.second=res_2
return res
instead of simply returning res_1, res_2 as before:
EDIT: I redid everything to benchmark the different solutions and:
-it turns out the list[list[double]] is not an option for me because later in my code I need to access specific elements of the list through indexing
-vector[np.ndarray[DTYPE, ndim=1]] does not work I guess you cannot form a vector with Python objects
-vector[pair[double,double]] is actually indeed faster than Python list versions !
For 100000 iterations the Python version takes in total:
1.10413002968s for the Python version
0.781275987625s for the (vector[pair[double,double]]) C version.
It still looks very ugly and I still want to hear if this is the right approach

Replace x if cond in Mata

I want to overwrite some elements of a vector x with a scalar a based on a 0/1 vector cond. In pseudocode: x[cond]=a.
x[cond] is not the right way of subsetting in Mata. I should use select(x,cond). Unfortunately, this latter object cannot be assigned to.
x[selectindex(cond)] = a fails because such an assignment requires the same dimensions on both sides of the =.
I could modify the latter approach to
x[selectindex(cond)] = J(sum(cond),1,a)
Is that the idiom in Mata? I was expecting something more straightforward because Stata has nice replace x = a if cond syntax.
In the general case, I think that's about as good as you're going to get. sum(cond) is safe if cond is 0 or 1, but a more general alternative is:
select = selectindex(cond)
x[select] = J(length(select), 1, a)
I agree that this is not the simplest syntax. An additional assignment colon operator := would be nice here.
If x and cond are views, st_store() is another option:
st_store(., st_viewvars(x), st_viewvars(cond), J(sum(cond), 1, a))
If you already know the variable names/indices and don't have to call st_viewvars(), all the better.

Function of a letter in C++

I have the following expression:
A = cos(5x),
where x is a letter indicating a generic parameter.
In my program I have to work on A, and after some calculations I must have a result that must still be a function of x , explicitly.
In order to do that, what kind of variable should A (and I guess all the other variables that I use for my calculations) be?
Many thanks to whom will answer
I'm guessing you need precision. In which case, double is probably what you want.
You can also use float if you need to operate on a lot of floating-point numbers (think in the order of thousands or more) and analysis of the algorithm has shown that the reduced range and accuracy don't pose a problem.
If you need more range or accuracy than double, long double can also be used.
To define function A(x) = cos(5 * x)
You may do:
Regular function:
double A(double x) { return std::cos(5 * x); }
Lambda:
auto A = [](double x) { return std::cos(5 * x); };
And then just call it as any callable object.
A(4.); // cos(20.)
It sounds like you're trying to do a symbolic calculation, ie
A = magic(cos(5 x))
B = acos(A)
print B
> 5 x
If so, there isn't a simple datatype that will do this for you, unless you're programming in Mathematica.
The most general answer is "A will be an Expression in some AST representation for which you have a general algebraic solver."
However, if you really want to end up with a C++ function you can call (instead of a symbolic representation you can print as well as evaluating), you can just use function composition. In that case, A would be a
std::function<double (double )>
or something similar.

How to do colwise operations in Eigen

I am doing this a lot:
auto f_conj = f.conjugate(); //f is a MatrixXcf, so is C;
for(n=0;n<X.cols();++n)
C.col(n) = X.col(n).cwiseProduct(f_conj);
Am I not supposed to be able to do something like
C.colwise() = X.colwise().cwiseProduct(f_conj)
instead?
What you are really doing is a diagonal product, so I'd recommend you the following expression:
C = f.conjugate().asDiagonal() * X;
If you want to use a colwise() expression, then do not put it on the left hand side:
C = X.colwise().cwiseProduct(f.conjugate());
Moreover, let me warn you about the use of the auto keyword. Here, let me emphasize that f_conj is not a VectorXcf, but an expression of the conjugate of a VectorXcf. So using f_conj or f.conjugate() is exactly the same. Since multiplying two complexes or one complex and one conjugate complex amount to the same cost, in this precise case it's ok to use the auto keyword. However, if f_conj would be for instance: auto f_conj = (f+g).conjugate(), then f+g would be recomputed many times in your for loop. Doing (f+g).conjugate().asDiagonal() * X is perfectly fine though, because Eigen knows what to do.