Reverse of the logical operator AND (&) - logical-operators

a + b = c
c - a = b
Ok, now
a & b = c
c ?? a = b
which operator replace "??" ?
Thanks

There is no such operator, because it will be ill-defined if it exists:
so, let a = 0, c = 0
we have
a & 0 = c
a & 1 = c
then, we should have
c ?? a = 0 and c ?? a = 1
, but an operator/function cannot return two values given the same input/parameters.

It's impossible. This is because a & b is a lossy transformation.
You don't know whether any dropped 1 bits were part of a or b.

You can't.
0 && 0 == 0
1 && 0 == 0
To reverse this, you'd need an operator that gives back both 0 and 1.
(But if b == 1 you do of course know that a == c.)

No operator can replace "??" !
Only relation (logical relation !) b = (c=a)x2 + (not c)
can replace ""??".
J.Kawecki: Logical Relations. Warsaw,2019 Druk24h.pl

Related

How could the following function be written using iteration instead of recursion?

How could the following function be written using iteration instead of recursion?
function mystery(b)
{
if b == 0 then
return 0
if (b / 2 == 0)
return mystery (b-1) + 3
else
return mystery (b-1) + 2
}
I assume that when you are doing b / 2 == 0 you are checking whether the number b is even or odd otherwise that is true only for case where b= 0,1.
Recursive function
def mystery(b):
if b == 0:
return 0
if b % 2 == 0:#check if b is even
return mystery(b-1)+3
else: return mystery(b-1)+ 2
Iterative function
def mystery_iter(b):
result= 0
while b > 0:
if b % 2 == 0:#check if b is even
result += 3
b= b-1
else:
result += 2
b= b-1
return result
At least two people suggest you reformulate the recursion as a loop. I instead suggest you first try to understand the mathematics of what the function is doing before considering unwinding the implicit recursive loop into an explicit iterative loop. Doing so in this case, you get a simpler answer:
def mystery(b):
if b == 0:
return 0
if b % 2 == 0:
return b // 2 * 5
return b // 2 * 5 + 2
This could be further reduced code-wise, possibly to a one-liner, if one desires. (Like #AkhileshPandey, I'm assuming that the division in (b / 2 == 0) was supposed to be a modulus operation (b % 2 == 0)) The above example is Python 3 as it wasn't clear what language the OP used, nor that the given code would run correctly in said language due to inconsistent use of then.

Number addition in Lua - avoiding negative values

This is not hardcore math but I simply cannot find the correct function to make this in a smooth way.
Lets say I have 3 values. Cost1 Cost2 Cost3. Each have a value, I want to add them together into a final number, TotalCost.
Cost1+Cost2+Cost3 = TotalCost
Problem is, if any of Cost1/2/3 is negative, I want to make that a ZERO, ie;
Cost1 = -100
Cost2 = 50
Cost3 = 150
Cost1+Cost2+Cost3 = TotalCost
equals
0 + 50 + 150 = 200
I know I have seen something with like (X*Math.Floor * 100) / 100 , to do just this, if im not completly mistaken.
Would be greatly apreciated if anyone could answer. I know its a basic question but I simply couldent figure out how (with a smart way that is) with the Math. functions.
Im coding in Lua: http://lua-users.org/wiki/MathLibraryTutorial
Possibly the shortest way to do this is math.max(x,0). So your expression would be:
math.max(Cost1,0) + math.max(Cost2,0) + math.max(Cost3,0)
Of course, you could also make a function out of it -- and you probably should, if you're going to use it for more than a one-liner.
The most straight-forward way is to use if statements to test if a number is negative.
This is another way:
function my_sum(...)
sum = 0
for k, v in ipairs{...} do
sum = sum + (v > 0 and v or 0)
end
return sum
end
print(my_sum(-50, 50, 100)) -- 150
The expression v > 0 and v or 0 has a value of v if v > 0 is true, 0 otherwise.
Just write exactly what you said in Lua instead of English:
(Cost1 > 0 and Cost1 or 0) + (Cost2 > 0 and Cost2 or 0) + (Cost3 > 0 and Cost3 or 0)
You can simply do something like this:
local value1 = 100
local value2 = -200
local value3 = 200
local value4 = (value1 > 0 and value1 or 0) + (value2 > 0 and value2 or 0) + (value3 > 0 and value3 or 0)
The nicest way would be to implement a function that sums up non-negative values
function sumOfPositives(tableOfValues)
local sum = 0
for i,v in ipairs(tableOfValues) do
sum = sum + v > 0 and v or 0
end
return sum
end
This way you can do it for any number of values.
If you prefer to just enter the values without having them in a table you can do what Yu Hao suggested and use the ... argument.

Extracting alternating sequence from vector in R

I have a data looking like the following:
A= c(0,0,0,-1,0,0,0,1,1,1,0,0,-1,0,0,-1,-1,1,1,1,-1,0,0,0,-1,0,0,-1,-1,1,1,0,0,0,0,1,-1)
The goal is to extract alternating -1s and 1s. I want to make a function where the input vector contains 0,1, and -1. The output ideally spits out all the 0s and alternating -1s and 1s.
For instance, the desired output for the above example is:
B= c(0,0,0,-1,0,0,0,1,0,0,0,0,-1,0,0,0,0,1,0,0,-1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,-1)
The two 1s in the 9th and 10th location in A is turned to 0 because we only keep the first 1 or -1 appearing. The -1s in 16th and 17th location of A is turned to 0 for this reason as well.
Anyone have a good idea for making such a function?
Identify positions of nonzero values:
w = which(A != 0)
For each run of similar values, in A[w], take the position of the first:
library(data.table)
wkeep = tapply(w, rleid(A[w]), FUN = function(x) x[1])
Set all other values to zero:
# following #alexis_laz's approach
B = numeric(length(A))
B[ wkeep ] = A[ wkeep ]
This way, you don't have to make comparisons in a loop, which R is slow at, I think.
rleid comes from data.table. With base R, you can make wkeep with #alexis_laz's suggestion:
wkeep = w[c(TRUE, A[w][-1L] != A[w][-length(w)])]
Or write your own rleid, as in Josh's answer.
This is really just a Reification of GWarius's pseudo-code. (I already had a structure but logic that was failing.)
last1 <- -A[which(A != 0)[1] ] # The opposite of the first non-zero item
for (i in seq_along(A) ){
if( last1==1 && A[i]==-1 ){ last1 <- -1
} else {if (last1 == -1 && A[i] == 1) { last1 <- 1
} else {A[i] <- 0}} }
A
[1] 0 0 0 -1 0 0 0 1 0 0 0 0 -1 0 0 0 0 1 0 0 -1 0 0
[24] 0 0 0 0 0 0 1 0 0 0 0 0 0 -1
> identical(A, B)
[1] TRUE
you have to slide all the array and with a flag variable you check if previously you found 1 or -1.
it could be possible pseudo-code algorithm:
while i < length(a):
if flag == 1 && a[i]=-1:
b[i]=a[i];
flag = -1;
else if flag == -1 && a[i] = 1:
b[i]=a[i];
flag = 1;
else:
b[i]=0;
i++;
}//end of while

