I have to implement the function below with XOR gates. I drew the Karnaugh map and wrote down the resulting minimized function. But now I'm stuck with AND and OR gates, what should I do in order to get XOR gates?
My solution looks as follows:
F = Sum(1,2,4,7,8,11,13,14);
F = A' B' C' D + A' B' C D' + A' B C' D' + A' B C D + A B' C' D' + A B' C D + A B C' D + A B C D';
F = XOR(C, D) & A' B' + XOR(C, D) & A B + XOR(A, B) & C' D' + XOR(A, B) & C D;
F = XOR(C, D) & XOR(A, B)' + XOR(A, B) & XOR(C, D) ';
F = XOR(XOR(A, B), XOR(C, D));
A B C D XOR(A, B) XOR(C, D) F
00 0 0 0 0 0 0 0
01 0 0 0 1 0 1 1
02 0 0 1 0 0 1 1
03 0 0 1 1 0 0 0
04 0 1 0 0 1 0 1
05 0 1 0 1 1 1 0
06 0 1 1 0 1 1 0
07 0 1 1 1 1 0 1
08 1 0 0 0 1 0 1
09 1 0 0 1 1 1 0
10 1 0 1 0 1 1 0
11 1 0 1 1 1 0 1
12 1 1 0 0 0 0 0
13 1 1 0 1 0 1 1
14 1 1 1 0 0 1 1
15 1 1 1 1 0 0 0
A handy tool for such questions is "Logic Friday 1"
Related
I have a matrix:
$cat ifile.txt
2 3 4 5 10 0 2 2 0 1 0 0 0 1
0 3 4 6 2 0 2 0 0 0 0 1 2 3
0 0 0 2 3 0 3 0 3 1 2 3 1 0
Here it has total 14 columns e.g. A1 B1 A2 B2 A3 B3 A4 B4 A5 B5 A6 B6 A7 B7. Each odd number columns correspond to A and even number columns correspond to B.
I would like to print all A in one column and all B in one column. So my desire file looks like:
$cat ofile.txt
2 3
0 3
0 0
4 5
4 6
0 2
10 0
2 0
3 0
2 0
0 0
0 3
....
It is possible for me to do manually in the following way, but I am looking for some more easy way to do it.
for c in 1 3 5 7 9 11 13;do
awk'{printf"%5s %5s",$c,$(c+1)} > A$c.txt
cat A1 A3 A5 A7 A9 A11 A13 > ofile.txt
$ cat tst.awk
{
for ( i=1; i<=NF; i++ ) {
a[NR,i] = $i
}
}
END {
for ( i=1; i<=NF; i+=2 ) {
for (j=1; j<=NR; j++ ) {
print a[j,i], a[j,i+1]
}
}
}
.
$ awk -f tst.awk file
2 3
0 3
0 0
4 5
4 6
0 2
10 0
2 0
3 0
2 2
2 0
3 0
0 1
0 0
3 1
0 0
0 1
2 3
0 1
2 3
1 0
If you want to generalize for more than 2 output columns:
$ cat tst.awk
BEGIN { n=(n ? n : 2) }
{
for (i=1; i<=NF; i++) {
a[NR,i] = $i
}
}
END {
for ( i=1; i<=NF; i+=n ) {
for (j=1; j<=NR; j++) {
for ( k=1; k<=n; k++ ) {
printf "%s%s", a[j,i+k-1], (k<n ? OFS : ORS)
}
}
}
}
.
$ awk -v n=2 -f tst.awk file
2 3
0 3
0 0
4 5
4 6
0 2
10 0
2 0
3 0
2 2
2 0
3 0
0 1
0 0
3 1
0 0
0 1
2 3
0 1
2 3
1 0
.
$ awk -v n=7 -f tst.awk file
2 3 4 5 10 0 2
0 3 4 6 2 0 2
0 0 0 2 3 0 3
2 0 1 0 0 0 1
0 0 0 0 1 2 3
0 3 1 2 3 1 0
We have a matrix:
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
I want to retain the data in these fields, but in the shape of a 2D-circle:
0 1 1 1 0
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
0 1 1 1 0
But this also scales up:
0 0 0 1 1 1 1 0 0 0
0 0 1 1 1 1 1 1 0 0
0 1 1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
0 1 1 1 1 1 1 1 1 0
0 0 1 1 1 1 1 1 0 0
0 0 0 1 1 1 1 0 0 0
What would the greatest way to approach this?
cy and cx are center points
r is the radius
tiles is an empty grid
function MakeCircle(tiles, cx, cy, r):
for x in range(cx - r, cx + r):
for y in range(cy - r, cy + r):
if (distance(cx, cy, x, y) <= r):
tiles[x][y] = 1
return(tiles)
function distance(x1, y1, x2, y2):
return(sqrt((x1 - x2)**2) + (y1 - y2)**2))
This dynamically creates a circle of ones in a square-matrix regardless of size of matrix.
I need to split a file with ~5million rows based on some columns, i.e, I need to keep some columns on the different chunks. I am aware of split command for row-wise splitting, but don't know if there is any similar function to split column-wise with as I would like to. My file has 196 ANN columns
SNPID CHR POS Z F N LNBF ANN1 ANN2 ANN3
rs367896724 1 10177 0 0 0 -3.36827717630604 0 0 0
rs555500075 1 10352 0 0 0 -2.30999509213213 0 1 0
rs575272151 1 11008 0 0 0 -1.14611711529388 0 0 1
rs544419019 1 11012 0 0 0 -1.14611711529388 1 1 1
The desired output will be
#chunk1
SNPID CHR POS Z F N LNBF ANN1
rs367896724 1 10177 0 0 0 -3.36827717630604 0
rs555500075 1 10352 0 0 0 -2.30999509213213 0
rs575272151 1 11008 0 0 0 -1.14611711529388 0
rs544419019 1 11012 0 0 0 -1.14611711529388 1
#chunk2
SNPID CHR POS Z F N LNBF ANN2
rs367896724 1 10177 0 0 0 -3.36827717630604 0
rs555500075 1 10352 0 0 0 -2.30999509213213 1
rs575272151 1 11008 0 0 0 -1.14611711529388 0
rs544419019 1 11012 0 0 0 -1.14611711529388 1
#chunk3
SNPID CHR POS Z F N LNBF ANN3
rs367896724 1 10177 0 0 0 -3.36827717630604 0
rs555500075 1 10352 0 0 0 -2.30999509213213 0
rs575272151 1 11008 0 0 0 -1.14611711529388 1
rs544419019 1 11012 0 0 0 -1.14611711529388 1
The names of my ANN columns are not like ANN1 ANN2, the names are quite different to each other, I have just used ANN for simplicity.
The speed would be an issue, since the file is quite huge
UPDATE: if it would be possible I would like to split the files every 10 or 20 ANN columns (the total number of ANN is 196)
Something like this might work:
% cat script.awk
{
for (i=8;i<=NF;i++) {
print $1, $2, $3, $4, $5, $6, $7, $i >> "chunk"(i-7)".txt"
}
}
This will write 8 columns for each ANN columns into chunk1.txt, chunk2.txt, ... chunkN.txt (First 7 and then one ANN column). Run it with:
awk -f script.awk input_file
I assume that >> will open a file handle, append the line and then close it. So it's properly possible to optimize it.
A solution with perl:
The initial file, with a few extra columns
$ cat file
SNPID CHR POS Z F N LNBF ANN1 ANN2 ANN3 ANN4 ANN5 ANN6 ANN7 ANN8
rs367896724 1 10177 0 0 0 -3.36827717630604 0 0 0 a b c d e
rs555500075 1 10352 0 0 0 -2.30999509213213 0 1 0 f g h i j
rs575272151 1 11008 0 0 0 -1.14611711529388 0 0 1 k l m n o
rs544419019 1 11012 0 0 0 -1.14611711529388 1 1 1 p q r s t
A perl script to split it up
$ perl -alne '
$n=4; # how many data columns to put into the "split" files
for ( ($i,$j)=(7,1); $i < #F; $i+=$n,$j++ ) {
open($fh{$j}, ">", "file.$j") unless $fh{$j};
#data = (#F[0..6], #F[$i .. $i+$n-1]);
print {$fh{$j}} "#data";
}
' file
The results
$ cat file.1
SNPID CHR POS Z F N LNBF ANN1 ANN2 ANN3 ANN4
rs367896724 1 10177 0 0 0 -3.36827717630604 0 0 0 a
rs555500075 1 10352 0 0 0 -2.30999509213213 0 1 0 f
rs575272151 1 11008 0 0 0 -1.14611711529388 0 0 1 k
rs544419019 1 11012 0 0 0 -1.14611711529388 1 1 1 p
$ cat file.2
SNPID CHR POS Z F N LNBF ANN5 ANN6 ANN7 ANN8
rs367896724 1 10177 0 0 0 -3.36827717630604 b c d e
rs555500075 1 10352 0 0 0 -2.30999509213213 g h i j
rs575272151 1 11008 0 0 0 -1.14611711529388 l m n o
rs544419019 1 11012 0 0 0 -1.14611711529388 q r s t
ok, let's I have a txt file like this...
X 1 : D i s t a n c e [ m m ]
Y 1 : I n t e n s i t y
X 2 : D i s t a n c e [ m m ]
Y 2 : I n t e n s i t y
I m a g e ( 2 3 7 . 2 3 u )
X 1 Y 1
0 . 0 0 0 0 0 0 4 0 . 0 0 0 0 0 0
0 . 0 0 2 0 0 0 5 7 . 0 0 0 0 0 0
...etc
And several others similar to this...
X 1 : D i s t a n c e [ m m ]
Y 1 : I n t e n s i t y
X 2 : D i s t a n c e [ m m ]
Y 2 : I n t e n s i t y
I m a g e ( 2 6 5 . 2 7 u )
X 1 Y 1
0 . 0 0 0 0 0 0 3 6 . 0 0 0 0 0 0
0 . 0 0 2 0 0 0 3 4 . 0 0 0 0 0 0
0 . 0 0 4 0 0 0 4 0 . 0 0 0 0 0 0
When I use paste, to merge horizontally the content of these files...
#! /bin/bash
zeta=$(ls)
paste $zeta >> file_1.txt
I get this (example if there were two files):
X 1 : D i s t a n c e [ m m ]
X 1 : D i s t a n c e [ m m ]
Y 1 : I n t e n s i t y
Y 1 : I n t e n s i t y
X 2 : D i s t a n c e [ m m ]
X 2 : D i s t a n c e [ m m ]
Y 2 : I n t e n s i t y
Y 2 : I n t e n s i t y
I m a g e ( 2 3 7 . 2 3 u )
I m a g e ( 2 6 5 . 2 7 u )
X 1 Y 1
X 1 Y 1
0 . 0 0 0 0 0 0 4 0 . 0 0 0 0 0 0
0 . 0 0 0 0 0 0 3 6 . 0 0 0 0 0 0
0 . 0 0 2 0 0 0 5 7 . 0 0 0 0 0 0
0 . 0 0 2 0 0 0 3 4 . 0 0 0 0 0 0
0 . 0 0 4 0 0 0 4 1 . 0 0 0 0 0 0
0 . 0 0 4 0 0 0 4 0 . 0 0 0 0 0 0
Why do I have this intermingle of lines?
How can I do to put exactly the content of a txt file just aside of the content of the other txt file? In this case have the columns 1 and 2 for my first file, and the columns 3 and 4 for my second file. And then massively for several files?
Thanks for any hint,
Maybe you can put several '\t' between context of the line and '\n' :
cat text1.txt | tr "\n" "\t\t\n" > text1.txt
After the processes, you can use your old method to paste them together. :)
I don't know if this question is considered to be related to stackoverflow (I'm sorry if it's not but I have searched and did not find an answer anywhere).
I have coded a full adder
Output:
Truth Table :
a1 a2 b1 b2 S1 S2 C
______________________________
0 0 0 0 0 0 0
0 0 0 1 0 1 0
0 0 1 0 1 0 0
0 0 1 1 1 1 0
0 1 0 0 0 1 0
0 1 0 1 0 0 1
0 1 1 0 1 1 0
0 1 1 1 1 0 1
1 0 0 0 1 0 0
1 0 0 1 1 1 0
1 0 1 0 0 1 0
1 0 1 1 0 0 1
1 1 0 0 1 1 0
1 1 0 1 1 0 1
1 1 1 0 0 0 1
1 1 1 1 0 1 1
If somebody has ever calculated this, can they tell me if my output is correct
a1 a2 b1 b2 S1 S2 C a b s c
______________________________
0 0 0 0 0 0 0 0 0 0 0 nothing plus nothing is nothing
0 0 0 1 0 1 0 0 2 2 0 nothing plus two is two
0 0 1 0 1 0 0 0 1 1 0 nothing plus one is one
0 0 1 1 1 1 0 0 3 3 0 nothing plus three is three
0 1 0 0 0 1 0 2 0 2 0 two plus nothing is two
0 1 0 1 0 0 1 2 2 0 1 two plus two is four (four not in 0-3)
0 1 1 0 1 1 0 2 1 3 0 two plus 1 is three
0 1 1 1 1 0 1 2 3 1 1 two plus three is five (one and four)
1 0 0 0 1 0 0 1 0 1 0 one plus nothing is one
1 0 0 1 1 1 0 1 2 3 0 one plus two is three
1 0 1 0 0 1 0 1 1 2 0 one plus one is two
1 0 1 1 0 0 1 1 3 0 1 one plus three is four
1 1 0 0 1 1 0 3 0 3 0 three plus nothing is three
1 1 0 1 1 0 1 3 2 1 1 three plus two is five (one and four)
1 1 1 0 0 0 1 3 1 0 1 three plus one is four
1 1 1 1 0 1 1 3 3 2 1 three plus three is 6 (two and four)
Looks right. Ordering your 16 rows a little differently would make them flow in a more logical order.
It's an adder! Just check if it's adding. Let's take this row:
a2 a1 b2 b1 C S2 S2
1 0 1 1 1 0 1
Here I have reordered the columns in an easier to read manner: higher order bits first.
The a input is 10 = 2 (base 10). The b input is 11 = 3 (base 10). The output is 101, which
is 5 (base 10). So this one is right: 2 + 3 == 5.
I'll let you check the other rows.