Detecting XOR in Karnaugh Maps - xor

I got the following Karnaugh Maps but I am still having problems working out the expression for XOR from each table.
Table 1
-------
WZ
00 01 11 10
-----------------------
00 | | | | 1 |
-----------------------
01 | 1 | | | |
-----------------------
XY 11 | | | | 1 |
-----------------------
10 | 1 | | | |
-----------------------
Table 2
-------
WZ
00 01 11 10
-----------------------
00 | | 1 | | |
-----------------------
01 | | | 1 | |
-----------------------
XY 11 | | 1 | | |
-----------------------
10 | | | 1 | |
-----------------------
It is XORs, but how can I easily deduce the XOR expressions?

I would not dismiss the variable z from the expression, because I think, the expression ¬z·(¬x·y·¬w + ¬x·w·¬y + ¬y·¬w·x + w·y·x) is not equal to (¬x·y·¬w + ¬x·w·¬y + ¬y·¬w·x + w·y·x). That would mean, that the K-map contains four doubles of ones, but there is only four singles.
I would rather find the expression in the K-map and then use the laws of Boolean algebra.
For the first table:
¬x·¬y·w·¬z + ¬x·y·¬w·¬z + x·y·w·¬z + x·¬y·¬w·¬z
¬z·((¬x + ¬y + w)·(¬x + y + ¬w)·(x + y + w)·(x + ¬y + ¬w)) //distributivity
¬z· (¬x + ¬y + w)·(¬x + y + ¬w)·(x + y + w)·(x + ¬y + ¬w) //relaxed syntax
¬z· (¬x·¬x + ¬x·y + ¬x·¬w + ¬y·¬x + ¬y·y + ¬y·¬w + w·¬x + w·y + w·¬w)·
(x·x + x·¬y + x·¬w + y·x + y·¬y + y·¬w + w·x + w·¬y + w·¬w) //distributivity
Because of the laws of
idempotence (e.g.: ¬x·¬x=¬x),
absorption (e.g.:¬x + ¬x·y=¬x)
and complementation (e.g.: ¬x·x=0)
the expression is equivalent to:
¬z· (¬x + 0 + ¬y·¬w + w·y + 0)·
( x + + 0 + y·¬w + + w·¬y + 0 )
¬z· (¬x + ¬y·¬w + w·y)·(x + y·¬w + w·¬y) //just formatted
¬z· (¬x·x + ¬x·y·¬w + ¬x·w·¬y
+ ¬y·¬w·x + ¬y·¬w·y·¬w + ¬y·¬w·w·¬y
+ w·y·x + w·y·y·¬w + w·y·w·¬y) //distributivity
¬z· ( 0 + ¬x·y·¬w + ¬x·w·¬y
+ ¬y·¬w·x + 0 + 0
+ w·y·x + 0 + 0 ) //using the three laws↑ again
¬z· (¬x·y·¬w + ¬x·w·¬y + ¬y·¬w·x + w·y·x) //how the 3-input XOR is defined
¬z· (x xor y xor w)
For the second table:
¬x·¬y·¬w·z + ¬x·y·w·z + x·y·¬w·z + x·¬y·w·z
z·((¬x + ¬y + ¬w)·(¬x + y + w)·(x + y + ¬w)·(x + ¬y + w)) //distributivity
z· (¬x + ¬y + ¬w)·(¬x + y + w)·(x + y + ¬w)·(x + ¬y + w) //relaxed syntax
z· (¬x·¬x + ¬x·y + ¬x·w + ¬y·¬x + ¬y·y + ¬y·w + ¬w·¬x + ¬w·y + ¬w·w)·
(x·x + x·¬y + x·w + y·x + y·¬y + y·w + ¬w·x + ¬w·¬y + ¬w·w) //distributivity
z· ( ¬x + + 0 + ¬y·w + + ¬w·y + 0 )·
( x + + 0 + y·w + + ¬w·¬y + 0 )
z· (¬x + ¬y·w + ¬w·y)·(x + y·w + ¬w·¬y) //just formatted
z· (¬x·x + ¬x·y·w + ¬x·¬w·¬y
+ ¬y·w·x + ¬y·w·y·w + ¬y·w·¬w·¬y
+ ¬w·y·x + ¬w·y·y·w + ¬w·y·¬w·¬y) //distributivity
z· ( 0 + ¬x·y·w + ¬x·¬w·¬y
+ ¬y·w·x + 0 + 0
+ ¬w·y·x + 0 + 0) //using the three laws↑ again
z· (¬x·y·w + ¬x·¬w·¬y + ¬y·w·x + ¬w·y·x) //how the 3-input XNOR is defined
z· (x xnor y xnor w)

