Generating DIMACS CNF file using bc2cnf is missing AND - boolean-expression

I tried using the bc2cnf tool to generate the DIMACS CNF file of a boolean equation.
The input file contains the equation of an AND gate as shown below :
BC1.1
f := A & B;
ASSIGN f;
Command used: ./bc2cnf -v inp.txt opt.txt
Content in the output file:
c The instance was satisfiable
c A <-> T
c B <-> T
c f <-> T
p cnf 1 1
1 0
Here, it can be observed that the correct DIMACS CNF format of the AND gate is not generated.
Please let me know how this problem can be rectified.

Use commandline parameter -nosimplify to suppress bc2cnf optimization.
Result is
c f <-> 1
c B <-> 2
c A <-> 3
p cnf 3 4
-1 2 0
-1 3 0
1 -3 -2 0
1 0
bc2cnf has a number of useful parameters. Try bc2cnf -? to get help.

Related

[kdb+/q]: Convert adjacency matrix to adjacency list

Given (rectangular) adjacency matrix m, how to construct adjacency list in q language?
In QIdioms wiki I've found solution in the k language which when run through q console with k) command gives me 'vs error:
m:(1 0 1;1 0 1)
k) (^m)_vs &,/m
'vs
Result should be:
0 0 1 1
0 2 0 2
This is what I was able to replicate in q:
k) &,/m
0 2 3 5
q) where raze m
0 2 3 5
k's^ a.k.a. shape verb is missing in q so I just did:
k) (^m)
000b
000b
q) 2 3#0b
000b
000b
Now, since:
q) parse "vs"
k) {x\:y}
I tried unsuccessfully both:
q) (2 3#0b) _vs where raze m
'
q) (2 3#0b) _\: where raze m
'type
Note that QIdioms wiki has q solution for inverse problem: from adj.list to adj.matrix.
You've got errors because original Q idioms are written in k2 - an old version of k which modern kdb+ version don't support. A current version of k is k4 and it's not backwards-compatible with k2.
For instance, X _vs Y where X and Y are integer atoms or lists in old k2 behaved like X vs Y will behave in kdb+ starting from 3.4t 2015.12.13: http://code.kx.com/q/ref/lists/#vs:
Since 3.4t 2015.12.13: For integer types, computes the base representation of Y in the radices X.
Another example. Indeed ^ in k2 was a shape operator, but it is not any longer. In k2 ^m would have returned 2 3 for a matrix m from your example whereas current implementation behaves like q's not null as far as I understand.
Now, back to your original question, "how to construct adjacency list in q language". One way to do it is this:
q)lm:{flip raze(til count x),''where each x}
or
k)lm:{+,/(!#x),''&:'x}
UPDATE: Here's how it works. If we were to build an adjacency list using any "verbose" language we would do something like this:
for i = 0 to <number of rows> - 1 <---- (1)
for j = 0 to <number of columns> - 1 <---- (2)
if M[i;j] <> 0 <---- (3)
print i, j
In an array language like q (1) can be "translated" into til count M because count will return the number of elements at top level, i.e. the number of rows. (2) and (3) combined can be represented by where each M. Indeed, for every row we return positions of non-zero elements. Given an original matrix m we will get:
til count m -> 0 1
where each m -> (0 2; 0 2)
All we need to do is join row and column indexes. We can't use just ,' because it will join 0 with the first 0 2 and 1 with the second 0 2 resulting in (0 0 2; 1 0 2). We need to go one level deeper, joining every element from the left with every element of every element of a nested list (0 2; 0 2) from the right, hence double apostrophes in ,''.
I hope it makes sense now.
Personally, I would not use flip (or + in k), I can't read an adjacency matrix in this form:
0 0 1 1
0 2 0 2
I think this is much more readable:
0 0
0 2
1 0
1 2
But it's up to you of course.

define custom model in simulink

let us consider following simulink model
i have following matrix
A=[2 1 3;1 4 5]
A =
2 1 3
1 4 5
and its singular value decomposition
>> [U E V]=svd(A)
U =
-0.4719 -0.8817
-0.8817 0.4719
E =
7.2965 0 0
0 1.6617 0
V =
-0.2502 -0.7772 -0.5774
-0.5480 0.6053 -0.5774
-0.7982 -0.1720 0.5774
>>
in custom matlab function taken form this link
http://www.mathworks.com/help/simulink/slref/matlabfunction.html
i have created my own function
function [U,E,V]=singular_decomposition(A)
%#codegen
[U E V]=svd(A);
numeric values and dimension of matrices are same,but at some point there is sign different,how can i fix this?
There's nothing wrong. SVD decomposition of a matrix is in general not unique.
If you perform U*E*V' you'll get the same value of A in both cases.

Crystal Report - First record or second record of a group

I have a one to many records relationship and want to convert it into one to one. Here is the example:
a 1
a 2
b j
b p
b k
b 4
c
d 0
d 1
d v
d 6
and I want to pull the first record of data like the following:
a 1
b j
c
d 0
Also how can I pull the second record of the data like the following?
a 2
b p
c
d 1
you have to take a group for that.
and do grouping on the particular field which you wants.

Get values for 2 arguments from 1 equation

I have this equation: p = (1 + (q-1)*B*T) ^ (-1/q-1)
The values p, T are known and the diagram p - T makes curbe. I want to calculate q and B so that curbe be as close to straight line is posible.
Some values are:
T p
1 0,999147061
2 0,997121331
3 0,994562513
Is there any way to make matlab (or sth else) to give me the values of B and q ?

Solving for variables in an over-parameterised system

I am trying to write a Matlab program that accepts variables for a system from the user, but there are more variables than system parameters. To be specific, six variables in three equations:
w - d - M = 0
l - d - T = 0
N - T + M = 0
This could be represented in matrix form as A*x=0 where
A = [1 0 0 -1 0 -1;
0 1 0 -1 -1 0;
0 0 1 0 -1 1];
x = [w l N d T M]';
I would like to be able to solve this system given a known subset of the variables. For example, if the user gives d, T, M, then the system is trivially solved for the other three variables. If the user supplies w, N, M, then it becomes a solvable 3-DOF system. And so on. (If the user over- or under-specifies the system then an error may of course result.)
Given any one of these combinations it's simple to (a priori) use matrix algebra to calculate the unknown quantities. But I don't know how to solve the general case, aside from using the symbolic toolbox (which I prefer not to do for compatibility reasons).
When I started with this approach I thought this step would be easy, but my linear algebra is rusty; am I missing something simple?
First, let x be a vector with NaN for the unknown values. This allows you to use ISNAN to find the indeces of the unknowns. If you calculate A*x for only the user-specified terms, that gives you a column of constants b. Take those constants to the right-hand side of the equation, and you have an equation of the form A*x = -b.
A = [1 0 0 -1 0 -1;
0 1 0 -1 -1 0;
0 0 1 0 -1 1];
idx = ~isnan(x);
b = A(:,idx)*x(idx); % user provided constants
z = A(:,~idx)\(-b); % solution of Ax = -b
x(~idx) = z;
With input x = [NaN NaN NaN 1 1 1]', for instance, you get the result [2 2 0 1 1 1]'. This uses MLDIVIDE, I'm not well versed enough in linear algebra to know whether PINV or something else would be better.
Given the linear system
A = [1 0 0 -1 0 -1;
0 1 0 -1 -1 0;
0 0 1 0 -1 1];
A*x = 0
Where the elements of x are identified as:
x = [w l N d T M]';
Now, suppose that {d,T,M} have known, fixed values. What we need are the indices of these elements in x. We've chosen the 4th, 5th and 6th elements of x to be knowns.
known_idx = [4 5 6];
unknown_idx = setdiff(1:6,known_idx);
Now, let me pick some arbitrary numbers for those known variables.
xknown = [1; -3; 7.5];
We will partition A into two submatrices, corresponding to the known and unknown variables.
Aknown = A(:,known_idx);
Aunknown = A(:,unknown_idx);
Now, move the known values to the right hand side of the equality, and solve. See that Aknown is a 3x3 matrix, so the problem is (hopefully) well posed.
xunknown = Aunknown\(-Aknown*xknown)
xunknown =
-8.5
2
10.5
Combine it all into the final solution.
x = zeros(6,1);
x(known_idx) = xknown;
x(unknown_idx) = xunknown;
x =
-8.5
2
10.5
1
-3
7.5
Note that I've expanded this all out into a few lines to show what is happening more clearly. But I could have done it all in just a line or two of code had I wanted to be parsimonious.
Finally, see that had I chosen some other sets of numbers to be the knowns, such as {l,d,T}, then the resulting system would be singular. So you must watch for that event. A test on the rank of Aunknown might be useful to weed out the problems. Or you might choose to employ pinv to build the solution.
The system of equations is fixed? What if you store the variables present in your three equations in a list per equation:
(w, d, M)
(l, d, T)
(N, T, M)
Then you get the user input and you can calculate the number of variables given in each equation:
User input: w, N, M
Given variables:
(w, d, M) -> 2
(l, d, T) -> 0
(N, T, M) -> 1
This would trivially give you d from the first equation. Therefore you end up with two equations containing two variables and you know you the equation system you have to solve.
It's basically your own simple symbolic solver for a single system of equations.