Symbolic executions over bit vectors - smt

Is there any tool for bit vectors (QF_BV logic) which will symbolically execute the operations and return the outputs in terms of symbolic values of the bit vectors so that I can apply my own computations on them as needed? Or Is there any SMT solver where symbolic values of variables can be extracted after bit blasting? For example:
Let,
X[3:0], Y[3:0], Z[4:0], W[4:0] are declared as bit vectors without initializing any value
print X[3:0]
X[3:0] <- X[3:0] >> 1 (logical shift)
print X[3:0]
Z[4:0] <- X[3:0] + Y[3:0]
print Z[4:0]
print Y[3:0]
W[4:0] <- Y[3:0] + 0000
print W[4:0]
.......
Desired output (something symbolic like this):
2. [x3 x2 x1 x0]
4. [0 x3 x2 x1]
6. [s4 s3 s2 s1 s0]
7. [y3 y2 y1 y0]
9. [0 y3 y2 y1 y0]

Related

Evaluate symbolic matrix in MATLAB

I defined 2 symbolic matrix in MATLAB, for example
w = sym('w',[10,10])
Then I do some operations on it and get function E dependent to symbolic matrix w and v. Now, I want to evaluate E numerically with numerical w and v.
How can I do this?
Simple example on R2017a:
>> syms E(v,w)
>> E(v,w) = v*w + v;
>> E(3,4)
ans =
15
On earlier versions, I believe symfun is the command to use.
We can use such code:
>> w=sym('w',[10 10]);
>> d=sym('d',[10 1]);
>> E=W*d + ...(some other operations)
>> define a numerical matrix f and vector x)
>> subs(subs(E,w,f),d,x)
This code performed in R2014a and had a correct answer.

Matlab: Test the system is linear or not

I have an equation: y(n) = a^x(n).
where x1(n) ={0,1,2,3}, x2(n)={1,2,3,4}, a1=a2=1, a=2.
So, How can i write MATLAB code to test the system is linear or not?
Just apply the definition.
Consider a system S, such that the output to an input signal x(n) is S( x(n) ). S is linear if and only if
S( x1(n) + x2(n) ) = S( x1(n) ) + S( x2(n) ) for any inputs x1, x2 (additivity)
S( b * x1(n) ) = b * S( x1(n) ) for any input x1 and any number b (homogeneity)
In your case it's clear that neither 1 nor 2 hold, so the system y(n) = S( x(n) ) is not linear.
To test it with code: randomly generate many examples of x1, x2 and b and check if the equalities above hold. Note that this way you will never be sure that the system is linear. You can only be sure it isn't, namely when you find x1, x2, b for which either 1 or 2 do not hold.
Example
>> a = 2; n = 3;
>> S = #(x) a*x.^n; %// anonymous function describing your system
>> x1 = 1:4; %// test input signal
>> x2 = 11:14; %// test input signal. Same length
>> S(x1)+S(x2)
ans =
2664 3472 4448 5616
>> S(x1+x2)
ans =
3456 5488 8192 11664
Since the results are different, the system does not satisfy property 1, and therefore it's not linear.

Symbolic Expression Differentiation using Symbolic Vector

