Hot answers tagged boolean-computation
15
I like more :
MapThread[ And, {{True, True, False}, {True, False, False}}]
{True, False, False}
Edit
We should test efficiency of various methods for a few different lists.
Definitions
Argento[l_] := (And @@ # & /@ Transpose[l]; // AbsoluteTiming // First)
Brett[l_] := (And @@@ Transpose[l]; // AbsoluteTiming // First)
Artes[l_] := ...
14
I like Artes' and kguler's answers, but I'd like to point out that in general
f @@ # & /@ list
can be more concisely written as
f @@@ list
For example:
And @@@ Transpose[{{True, True, False}, {True, False, False}}]
(* {True, False, False} *)
13
Have you seen, that Mathematica is capable of many boolean computations using special boolean functions? Let's assume someone from the island makes a statement, then when the statment is true, whether or not he tells the statement is true, depends on whether or not he is a truth-teller. When we know, which kind he is, we know the correct statement through ...
12
You can use And and still get what you want:
x := 1
If[And @@ {True, False, True, False, (x := 2; True)}, Print["Yes"], Print["No"]];
x
(* No
2 *)
What happens is that the List and its arguments are evaluated before And is applied to it, hence setting the value of x.
11
You want to complement bits based on a given length. Easy enough.
complementBits[bits_Integer, len_Integer] /; bits >= 0 && len >= 0 :=
BitXor[bits, 2^len - 1]
(If you really want to compliment them, tell them the size is much too big for them..)
Quick test.
complementBits[43, 8]
(* Out[237]= 212 *)
Getting back to the question at ...
10
Unfortunately, the approach of using Apply[Plus, Flatten[nbhd]] is fatal to your algorithm because summing up the cell-values introduces ambiguities. As an example take these rules from your excel file (White (0), Red (1) and Black (2)). This is in the Excel-file at A43:
{{2,2,2},{2,2,0},{0,0,0}} -> 2
And this is in A179:
{{1,1,1},{1,2,1},{1,1,1}} ...
8
In addition to HoldAll as highlighted by Pinguin Dirk the other component of the behavior is that And directly returns single arguments:
And[73]
73
Combined, And[check] spits out check which at the top level evaluates to Sequence[True, . . .].
One problem with your method for checking the matrix is that it does not short-circuit on a non-numeric ...
8
As in the comments above, we see that the HoldAll attribute is causing the "problem".
Note that if we unset HoldAll it works:
Unprotect@And;
ClearAttributes[And, HoldAll];
Protect@And;
Then:
And[check]
True
EDIT: based on Oleksandr R.'s comment, I must stress that I showed this only to illustrate the "problem". It is not a good idea to unset ...
7
tutorial/RealPolynomialSystems claims "Reduce, Resolve, and FindInstance always put real polynomial systems in the prenex normal form, with quantifier-free parts in the disjunctive normal form..."
For obtaining Skolem form from prenex, possibly could proceed as described at
http://demonstrations.wolfram.com/Skolemization/
or
...
6
Given the rule as explained by Halirutan...
If the number of Red cells is equal to the number of Black cells, then
the center cell gets White. Otherwise the center gets the color Red,
if we have more Red cells and Black if we have more Black cells.
...it's interesting to note how things work if we use the following mapping of colours to numbers:
...
6
You can do this by explicitly constructing the InterpolatingPolynomial corresponding to yan, and then using FullSimplify:
yin = InterpolatingPolynomial[Transpose[Flatten /@ {yan[[3]],yan[[4]]}],x];
FullSimplify[yin > -1, -1 < x < 1]
(*True*)
Why does this work? Because yan actually has a list of points:
FullForm[yan]
so I can extract them ...
6
Just to illustrate what the comments are getting at, consider these variations below. First, the three If calculations are consistently faster than the corresponding operator versions.
Second, the time it takes to execute Print is highly variable (see below). Finally, let me add that operators are limited in their possible values (for example, Or yields ...
5
It seems I misunderstood the question. Here's an update, which is a considerable improvement, too. It relies on Implies[x, y] being equivalent to Boole[x] (1 - Boole[y]) == 0.
falsePattern = Table[False, {15}, {15}];
truePattern = Table[True, {15}, {15}];
SeedRandom[1];
randomPattern = RandomChoice[{True, False}, {15, 15}];
impliesPosition[board_, ...
5
The following is basically the same @acl did, but using the package InterpolatingFunctionAnatomy which (in principle) will behave better than peeking at the internal structures when Mma version changes.
Needs["DifferentialEquations`InterpolatingFunctionAnatomy`"];
yan = FunctionInterpolation[x^2, {x, -1, 1}];
yin = InterpolatingPolynomial[Transpose[Flatten ...
5
This happens because Simplify converts the expression to a BooleanFunction representation. Checking the documentation for BooleanFunction you find that:
Elements of both inputs and outputs can be specified either as True
and False or as 1 and 0.
4
To convert a Boolean test function such as PrimeQ or IntegerQ into a pattern one typically uses either Condition (/;) or PatternTest (?). There are specific strengths and purposes for each which are described in: Using a PatternTest versus a Condition for pattern matching.
Here is a simple and brief example of each:
Cases[Range@10, _?PrimeQ]
{2, 3, 5, ...
4
The Wolfram Alpha example suggests you want to treat $0$ and $1$ as the Boolean values False and True respectively and, with this convention, to parameterize an arbitrary binary Boolean operator $f$ by means of its truth table values $(a,b,c,d)$:
f[x_, y_, {a_, b_, c_, d_}] := {1 - x, x} . {{a, b}, {c, d}} . {1 - y, y}
(This method exhibits such binary ...
4
Coding Issue:
You can try to use DiscretePlot for the above expression out of the box in Mathematica!
DiscretePlot[
Evaluate@(-((Sqrt[2] FresnelS[Sqrt[2] Sqrt[n]])/
n^(3/2)) - (Sqrt[2] FresnelS[Sqrt[2] Sqrt[Abs[n]]])/
Abs[n]^(3/2) + (2 Sin[n \[Pi]])/n + (2 Sin[\[Pi] Abs[n]])/
Abs[n])/(2 Sqrt[\[Pi]]), {n, 1, 150}]
Your code has minor typos! ...
4
A silly one (convert both lists to numbers, multiply, convert back) and some timings:
(l1 /. {True -> 1, False -> 0}) (l2 /. {True -> 1, False -> 0}) /. {0 -> False, 1 -> True})
n = 1000000;
res = Table[
l1 = RandomChoice[{True, False}, n];
l2 = RandomChoice[{True, False}, n];
{
(r1 = And[l1, l2] // Thread) // AbsoluteTiming ...
4
tableImplies[a_, b_] := And @@ Flatten@MapThread[Implies, {a, b}, 2]
usage
a = Table[aa[i, j], {i, 2}, {j, 2}];
b = Table[bb[i, j], {i, 2}, {j, 2}];
tableImplies[a, b]
(*
(aa[1, 1] \[Implies] bb[1, 1]) &&
(aa[1, 2] \[Implies] bb[1, 2]) &&
(aa[2, 1] \[Implies] bb[2, 1]) &&
(aa[2, 2] \[Implies] bb[2, 2])
*)
3
As far as I can see Mathematica's result is the correct one.
[I will use a byte representation of your bits in the following, but a longer representation will work as well.]
$2B equals 00101011, which is turned into 11010100 ($D4) with a bitwise Not.
The interpretation of this bitpattern as an integer follows the usual 2's complement rules. The first ...
3
String matching works a little better. Think of each row in the board as a string on the alphabet {"0", "1"}. The "pattern" is a set of instructions to look for particular configurations of "0" on the board, because a presence of a "1" in the pattern is no restriction at all and a "0" in the pattern means there must be a corresponding "0" on the board. ...
2
And in a late bid for the silver medal by subversive means:
Unprotect@And; SetAttributes[And, {Flat, OneIdentity, Protected, Listable}];
l1 = RandomChoice[{True, False}, {2, 10^6}];
Argento[l1]
Brett[l1]
Artes[l1]
kguler[l1]
RM[l1]
And @@ l1 // AbsoluteTiming // First
0.705648
0.288288
0.193292
0.163886
0.149485
0.160957
1
Technical Support responded:
"[Developers] mentioned to me that indeed it is the case that BooleanFunction
treats 1 as True and 0 as False, and these "simplifications" occur
only when the expression is converted to BooleanFunction format.
If any changes to the documentation or these boolean operators I will
certainly let you know."
From this ...
1
Why this happens
The documentation states
Integers are assumed to be represented in two's complement form, with an unlimited number of digits, so that BitNot[n] is simply equivalent to $-1-n$.
So Mathematica does not assume a fixed number of binary digits (you can't assume your number to be 64 bit long or 32 bit long). It always takes the exact ...
Only top voted, non community-wiki answers of a minimum length are eligible


