Tag Info

Hot answers tagged

31

I'm coming to the party a bit late, but here's my approach. It should work for any two polygons, including non-convex and self-intersecting ones. winding[poly_, pt_] := Round[(Total@ Mod[(# - RotateRight[#]) &@(ArcTan @@ (pt - #) & /@ poly), 2 Pi, -Pi]/2/Pi)] cross[e1_, e2_] /; (N[Det[{Subtract @@ e1, Subtract @@ e2}]] === 0.) = ...


30

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


18

I prefer using Inner for this, as conceptually, it is a generalized dot of the two Boolean lists with And as the operator. Inner[And, {True, True, False}, {True, False, False}, List] (* {True, False, False} *)


18

How about RegionPlot? RegionPlot[ { (x - 0.2)^2 + y^2 < 0.5 && 0 < x < 1 && 0 < y < 1, (x - 0.2)^2 + y^2 < 0.5 && ! (0 < x < 1 && 0 < y < 1), ! ((x - 0.2)^2 + y^2 < 0.5) && 0 < x < 1 && 0 < y < 1 }, {x, -1, 1.5}, {y, -1, 1.5}, PlotStyle -> ...


17

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


16

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_] := ...


15

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


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

The (undocumented!) function Graphics`Mesh`PolygonIntersection[] seems up to the task. Using Sjoerd's example: Graphics`Mesh`MeshInit[]; polys = {Polygon[{{1, 3}, {3, 4}, {4, 7}, {5, -1}, {3, -3}}], Polygon[{{2, 2}, {3, 3}, {4, 2}, {0, 0}}]}; Graphics[Append[{Gray, polys}, {Blue, PolygonIntersection[polys]}]] Disk[] objects are not covered by ...


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


13

I am not aware of any built-in functionality (I might easily be wrong), but there's an example at MathWorld for calculating intersections of convex polygons. You'd need to approximate the circle with a polygon. Get the notebook from that page: there's an intersection calculation inside that uses the IMTEK Mathematica supplement. Example: << ...


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.


12

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


11

You can use Thread: Thread[And[{True, True, False}, {True, False, False}]] or, Thread with Apply (@@) : Thread[And@@{{True, True, False}, {True, False, False}}] (* {True, False, False} *)


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


10

Another way for All is to use VectorQ function VectorQ[lis, TrueQ]


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

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


8

Another option is to use image processing features such as ImageCompose: { g1 = Graphics[Rectangle[], PlotRange -> 1], g2 = Graphics[Disk[{0.2, 0}, .5], PlotRange -> 1], ImageCompose[g1, g2, Center, Center, {1, 1, 0}] } The output of the above looks like this: (Note that in this case your Graphics get rasterized and the result is an Image.)


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

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

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


8

If I'm not mistaken you should be happy to use Array: Array[x, 10, 0, And] x[0] && x[1] && x[2] && x[3] && x[4] && x[5] && x[6] && x[7] && x[8] && x[9] It works on V9, don't know if for previous versions too.


7

You may be able to do this using FilledCurve in version 8. I see examples of subtraction and exclusion but not intersection.


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

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


7

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


7

You can simply do: {#, #2} -> #3 & @@@ {{0, 0, 0}, {0, 1, 1}, {1, 0, 1}, {1, 1, 1}} {{0, 0} -> 0, {0, 1} -> 1, {1, 0} -> 1, {1, 1} -> 1}


6

f[{a_, b_, c_, d_}] := BooleanFunction[Thread[Tuples[{0, 1}, 2] -> {a, b, c, d}]] Usage f[{True, False, True, False}][0, 1] (* False *)



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