Can anyone help me with a little Boolean algebra. Wolfram alpha doesn't seem to like this very much. The problem is huge so I'm only showing a small small part of it.
( ( A' or B or C )' or ( A or D )' or D')'
thank you.
This is the whole problem.
the blue is what i have posted
The DNF form of your written equation is (A'D)+(BD)+(CD)
More forms on Wolfram|Alpha
If the ' in your sample code stands for negativity, then in C# or Java you'd have:
boolean A,B,C,D;
and then:
if(!(!A || B || C) || !(A || D) || !D)
{
//Do something
}
Related
I have a program that often has the boolean algorithm pattern of: (f(a) && g(b)) || (f(b) && g(a)).
For example,
if ((isA(event[1]) && isTens(event[3])) || (isA(event[3]) && isTens(event[1])))
cnt += 1
end
How can I use a macro, #pmacro, to do the same, so to be called as such:
if (#pmacro(isA(), isTens(), event[1], event[3]))
cnt += 1
end
So far, I tried something like this
function isCombination(f, g, e1, e2)
if (f(e1) && g(e2)) || (f(e2) && g(e1))
true
else
false
end
end
And,
julia> isCombination(BJcore.isA, BJcore.isTens, event[1], event[3])
Works but, should I use meta-programming here?
You don't need a macro for that. Since Julia supports higher order programming, you can define a function - I'll call it predicate here - that accepts the functions f and g as arguments and performs the required check.
julia> predicate(f, g, a, b) = (f(a) && g(b) || f(b) && g(a))
predicate (generic function with 1 method)
julia> predicate(iseven, isodd, 4, 3)
true
julia> predicate(iseven, isodd, 3, 3)
false
julia> predicate(iseven, isodd, 3, 4)
true
Edit: The edit to the question happened while I was writing the answer, but you've hit upon the solution. isCombination and predicate are pretty much the same function.
As for metaprogramming: when there's a non-metaprogramming solution to a problem, the answer to "should I use metaprogramming" is almost always no. Metaprogramming is a powerful tool, but can make your code harder to read and maintain unless applied judiciously and with care.
I have been trying to execute a piece of code with some if conditions. This is a simple version of it.
X=100;Y=100;
if ((((X+1) && (Y+1))<=99) && (((X+1) && (Y+1))<=102))
disp(X);
end
Despite both X and Y not satisfying the first condition, I still get the output as 100. I have tried all combinations of & and && to make the and operations in the work. I checked the difference between the two and I found that & is a logical bit-wise operator while && is a short-circuit operator, which probably doesn't change much in this context. What's the error with this syntax?
Of course the code works when I do this:
X=100;Y=100;
if (X+1)<=99 && (Y+1)<=99 && (((X+1) && (Y+1))<=102)
disp(X);
end
But this is so inefficient when there are lot of conditions and each sub-condition must include the constraints. I'm sure this must be answered somewhere and this question might be a duplicate, so please point me to the answer.
So it looks like you understand what (X+1)<=99 && (Y+1)<=99 does. Let's look at ((X+1) && (Y+1))<=99:
&& requires a logical value on each side. a && b will turn a and b into logicals, effectively becoming a~=0 && b~=0. Thus:
((X+1) && (Y+1) ) <= 99
((X+1)~=0 && (Y+1)~=0) <= 99
( true && true ) <= 99
1 <= 99
true
Of course the truth value of (X+1)~=0 and (Y+1)~=0 could be different, but here you see this. In MATLAB, true is equal to 1 in a non-logical context, as when compared to 99.
If you want to simplify this expression, use max instead of &&:
X=100;Y=100;
if max(X+1,Y+1)<=99 && max(X+1,Y+1)<=102
disp(X);
end
If the max of a and b is smaller than 99, then both a and b are smaller than 99.
(Obviously, the statement can be further simplified to if max(X+1,Y+1)<=102, since if the second inequality holds than so must the first.)
Hi I have this expression XY'Z+YY'+X+XY'+XZ ( ' means not) Can anyone please show me the simplification of this expression? Thanks in advance.
This is what i got so far:
XY'Z+YY'+XY'+X(1+Z)
XY'Z+YY'+XY'+XY'+X
Y'(XZ+Y)+X(1+Y')
Y'(XZ+Y)+X
I know 1+Y=1 but 1+Y'=1 too?
Y'(XZ+Y)+X
change this back to:
XY'Z+YY'+X
Then simplify a bit:
XY'Z+YY'+X
YY' + X(1+Y'Z)
0 + X
X
I'm trying to execute a block of code if one of several conditions is met. Again, for clarity, if any of the conditions are true, I'd like for the code to execute (chain of logical ORs).
When I type this code into Matlab:
if strmatch(T,'Sd') || strmatch(T,'SuperDev') || ...
strmatch(T,'lrnTrialVD') || strmatch(T,'lrnTrialVIS') || ...
strmatch(T,'lrnTrialTARGET') || strmatch(T,'lrnTrialAUD')
I get this error:
??? Operands to the || and && operators must be convertible to logical scalar values.
Can someone please tell me where I've gone wrong?
/blz
EDIT: I used the wrong function. strcmp is what I meant to use!
I believe this is because the return value of strmatch() is an array, not a scalar, and || might not be defined on array arguments. I don't have MATLAB in front of me (only Octave), but does [1, 2, 3] || [4, 5, 6, 7] work for you?
Additonally, it would be better to match a regular expression like (Sd|SuperDev|lrnTrial(VD|VIS|TARGET|AUD)) which is a bit more compact, more readable, and only needs to inspect the string 'T' once (not six times.)
This would look like:
octave-3.2.4.exe:10> T1 = "Sd"
T1 = Sd
octave-3.2.4.exe:11> T2 = "Lollipop"
T2 = Lollipop
octave-3.2.4.exe:12> regexp(T1,"(Sd|SuperDev|lrnTrial(VD|VIS|TARGET|AUD))" )
ans = 1
octave-3.2.4.exe:13> regexp(T2,"(Sd|SuperDev|lrnTrial(VD|VIS|TARGET|AUD))" )
ans = [](1x0)
strmatch doesn't necessarily return something which can be converted to a logical scalar value. It might, for example, return a vector of match locations which does not convert without further effort on your part. It's all there in the error message and the product documentation, it really is !
I was asked this question in an interview.
Write a c# program which will print out all the errors in a statement.
(a + b == 3 and x == y or b / c == d)
Allowed keywords are and/or, braces are allowed and have to match. The statement has to be logically correct. Print out all the errors.
Something like a compiler or sql analyzer.
Any idea how to go about syntax checking?
You're being asked to build a parser.
The computer science theory behind compilers is extensive and complex. To keep it simple, I recommend reading Jack Crenshaw's compiler tutorial. It will show you in the first few chapters how to do exactly this by building a top-down recursive descent parser. His example is almost exactly what you're trying to do. It's in Pascal, but it's still easy enough to follow and the concept still applies.
Google for "jack crenshaw let's build a compiler", or browse to:
http://compilers.iecc.com/crenshaw/
That's actually pretty complicated if you want to get it 100% right. The direction to research would have to do with the keywords "finite state automata", "regular expressions" (not to be confused with Regex) and "context-free grammars". Also there's an approach called "recursive descent parser" but you'll need to understand the above concepts first.
Matching brackets and parenthesis is easy.
string expression = "(a + b + (c + d) )";
int brackets = 0;
int parenthesis = 0;
foreach(char c in expression)
{
if(c == '(') ) parenthesis++;
if(c == ')') ) parenthesis--;
if(parenthesis < 0) { // ERROR!!! }
// Same logic with brackets
}
if (parenthesis != 0) { ERROR!!! }
Also, I would use a similar approach with "statements mergers" ( +, -, *, / ) and "statements comparators" ( ==, !=, <, <=, ... )
For each word in your expression, only a few valid keywords can follow.
As it was mentionned before, writing parsers is a difficult task, planning every exception case right away is hardly possible. This should be the general idea instead of a definitive solution.
On a final note, it is perfectly ok to have multiple passes to scan for different things. First pass is brackts/parenthesis only, then check for something else in another loop.