I am trying to differentiate the following symbolic expression using a created symbolic vector but I keep getting errors. That is, I would like df/dx1, df/dx2, and df/dx3. Here is what I have tried:
>> x = sym('x', [3 1])
x =
x1
x2
x3
>> symbolic = 0.5*transpose(x)*eye(1)*x + [1 1 1]*x
symbolic =
x1^2/2 + x1 + x2^2/2 + x2 + x3^2/2 + x3
>> diff(symbolic, x)
Error using mupadmex
Error in MuPAD command: Invalid argument. [contains]
Evaluating: (Dom::Matrix(Dom::ExpressionField()))::_mult1
Error in sym/diff (line 44)
R = mupadmex('symobj::diff', S.s, x.s, int2str(n));
>> diff(symbolic, x.x1)
Error using sym/subsref
Too many output arguments.
Any assistance would be greatly appreciated. Thanks!
You can try one of these two options:
% option 1
x = sym('x', [3 1]);
f = 0.5*transpose(x)*eye(1)*x + [1 1 1]*x;
for i=1:3
Df(1,i) = diff(f, x(i));
end
% I do not like this option because I do not know
% how to evaluate the expressions with numeric values
x(1) = 1;
eval(Df)
I prefer the 'option 2', because it is easier to evaluate expressions.
% option 2
syms x1 x2 x3 real; % 'real' fixes x1 x2 x3 as real numbers (not complex ones)
x = [x1 x2 x3]'; % '
f = 0.5*transpose(x)*eye(1)*x + [1 1 1]*x;
for i=1:3
eval(['Df(1,i) = diff(f,x',num2str(i),');']);
end
% To eval at a certain value
x1 = 1;
x2 = 2;
x3 = 3;
eval(Df)
I think that eval has only the two functions I used above:
To eval symbolic expressions to specific values of the symbolic variables, like when I wrote eval(Df).
You can use eval to evaluate a matlab command written as a string, as if you were writing it as normal code. Try this to see what I mean:
a = 1; % set value of a to 1
eval('a = 2'); % change value of a to 2
eval(['a = ',num2str(5)]); % set value of a to 5;
Hope this helps,

Transpose 1D to 2D by Group in Matlab

I need to transpose a vector into a 2D matrix according to a group of values that are equal in another column of a matrix. For example:
1 x1
1 x2
1 x3
1 x4
2 x5
2 x6
2 x7
2 x8
Should look like:
x1 x2 x3 x4;
x5 x6 x7 x8;
This is the same procedure you would do in SAS using proc tabulate. Reshape didn't work for me because it doesn't transpose it, and tried permute with no luck either. Is there any built in command that does this besides having to program it in using find, transpose, and vertcat?
If for some reason you want to avoid reshape, although the solution in comments will work, you can use sub2ind to get the linear indices of the new matrix V, given that your first column will always provide the new line sub:
X = [[1,1,1,1,2,2,2,2]' (1:8)'];
subs = X(:,1);
M = length(unique(subs)); % count unique ids
N = length(X)./M; % Problem assumption: M sets of size N (MxN=length(X))
V = zeros(M, N);
i = sub2ind([M, N], subs, repmat(1:N,1,M)');
V(i) = X(:,2);
The above, according to your specs, will work as long as there is an equal number of unique elements in X, so you can form an MxN matrix.

Confusions about Multiplication of Complex Value in MatLab

I noticed a confused computation of complex valued multiplication in Matlab. One simple example is as below:
syms x1 x2 x3 x4
s=[x1 x2]*[x3 x4]'
And the return value of s is like:
s=x1*conj(x3) + x2*conj(x4)
In my opinion s should be equal to x1*x3+x2*x4. So, what's the problem here?
Then, how should I do if I want to get a multiplication of two complex vector?
update: I find out it will be solved through using .' rather than . like:
s=[x1 x2]*[x3 x4]
The operator ' is the also called Complex conjugate transpose in Matlab ctranspose, which basically means that it applies conj and and transpose functions. Note that this operator is call Hermitian operator in mathematics.
What you actually want is the operator transpose that is shortcut as .'
In order to get the expected output, and given that you just want to multiply without conjugating the second vector, you should do:
>> syms x1 x2 x3 x4
>> s = [x1 x2]*[x3 x4].'
so your output will be:
x1*x3 + x2*x4
For further information you can check help ., to see the list of operators, help transpose and help ctranspose
Note that the ' operator in Matlab is the conjugate transpose, i.e. it both transposes a matrix and takes the complex conjugate:
>> (1+1i)'
ans =
1.0000 - 1.0000i
If you want the matrix transpose then you should use the .' operator:
>> (1+1i).'
ans =
1.0000 + 1.0000i
Maybe this will help explain:
>> syms x1 x2 x3 x4
>> s=[x1 x2]*[x3 x4]'
s =
x1*conj(x3) + x2*conj(x4)
>> s=[x1 x2]*[x3; x4]
s =
x1*x3 + x2*x4
>> [x3 x4]'
ans =
conj(x3)
conj(x4)
The ' version of transpose isn't doing what you want. Use transpose instead:
>> transpose([x3 x4])
ans =
x3
x4