The first table contains an Xor expression :
`First table`
w
\ wz ___________
xy \-----------------------+
| | | | 1 |
+-----+-----+-----+-----+
| 1 | | | | |
+-----+-----+-----+-----+ | y
| | | | | 1 | |
x | +-----+-----+-----+-----+
| | 1 | | | |
+-----------------------+
___________
z
as you could see the middle of the table (Z area) is fake.
that is, the table function is :
F(Table1) = w'x'yz' + wx'y'z' + w'xy'z' + wxyz'
in binary form you could see a zero column :
F(Table) = 0010 eliminating Z F(xor)= 001
0100 ---------------\ 010
1110 ---------------/ 111
1000 100
^--> fake
and the final table must be something like this :
`simplified xor table`
w
\ w 0 __1__
xy \-----------+
00 | | 1 |
+-----+-----+
01 | 1 | | |
+-----+-----+ | y And " F = wy' + w'y " is an Xor only
|10 | 1 | | | between 2 variables, right?
x | +-----+-----+
|11 | | 1 |
+-----------+
The second table just contains an Xnor expression of the first one :
`Second Table`
F(Table2) = w'xyz + wxy'z + w'x'y'z + wx'yz
w
\ wz ___________
xy \-----------------------+ negation of table 2 is table 1 and vise versa
| | 1 | | | F(Table2) = 1101 F(Table2)'= F(Table1) = 0010
+-----+-----+-----+-----+ 1011 0100
| | | 1 | | | 0001 1110
+-----+-----+-----+-----+ | y 0111 1000
| | | 1 | | | | ^--> fake ^
x | +-----+-----+-----+-----+
| | | | 1 | |
+-----------------------+
^ ___________ ^
^ z ^
^ ^
^--------z'-------^
the final table is:
w
\ w 0 __1__
xy \-----------+
00 | 1 | |
+-----+-----+
01 | | 1 | |
+-----+-----+ | y And " F = w'y' + wy " is an Xnor
|10 | | 1 | |
x | +-----+-----+
|11 | 1 | |
+-----------+
Always remember the tables that contain the zigzag pattern
are either an Xor or Xnor expression.

Just put a copy of this map on the right hand side of it (or left, no difference) and then choose two tilted cubes.
Now, we write the simplified function for both of them:
(A = 1) (AND) (B=0 when C=1 and B=1 when C=0)
(OR)
(A = 0) (AND) (B=0 when C=0 and B=1 when C=1)
that finaly gives this:
(A AND (B XOR C)) OR (¬A AND (B XNOR C))

Basic rule for xor is that it gives 1 when odd number of input are 1.
So in KMAP just see if 1 is present in all the odd number of 1's.
Like WXYZ ( 0010, 1110 etc) if all gives 1 than there is a XOR in kmap.

Related

Parsing an excel sheet with VBA

