Tag Info

Hot answers tagged

29

Both, And and Or should work for All and Any respectively. You may have to get creative in how you apply them, though. For instance, And @@ {True, False, True} works just like you would expect AllOf @ {True, False, True} to without any additional work. Similarly, Or @@ {False, True, False} works like AnyOf.


23

You can implement equivalents of the any and all functions in MATLAB and python in Mathematica using the MemberQ and FreeQ functions as: any[x_List] := MemberQ[x, True] all[x_List] := FreeQ[x, False] For large lists, these will be about an order of magnitude faster in the worst case to several orders faster in the best case, when compared to the And and ...


16

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 ...


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

In addition to the simple form where you already have a list of True|False elements, you may want lazy evaluation in creating that list, short circuiting if the test fails. You can do this with Hold. I include a Print statement so that you can see what actually evaluates: (Print@#; # != 0) & /@ Hold[1, 0, 0, 1, 1, 0, 1, 0] And @@ % (Print@#; # != ...


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

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 ...


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

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}} ...


9

v1 = RandomChoice[{-1, 1}, 100]; v2 = RandomChoice[{-1, 1}, 100]; vL = UnitStep[v1]; vL*v1 + (1 - vL)*v2 Using PatoCriollo's comparison (and now updated to include rasher and Mr. Wizard's methods): n = 10^6; v1 = RandomChoice[{-1, 1}, n]; v2 = RandomChoice[{-1, 1}, n]; (bp = Boole[Positive[v1]]; test1 = bp v1 + (1 - bp) v2); // Timing test2 = With[{c = ...


8

The Wolfram Language and Mathematica 10 (available now on the Raspberry Pi) have new functions — AnyTrue, AllTrue, NoneTrue — which take a predicate and test any/all/none on the input list. For example: AnyTrue[Range@5, EvenQ] (* True *) AllTrue[{True, False, False}, TrueQ] (* or Identity in place of TrueQ *) (* False *) These functions can also be ...


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 ...


8

BooleanMinimize[BooleanFunction[table]] (* (#1 && ! #2) || (#1 && ! #3) || (#1 && ! #4) || (! #1 && #2 && #3 && #4) & *) Note in the documentation how 1/0 vs True/False are treated here, massage as needed. Quick verification: bf = BooleanFunction[table]; Rule @@@ Transpose[{table[[All, 1]], ...


7

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: ...


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

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

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.


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 ...


6

Here is a method using SparseArray properties to find positions (indexes), then Part to make the replacements. It may be a bit more general than the mathematical methods and it appears to be roughly as efficient on my system (running version 7). Note that unless it is acceptable to count zero as positive it is inappropriate to use UnitStep[v1] -- instead ...


5

Streams and iterators Using streams would certainly be one of the most elegant ways to do this. In any case, you will at least need an iterator for your sequence of numbers. The reason why an abstraction of an iterator is useful is because it separates the iteration over your sequence from the stuff you want to do with individual elements, so that you can ...


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 ...


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]) *)


4

I don't now that I really understand your question, but from a pure expression manipulation perspective this might be useful: Or @@ And @@@ Tuples[{{a && ! b}, {c, ! c}, {d, ! d}}] (a && ! b && c && d) || (a && ! b && c && ! d) || (a && ! b && ! c && d) || (a ...


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. ...



Only top voted, non community-wiki answers of a minimum length are eligible