A logic circuit is given two 2-bit binary numbers A and Bas its inputs. The circuit consists of two outputs Y1 and Y2 - logic

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.

Related

Convert f(x1,x2,x3,x4) truth table to 4:1 multiplexer

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.

Forming a matrix that allows columns but not rows [R]

I am trying to create a matrix like so:
x1 <- rbern(1000,.5)
x2 <- (1-x1)
head(x2)
head(x1)
M <- cbind(x1,x2)
M
When I input M, it shows me x1 and x2 as the columns with 1000 rows. I presume this is the matrix. The issue is when I find the transpose
A<- t(M)
A
tail(A)
It is like x2 disappears, and when I try to multiply A*M, I get the error that these are non comformable arrays.
I have tried this
M <- rbind(x1,x2)
M
tail(M)
Still, it is like x2 does not exist. What am I missing here?
dim() allows to see the dimensions, where the first number is the number of rows and the second the numbers of columns, see that in M we have 2 columns, x1 and x2, since you used cbind()
> dim(M)
[1] 1000 2
> dim(A)
[1] 2 1000
After transposin M to A see that you have 2 rows, so x2 still there, but as the second row.

Gas mixture algorithm

I have a pool of ten different gas mixtures. Each gas mixture is made of nitrogen, oxygen and carbon dioxide in different percentages.
The goal is to mix and match sets of gasses to to get a requested percentage output.
Lets say we want 33-33-33 out. Whats the best methodology to select the best subset of gases and mixing proportions to get an optimal output?
Just solve 3 equations with 3 unknowns.
if mixture 1 has a1 fraction of chemical a (and similarly with other chemicals/mixtures), and you need xa : xb : xc mixture, where xa+xb+xc=1, this will be:
a1 * x1 + a2 * x2 + a3 * x3 = xa
b1 * x1 + b2 * x2 + b3 * x3 = xb
c1 * x1 + c2 * x2 + c3 * x3 = xc
Solve for x1, x2, x3. If you get negative numbers (or no solutions), this means that it's impossible to get the wanted mixture.

How to optimize using Genetic Algorithm to get integer values?

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.

Merging two very large lists

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']

Resources