Scenario: In my worksheet I have data in multiple columns on the following format (this is an ASCII generated table to facilitate visualization, the symbols are not on the original spreadsheet):
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
| Count | Indentifier1 | Identifier2 | Identifier3 | ProductName | Location | Type | Status | RegistryDate |
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
| 1 | azjzjzj3611a | | | Super electric board - high load | SEA | General | Sold out -- Used -- | 4/17/2019 4:19 |
| | | | | | | | Good | |
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
| 2 | y000zj9y30 | | | electric board - low battery | SEA | General | Sold out -- Used -- Good | 5/12/2015 3:48 |
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
| 3 | | | | Super jet-ski 01 Special | SHORE | TEST - Utility - Vehicle | | |
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
| 4 | a30y970y3 | | | Super jet-ski 01 Special | SHORE | TEST - Utility - Vehicle | Used - Good | |
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
| 5 | | | | Ball 10-Type1 | BEACH | TEST - Utility - Small-Item | | |
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
| 6 | azjzjzja9zjy9 | | | Ball 10-Type1 | BEACH | | | |
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
| 7 | azjzjzja9zjy9 | | | Ball 10-Type1 | BEACH | TEST - Utility - Small-Item | Sold out -- Used - Good | 6/10/2013 0:00 |
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
| 8 | a3044aa69 | 1007750 | | Ball 10-Type1 | | | | |
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
| 9 | | | | Ball 10-Type1 | BEACH | TEST - Utility - Small Unit | | |
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
| 10 | a3044aa69 | | BLL101 | Ball 10-Type1 | BEACH | TEST - Utility - Small Unit | Used - Good | |
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
| 11 | a3044aa69 | 1007750 | BLL101 | BALL 10-TYPE1 | Beach | TEST - Utility | | |
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
| 12 | y0003aa67 | 36021 | | Ball 5-Type1 | | | | |
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
| 13 | | | | Ball 5-Type1 | RIDGE | Group - Special | | |
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
| 14 | y0003aa67 | | | Ball 5-Type1 | RIDGE | | | |
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
| 15 | y0003aa67 | | BLL051 | Ball 5-Type1 | RIDGE | Group - Special | Used - Good | |
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
| 16 | y0003aa67 | 36021 | BLL051 | BALL 5-TYPE1 | Ridge | Group - Special | | |
+-------+---------------+-------------+-------------+----------------------------------+----------+-----------------------------+--------------------------+----------------+
Obs.: As per the table, the data can have multiple entries, where different information is displayed in each column. The differences may be either the format (upper/lower-case) or content (has data or not).
Objective: I am trying to retrieve the information from this table, without loosing data. Ex: If two rows have the same data, but one is lower case whereas the other is upper case, either could be retrieved (no preference), but if there is different data (maybe there is a slight difference in the item name or status), I am trying to retrieve all the data.
Done so far: I was able to simple create an array, loop it, and compare the data manually, but the result is sub-optimal, since it just aggregates the data in the same cell where it is different. Which would still require manual checking/cleaning.
Obs2.: This table has around 22 thousand rows.
Obs3.: Another issue of this data-set is that none of the columns is completely filled. Sometimes I have data for all identifiers and item name, sometimes an identifier is missing, sometimes the name is missing, and sometimes just one identifier is available (no name or anything else).
Question: Is there a way to do this?
Wanted output:
+-------+--------------+-------------+-------------+------------------------------+----------+-----------------------------+--------------------------+----------------+
| Count | Indentifier1 | Identifier2 | Identifier3 | ProductName | Location | Type | Status | RegistryDate |
+-------+--------------+-------------+-------------+------------------------------+----------+-----------------------------+--------------------------+----------------+
| 1 | azjzjzj3611a | | | Super electric | SEA | General | Sold out -- Used -- | 4/17/2019 4:19 |
| | | | | board - high load | | | Good | |
+-------+--------------+-------------+-------------+------------------------------+----------+-----------------------------+--------------------------+----------------+
| 2 | y000zj9y30 | | | electric board - low battery | SEA | General | Sold out -- Used -- Good | 5/12/2015 3:48 |
+-------+--------------+-------------+-------------+------------------------------+----------+-----------------------------+--------------------------+----------------+
| 3 | a30y970y3 | | | Super jet-ski 01 Special | SHORE | TEST - Utility - Vehicle | Used - Good | |
+-------+--------------+-------------+-------------+------------------------------+----------+-----------------------------+--------------------------+----------------+
| 4 | a3044aa69 | 1007750 | | Ball 10-Type2 | BEACH | TEST - Utility - Small-Item | Sold out -- Used - Good | 6/10/2013 0:00 |
+-------+--------------+-------------+-------------+------------------------------+----------+-----------------------------+--------------------------+----------------+
| 5 | a3044aa69 | 1007750 | BLL101 | Ball 10-Type1 | BEACH | TEST - Utility - Small Unit | Used - Good | |
+-------+--------------+-------------+-------------+------------------------------+----------+-----------------------------+--------------------------+----------------+
| 6 | y0003aa67 | 36021 | BLL051 | BALL 5-TYPE1 | Ridge | Group - Special | Used - Good | |
+-------+--------------+-------------+-------------+------------------------------+----------+-----------------------------+--------------------------+----------------+
Code for array approach:
Sub cleanup_detail()
Dim raw_array As Variant
Dim w As Workbook
Dim loopvar1 As Long
Set w = ThisWorkbook
raw_array = w.Worksheets("Sheet1").UsedRange
For loopvar1 = 2 To UBound(raw_array, 1)
If raw_array(loopvar1 + 1, 2) = "" Or raw_array(loopvar1 + 1, 2) = "0" Then
If raw_array(loopvar1 + 1, 3) = "" Or raw_array(loopvar1 + 1, 3) = "0" Then
If raw_array(loopvar1 + 1, 4) = "" Or raw_array(loopvar1 + 1, 4) = "0" Then
If raw_array(loopvar1 + 1, 5) = "" Or raw_array(loopvar1 + 1, 5) = "0" Then
Next loopvar1
ElseIf raw_array(loopvar1 + 1, 5) = raw_array(loopvar1, 5) Then
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 2) = raw_array(loopvar1 + 1, 2) & "/" & raw_array(loopvar1, 2)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 3) & "/" & raw_array(loopvar1, 3)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 4) & "/" & raw_array(loopvar1, 4)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 5)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 6) & "/" & raw_array(loopvar1, 6)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 7) & "/" & raw_array(loopvar1, 7)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 8) & "/" & raw_array(loopvar1, 8)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 9) & "/" & raw_array(loopvar1, 9)
End If
ElseIf raw_array(loopvar1 + 1, 4) = raw_array(loopvar1, 4) Then
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 2) = raw_array(loopvar1 + 1, 2) & "/" & raw_array(loopvar1, 2)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 3) & "/" & raw_array(loopvar1, 3)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 4)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 5) & "/" & raw_array(loopvar1, 5)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 6) & "/" & raw_array(loopvar1, 6)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 7) & "/" & raw_array(loopvar1, 7)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 8) & "/" & raw_array(loopvar1, 8)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 9) & "/" & raw_array(loopvar1, 9)
End If
ElseIf raw_array(loopvar1 + 1, 3) = raw_array(loopvar1, 3) Then
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 2) = raw_array(loopvar1 + 1, 2) & "/" & raw_array(loopvar1, 2)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 3) & "/" & raw_array(loopvar1, 3)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 4) & "/" & raw_array(loopvar1, 4)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 5) & "/" & raw_array(loopvar1, 5)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 6) & "/" & raw_array(loopvar1, 6)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 7) & "/" & raw_array(loopvar1, 7)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 8) & "/" & raw_array(loopvar1, 8)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 9) & "/" & raw_array(loopvar1, 9)
End If
ElseIf raw_array(loopvar1 + 1, 2) = raw_array(loopvar1, 2) Then
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 2) = raw_array(loopvar1 + 1, 2)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 3) & "/" & raw_array(loopvar1, 3)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 4) & "/" & raw_array(loopvar1, 4)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 5) & "/" & raw_array(loopvar1, 5)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 6) & "/" & raw_array(loopvar1, 6)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 7) & "/" & raw_array(loopvar1, 7)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 8) & "/" & raw_array(loopvar1, 8)
w.Worksheets("Sheet1").Cells(loopvar1 + 1, 3) = raw_array(loopvar1 + 1, 9) & "/" & raw_array(loopvar1, 9)
End If
Next loopvar1
End Sub