Simplify expression Boolean-Algebra

For my class I have to study some Boolean algebra. Now I'm having some difficulties with simplifying expression.
For example I get:
A.B.C + NOT(A) + NOT(B) + NOT(C)
I tried checking wolfram alpha but there's not simplification showing up there.
Can you tell me how to simplify this expression?
Thanks in advance
Truth table:
A B C X
0 0 0 1
0 0 1 1
0 1 0 1
0 1 1 1
1 0 0 1
1 0 1 1
1 1 0 1
1 1 1 1
So the simplification is just:
X = 1
Boolean Algebraic Solution (using a more traditional notation):
Given Boolean expression:
abc + a' + b' + c'
Apply double negation:
(abc + a' + b' + c')''
Apply De Morgan's Law for a disjunction:
((abc)'a''b''c'')'
Reduce double negations:
((abc)'abc)'
AND of x and x' is 0:
(0)'
Negation of 0 is 1:
1
Boolean Algebraic Solution (using the given notation):
Given Boolean expression:
a.b.c + NOT(a) + NOT(b) + NOT(c)
Apply double negation:
NOT(NOT(a.b.c + NOT(a) + NOT(b) + NOT(c)))
Apply De Morgan's Law for a disjunction:
NOT(NOT(a.b.c).NOT(NOT(a)).NOT(NOT(b)).NOT(NOT(c))))
Reduce double negations:
NOT(NOT(a.b.c).a.b.c)
AND of x and x' is 0:
NOT(0)
Negation of 0 is 1:
1
Wolfram Alpha wasn't giving a simplification because it didn't understand your notation. Using (A and B and C) or NOT(A) or NOT(B) or NOT(C) shows that it simplifies to true.
Or you can just look at it: if any are false, the NOT will makke everything true, and if they're all true, then so is the first clause.
Use http://en.wikipedia.org/wiki/De_Morgan%27s_laws
A AND B = NOT(NOT(A) OR NOT(B))
A OR B = NOT(NOT(A) AND NOT(B))
and the normal methods for distribution, commutation, etc. See
http://en.wikipedia.org/wiki/Boolean_algebra#Laws
Note that in the linked texts above the symbols are written differently.
AND is ∧
OR is ∨
NOT is ¬

Remove bits from number

I have some bit flag, where:
A = 1 (0001)
B = 2 (0010)
C = 4 (0100)
D = 8 (1000)
I would like to set bit A and C in my flag: flag = A | C
Now my flag is 5 (0101).
I need to delete bit C from flag. How can I do this?
To clear a flag you typically AND with its complement, e.g. in C and related languages:
x = 5; // x = 0101
x = x & ~C; // x = x & 1011 = 0101 & 1011 = 0001
Note: this can be expressed slightly more succinctly as:
x &= ~C;
Alternatively if you already know that a particular bit is 1 and you want to set it to 0 then you can just toggle (invert) it using XOR:
x = x ^ C; // x = x ^ 0100 = 0101 ^ 0100 = 0001
or:
x ^= C;
My solution is:
1) Check, is bit set
flag & C == C
2) If bit set apply XOR operation:
flag ^ C
Now my flag is 5 (0001).
May be there are more simple solutions?

Resources