Getting Integer value from Instance in Z3 - casting

My code is
def test():
s = Solver()
a = Int('x')
b = Int('y')
s.add(a*b==22)
print s.check()
return s.model()[a], s.model()[b]
This will print numbers, but when you see type(s.model()[a]) or type(s.model()[b]) it will give <type 'instance'> . How to cast it in a way it will return <type 'int'> ? I cannot use the return type into my code any further as it returned instance even though if you print s.model()[a] it will look like a integer but it isn't.

Simply use the as_long method. That is, replace the last line in your function with:
return s.model()[a].as_long(), s.model()[b].as_long()

Related

how do i typeshift <class 'sympy.sets.sets.FiniteSet'> to int

I want to typeshift a value from a sympy solveset to be an int.
I try this:
from sympy import *
x = symbols('x')
a = solveset(Eq(x+radii[0],2*(-x+radii[-1])),x)
a = int(a)
I get this error:
int(gearAdjust(radii))
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'FiniteSet'
my desired result is for this code to just make a an int.
a is an object of type FiniteSet. You can convert it to a list and extract its element:
int(list(a)[0])

No viable conversion from returned value of type 'int' to function return type (vectors)

I am trying the hackerrank question and they gave said to give the output as "Print two space-separated integers describing the respective numbers of times the best (highest) score increased and the worst (lowest) score decreased." Now, whenever I try to give only cout, it throws an error saying "Non-void function does not return a value". Now normally, I used to give return 0 in these situations but here it is saying "No viable conversion from returned value of type 'int' to function return type." I really don't know how to either return int in a vector function or simply put, how to return the answers back to the compiler.
Here is my function
vector<int> breakingRecords(vector<int> scores) {
int min=0,max=0;
int maxUpdate=0;
int minUpdate=0;
for(int i=0;i<scores.size();i++)
{
if(i==0)
{
min=max=scores[0];
break;
}
if(scores[i]>scores[i-1])
{
max=scores[i];
maxUpdate++;
}
else if(scores[i]<scores[i-1])
{
min=scores[i];
minUpdate++;
}
}
return 0;
}
Note: If my logic has any error then please don't correct it. I want to find errors in my logic on my own just help me get my code run.
The problem is that you set your function to return vector type of object, so returning 0 or not returning anything is unacceptable. You basically have 3 options here:
Set your function as a void type - then you can only "cout" and you're good, or
even nothing at all.
Return "scores" itself without actually using it outside your function
Set your function as an int type - to return 0, if necessary.
Good Luck!
Your probleme come from the return type, you wan't to return a std::vector<int> but you are actualy returning an int:
return 0;
To fix it, just change the return value 0 to a std::vector<int> value or change your return type to int instead of std::vector<int>.

C++ assign a logical operator function to a variable