Binary addition

I am trying to understand how Bitwise operators work in C, but I have an misunderstanding about the << operator.
I have the following code:
#include <stdio.h>
int Add(int x, int y);
int Add(int x, int y)
{
while ( y != 0 ) /// Iterate till there is no carry
{
int carry = x & y; /// carry now contains common set bits of x and y
x = x ^ y; /// Sum of bits of x and y where at least one of the bits is not set
y = carry << 1; /// Carry is shifted by one so that adding it to x gives the required sum
}
return x;
}
int main( void )
{
printf( "%d", Add( 13, 17 ) );
return 0;
}
If I understand correctly works like this:
First Iteration:
|=======================================|
| |
| while ( y != 0 ) |
| while ( 17 != 0 ) |
| while ( 10001 != 00000 ) |
| |
| c = x & y; |
| 1 = 13 & 17 |
| 00001 = 01101 & 10001 |
| |
| x = x ^ y |
| 28 = 13 ^ 17 |
| 11100 = 01101 ^ 10001 |
| |
| y = c << 1 |
| 17 = 1 << 1 |
| 10001 = 00001 << 00001 |
| 00010 = 00001 << 00001 |
| |
|=======================================|
Second Iteration:
|=======================================|
| |
| while ( y != 0 ) |
| while ( 2 != 0 ) |
| while ( 00010 != 00000 ) |
| |
| c = x & y; |
| 0 = 28 & 2 |
| 00000 = 11100 & 00010 |
| |
| x = x ^ y |
| 30 = 28 ^ 2 |
| 11110 = 11100 ^ 00010 |
| |
| y = c << 1 |
| 2 = 0 << 1 |
| 00010 = 00000 << 00001 |
| 00000 = 00000 << 00001 |
| |
|=======================================|
Then Y becomes 0 and X returns 30.
Now in the following code I have an issue:
#include <stdio.h>
int main( void )
{
int x = 13;
int y = x << 1; /// 11010 = 01101 << 00001
x = 0 << 1; /// 00000 = 00000 << 00001
printf("y = %d\n", y ); /// 26 | 11010
printf("x = %d\n", x ); /// 26 | 11010
return 0;
}
Here if I understand right we shift all bits one to the left:
int y = x << 1; /// 11010 = 01101 << 00001
But what exactly happens here:
x = 0 << 1; /// 00000 = 00000 << 00001
Does x get cleared and filled with the rezult of 0 << 1 ?
Does x get cleared and filled with the rezult of 0 << 1 ?
x is just assigned the value of the expression 0 << 1. Zero left or right shifted by any amount remains 0.
So this means that the representation of the First and Second Iteration is correct?
It is correct, except that substitution of the old values of variables (on the lhs) is a bit confusing as in the following cases.
17 = 1 << 1
10001 = 00001 << 00001
and
2 = 0 << 1
00010 = 00000 << 00001
Instead depict it as:
y = 1 << 1
y = 00001 << 00001
and
y = 0 << 1
y = 00000 << 00001
n << k is actually n * (2^k) as long as you have enough bits available to keep all of the resulting bits. So 0 << k is 0 * (2^k) = 0 whatever the (positive integer) value of k is.
Note that for usual 32 bit integers, a number on p = 17 bits or more, like 65537 (0x0001_0001), will stop behaving like the multiplication once k is greater or equal to (32+1)-p = (32+1)-17 = 16, as for example 65537 << 16 is 0x1_0001_0000 which is using 33 bits and is truncated to 0x0001_0000 = 65536.
With 65536 << 15 you may also start having strange results as the result, 0x1000_0000, is changing the left most bit, which is the sign bit if you're not using unsigned values...

