How can I convert truth table of 4 variables into one 4:1 multiplexer
Here is an example how to convert it into 8:1 multiplexer
The truth-table can in fact be implemented with a 2-1 multiplexer:
A minimized expression for the function depicted by the truth-table is
Y = X1 X3 + X3' X4
In words:
Output Y is X1 for X3 true. For X3 false, Y is X4
Input X3 selects either X1 or X4 to be forwarded to output Y.
Input X2 is ignored (as don't care).
You could use a 4-1 multiplexer and set the lower select input to X3, while the higher select input is set to constant 0.
Related
A logic circuit is given two 2-bit binary numbers A and Bas its inputs. The circuit consists of two outputs Y1 and Y2. The output values of Y1 and Y2 are obtained as follows:
If A<B, then Y1 and Y2 will be equal to A-B. Else Y1 and Y2 will be equal to A.
How To Determinate truth table for this
There are two inputs A[0:1] and B[0:1], 4 inputs in total. Your truth table will have 16 inputs(rows).Are Y1 and Y2 2-bit outputs and is it magnitude of A-B? If yes, The left two columns can be A and next two will be B. For 6 of these rows from 16 cases, A<B in 6 cases ([A,B] = {[0,1],[0,2],[0,3],[1,2],[1,3],[2,3]}). these six rows will have Y1 = Y2 = [B-A]. All other rows will have Y1 = Y1 = A input. Seems straightforward, but I may be missing something here.
Warren’s Abstract Machine A Tutorial Reconstruction states the following for Variable Register Allocation rules:
Variable registers are allocated according to least available index.
Register X1 is always allocated to the outermost term.
A same register is allocated to all the occurrences of
a given variable.
Further in the tutorial, while building program queries, the following example is given:
p(f(X), h(Y, f(a)), Y).
X1 = p(X2, X3, X4)
X2 = f(X5)
X3 = h(X4, X6)
X4 = Y
X5 = X
X6 = f(X7)
X7 = a
My doubt is when considering the two occurrences of the f clause, both are f/1 structures, but with a different body and therefore needed to be instantiated differently. But what exactly is considered a variable in the WAM context, a prolog variable, or every term? How would the clause p(f(a), f(a)) be constructed:
X1 = p(X2, X2)
X2 = f(X3)
X3 = a
or
X1 = p(X2, X3)
X2 = f(X4)
X3 = f(X4)
X4 = a
I am working through Hassan Ait-Kaci's book Warren's Abstract Machine - A Tutorial Reconstruction. Currently I am stuck on section 2.4, "Argument Registers".
To be precise, what I don't understand is how to get from these register assignments (p.22) (for query p(Z,h(Z,W),f(W))):
A1 = Z
A2 = h(A1,X4)
A3 = f(X4)
X4 = W
to these instructions (p.24):
put_variable X4,A1
put_structure h/2,A2
set_value X4
set_variable X5
put_structure f/1,A3
set_value X5
call p/3
Like, where does X5 come from all of a sudden? In the register assignments, X4 refers to variable W, and there's no X5. But in the instructions, X5 refers to (what is/was essentially) W, and X4 now refers to Z. I am not seeing an explanation in the book. What am I missing?
I tried to understand this but to no avail. Nothing in the errata. Here are some notes:
Instruction review
put_structure f/n,Xi: push a new STR (and adjoining functor) cell onto the heap and copy that cell into the allocated register address;
set_variable Xi: push a new REF cell onto the heap containing its own address, and copy it into the given register;
set_value Xi: push a new cell onto the heap and copy into the register's value.
In case of query:
put_variable Xn,Ai: the first occurrence of a variable in i-th argument position pushes a new unbound REF cell onto the heap and copies it into that variable's register as well as argument register Ai; and
put_value Xn,Ai (used for query): a later occurrence copies its value into argument register Ai.
In case of fact:
get_variable Xn,Ai: the first occurrence of a variable in i-th argument position sets it to the value of argument register Ai; and
get_value Xn,Ai: a later occurrence unifies with the value of Ai.
So, about that query
p(Z,h(Z,W),f(W)))
It seems to be coded differently on pages 17 and 19
Page 17 in the print edition Page 19 in the print edition
(given as is) (translated by me from the WAM code)
A1 = Z A1 = Z
A2 = h(A1,X4) A2 = h(X4,X5)
A3 = f(X4) A3 = f(X5)
X4 = W X4 = Z
X5 = W
Edit: It seems the code on the left allows variables appearing in non-root-positions to be in "argument registers" which is disallowed on the right, hence indirection.
The code
The Ai are the argument registers, the Xi are some value registers.
put_variable X4,A1 Z is a argument root freshvar created in X4
and also goes into A1
put_structure h/2,A2 h/2 functor goes into A2
(1)
set_value X4 New (empty) cell is created, goes into the
value of X4
(2)
set_variable X5 W is a non-root freshvar created in X5
(3)
put_structure f/1,A3 f/1 functor goes into A3
set_value X5 New (empty) cell is created, goes into the
value of X5
(4)
call p/3 Go!
At position (1), so far, so good. X4 and X5 seem to implicitly be the first and second arguments of the h/2 in A2 (is that right?)
X4 ----+---> [unbound REF] = Z (variable appearing at root)
|
A1 ----+
A2 --------> [h/2] = h(X4,X5)
At (2)
X4 ----+---> [] = Z (variable appearing at root)
|
A1 ----+
A2 --------> [h/2] = h(X4,X5)
At (3)
X4 ----+---> [] = Z (variable appearing at root)
|
A1 ----+
A2 --------> [h/2] = h(X4,X5)
X5 --------> [myself REF] (variable not appearing at root)
At (4)
X4 ----+---> [] = Z (variable appearing at root)
|
A1 ----+
A2 --------> [h/2] = h(X4,X5)
A3 --------> [f/1] = f(X5)
X5 --------> [] = W
Say I have 3 parameters x1, x2 and x3, where x1, x2 and x3 can all take integer values between 0 to 9. I use GA to minimize an objective function f(x1,x2,x3), but the values of x1, x2 and x3 obtained should be integers. Any R-based implementation of GA with such a function to obtain integer values instead of the usual real values would be helpful.
Given a list of size 2n -1 elements and the list looks like this:
x1, x2, x3, ....., xn, y1, y2, y3, ....y(n-1)
Convert it to:
x1, y1, x2, y2, x3, y3, ........., y(n-1), xn
I can use two iterators for each of the lists and get the solution in O(n) time complexity and O(n) space complexity. But if my n was very large, is there a way to do this in lesser space complexity?
It feels like this can be done with O(1) space and O(n) time but the algorithm is far from trivial. Basically take an element that is out of place, say x2, look where it needs to be in the final arrangement take out the element that is there (i.e. x3) and put in x2.
Now look where x3 needs to go and so on.
When the cycle is closed, take the next element that is out of place (if there is any).
Lets do an example:
x1 x2 x3 y1 y2 x2 is out of place so take it into temp storage
x1 -- x3 y1 y2 temp: x2 needs to go where x3 currently is
x1 -- x2 y1 y2 temp: x3 needs to go where y2 currently is
x1 -- x2 y1 x3 temp: y2 needs to go where y1 currently is
x1 -- x2 y2 x3 temp: y1 needs to go into the empty slot
x1 y1 x2 y2 x3 all elements in place -> finished
If the array indices start at 0, the final position of the element at k is given by
2k if k < n
2(k-n) + 1 if k >= n
The difficulty is to find out an element of a cycle that is not yet handled. For example if n = 4 there are 3 cycles:
0 -> 0
1 -> 2 -> 4 -> 1
3 -> 6 -> 5 -> 3
I do not have an easy solution for that at the moment.
If you have one bit of storage available per array element it is trivial but then we are back to O(n) storage.
In Python:
lst = 'x1 x2 x3 x4 x5 y1 y2 y3 y4 y5'.split()
lst
Out[9]: ['x1', 'x2', 'x3', 'x4', 'x5', 'y1', 'y2', 'y3', 'y4', 'y5']
out = sum((list(xy) for xy in zip(lst[:len(lst)//2], lst[len(lst)//2:])), [])
out
Out[11]: ['x1', 'y1', 'x2', 'y2', 'x3', 'y3', 'x4', 'y4', 'x5', 'y5']