I am trying to assign a logical operator function to a variable but am unable to get it to work. I am using:
function<bool(double,double)> opFunct = less<bool>();
double highorlow = pOut.high;
if(pCheck){
opFunct = greater<bool>();
highorlow = pOut.low;
}
if(opFunct(highorlow,pStay.middle){
//never gets done
}
The problem with this code is no matter what the highorlow,pStay.middle doubles are, it returns false.
What am I missing?
Thanks
Short version:
less<bool> compares bools. Use less<double> to compare doubles (also in greater<>).
Long version:
This is an interesting question. Specifically, how come the following line compiles?
function<bool(double, double)> opFunct = less<bool>();
After all std::less<bool>:: operator() looks like bool(bool, bool), why does it match bool(double, double)?
Well that's because the check that std::function's constructor performs is simply whether less<bool>() can be invoked as bool(double, double), and yes it can! double is implicitly-convertible to bool, 0.0 becomes false and any other value true.
That obviously won't produce the expected result because e.g. opFunct(1.0, 2.0) will invoke less(true, true) which will return false.
The fix is to change bool to double
function<bool(double, double)> opFunct = less<double>();
And also here
opFunct = greater<double>();
But wait, std::function overhead aside1, depending on how the rest of your code looks like, the fragment shown can potentially be simplified to just:
if (pCheck ? pStay.middle < pOut.low : pOut.high < pStay.middle) {
// . . .
}
Or maybe even
if (pStay.middle < pOut.low || pOut.high < pStay.middle) {
// both checks at once . . .
}
1std::function comes at a cost of some 48-96 bytes of memory and an extra indirection or two. Compare the generated code for version with std::function vs. version without std::function.

ADTs and values

In Rascal, say I have the code:
value x = 2;
data Exp = con(int n);
Is there a way to call con(x), while x is a value (but actually an integer), without knowing on beforehand what the type of con's first argument is supposed to be (thus without explicitly casting it to an int)?
Why is it possible to call a function, say int something(int n) = n, with an integer defined as a value (e.g. value y = 2) passed into its first argument, while it gives me an error when I try to do the same with user-defined ADTs?
When you call a function in Rascal it actually is doing a pattern match on the arguments. So, if you define int something(int n) = n;, and then call something(x), it matches x with int n, sees that x is actually an int (so it can bind the value to n), and calls the function.
If you were to define value x = 2.5 instead and then call something(x) you would get an error since it cannot bind the value 2.5 to int n. You could overload something with a second definition that takes a real instead, like int something(real r) = toInt(r);, and it would then work. Two items to note here, though: something needs to return the same type in both cases, and you need to import util::Math to get access to toInt.
When you are using a constructor, like con(x), it doesn't do a pattern match for you automatically. The type that you give it has to match the type it expects. If you know that x will always be an int, it would be best to just declare it as such. Another option would be to create a function like Exp makeCon(int n) = con(n); which you could then use as you would like, i.e., Exp myExp = makeCon(x);. It would be best in this case to include a default version of the function, just in case you give it something unexpected, like default Exp makeCon(value x) { throw "Unexpected value <x>"; }, this way if you ever try to create a con with something that isn't an int you will get an error that you can handle, with the ability to create your own error message, add additional error handling versus just showing a message, see the value causing the problem, etc, versus just having the interpreter give an error (which may not give you all the info you want).

Proper casting of the object in C++, value field is not changing

I have a class:
class Para{
public:
int wrt, liczbaWystapien;
Para(){}
Para(int wrt, int liczbaWystapien){
this->wrt = wrt;
this->liczbaWystapien = liczbaWystapien;
}
Then there is other template class, and I do not know how to cast object to Para, becuase first way does not affect field value at all.
else if (is_same<T, Para>::value){
//dynamic_cast<Node<Para>*>(node)->key.wrt++;//this way no error occured but value of field **wrt** stays the same
node->key.wrt++;//error below
Error 4 error C2039: 'wrt' : is not a member of 'std::basic_string<_Elem,_Traits,_Ax>'
EDIT:
Node* paraNode = static_cast*>(node);
Para para = paraNode->key;
para.wrt = para.wrt + 1;
That gives
Error
4 error C2440: 'static_cast' : cannot convert from 'Node *' to 'Node *'
Something seems weird, because key's type supposed to be Para. What happens if you spell everything out?
Node<Para>* paraNode = dynamic_cast<Node<Para>*>(node);
Para para = paraNode->key;
key.wrt = key.wrt + 1;
Other suggestions: if fore some reason you are sure about the template type, you can use static_cast<> (or reinterpret_cast<>), it's faster than dynamic_cast<>, which really discovers and check the type hierarchy tree.
If you do
Para para = paraNode->key;
you get a copy of the key. Then you increment wrt. If you do a printf following that line, you will probably get the expected value. However if you call this from inside a function then the original paraNode will not be modified.
You need to store key as Para* key then access key.wrt with key->wrt++ then after the function you will get the expected value. I suggest you read on how stack and heap variables work as well as copy constructor in c++