Flatten table in Excel using VBA

I'm looking to use VBA to transform a raw data extract into a flattened table for querying. Currently, I have a raw data table in Excel that summarizes the status of Phases A, B, and C for a given Engagement (note: some Engagements may not have data for all 3 phases).
Row| EngagementID | A_date | A_status | B_date | B_status | C_date | C_status
1 | 201 | 2/2 | Approved | | | |
2 | 201 | | | 3/5 | Approved | |
3 | 201 | | | | | 4/1 | Pending
4 | 203 | 2/12 | Submitted| | | |
5 | 203 | | | 2/20 | Approved | |
6 | 207 | 2/5 | Approved | | | |
I need to flatten the table to look like something this:
Row| EngagementID | Date | Status
1 | 201 | 2/2 | Approved
2 | 201 | 3/5 | Approved
3 | 201 | 4/1 | Pending
4 | 203 | 2/12| Submitted
5 | 203 | 2/20| Approved
6 | 207 | 2/5 | Approved
Additionally, I'd like to add a column for the Phase so that I can "tag" each row with the Phase (A, B, or C) that it is associated with.
I've tried the following VBA code, but it flattens the table vertically, as opposed to horizontally (merging 3 rows into 1, as opposed to 3 columns into 1):
Private Sub test()
Dim R As Long
Dim i As Integer
i = 1
R = 2
Count = 0
Do While Not IsEmpty(Range("A" & R))
If Cells(R, 1).Value = Cells(R + 1, 1).Value Then
Count = Count + 1
Else
i = 1
Do While i <= Count
Cells(R - Count, 2 + (2 * i)).Value = Cells(R - Count + i, 2 + (2 * i))
Cells(R - Count, 3 + (2 * i)).Value = Cells(R - Count + i, 3 + (2 * i))
i = i + 1
Loop
i = 1
Do While i <= Count
Rows(R - Count + i).Delete
i = i + 1
R = R - 1
Loop
Count = 0
End If
R = R + 1
Loop
End Sub
Please help!!
Try this code
Sub Test()
Dim a As Variant
Dim b As Variant
Dim i As Long
Dim j As Long
Dim k As Long
a = Range("A1").CurrentRegion.Value
ReDim b(1 To UBound(a, 1) * 3, 1 To 3)
For i = 2 To UBound(a, 1)
For j = 2 To UBound(a, 2) Step 2
If a(i, j) <> "" And a(i, j + 1) <> "" Then
k = k + 1
b(k, 1) = a(i, 1)
b(k, 2) = a(i, j)
b(k, 3) = a(i, j + 1)
End If
Next j
Next i
Range("J1").Resize(k, UBound(b, 2)).Value = b
End Sub

Replace Square-Expression in Maple

Given a maple expression like
f := (y + 5)^2 + x^2
How can I replace all occurrences of the square function with a custom function? The result I want to have would be
square(y + 5) + square(x)
I tried something like
subs({x^2 = square(x)}, f)
However, that only replaces the x^2 expression. I could of course list explicitly the (y + 5) term as well, but I want to replace any occurrence of (.)^2 without listing everything explicitly.
How can I do this in Maple?
You can accomplish this using either subsindets or applyrule.
expression:=(y-5)^2+3+sin(((z-1/3)^2/(t-(y-s)^2)))+F(f(17+p^2)^2);
/ 2 \
| / 1\ |
| |z - -| | / 2\
2 | \ 3/ | | / 2 \ |
expression := (y - 5) + 3 + sin|------------| + F\f\p + 17/ /
| 2|
\t - (y - s) /
applyrule(_a::anything^2=square(_a), expression);
/ / 1\ \
| square|z - -| |
| \ 3/ |
square(y - 5) + 3 + sin|-----------------| + F(square(f(square(p) + 17)))
\t - square(y - s)/
subsindets(expression, '`^`'(anything,2), u->square(op(1,u)));
/ / 1\ \
| square|z - -| |
| \ 3/ |
square(y - 5) + 3 + sin|-----------------| + F(square(f(square(p) + 17)))
\t - square(y - s)/

Why this type of power function work?

res = 1;
for ( i = 1; i <= n; i <<= 1 ) // n = exponent
{
if ( n & i )
res *= a; // a = base
a *= a;
}
This should be more effective code for power and I don't know why this works.
First line of for() is fine I know why is there i <<= i. But I don't understand the line where is: if ( n & i ). I know how that works but I don't know why...
Let us say you have a binary representation of an unsigned number. How do you find the decimal representation?
Let us take a simple four bit example:
N = | 0 | 1 | 0 | 1 |
-----------------------------------------
| 2^3 = 8 | 2^2 = 4 | 2^1 = 2 | 2^0 = 1 |
-----------------------------------------
| 0 | 4 | 0 | 1 | N = 4 + 1 = 5
Now what would happen if the base wasn't fixed at 2 for each bit but instead was the square of the previous bit and you multiply the contribution from each bit instead of adding:
N = | 0 | 1 | 0 | 1 |
----------------------------
| a^8 | a^4 | a^2 | a^1 |
----------------------------
| 0 | a^4 | 0 | a^1 | N = a^4 * a^1 = a^(4+1) = a^5
As you can see, the code calculate a^N

Resources